|
| 1 | +<?php defined('BASEPATH') or exit('No direct script access allowed'); |
| 2 | + |
| 3 | +/* ---------------------------------------------------------------------------- |
| 4 | + * Easy!Appointments - Online Appointment Scheduler |
| 5 | + * |
| 6 | + * @package EasyAppointments |
| 7 | + * @author A.Tselegidis <[email protected]> |
| 8 | + * @copyright Copyright (c) Alex Tselegidis |
| 9 | + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 |
| 10 | + * @link https://easyappointments.org |
| 11 | + * @since v1.5.2 |
| 12 | + * ---------------------------------------------------------------------------- */ |
| 13 | + |
| 14 | +/** |
| 15 | + * Blocked_periods API v1 controller. |
| 16 | + * |
| 17 | + * @package Controllers |
| 18 | + */ |
| 19 | +class Blocked_periods_api_v1 extends EA_Controller |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Blocked_periods_api_v1 constructor. |
| 23 | + */ |
| 24 | + public function __construct() |
| 25 | + { |
| 26 | + parent::__construct(); |
| 27 | + |
| 28 | + $this->load->library('api'); |
| 29 | + |
| 30 | + $this->api->auth(); |
| 31 | + |
| 32 | + $this->api->model('blocked_periods_model'); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get an blocked_periods collection. |
| 37 | + */ |
| 38 | + public function index(): void |
| 39 | + { |
| 40 | + try { |
| 41 | + $keyword = $this->api->request_keyword(); |
| 42 | + |
| 43 | + $limit = $this->api->request_limit(); |
| 44 | + |
| 45 | + $offset = $this->api->request_offset(); |
| 46 | + |
| 47 | + $order_by = $this->api->request_order_by(); |
| 48 | + |
| 49 | + $fields = $this->api->request_fields(); |
| 50 | + |
| 51 | + $with = $this->api->request_with(); |
| 52 | + |
| 53 | + $where = null; |
| 54 | + |
| 55 | + // Date query param. |
| 56 | + |
| 57 | + $date = request('date'); |
| 58 | + |
| 59 | + if (!empty($date)) { |
| 60 | + $where['DATE(start_datetime)'] = (new DateTime($date))->format('Y-m-d'); |
| 61 | + } |
| 62 | + |
| 63 | + // From query param. |
| 64 | + |
| 65 | + $from = request('from'); |
| 66 | + |
| 67 | + if (!empty($from)) { |
| 68 | + $where['DATE(start_datetime) >='] = (new DateTime($from))->format('Y-m-d'); |
| 69 | + } |
| 70 | + |
| 71 | + // Till query param. |
| 72 | + |
| 73 | + $till = request('till'); |
| 74 | + |
| 75 | + if (!empty($till)) { |
| 76 | + $where['DATE(end_datetime) <='] = (new DateTime($till))->format('Y-m-d'); |
| 77 | + } |
| 78 | + |
| 79 | + $blockedperiods = empty($keyword) |
| 80 | + ? $this->blocked_periods_model->get($where, $limit, $offset, $order_by) |
| 81 | + : $this->blocked_periods_model->search($keyword, $limit, $offset, $order_by); |
| 82 | + |
| 83 | + foreach ($blockedperiods as &$blockedperiod) { |
| 84 | + $this->blocked_periods_model->api_encode($blockedperiod); |
| 85 | + |
| 86 | + if (!empty($fields)) { |
| 87 | + $this->blocked_periods_model->only($blockedperiod, $fields); |
| 88 | + } |
| 89 | + |
| 90 | + if (!empty($with)) { |
| 91 | + $this->blocked_periods_model->load($blockedperiod, $with); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + json_response($blockedperiods); |
| 96 | + } catch (Throwable $e) { |
| 97 | + json_exception($e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get a single blocked_period. |
| 103 | + * |
| 104 | + * @param int|null $id blocked_period ID. |
| 105 | + */ |
| 106 | + public function show(?int $id = null): void |
| 107 | + { |
| 108 | + try { |
| 109 | + $occurrences = $this->blocked_periods_model->get(['id' => $id]); |
| 110 | + |
| 111 | + if (empty($occurrences)) { |
| 112 | + response('', 404); |
| 113 | + |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + $fields = $this->api->request_fields(); |
| 118 | + |
| 119 | + $with = $this->api->request_with(); |
| 120 | + |
| 121 | + $blocked_period = $this->blocked_periods_model->find($id); |
| 122 | + |
| 123 | + $this->blocked_periods_model->api_encode($blocked_period); |
| 124 | + |
| 125 | + if (!empty($fields)) { |
| 126 | + $this->blocked_periods_model->only($blocked_period, $fields); |
| 127 | + } |
| 128 | + |
| 129 | + if (!empty($with)) { |
| 130 | + $this->blocked_periods_model->load($blocked_period, $with); |
| 131 | + } |
| 132 | + |
| 133 | + json_response($blocked_period); |
| 134 | + } catch (Throwable $e) { |
| 135 | + json_exception($e); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Store a new blocked_period. |
| 141 | + */ |
| 142 | + public function store(): void |
| 143 | + { |
| 144 | + try { |
| 145 | + $blocked_period = request(); |
| 146 | + |
| 147 | + $this->blocked_periods_model->api_decode($blocked_period); |
| 148 | + |
| 149 | + if (array_key_exists('id', $blocked_period)) { |
| 150 | + unset($blocked_period['id']); |
| 151 | + } |
| 152 | + |
| 153 | + $blocked_period_id = $this->blocked_periods_model->save($blocked_period); |
| 154 | + |
| 155 | + $created_blocked_period = $this->blocked_periods_model->find($blocked_period_id); |
| 156 | + |
| 157 | + $this->blocked_periods_model->api_encode($created_blocked_period); |
| 158 | + |
| 159 | + json_response($created_blocked_period, 201); |
| 160 | + } catch (Throwable $e) { |
| 161 | + json_exception($e); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Update an blocked_period. |
| 167 | + * |
| 168 | + * @param int $id blocked_period ID. |
| 169 | + */ |
| 170 | + public function update(int $id): void |
| 171 | + { |
| 172 | + try { |
| 173 | + $occurrences = $this->blocked_periods_model->get(['id' => $id]); |
| 174 | + |
| 175 | + if (empty($occurrences)) { |
| 176 | + response('', 404); |
| 177 | + |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + $original_blocked_period = $occurrences[0]; |
| 182 | + |
| 183 | + $blocked_period = request(); |
| 184 | + |
| 185 | + $this->blocked_periods_model->api_decode($blocked_period, $original_blocked_period); |
| 186 | + |
| 187 | + $blocked_period_id = $this->blocked_periods_model->save($blocked_period); |
| 188 | + |
| 189 | + $updated_blocked_period = $this->blocked_periods_model->find($blocked_period_id); |
| 190 | + |
| 191 | + $this->blocked_periods_model->api_encode($updated_blocked_period); |
| 192 | + |
| 193 | + json_response($updated_blocked_period); |
| 194 | + } catch (Throwable $e) { |
| 195 | + json_exception($e); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Delete an blocked_period. |
| 201 | + * |
| 202 | + * @param int $id blocked_period ID. |
| 203 | + */ |
| 204 | + public function destroy(int $id): void |
| 205 | + { |
| 206 | + try { |
| 207 | + $occurrences = $this->blocked_periods_model->get(['id' => $id]); |
| 208 | + |
| 209 | + if (empty($occurrences)) { |
| 210 | + response('', 404); |
| 211 | + |
| 212 | + return; |
| 213 | + } |
| 214 | + |
| 215 | + $this->blocked_periods_model->delete($id); |
| 216 | + |
| 217 | + response('', 204); |
| 218 | + } catch (Throwable $e) { |
| 219 | + json_exception($e); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments