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.3.2
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Webhooks model.
|
|
|
|
*
|
|
|
|
* Handles all the database operations of the webhook resource.
|
|
|
|
*
|
|
|
|
* @package Models
|
|
|
|
*/
|
|
|
|
class Webhooks_model extends EA_Model {
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected array $casts = [
|
2022-06-19 20:05:45 +03:00
|
|
|
'id' => 'integer',
|
|
|
|
'is_active' => 'boolean',
|
|
|
|
'is_ssl_verified' => 'boolean',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected array $api_resource = [
|
2022-06-19 20:05:45 +03:00
|
|
|
'id' => 'id',
|
|
|
|
'name' => 'name',
|
|
|
|
'url' => 'url',
|
|
|
|
'action' => 'action',
|
|
|
|
'secretToken' => 'secret_token',
|
|
|
|
'isActive' => 'is_active',
|
|
|
|
'isSslVerified' => 'is_ssl_verified',
|
|
|
|
'notes' => 'notes',
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save (insert or update) a webhook.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
*
|
|
|
|
* @return int Returns the webhook ID.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function save(array $webhook): int
|
|
|
|
{
|
|
|
|
$this->validate($webhook);
|
|
|
|
|
|
|
|
if (empty($webhook['id']))
|
|
|
|
{
|
|
|
|
return $this->insert($webhook);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->update($webhook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the webhook data.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function validate(array $webhook)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
empty($webhook['name'])
|
|
|
|
|| empty($webhook['url'])
|
|
|
|
|| empty($webhook['actions'])
|
|
|
|
)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($webhook, TRUE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a new webhook into the database.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
*
|
|
|
|
* @return int Returns the webhook ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function insert(array $webhook): int
|
|
|
|
{
|
|
|
|
$webhook['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$webhook['update_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
if ( ! $this->db->insert('webhooks', $webhook))
|
|
|
|
{
|
|
|
|
throw new RuntimeException('Could not insert webhook.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->db->insert_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing webhook.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
*
|
|
|
|
* @return int Returns the webhook ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
protected function update(array $webhook): int
|
|
|
|
{
|
|
|
|
$webhook['update_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
|
|
|
|
if ( ! $this->db->update('webhooks', $webhook, ['id' => $webhook['id']]))
|
|
|
|
{
|
|
|
|
throw new RuntimeException('Could not update webhook.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhook['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an existing webhook from the database.
|
|
|
|
*
|
|
|
|
* @param int $webhook_id Webhook ID.
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
public function delete(int $webhook_id): void
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
$this->db->delete('webhooks', ['id' => $webhook_id]);
|
2022-06-19 20:05:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific webhook from the database.
|
|
|
|
*
|
|
|
|
* @param int $webhook_id The ID of the record to be returned.
|
|
|
|
*
|
|
|
|
* @return array Returns an array with the webhook data.
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
public function find(int $webhook_id): array
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
|
|
|
$webhook = $this->db->get_where('webhooks', ['id' => $webhook_id])->row_array();
|
|
|
|
|
|
|
|
if ( ! $webhook)
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('The provided webhook ID was not found in the database: ' . $webhook_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cast($webhook);
|
|
|
|
|
|
|
|
return $webhook;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific field value from the database.
|
|
|
|
*
|
|
|
|
* @param int $webhook_id Webhook ID.
|
|
|
|
* @param string $field Name of the value to be returned.
|
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
* @return mixed Returns the selected webhook value from the database.
|
2022-06-19 20:05:45 +03:00
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
public function value(int $webhook_id, string $field): mixed
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
|
|
|
if (empty($field))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($webhook_id))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('The webhook ID argument cannot be empty.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the webhook exists.
|
|
|
|
$query = $this->db->get_where('webhooks', ['id' => $webhook_id]);
|
|
|
|
|
|
|
|
if ( ! $query->num_rows())
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('The provided webhook ID was not found in the database: ' . $webhook_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the required field is part of the webhook data.
|
|
|
|
$webhook = $query->row_array();
|
|
|
|
|
|
|
|
$this->cast($webhook);
|
|
|
|
|
|
|
|
if ( ! array_key_exists($field, $webhook))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('The requested field was not found in the webhook data: ' . $field);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhook[$field];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all webhooks that match the provided criteria.
|
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
* @param array|string|null $where Where conditions.
|
2022-06-19 20:05:45 +03:00
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
*
|
|
|
|
* @return array Returns an array of webhooks.
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
public function get(array|string $where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
|
|
|
if ($where !== NULL)
|
|
|
|
{
|
|
|
|
$this->db->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order_by !== NULL)
|
|
|
|
{
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhooks = $this->db->get('webhooks', $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
foreach ($webhooks as &$webhook)
|
|
|
|
{
|
|
|
|
$this->cast($webhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the query builder interface, configured for use with the webhooks table.
|
|
|
|
*
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
*/
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
{
|
|
|
|
return $this->db->from('webhooks');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search webhooks by the provided keyword.
|
|
|
|
*
|
|
|
|
* @param string $keyword Search keyword.
|
|
|
|
* @param int|null $limit Record limit.
|
|
|
|
* @param int|null $offset Record offset.
|
|
|
|
* @param string|null $order_by Order by.
|
|
|
|
*
|
|
|
|
* @return array Returns an array of webhooks.
|
|
|
|
*/
|
2023-10-19 17:25:45 +03:00
|
|
|
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
|
2022-06-19 20:05:45 +03:00
|
|
|
{
|
|
|
|
$webhooks = $this
|
|
|
|
->db
|
|
|
|
->select()
|
|
|
|
->from('webhooks')
|
|
|
|
->group_start()
|
|
|
|
->like('name', $keyword)
|
|
|
|
->or_like('url', $keyword)
|
|
|
|
->or_like('actions', $keyword)
|
|
|
|
->group_end()
|
|
|
|
->limit($limit)
|
|
|
|
->offset($offset)
|
|
|
|
->order_by($order_by)
|
|
|
|
->get()
|
|
|
|
->result_array();
|
|
|
|
|
|
|
|
foreach ($webhooks as &$webhook)
|
|
|
|
{
|
|
|
|
$this->cast($webhook);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webhooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load related resources to a webhook.
|
|
|
|
*
|
|
|
|
* @param array $webhook Associative array with the webhook data.
|
|
|
|
* @param array $resources Resource names to be attached.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function load(array &$webhook, array $resources)
|
|
|
|
{
|
2023-10-19 17:25:45 +03:00
|
|
|
// Webhooks do not currently have any related resources.
|
2022-06-19 20:05:45 +03:00
|
|
|
}
|
|
|
|
}
|