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
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
require_once __DIR__ . '/API_V1_Controller.php';
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
use EA\Engine\Api\V1\Request;
|
|
|
|
use EA\Engine\Api\V1\Response;
|
|
|
|
use EA\Engine\Types\NonEmptyText;
|
2016-07-10 17:01:49 +03:00
|
|
|
|
2016-07-08 22:23:03 +03:00
|
|
|
/**
|
|
|
|
* Secretaries Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
|
|
|
class Secretaries extends API_V1_Controller {
|
2016-07-10 17:01:49 +03:00
|
|
|
/**
|
|
|
|
* Secretaries Resource Parser
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2016-07-10 17:01:49 +03:00
|
|
|
* @var \EA\Engine\Api\V1\Parsers\Secretaries
|
|
|
|
*/
|
|
|
|
protected $parser;
|
|
|
|
|
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();
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->load->model('secretaries_model');
|
2016-07-10 17:01:49 +03:00
|
|
|
$this->parser = new \EA\Engine\Api\V1\Parsers\Secretaries;
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* GET API Method
|
|
|
|
*
|
2016-07-08 22:23:03 +03:00
|
|
|
* @param int $id Optional (null), the record ID to be returned.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get($id = NULL)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-12-05 12:14:13 +03:00
|
|
|
$conditions = $id !== NULL ? ['id' => $id] : NULL;
|
2020-12-09 15:17:45 +03:00
|
|
|
|
2021-10-28 15:01:27 +03:00
|
|
|
$secretaries = $this->secretaries_model->get($conditions);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
if ($id !== NULL && count($secretaries) === 0)
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->throw_record_not_found();
|
2016-07-10 17:01:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = new Response($secretaries);
|
|
|
|
|
|
|
|
$response->encode($this->parser)
|
2017-09-15 14:36:37 +03:00
|
|
|
->search()
|
|
|
|
->sort()
|
|
|
|
->paginate()
|
|
|
|
->minimize()
|
|
|
|
->singleEntry($id)
|
|
|
|
->output();
|
|
|
|
|
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
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->handle_exception($exception);
|
2016-07-10 17:01:49 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* POST API Method
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function post()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
// Insert the secretary to the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
$request = new Request();
|
2020-11-16 12:54:59 +03:00
|
|
|
$secretary = $request->get_body();
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->parser->decode($secretary);
|
|
|
|
|
2020-12-17 17:49:04 +03:00
|
|
|
if (array_key_exists('id', $secretary))
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-07-10 17:01:49 +03:00
|
|
|
unset($secretary['id']);
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:49:04 +03:00
|
|
|
if ( ! array_key_exists('providers', $secretary))
|
|
|
|
{
|
|
|
|
throw new Exception('No providers property provided.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! array_key_exists('settings', $secretary))
|
|
|
|
{
|
|
|
|
throw new Exception('No settings property provided.');
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:01:27 +03:00
|
|
|
$id = $this->secretaries_model->save($secretary);
|
2016-07-10 17:01:49 +03:00
|
|
|
|
|
|
|
// Fetch the new object from the database and return it to the client.
|
2021-10-28 15:01:27 +03:00
|
|
|
$batch = $this->secretaries_model->get(['id' => $id]);
|
2017-09-15 14:36:37 +03:00
|
|
|
$response = new Response($batch);
|
2016-11-09 21:56:24 +03:00
|
|
|
$status = new NonEmptyText('201 Created');
|
2017-09-15 14:36:37 +03:00
|
|
|
$response->encode($this->parser)->singleEntry(TRUE)->output($status);
|
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
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->handle_exception($exception);
|
2016-07-10 17:01:49 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* PUT API Method
|
2016-07-08 22:23:03 +03:00
|
|
|
*
|
|
|
|
* @param int $id The record ID to be updated.
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function put($id)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-22 22:48:56 +03:00
|
|
|
// Update the secretary record.
|
2021-10-28 15:01:27 +03:00
|
|
|
$batch = $this->secretaries_model->get(['id' => $id]);
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($id !== NULL && count($batch) === 0)
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->throw_record_not_found();
|
2016-07-10 17:01:49 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
$request = new Request();
|
2020-11-16 12:54:59 +03:00
|
|
|
$updated_secretary = $request->get_body();
|
2020-10-21 21:49:05 +03:00
|
|
|
$base_secretary = $batch[0];
|
|
|
|
$this->parser->decode($updated_secretary, $base_secretary);
|
|
|
|
$updated_secretary['id'] = $id;
|
2021-10-28 15:01:27 +03:00
|
|
|
$id = $this->secretaries_model->save($updated_secretary);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2016-07-10 17:01:49 +03:00
|
|
|
// Fetch the updated object from the database and return it to the client.
|
2021-10-28 15:01:27 +03:00
|
|
|
$batch = $this->secretaries_model->get(['id' => $id]);
|
2017-09-15 14:36:37 +03:00
|
|
|
$response = new Response($batch);
|
|
|
|
$response->encode($this->parser)->singleEntry($id)->output();
|
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
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->handle_exception($exception);
|
2016-07-10 17:01:49 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* DELETE API Method
|
2016-07-08 22:23:03 +03:00
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* @param int $id The record ID to be deleted.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-07-10 17:01:49 +03:00
|
|
|
$result = $this->secretaries_model->delete($id);
|
|
|
|
|
|
|
|
$response = new Response([
|
2017-09-15 14:36:37 +03:00
|
|
|
'code' => 200,
|
2016-07-10 17:01:49 +03:00
|
|
|
'message' => 'Record was deleted successfully!'
|
|
|
|
]);
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2016-07-10 17:01:49 +03:00
|
|
|
$response->output();
|
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
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->handle_exception($exception);
|
2016-07-10 17:01:49 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
}
|