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
|
2016-07-08 22:23:03 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @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 15:17:29 +03:00
|
|
|
|
2016-07-08 22:23:03 +03:00
|
|
|
/**
|
|
|
|
* Providers Controller
|
|
|
|
*
|
2020-09-23 12:47:18 +03:00
|
|
|
* @property CI_Session $session
|
|
|
|
* @property CI_Loader $load
|
|
|
|
* @property CI_Input $input
|
|
|
|
* @property CI_Output $output
|
|
|
|
* @property CI_Config $config
|
|
|
|
* @property CI_Lang $lang
|
|
|
|
* @property CI_Cache $cache
|
|
|
|
* @property CI_DB_query_builder $db
|
|
|
|
* @property CI_Security $security
|
|
|
|
* @property Google_Sync $google_sync
|
|
|
|
* @property Ics_file $ics_file
|
2020-11-13 12:00:57 +03:00
|
|
|
* @property Appointments_model $appointments_model
|
|
|
|
* @property Providers_model $providers_model
|
|
|
|
* @property Services_model $services_model
|
|
|
|
* @property Customers_model $customers_model
|
|
|
|
* @property Settings_model $settings_model
|
2020-09-23 12:47:18 +03:00
|
|
|
* @property Timezones $timezones
|
2020-11-13 12:00:57 +03:00
|
|
|
* @property Roles_model $roles_model
|
|
|
|
* @property Secretaries_model $secretaries_model
|
|
|
|
* @property Admins_model $admins_model
|
|
|
|
* @property User_model $user_model
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
2016-07-08 22:23:03 +03:00
|
|
|
* @package Controllers
|
|
|
|
*/
|
|
|
|
class Providers extends API_V1_Controller {
|
2016-07-10 15:17:29 +03:00
|
|
|
/**
|
|
|
|
* Providers Resource Parser
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2016-07-10 15:17:29 +03:00
|
|
|
* @var \EA\Engine\Api\V1\Parsers\Providers
|
|
|
|
*/
|
|
|
|
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('providers_model');
|
2016-07-10 15:17:29 +03:00
|
|
|
$this->parser = new \EA\Engine\Api\V1\Parsers\Providers;
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* GET API Method
|
|
|
|
*
|
|
|
|
* @param int $id Optional (null), the record ID to be returned.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get($id = NULL)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$condition = $id !== NULL ? 'id = ' . $id : NULL;
|
|
|
|
$providers = $this->providers_model->get_batch($condition);
|
|
|
|
|
|
|
|
if ($id !== NULL && count($providers) === 0)
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->throw_record_not_found();
|
2016-07-10 15:17:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$response = new Response($providers);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->handle_exception($exception);
|
2016-07-10 15:17:29 +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-03-11 12:10:59 +03:00
|
|
|
// Insert the provider to the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
$request = new Request();
|
|
|
|
$provider = $request->getBody();
|
|
|
|
$this->parser->decode($provider);
|
|
|
|
|
|
|
|
if (isset($provider['id']))
|
|
|
|
{
|
2016-07-10 15:17:29 +03:00
|
|
|
unset($provider['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->providers_model->add($provider);
|
|
|
|
|
|
|
|
// Fetch the new object from the database and return it to the client.
|
2017-09-15 14:36:37 +03:00
|
|
|
$batch = $this->providers_model->get_batch('id = ' . $id);
|
|
|
|
$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
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->handle_exception($exception);
|
2016-07-10 15:17:29 +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
|
|
|
*
|
2017-09-15 14:36:37 +03:00
|
|
|
* @param int $id The record ID to be updated.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function put($id)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-03-11 12:10:59 +03:00
|
|
|
// Update the provider record.
|
2017-09-15 14:36:37 +03:00
|
|
|
$batch = $this->providers_model->get_batch('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 15:17:29 +03:00
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
$request = new Request();
|
2020-10-21 21:49:05 +03:00
|
|
|
$updated_provider = $request->getBody();
|
|
|
|
$base_provider = $batch[0];
|
|
|
|
$this->parser->decode($updated_provider, $base_provider);
|
|
|
|
$updated_provider['id'] = $id;
|
|
|
|
$id = $this->providers_model->add($updated_provider);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2016-07-10 15:17:29 +03:00
|
|
|
// Fetch the updated object from the database and return it to the client.
|
2017-09-15 14:36:37 +03:00
|
|
|
$batch = $this->providers_model->get_batch('id = ' . $id);
|
|
|
|
$response = new Response($batch);
|
|
|
|
$response->encode($this->parser)->singleEntry($id)->output();
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->handle_exception($exception);
|
2016-07-10 15:17:29 +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 15:17:29 +03:00
|
|
|
$result = $this->providers_model->delete($id);
|
|
|
|
|
|
|
|
$response = new Response([
|
2017-09-15 14:36:37 +03:00
|
|
|
'code' => 200,
|
2016-07-10 15:17:29 +03:00
|
|
|
'message' => 'Record was deleted successfully!'
|
|
|
|
]);
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2016-07-10 15:17:29 +03:00
|
|
|
$response->output();
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
catch (Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->handle_exception($exception);
|
2016-07-10 15:17:29 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
}
|