2017-09-15 14:44:40 +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>
|
2018-03-27 10:23:09 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2018, 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:13:12 +03:00
|
|
|
use \EA\Engine\Api\V1\Response;
|
|
|
|
use \EA\Engine\Api\V1\Request;
|
2016-11-09 21:56:24 +03:00
|
|
|
use \EA\Engine\Types\NonEmptyText;
|
2016-07-10 17:13:12 +03:00
|
|
|
|
2016-07-08 22:23:03 +03:00
|
|
|
/**
|
|
|
|
* Categories Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
* @subpackage API
|
|
|
|
*/
|
|
|
|
class Categories extends API_V1_Controller {
|
2016-07-10 17:13:12 +03:00
|
|
|
/**
|
|
|
|
* Categories Resource Parser
|
2017-09-15 14:36:37 +03:00
|
|
|
*
|
2016-07-10 17:13:12 +03:00
|
|
|
* @var \EA\Engine\Api\V1\Parsers\Categories
|
|
|
|
*/
|
2017-09-15 14:36:37 +03:00
|
|
|
protected $parser;
|
2016-07-10 17:13:12 +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();
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->load->model('services_model');
|
2016-07-10 17:13:12 +03:00
|
|
|
$this->parser = new \EA\Engine\Api\V1\Parsers\Categories;
|
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
|
|
|
|
{
|
|
|
|
$condition = $id !== NULL ? 'id = ' . $id : '';
|
|
|
|
$categories = $this->services_model->get_all_categories($condition);
|
|
|
|
|
|
|
|
if ($id !== NULL && count($categories) === 0)
|
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
$this->_throwRecordNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = new Response($categories);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
catch (\Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
$this->_handleException($exception);
|
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-10 17:13:12 +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
|
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
// Insert the category to the database.
|
2017-09-15 14:36:37 +03:00
|
|
|
$request = new Request();
|
|
|
|
$category = $request->getBody();
|
|
|
|
$this->parser->decode($category);
|
|
|
|
|
|
|
|
if (isset($category['id']))
|
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
unset($category['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->services_model->add_category($category);
|
|
|
|
|
|
|
|
// Fetch the new object from the database and return it to the client.
|
2017-09-15 14:36:37 +03:00
|
|
|
$batch = $this->services_model->get_all_categories('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
|
|
|
}
|
|
|
|
catch (\Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
$this->_handleException($exception);
|
|
|
|
}
|
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
|
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
// Update the category record.
|
2017-09-15 14:36:37 +03:00
|
|
|
$batch = $this->services_model->get_all_categories('id = ' . $id);
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2017-09-15 14:36:37 +03:00
|
|
|
if ($id !== NULL && count($batch) === 0)
|
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
$this->_throwRecordNotFound();
|
|
|
|
}
|
2017-09-15 14:36:37 +03:00
|
|
|
|
|
|
|
$request = new Request();
|
|
|
|
$updatedCategory = $request->getBody();
|
2016-07-10 17:13:12 +03:00
|
|
|
$baseCategory = $batch[0];
|
2017-09-15 14:36:37 +03:00
|
|
|
$this->parser->decode($updatedCategory, $baseCategory);
|
|
|
|
$updatedCategory['id'] = $id;
|
2016-07-10 17:13:12 +03:00
|
|
|
$id = $this->services_model->add_category($updatedCategory);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2016-07-10 17:13:12 +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->services_model->get_all_categories('id = ' . $id);
|
|
|
|
$response = new Response($batch);
|
|
|
|
$response->encode($this->parser)->singleEntry($id)->output();
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
|
|
|
catch (\Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
$this->_handleException($exception);
|
|
|
|
}
|
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:13:12 +03:00
|
|
|
$result = $this->services_model->delete_category($id);
|
|
|
|
|
|
|
|
$response = new Response([
|
2017-09-15 14:36:37 +03:00
|
|
|
'code' => 200,
|
2016-07-10 17:13:12 +03:00
|
|
|
'message' => 'Record was deleted successfully!'
|
|
|
|
]);
|
2016-07-08 22:23:03 +03:00
|
|
|
|
2016-07-10 17:13:12 +03:00
|
|
|
$response->output();
|
2017-09-23 02:30:22 +03:00
|
|
|
}
|
|
|
|
catch (\Exception $exception)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-07-10 17:13:12 +03:00
|
|
|
$this->_handleException($exception);
|
|
|
|
}
|
2016-07-08 22:23:03 +03:00
|
|
|
}
|
|
|
|
}
|