2020-04-22 22:48:56 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-07-08 22:23:03 +03:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
2020-11-14 22:36:25 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2016-07-08 22:23:03 +03:00
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2021-11-03 10:38:59 +03:00
|
|
|
* Admins API v1 controller.
|
2016-07-08 22:23:03 +03:00
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2021-11-03 10:38:59 +03:00
|
|
|
class Admins extends EA_Controller {
|
2016-07-08 22:23:03 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-07-08 22:23:03 +03:00
|
|
|
parent::__construct();
|
2021-11-03 10:38:59 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->load->model('admins_model');
|
2021-11-03 10:38:59 +03:00
|
|
|
|
|
|
|
$this->load->library('api');
|
|
|
|
|
|
|
|
$this->api->cors();
|
|
|
|
|
|
|
|
$this->api->auth();
|
|
|
|
|
|
|
|
$this->api->model('admins_model');
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-03 10:38:59 +03:00
|
|
|
* Get a single admin or an admin collection.
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2021-11-03 10:38:59 +03:00
|
|
|
* @param int|null $id Admin ID.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2021-11-03 10:38:59 +03:00
|
|
|
public function get(int $id = NULL)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
$where = $id ? ['id' => $id] : NULL;
|
|
|
|
|
|
|
|
$keyword = $this->api->request_keyword();
|
|
|
|
|
|
|
|
$limit = $this->api->request_limit();
|
|
|
|
|
|
|
|
$offset = $this->api->request_offset();
|
2020-12-05 12:14:13 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
$order_by = $this->api->request_order_by();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
$fields = $this->api->request_fields();
|
|
|
|
|
|
|
|
$admins = empty($keyword)
|
|
|
|
? $this->admins_model->get($where, $limit, $offset, $order_by)
|
|
|
|
: $this->admins_model->search($keyword, $limit, $offset, $order_by);
|
|
|
|
|
|
|
|
foreach ($admins as &$admin)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
$this->admins_model->api_encode($admin);
|
|
|
|
|
|
|
|
if ( ! empty($fields))
|
|
|
|
{
|
|
|
|
$this->admins_model->only($admin, $fields);
|
|
|
|
}
|
2016-07-10 16:53:22 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
$response = $id && ! empty($admins) ? $admins[0] : $admins;
|
2016-07-10 16:53:22 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
if ( ! $response)
|
|
|
|
{
|
|
|
|
response('Not Found', 404);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_response($response);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2021-10-28 15:01:27 +03:00
|
|
|
catch (Throwable $e)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
json_exception($e);
|
2016-07-10 16:53:22 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-03 10:38:59 +03:00
|
|
|
* Create an admin.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function post()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
$admin = request();
|
|
|
|
|
|
|
|
$this->admins_model->api_decode($admin);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2020-12-17 17:49:04 +03:00
|
|
|
if (array_key_exists('id', $admin))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-07-10 16:53:22 +03:00
|
|
|
unset($admin['id']);
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:49:04 +03:00
|
|
|
if ( ! array_key_exists('settings', $admin))
|
|
|
|
{
|
|
|
|
throw new Exception('No settings property provided.');
|
|
|
|
}
|
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
$admin_id = $this->admins_model->save($admin);
|
|
|
|
|
|
|
|
$created_admin = $this->admins_model->find($admin_id);
|
|
|
|
|
|
|
|
$this->admins_model->api_encode($created_admin);
|
2016-07-10 16:53:22 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
json_response($created_admin, 201);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2021-10-28 15:01:27 +03:00
|
|
|
catch (Throwable $e)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
json_exception($e);
|
2016-07-10 16:53:22 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-03 10:38:59 +03:00
|
|
|
* Update an admin.
|
2016-07-08 22:23:03 +03:00
|
|
|
*
|
2021-11-03 10:38:59 +03:00
|
|
|
* @param int $id Admin ID.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2021-11-03 10:38:59 +03:00
|
|
|
public function put(int $id)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
$occurrences = $this->admins_model->get(['id' => $id]);
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
if (empty($occurrences))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
response('', 404);
|
|
|
|
|
|
|
|
return;
|
2016-07-10 16:53:22 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
$original_admin = $occurrences[0];
|
|
|
|
|
|
|
|
$admin = request();
|
|
|
|
|
|
|
|
$this->admins_model->api_decode($admin, $original_admin);
|
|
|
|
|
|
|
|
$admin_id = $this->admins_model->save($admin);
|
|
|
|
|
|
|
|
$updated_admin = $this->admins_model->find($admin_id);
|
|
|
|
|
|
|
|
$this->admins_model->api_encode($updated_admin);
|
|
|
|
|
|
|
|
json_response($updated_admin);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2021-10-28 15:01:27 +03:00
|
|
|
catch (Throwable $e)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
json_exception($e);
|
2016-07-10 16:53:22 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-03 10:38:59 +03:00
|
|
|
* Delete an admin.
|
2016-07-08 22:23:03 +03:00
|
|
|
*
|
2021-11-03 10:38:59 +03:00
|
|
|
* @param int $id Admin ID.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2021-11-03 10:38:59 +03:00
|
|
|
public function delete(int $id)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
$occurrences = $this->admins_model->get(['id' => $id]);
|
2016-07-10 16:53:22 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
if (empty($occurrences))
|
|
|
|
{
|
|
|
|
response('', 404);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2021-11-03 10:38:59 +03:00
|
|
|
$this->admins_model->delete($id);
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2021-10-28 15:01:27 +03:00
|
|
|
catch (Throwable $e)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-11-03 10:38:59 +03:00
|
|
|
json_exception($e);
|
2016-07-10 16:53:22 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
}
|