2022-06-19 20:05:45 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.5.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Webhooks API v1 controller.
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Webhooks_api_v1 extends EA_Controller
|
|
|
|
{
|
2022-06-19 20:05:45 +03:00
|
|
|
/**
|
|
|
|
* Webhooks_api_v1 constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->library('api');
|
|
|
|
|
|
|
|
$this->api->auth();
|
|
|
|
|
|
|
|
$this->api->model('webhooks_model');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a webhook collection.
|
|
|
|
*/
|
2024-01-05 21:12:25 +03:00
|
|
|
public function index(): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2022-06-19 20:05:45 +03:00
|
|
|
$keyword = $this->api->request_keyword();
|
|
|
|
|
|
|
|
$limit = $this->api->request_limit();
|
|
|
|
|
|
|
|
$offset = $this->api->request_offset();
|
|
|
|
|
|
|
|
$order_by = $this->api->request_order_by();
|
|
|
|
|
|
|
|
$fields = $this->api->request_fields();
|
2023-11-29 12:24:09 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$with = $this->api->request_with();
|
|
|
|
|
|
|
|
$webhooks = empty($keyword)
|
2023-11-29 12:24:09 +03:00
|
|
|
? $this->webhooks_model->get(null, $limit, $offset, $order_by)
|
2022-06-19 20:05:45 +03:00
|
|
|
: $this->webhooks_model->search($keyword, $limit, $offset, $order_by);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($webhooks as &$webhook) {
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_model->api_encode($webhook);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($fields)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_model->only($webhook, $fields);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($with)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_model->load($webhook, $with);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
json_response($webhooks);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a single webhook.
|
|
|
|
*
|
|
|
|
* @param int|null $id Webhook ID.
|
|
|
|
*/
|
2024-01-05 21:12:25 +03:00
|
|
|
public function show(int $id = null): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2022-06-19 20:05:45 +03:00
|
|
|
$fields = $this->api->request_fields();
|
2023-11-29 12:24:09 +03:00
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$with = $this->api->request_with();
|
|
|
|
|
|
|
|
$webhook = $this->webhooks_model->find($id);
|
|
|
|
|
|
|
|
$this->webhooks_model->api_encode($webhook);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($fields)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_model->only($webhook, $fields);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($with)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_model->load($webhook, $with);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$webhook) {
|
2022-06-19 20:05:45 +03:00
|
|
|
response('', 404);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_response($webhook);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-26 06:41:25 +03:00
|
|
|
* Store a new webhook.
|
2022-06-19 20:05:45 +03:00
|
|
|
*/
|
2024-01-05 21:12:25 +03:00
|
|
|
public function store(): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2022-06-19 20:05:45 +03:00
|
|
|
$webhook = request();
|
|
|
|
|
|
|
|
$this->webhooks_model->api_decode($webhook);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('id', $webhook)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
unset($webhook['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhook_id = $this->webhooks_model->save($webhook);
|
|
|
|
|
|
|
|
$created_webhook = $this->webhooks_model->find($webhook_id);
|
|
|
|
|
|
|
|
$this->webhooks_model->api_encode($created_webhook);
|
|
|
|
|
|
|
|
json_response($created_webhook, 201);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a webhook.
|
|
|
|
*
|
|
|
|
* @param int $id Webhook ID.
|
|
|
|
*/
|
2024-01-05 21:12:25 +03:00
|
|
|
public function update(int $id): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2022-06-19 20:05:45 +03:00
|
|
|
$occurrences = $this->webhooks_model->get(['id' => $id]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($occurrences)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
response('', 404);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$original_webhook = $occurrences[0];
|
|
|
|
|
|
|
|
$webhook = request();
|
|
|
|
|
|
|
|
$this->webhooks_model->api_decode($webhook, $original_webhook);
|
|
|
|
|
|
|
|
$webhook_id = $this->webhooks_model->save($webhook);
|
|
|
|
|
|
|
|
$updated_webhook = $this->webhooks_model->find($webhook_id);
|
|
|
|
|
|
|
|
$this->webhooks_model->api_encode($updated_webhook);
|
|
|
|
|
|
|
|
json_response($updated_webhook);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a webhook.
|
|
|
|
*
|
|
|
|
* @param int $id Webhook ID.
|
|
|
|
*/
|
2024-01-05 21:12:25 +03:00
|
|
|
public function destroy(int $id): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
2022-06-19 20:05:45 +03:00
|
|
|
$occurrences = $this->webhooks_model->get(['id' => $id]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($occurrences)) {
|
2022-06-19 20:05:45 +03:00
|
|
|
response('', 404);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->webhooks_model->delete($id);
|
|
|
|
|
|
|
|
response('', 204);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2022-06-19 20:05:45 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|