2016-07-07 23:05:10 +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>
|
|
|
|
* @copyright Copyright (c) 2013 - 2016, Alex Tselegidis
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2016-07-08 22:17:06 +03:00
|
|
|
require_once __DIR__ . '/API_V1_Controller.php';
|
|
|
|
|
2016-07-09 14:14:08 +03:00
|
|
|
use \EA\Engine\Api\V1\Response;
|
|
|
|
use \EA\Engine\Types\NonEmptyString;
|
|
|
|
|
2016-07-07 23:05:10 +03:00
|
|
|
/**
|
|
|
|
* Appointments Controller
|
|
|
|
*
|
|
|
|
* @package Controllers
|
2016-07-08 22:17:06 +03:00
|
|
|
* @subpackage API
|
2016-07-07 23:05:10 +03:00
|
|
|
*/
|
2016-07-08 22:17:06 +03:00
|
|
|
class Appointments extends API_V1_Controller {
|
2016-07-09 14:14:08 +03:00
|
|
|
/**
|
2016-07-09 22:53:28 +03:00
|
|
|
* Appointments Resource Parser
|
2016-07-09 14:14:08 +03:00
|
|
|
*
|
2016-07-09 22:53:28 +03:00
|
|
|
* @var \EA\Engine\Api\V1\Parsers\Appointments
|
2016-07-09 14:14:08 +03:00
|
|
|
*/
|
2016-07-09 22:53:28 +03:00
|
|
|
protected $parser;
|
2016-07-09 14:14:08 +03:00
|
|
|
|
2016-07-08 22:17:06 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2016-07-09 14:14:08 +03:00
|
|
|
$this->load->model('appointments_model');
|
2016-07-09 22:53:28 +03:00
|
|
|
$this->parser = new \EA\Engine\Api\V1\Parsers\Appointments;
|
2016-07-08 22:17:06 +03:00
|
|
|
}
|
2016-07-07 23:05:10 +03:00
|
|
|
|
2016-07-08 22:17:06 +03:00
|
|
|
/**
|
|
|
|
* GET API Method
|
|
|
|
*
|
|
|
|
* @param int $id Optional (null), the record ID to be returned.
|
|
|
|
*/
|
2016-07-07 23:05:10 +03:00
|
|
|
public function get($id = null) {
|
2016-07-09 23:01:03 +03:00
|
|
|
try {
|
|
|
|
$condition = $id !== null ? 'id = ' . $id : null;
|
|
|
|
$appointments = $this->appointments_model->get_batch($condition);
|
2016-07-09 14:14:08 +03:00
|
|
|
|
2016-07-09 23:07:26 +03:00
|
|
|
if ($id !== null && count($appointments) === 0) {
|
|
|
|
throw new \EA\Engine\Api\V1\Exception('The requested appointment record was not found!', 404,
|
|
|
|
'Not Found');
|
|
|
|
}
|
|
|
|
|
2016-07-09 23:01:03 +03:00
|
|
|
$response = new Response($appointments);
|
|
|
|
$response->encode($this->parser)->search()->sort()->paginate()->minimize();
|
2016-07-09 22:11:33 +03:00
|
|
|
|
2016-07-09 23:01:03 +03:00
|
|
|
if ($id !== null) {
|
|
|
|
$response->singleEntry();
|
|
|
|
}
|
2016-07-09 14:14:08 +03:00
|
|
|
|
2016-07-09 23:01:03 +03:00
|
|
|
$response->output();
|
|
|
|
} catch(\Exception $exception) {
|
|
|
|
exit($this->_handleException($exception));
|
|
|
|
}
|
2016-07-07 23:05:10 +03:00
|
|
|
}
|
|
|
|
|
2016-07-08 22:17:06 +03:00
|
|
|
/**
|
|
|
|
* POST API Method
|
|
|
|
*/
|
2016-07-07 23:05:10 +03:00
|
|
|
public function post() {
|
2016-07-09 23:01:03 +03:00
|
|
|
try {
|
|
|
|
$request = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$this->parser->decode($request);
|
|
|
|
$id = $this->appointments_model->add($request);
|
|
|
|
$appointments = $this->appointments_model->get_batch('id = ' . $id);
|
|
|
|
$response = new Response($appointments);
|
|
|
|
$status = new NonEmptyString('201 Created');
|
|
|
|
$response->encode($this->parser)->singleEntry()->output($status);
|
|
|
|
} catch(\Exception $exception) {
|
|
|
|
exit($this->_handleException($exception));
|
|
|
|
}
|
2016-07-08 22:17:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PUT API Method
|
|
|
|
*
|
|
|
|
* @param int $id The record ID to be updated.
|
|
|
|
*/
|
|
|
|
public function put($id) {
|
2016-07-09 23:01:03 +03:00
|
|
|
try {
|
2016-07-09 23:07:26 +03:00
|
|
|
$appointment = $this->appointments_model->get_batch('id = ' . $id);
|
2016-07-08 22:17:06 +03:00
|
|
|
|
2016-07-09 23:07:26 +03:00
|
|
|
if ($id !== null && count($appointments) === 0) {
|
|
|
|
throw new \EA\Engine\Api\V1\Exception('The requested appointment record was not found!', 404,
|
|
|
|
'Not Found');
|
|
|
|
}
|
|
|
|
|
2016-07-09 23:01:03 +03:00
|
|
|
$request = json_decode(file_get_contents('php://input'), true);
|
|
|
|
$this->parser->decode($request, $appointment);
|
|
|
|
$request['id'] = $id;
|
|
|
|
$id = $this->appointments_model->add($request);
|
|
|
|
$appointments = $this->appointments_model->get_batch('id = ' . $id);
|
|
|
|
$response = new Response($appointments);
|
|
|
|
$status = new NonEmptyString('201 Created');
|
|
|
|
$response->encode($this->parser)->singleEntry()->output($status);
|
|
|
|
} catch(\Exception $exception) {
|
|
|
|
exit($this->_handleException($exception));
|
|
|
|
}
|
2016-07-08 22:17:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE API Method
|
|
|
|
*
|
|
|
|
* @param int $id The record ID to be deleted.
|
|
|
|
*/
|
|
|
|
public function delete($id) {
|
2016-07-09 23:01:03 +03:00
|
|
|
try {
|
2016-07-09 23:07:26 +03:00
|
|
|
$result = $this->appointments_model->delete($id);
|
|
|
|
|
|
|
|
$response = new Response([
|
|
|
|
'code' => 200,
|
|
|
|
'message' => 'Appointment was deleted successfully!'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->output();
|
2016-07-08 22:17:06 +03:00
|
|
|
|
2016-07-09 23:01:03 +03:00
|
|
|
} catch(\Exception $exception) {
|
|
|
|
exit($this->_handleException($exception));
|
|
|
|
}
|
2016-07-07 23:05:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 22:23:03 +03:00
|
|
|
/* End of file Appointments.php */
|
|
|
|
/* Location: ./application/controllers/api/v1/Appointments.php */
|