2016-07-08 22:23:03 +03:00
|
|
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2017-01-31 09:35:34 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2017, 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';
|
|
|
|
|
2016-07-10 17:56:43 +03:00
|
|
|
use \EA\Engine\Api\V1\Response;
|
|
|
|
use \EA\Engine\Api\V1\Request;
|
|
|
|
|
2016-07-08 22:23:03 +03:00
|
|
|
/**
|
|
|
|
* Settings Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
* @subpackage API
|
|
|
|
*/
|
|
|
|
class Settings extends API_V1_Controller {
|
2016-07-10 17:56:43 +03:00
|
|
|
/**
|
|
|
|
* Settings Resource Parser
|
|
|
|
*
|
|
|
|
* @var \EA\Engine\Api\V1\Parsers\Settings
|
|
|
|
*/
|
|
|
|
protected $parser;
|
|
|
|
|
2016-07-08 22:23:03 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2016-07-10 17:56:43 +03:00
|
|
|
$this->load->model('settings_model');
|
|
|
|
$this->parser = new \EA\Engine\Api\V1\Parsers\Settings;
|
2016-07-08 22:23:03 +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
|
|
|
*/
|
2016-07-10 17:56:43 +03:00
|
|
|
public function get($name = null) {
|
|
|
|
try {
|
|
|
|
$settings = $this->settings_model->get_settings();
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2016-07-10 17:56:43 +03:00
|
|
|
if ($name !== null) {
|
|
|
|
$setting = null;
|
|
|
|
|
|
|
|
foreach ($settings as $entry) {
|
|
|
|
if ($entry['name'] === $name) {
|
|
|
|
$setting = $entry;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($setting)) {
|
|
|
|
$this->_throwRecordNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($setting['id']);
|
|
|
|
|
|
|
|
$settings = [
|
|
|
|
$setting
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = new Response($settings);
|
|
|
|
|
|
|
|
$response->encode($this->parser)
|
|
|
|
->search()
|
|
|
|
->sort()
|
|
|
|
->paginate()
|
|
|
|
->minimize()
|
|
|
|
->singleEntry($name)
|
|
|
|
->output();
|
|
|
|
|
|
|
|
} catch(\Exception $exception) {
|
|
|
|
exit($this->_handleException($exception));
|
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PUT API Method
|
|
|
|
*
|
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
|
|
|
*/
|
2016-07-10 17:56:43 +03:00
|
|
|
public function put($name) {
|
|
|
|
try {
|
|
|
|
$request = new Request();
|
|
|
|
$value = $request->getBody()['value'];
|
|
|
|
$this->settings_model->set_setting($name, $value);
|
|
|
|
|
|
|
|
// Fetch the updated object from the database and return it to the client.
|
|
|
|
$response = new Response([
|
|
|
|
[
|
|
|
|
'name' => $name,
|
|
|
|
'value' => $value
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$response->encode($this->parser)->singleEntry($name)->output();
|
|
|
|
} catch(\Exception $exception) {
|
|
|
|
exit($this->_handleException($exception));
|
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE API Method
|
|
|
|
*
|
2016-07-10 17:56:43 +03:00
|
|
|
* @param string $name The setting name to be deleted.
|
2016-07-08 22:23:03 +03:00
|
|
|
*/
|
2016-07-10 17:56:43 +03:00
|
|
|
public function delete($name) {
|
|
|
|
try {
|
|
|
|
$result = $this->settings_model->remove_setting($name);
|
|
|
|
|
|
|
|
$response = new Response([
|
|
|
|
'code' => 200,
|
|
|
|
'message' => 'Record was deleted successfully!'
|
|
|
|
]);
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2016-07-10 17:56:43 +03:00
|
|
|
$response->output();
|
|
|
|
} catch(\Exception $exception) {
|
|
|
|
exit($this->_handleException($exception));
|
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
}
|