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;
|
2016-07-10 17:56:43 +03:00
|
|
|
|
2016-07-08 22:23:03 +03:00
|
|
|
/**
|
|
|
|
* Settings Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
|
|
|
class Settings extends API_V1_Controller {
|
2016-07-10 17:56:43 +03:00
|
|
|
/**
|
|
|
|
* Settings Resource Parser
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2016-07-10 17:56:43 +03:00
|
|
|
* @var \EA\Engine\Api\V1\Parsers\Settings
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
protected $parser;
|
2016-07-10 17:56:43 +03:00
|
|
|
|
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-10-29 14:21:03 +03:00
|
|
|
|
2016-07-10 17:56:43 +03:00
|
|
|
$this->load->model('settings_model');
|
2021-10-29 14:21:03 +03:00
|
|
|
|
2016-07-10 17:56:43 +03:00
|
|
|
$this->parser = new \EA\Engine\Api\V1\Parsers\Settings;
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-09-15 14:36:37 +03:00
|
|
|
* GET API Method
|
|
|
|
*
|
2016-07-10 17:56:43 +03:00
|
|
|
* @param string $name Optional (null), the setting name to be returned.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function get($name = NULL)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-10-28 15:01:27 +03:00
|
|
|
$settings = $this->settings_model->get();
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
if ($name !== NULL)
|
|
|
|
{
|
|
|
|
$setting = NULL;
|
|
|
|
|
|
|
|
foreach ($settings as $entry)
|
|
|
|
{
|
|
|
|
if ($entry['name'] === $name)
|
|
|
|
{
|
2016-07-10 17:56:43 +03:00
|
|
|
$setting = $entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if (empty($setting))
|
|
|
|
{
|
2020-10-21 21:49:05 +03:00
|
|
|
$this->throw_record_not_found();
|
2016-07-10 17:56:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($setting['id']);
|
|
|
|
|
|
|
|
$settings = [
|
|
|
|
$setting
|
2017-09-15 14:36:37 +03:00
|
|
|
];
|
|
|
|
}
|
2016-07-10 17:56:43 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$response = new Response($settings);
|
2016-07-10 17:56:43 +03:00
|
|
|
|
|
|
|
$response->encode($this->parser)
|
2017-09-15 14:36:37 +03:00
|
|
|
->search()
|
|
|
|
->sort()
|
|
|
|
->paginate()
|
|
|
|
->minimize()
|
|
|
|
->singleEntry($name)
|
|
|
|
->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
|
|
|
{
|
2021-10-29 14:16:21 +03:00
|
|
|
$this->handle_exception($e);
|
2016-07-10 17:56:43 +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
|
|
|
*
|
2016-07-10 19:48:25 +03:00
|
|
|
* @param string $name The setting name to be inserted/updated.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function put($name)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$request = new Request();
|
2021-10-29 14:21:03 +03:00
|
|
|
|
2020-11-16 12:54:59 +03:00
|
|
|
$value = $request->get_body()['value'];
|
2021-10-29 14:21:03 +03:00
|
|
|
|
|
|
|
$setting = $this->settings_model->query()->get_where('name', $name)->row_array();
|
|
|
|
|
|
|
|
if ( ! empty($setting))
|
|
|
|
{
|
|
|
|
$setting['value'] = $value;
|
|
|
|
|
|
|
|
$this->settings_model->save($setting);
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2016-07-10 17:56:43 +03:00
|
|
|
// Fetch the updated object from the database and return it to the client.
|
|
|
|
$response = new Response([
|
|
|
|
[
|
|
|
|
'name' => $name,
|
|
|
|
'value' => $value
|
|
|
|
]
|
2017-09-15 14:36:37 +03:00
|
|
|
]);
|
2021-10-29 14:21:03 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
$response->encode($this->parser)->singleEntry($name)->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
|
|
|
{
|
2021-10-29 14:16:21 +03:00
|
|
|
$this->handle_exception($e);
|
2017-09-15 14:36:37 +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 string $name The setting name to be deleted.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
public function delete($name)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2021-10-29 14:21:03 +03:00
|
|
|
$setting = $this->settings_model->query()->get_where('name', $name)->row_array();
|
|
|
|
|
|
|
|
if ( ! empty($setting))
|
|
|
|
{
|
|
|
|
$this->settings_model->delete($setting['id']);
|
|
|
|
}
|
2016-07-10 17:56:43 +03:00
|
|
|
|
|
|
|
$response = new Response([
|
2017-09-15 14:36:37 +03:00
|
|
|
'code' => 200,
|
2016-07-10 17:56:43 +03:00
|
|
|
'message' => 'Record was deleted successfully!'
|
|
|
|
]);
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2016-07-10 17:56:43 +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
|
|
|
{
|
2021-10-29 14:16:21 +03:00
|
|
|
$this->handle_exception($e);
|
2017-09-15 14:36:37 +03:00
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
}
|