2020-04-22 22:48:56 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
2016-01-01 16:52:43 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2016-01-01 16:52:43 +02:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
2016-01-01 16:52:43 +02:00
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2021-11-06 18:21:27 +03:00
|
|
|
* Providers model.
|
2016-01-01 16:52:43 +02:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* Handles all the database operations of the provider resource.
|
2016-01-01 16:52:43 +02:00
|
|
|
*
|
|
|
|
* @package Models
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Providers_model extends EA_Model
|
|
|
|
{
|
2021-10-29 13:39:02 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected array $casts = [
|
2021-10-29 13:39:02 +03:00
|
|
|
'id' => 'integer',
|
2022-01-24 22:50:14 +03:00
|
|
|
'is_private' => 'boolean',
|
2023-11-29 12:24:09 +03:00
|
|
|
'id_roles' => 'integer'
|
2021-10-29 13:39:02 +03:00
|
|
|
];
|
|
|
|
|
2021-11-06 17:02:40 +03:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2023-03-13 11:06:18 +03:00
|
|
|
protected array $api_resource = [
|
2021-11-06 17:02:40 +03:00
|
|
|
'id' => 'id',
|
|
|
|
'firstName' => 'first_name',
|
|
|
|
'lastName' => 'last_name',
|
|
|
|
'email' => 'email',
|
|
|
|
'mobile' => 'mobile_number',
|
|
|
|
'phone' => 'phone_number',
|
|
|
|
'address' => 'address',
|
|
|
|
'city' => 'city',
|
|
|
|
'state' => 'state',
|
|
|
|
'zip' => 'zip_code',
|
|
|
|
'timezone' => 'timezone',
|
|
|
|
'language' => 'language',
|
|
|
|
'notes' => 'notes',
|
2022-01-24 22:50:14 +03:00
|
|
|
'isPrivate' => 'is_private',
|
2023-11-29 12:24:09 +03:00
|
|
|
'roleId' => 'id_roles'
|
2021-11-06 17:02:40 +03:00
|
|
|
];
|
2021-10-29 13:39:02 +03:00
|
|
|
|
2016-01-01 16:52:43 +02:00
|
|
|
/**
|
2021-10-27 11:04:21 +03:00
|
|
|
* Save (insert or update) a provider.
|
2016-01-01 16:52:43 +02:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @param array $provider Associative array with the provider data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @return int Returns the provider ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @throws InvalidArgumentException
|
2023-03-13 11:06:18 +03:00
|
|
|
* @throws Exception
|
2016-01-01 16:52:43 +02:00
|
|
|
*/
|
2021-10-27 11:04:21 +03:00
|
|
|
public function save(array $provider): int
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2016-01-01 16:52:43 +02:00
|
|
|
$this->validate($provider);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($provider['id'])) {
|
2021-10-27 11:04:21 +03:00
|
|
|
return $this->insert($provider);
|
2023-11-29 12:24:09 +03:00
|
|
|
} else {
|
2021-10-27 11:04:21 +03:00
|
|
|
return $this->update($provider);
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:21 +03:00
|
|
|
* Validate the provider data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @param array $provider Associative array with the provider data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @throws InvalidArgumentException
|
2016-01-01 16:52:43 +02:00
|
|
|
*/
|
2021-10-27 11:49:42 +03:00
|
|
|
public function validate(array $provider)
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2021-10-27 11:04:21 +03:00
|
|
|
// If a provider ID is provided then check whether the record really exists in the database.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($provider['id'])) {
|
2021-10-27 11:04:21 +03:00
|
|
|
$count = $this->db->get_where('users', ['id' => $provider['id']])->num_rows();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$count) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'The provided provider ID does not exist in the database: ' . $provider['id']
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
// Make sure all required fields are provided.
|
2021-10-27 11:04:21 +03:00
|
|
|
if (
|
2023-11-29 12:24:09 +03:00
|
|
|
empty($provider['first_name']) ||
|
|
|
|
empty($provider['last_name']) ||
|
|
|
|
empty($provider['email']) ||
|
|
|
|
empty($provider['phone_number'])
|
|
|
|
) {
|
|
|
|
throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($provider, true));
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
// Validate the email address.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!filter_var($provider['email'], FILTER_VALIDATE_EMAIL)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new InvalidArgumentException('Invalid email address provided: ' . $provider['email']);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate provider services.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($provider['services'])) {
|
2021-10-27 11:04:21 +03:00
|
|
|
// Make sure the provided service entries are numeric values.
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($provider['services'] as $service_id) {
|
|
|
|
if (!is_numeric($service_id)) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'The provided provider services are invalid: ' . print_r($provider, true)
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
// Make sure the username is unique.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($provider['settings']['username'])) {
|
|
|
|
$provider_id = $provider['id'] ?? null;
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->validate_username($provider['settings']['username'], $provider_id)) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'The provided username is already in use, please use a different one.'
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
// Validate the password.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($provider['settings']['password'])) {
|
|
|
|
if (strlen($provider['settings']['password']) < MIN_PASSWORD_LENGTH) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'The provider password must be at least ' . MIN_PASSWORD_LENGTH . ' characters long.'
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 17:25:45 +03:00
|
|
|
// New users must always have a password value set.
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($provider['id']) && empty($provider['settings']['password'])) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new InvalidArgumentException('The provider password cannot be empty when inserting a new record.');
|
2020-12-11 22:18:33 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
// Validate calendar view type value.
|
|
|
|
if (
|
2023-11-29 12:24:09 +03:00
|
|
|
!empty($provider['settings']['calendar_view']) &&
|
|
|
|
!in_array($provider['settings']['calendar_view'], [CALENDAR_VIEW_DEFAULT, CALENDAR_VIEW_TABLE])
|
|
|
|
) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'The provided calendar view is invalid: ' . $provider['settings']['calendar_view']
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
// Make sure the email address is unique.
|
2023-11-29 12:24:09 +03:00
|
|
|
$provider_id = $provider['id'] ?? null;
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$count = $this->db
|
2021-10-27 11:04:21 +03:00
|
|
|
->select()
|
2020-04-23 21:48:20 +03:00
|
|
|
->from('users')
|
2020-05-02 13:51:06 +03:00
|
|
|
->join('roles', 'roles.id = users.id_roles', 'inner')
|
2020-04-23 21:48:20 +03:00
|
|
|
->where('roles.slug', DB_SLUG_PROVIDER)
|
|
|
|
->where('users.email', $provider['email'])
|
2020-12-05 12:38:57 +03:00
|
|
|
->where('users.id !=', $provider_id)
|
2020-04-22 22:48:56 +03:00
|
|
|
->get()
|
|
|
|
->num_rows();
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($count > 0) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'The provided email address is already in use, please use a different one.'
|
|
|
|
);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:21 +03:00
|
|
|
* Validate the provider username.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @param string $username Provider username.
|
|
|
|
* @param int|null $provider_id Provider ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2020-04-22 22:48:56 +03:00
|
|
|
* @return bool Returns the validation result.
|
2016-01-01 16:52:43 +02:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function validate_username(string $username, int $provider_id = null): bool
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!empty($provider_id)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
$this->db->where('id_users !=', $provider_id);
|
2020-12-08 12:00:31 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
return $this->db
|
|
|
|
->from('users')
|
|
|
|
->join('user_settings', 'user_settings.id_users = users.id', 'inner')
|
|
|
|
->where(['username' => $username])
|
|
|
|
->get()
|
|
|
|
->num_rows() === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all providers that match the provided criteria.
|
|
|
|
*
|
|
|
|
* @param array|string|null $where Where conditions
|
|
|
|
* @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 providers.
|
|
|
|
*/
|
|
|
|
public function get(
|
|
|
|
array|string $where = null,
|
|
|
|
int $limit = null,
|
|
|
|
int $offset = null,
|
|
|
|
string $order_by = null
|
|
|
|
): array {
|
|
|
|
$role_id = $this->get_provider_role_id();
|
|
|
|
|
|
|
|
if ($where !== null) {
|
|
|
|
$this->db->where($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($order_by !== null) {
|
|
|
|
$this->db->order_by($order_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
$providers = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
|
|
|
|
|
|
|
|
foreach ($providers as &$provider) {
|
|
|
|
$this->cast($provider);
|
|
|
|
|
|
|
|
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
|
|
|
|
|
|
|
|
unset($provider['settings']['id_users'], $provider['settings']['password'], $provider['settings']['salt']);
|
|
|
|
|
|
|
|
$provider['services'] = [];
|
|
|
|
|
|
|
|
$service_provider_connections = $this->db
|
|
|
|
->get_where('services_providers', ['id_users' => $provider['id']])
|
|
|
|
->result_array();
|
|
|
|
|
|
|
|
foreach ($service_provider_connections as $service_provider_connection) {
|
|
|
|
$provider['services'][] = (int) $service_provider_connection['id_services'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $providers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the provider role ID.
|
|
|
|
*
|
|
|
|
* @return int Returns the role ID.
|
|
|
|
*/
|
|
|
|
public function get_provider_role_id(): int
|
|
|
|
{
|
|
|
|
$role = $this->db->get_where('roles', ['slug' => DB_SLUG_PROVIDER])->row_array();
|
|
|
|
|
|
|
|
if (empty($role)) {
|
|
|
|
throw new RuntimeException('The provider role was not found in the database.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $role['id'];
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:21 +03:00
|
|
|
* Insert a new provider into the database.
|
2016-01-01 16:52:43 +02:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @param array $provider Associative array with the provider data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @return int Returns the provider ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
* @throws RuntimeException|Exception
|
2016-01-01 16:52:43 +02:00
|
|
|
*/
|
2021-10-27 11:04:21 +03:00
|
|
|
protected function insert(array $provider): int
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
$provider['create_datetime'] = date('Y-m-d H:i:s');
|
|
|
|
$provider['update_datetime'] = date('Y-m-d H:i:s');
|
2021-10-27 11:04:21 +03:00
|
|
|
$provider['id_roles'] = $this->get_provider_role_id();
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
$service_ids = $provider['services'];
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
$settings = $provider['settings'];
|
2022-11-30 13:21:39 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
unset($provider['services'], $provider['settings']);
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->db->insert('users', $provider)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new RuntimeException('Could not insert provider.');
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
$provider['id'] = $this->db->insert_id();
|
|
|
|
$settings['salt'] = generate_salt();
|
|
|
|
$settings['password'] = hash_password($settings['salt'], $settings['password']);
|
2017-09-15 14:36:37 +03:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
$this->save_settings($provider['id'], $settings);
|
|
|
|
$this->save_service_ids($provider['id'], $service_ids);
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
return $provider['id'];
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Save the provider settings.
|
|
|
|
*
|
|
|
|
* @param int $provider_id Provider ID.
|
|
|
|
* @param array $settings Associative array with the settings data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function save_settings(int $provider_id, array $settings)
|
|
|
|
{
|
|
|
|
if (empty($settings)) {
|
|
|
|
throw new InvalidArgumentException('The settings argument cannot be empty.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the settings record exists in the database.
|
|
|
|
$count = $this->db->get_where('user_settings', ['id_users' => $provider_id])->num_rows();
|
|
|
|
|
|
|
|
if (!$count) {
|
|
|
|
$this->db->insert('user_settings', ['id_users' => $provider_id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($settings as $name => $value) {
|
|
|
|
// Sort working plans exceptions in descending order that they are easier to modify later on.
|
|
|
|
if ($name === 'working_plan_exceptions') {
|
|
|
|
$value = json_decode($value, true);
|
|
|
|
|
|
|
|
if (!$value) {
|
|
|
|
$value = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
krsort($value);
|
|
|
|
|
|
|
|
$value = json_encode(empty($value) ? new stdClass() : $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->set_setting($provider_id, $name, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of a provider setting.
|
|
|
|
*
|
|
|
|
* @param int $provider_id Provider ID.
|
|
|
|
* @param string $name Setting name.
|
|
|
|
* @param mixed|null $value Setting value.
|
|
|
|
*/
|
|
|
|
public function set_setting(int $provider_id, string $name, mixed $value = null)
|
|
|
|
{
|
|
|
|
if (!$this->db->update('user_settings', [$name => $value], ['id_users' => $provider_id])) {
|
|
|
|
throw new RuntimeException('Could not set the new provider setting value: ' . $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 16:52:43 +02:00
|
|
|
/**
|
2021-10-27 11:04:21 +03:00
|
|
|
* Update an existing provider.
|
2016-01-01 16:52:43 +02:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @param array $provider Associative array with the provider data.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @return int Returns the provider ID.
|
2017-09-08 15:03:45 +03:00
|
|
|
*
|
2023-03-13 11:06:18 +03:00
|
|
|
* @throws RuntimeException|Exception
|
2016-01-01 16:52:43 +02:00
|
|
|
*/
|
2021-10-27 11:04:21 +03:00
|
|
|
protected function update(array $provider): int
|
2017-09-15 14:36:37 +03:00
|
|
|
{
|
2022-01-25 01:42:13 +03:00
|
|
|
$provider['update_datetime'] = date('Y-m-d H:i:s');
|
2022-11-30 13:21:39 +03:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
$service_ids = $provider['services'];
|
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
$settings = $provider['settings'];
|
2022-03-25 14:46:49 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
unset($provider['services'], $provider['settings']);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (isset($settings['password'])) {
|
2021-10-27 11:04:21 +03:00
|
|
|
$existing_settings = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($existing_settings)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new RuntimeException('No settings record found for provider with ID: ' . $provider['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$settings['password'] = hash_password($existing_settings['salt'], $settings['password']);
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$this->db->update('users', $provider, ['id' => $provider['id']])) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new RuntimeException('Could not update provider.');
|
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
$this->save_settings($provider['id'], $settings);
|
|
|
|
$this->save_service_ids($provider['id'], $service_ids);
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
return $provider['id'];
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-29 12:24:09 +03:00
|
|
|
* Save the provider service IDs.
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @param int $provider_id Provider ID.
|
2023-11-29 12:24:09 +03:00
|
|
|
* @param array $service_ids Service IDs.
|
2020-04-22 22:48:56 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
protected function save_service_ids(int $provider_id, array $service_ids)
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
// Re-insert the provider-service connections.
|
|
|
|
$this->db->delete('services_providers', ['id_users' => $provider_id]);
|
|
|
|
|
|
|
|
foreach ($service_ids as $service_id) {
|
|
|
|
$service_provider_connection = [
|
|
|
|
'id_users' => $provider_id,
|
|
|
|
'id_services' => $service_id
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->db->insert('services_providers', $service_provider_connection);
|
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-29 12:24:09 +03:00
|
|
|
* Remove an existing provider from the database.
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
2023-11-29 12:24:09 +03:00
|
|
|
* @param int $provider_id Provider ID.
|
2021-10-27 11:04:21 +03:00
|
|
|
*
|
2023-11-29 12:24:09 +03:00
|
|
|
* @throws RuntimeException
|
2020-04-22 22:48:56 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function delete(int $provider_id): void
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
$this->db->delete('users', ['id' => $provider_id]);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2020-04-22 22:48:56 +03:00
|
|
|
/**
|
2021-10-27 11:04:21 +03:00
|
|
|
* Get a specific field value from the database.
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @param int $provider_id Provider ID.
|
|
|
|
* @param string $field Name of the value to be returned.
|
2020-04-22 22:48:56 +03:00
|
|
|
*
|
2023-01-14 11:39:14 +03:00
|
|
|
* @return mixed Returns the selected provider value from the database.
|
2020-11-16 11:41:04 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @throws InvalidArgumentException
|
2020-04-22 22:48:56 +03:00
|
|
|
*/
|
2023-01-14 11:39:14 +03:00
|
|
|
public function value(int $provider_id, string $field): mixed
|
2020-04-22 22:48:56 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($field)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new InvalidArgumentException('The field argument is cannot be empty.');
|
|
|
|
}
|
2020-04-22 22:48:56 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($provider_id)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new InvalidArgumentException('The provider ID argument cannot be empty.');
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
// Check whether the provider exists.
|
|
|
|
$query = $this->db->get_where('users', ['id' => $provider_id]);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!$query->num_rows()) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'The provided provider ID was not found in the database: ' . $provider_id
|
|
|
|
);
|
2016-07-17 13:49:27 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
// Check if the required field is part of the provider data.
|
|
|
|
$provider = $query->row_array();
|
2020-12-05 12:38:57 +03:00
|
|
|
|
2021-10-29 13:39:02 +03:00
|
|
|
$this->cast($provider);
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists($field, $provider)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new InvalidArgumentException('The requested field was not found in the provider data: ' . $field);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
2021-10-27 11:04:21 +03:00
|
|
|
|
|
|
|
return $provider[$field];
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
/**
|
|
|
|
* Get the value of a provider setting.
|
|
|
|
*
|
|
|
|
* @param int $provider_id Provider ID.
|
|
|
|
* @param string $name Setting name.
|
|
|
|
*
|
|
|
|
* @return string Returns the value of the requested user setting.
|
|
|
|
*/
|
|
|
|
public function get_setting(int $provider_id, string $name): string
|
|
|
|
{
|
|
|
|
$settings = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array();
|
2016-01-01 16:52:43 +02:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists($name, $settings)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new RuntimeException('The requested setting value was not found: ' . $provider_id);
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
return $settings[$name];
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 16:03:48 +03:00
|
|
|
/**
|
2021-10-27 11:04:21 +03:00
|
|
|
* Save a new or existing working plan exception.
|
2018-04-12 16:03:46 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @param int $provider_id Provider ID.
|
|
|
|
* @param string $date Working plan exception date (in YYYY-MM-DD format).
|
|
|
|
* @param array $working_plan_exception Associative array with the working plan exception data.
|
2018-04-12 16:03:46 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @throws InvalidArgumentException
|
2023-03-13 11:06:18 +03:00
|
|
|
* @throws Exception
|
2018-04-12 16:03:46 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function save_working_plan_exception(int $provider_id, string $date, array $working_plan_exception = null)
|
2018-04-12 16:03:46 +03:00
|
|
|
{
|
2020-10-20 16:03:48 +03:00
|
|
|
// Validate the working plan exception data.
|
|
|
|
$start = date('H:i', strtotime($working_plan_exception['start']));
|
2021-10-27 11:04:21 +03:00
|
|
|
|
2020-10-20 16:03:48 +03:00
|
|
|
$end = date('H:i', strtotime($working_plan_exception['end']));
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($start > $end) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new InvalidArgumentException('Working plan exception start date must be before the end date.');
|
2018-04-12 16:03:46 +03:00
|
|
|
}
|
|
|
|
|
2020-10-20 16:03:48 +03:00
|
|
|
// Make sure the provider record exists.
|
2021-10-27 11:04:21 +03:00
|
|
|
$where = [
|
2018-04-12 16:03:46 +03:00
|
|
|
'id' => $provider_id,
|
2020-04-23 21:48:20 +03:00
|
|
|
'id_roles' => $this->db->get_where('roles', ['slug' => DB_SLUG_PROVIDER])->row()->id
|
2018-04-12 16:03:46 +03:00
|
|
|
];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($this->db->get_where('users', $where)->num_rows() === 0) {
|
2021-10-27 11:04:21 +03:00
|
|
|
throw new InvalidArgumentException('Provider ID was not found in the database: ' . $provider_id);
|
2018-04-12 16:03:46 +03:00
|
|
|
}
|
|
|
|
|
2022-01-18 17:50:38 +03:00
|
|
|
$provider = $this->find($provider_id);
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
// Store the working plan exception.
|
2023-11-29 12:24:09 +03:00
|
|
|
$working_plan_exceptions = json_decode($provider['settings']['working_plan_exceptions'], true);
|
2018-04-12 16:03:46 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (is_array($working_plan_exception) && !isset($working_plan_exception['breaks'])) {
|
2020-10-20 16:03:48 +03:00
|
|
|
$working_plan_exception['breaks'] = [];
|
|
|
|
}
|
2018-04-12 16:03:46 +03:00
|
|
|
|
2020-10-20 16:03:48 +03:00
|
|
|
$working_plan_exceptions[$date] = $working_plan_exception;
|
2018-04-12 16:03:46 +03:00
|
|
|
|
2022-01-18 17:50:38 +03:00
|
|
|
$provider['settings']['working_plan_exceptions'] = json_encode($working_plan_exceptions);
|
|
|
|
|
|
|
|
$this->update($provider);
|
2020-04-22 22:48:56 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
/**
|
|
|
|
* Get a specific provider from the database.
|
|
|
|
*
|
|
|
|
* @param int $provider_id The ID of the record to be returned.
|
|
|
|
*
|
|
|
|
* @return array Returns an array with the provider data.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function find(int $provider_id): array
|
|
|
|
{
|
|
|
|
$provider = $this->db->get_where('users', ['id' => $provider_id])->row_array();
|
|
|
|
|
|
|
|
if (!$provider) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'The provided provider ID was not found in the database: ' . $provider_id
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cast($provider);
|
|
|
|
|
|
|
|
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider_id])->row_array();
|
|
|
|
|
|
|
|
unset($provider['settings']['id_users'], $provider['settings']['password'], $provider['settings']['salt']);
|
|
|
|
|
|
|
|
$service_provider_connections = $this->db
|
|
|
|
->get_where('services_providers', ['id_users' => $provider_id])
|
|
|
|
->result_array();
|
|
|
|
|
|
|
|
$provider['services'] = [];
|
|
|
|
|
|
|
|
foreach ($service_provider_connections as $service_provider_connection) {
|
|
|
|
$provider['services'][] = (int) $service_provider_connection['id_services'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $provider;
|
|
|
|
}
|
|
|
|
|
2018-04-12 16:03:46 +03:00
|
|
|
/**
|
2020-10-20 16:03:48 +03:00
|
|
|
* Delete a provider working plan exception.
|
2018-04-12 16:03:46 +03:00
|
|
|
*
|
2020-10-20 16:03:48 +03:00
|
|
|
* @param string $date The working plan exception date (in YYYY-MM-DD format).
|
2018-04-12 16:03:46 +03:00
|
|
|
* @param int $provider_id The selected provider record id.
|
|
|
|
*
|
|
|
|
* @throws Exception If $provider_id argument is invalid.
|
|
|
|
*/
|
2021-10-27 11:49:42 +03:00
|
|
|
public function delete_working_plan_exception(int $provider_id, string $date)
|
2018-04-12 16:03:46 +03:00
|
|
|
{
|
2021-10-27 11:04:21 +03:00
|
|
|
$provider = $this->find($provider_id);
|
2020-10-20 16:03:48 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$working_plan_exceptions = json_decode($provider['settings']['working_plan_exceptions'], true);
|
2018-04-12 16:03:46 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (!array_key_exists($date, $working_plan_exceptions)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
return; // The selected date does not exist in provider's settings.
|
2018-04-12 16:03:46 +03:00
|
|
|
}
|
|
|
|
|
2020-10-20 16:03:48 +03:00
|
|
|
unset($working_plan_exceptions[$date]);
|
2018-04-12 16:03:46 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$provider['settings']['working_plan_exceptions'] = empty($working_plan_exceptions)
|
|
|
|
? '{}'
|
|
|
|
: $working_plan_exceptions;
|
2022-01-18 17:50:38 +03:00
|
|
|
|
|
|
|
$this->update($provider);
|
2018-04-12 16:03:46 +03:00
|
|
|
}
|
2020-12-05 12:38:57 +03:00
|
|
|
|
|
|
|
/**
|
2021-10-27 11:04:21 +03:00
|
|
|
* Get all the provider records that are assigned to at least one service.
|
2020-12-05 12:38:57 +03:00
|
|
|
*
|
2022-01-24 22:50:14 +03:00
|
|
|
* @param bool $without_private Only include the public providers.
|
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @return array Returns an array of providers.
|
2020-12-05 12:38:57 +03:00
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function get_available_providers(bool $without_private = false): array
|
2020-12-05 12:38:57 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if ($without_private) {
|
|
|
|
$this->db->where('users.is_private', false);
|
2022-01-24 22:50:14 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$providers = $this->db
|
2021-10-27 11:04:21 +03:00
|
|
|
->select('users.*')
|
|
|
|
->from('users')
|
|
|
|
->join('roles', 'roles.id = users.id_roles', 'inner')
|
|
|
|
->where('roles.slug', DB_SLUG_PROVIDER)
|
|
|
|
->order_by('first_name ASC, last_name ASC, email ASC')
|
|
|
|
->get()
|
|
|
|
->result_array();
|
2020-12-05 12:38:57 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($providers as &$provider) {
|
2023-12-12 13:58:07 +03:00
|
|
|
$this->cast($provider);
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
|
|
|
|
|
|
|
|
unset(
|
|
|
|
$provider['settings']['id_users'],
|
|
|
|
$provider['settings']['username'],
|
|
|
|
$provider['settings']['password'],
|
|
|
|
$provider['settings']['salt']
|
|
|
|
);
|
|
|
|
|
|
|
|
$provider['services'] = [];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$service_provider_connections = $this->db
|
|
|
|
->get_where('services_providers', ['id_users' => $provider['id']])
|
|
|
|
->result_array();
|
2021-10-27 11:04:21 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($service_provider_connections as $service_provider_connection) {
|
|
|
|
$provider['services'][] = (int) $service_provider_connection['id_services'];
|
2021-10-27 11:04:21 +03:00
|
|
|
}
|
2020-12-05 12:38:57 +03:00
|
|
|
}
|
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
return $providers;
|
|
|
|
}
|
2020-12-05 12:38:57 +03:00
|
|
|
|
2021-10-27 11:04:21 +03:00
|
|
|
/**
|
|
|
|
* Get the query builder interface, configured for use with the users (provider-filtered) table.
|
|
|
|
*
|
|
|
|
* @return CI_DB_query_builder
|
|
|
|
*/
|
|
|
|
public function query(): CI_DB_query_builder
|
|
|
|
{
|
|
|
|
$role_id = $this->get_provider_role_id();
|
|
|
|
|
|
|
|
return $this->db->from('users')->where('id_roles', $role_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search providers 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.
|
2022-11-30 13:21:39 +03:00
|
|
|
*
|
2021-10-27 11:04:21 +03:00
|
|
|
* @return array Returns an array of providers.
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function search(string $keyword, int $limit = null, int $offset = null, string $order_by = null): array
|
2021-10-27 11:04:21 +03:00
|
|
|
{
|
|
|
|
$role_id = $this->get_provider_role_id();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$providers = $this->db
|
2021-10-27 11:04:21 +03:00
|
|
|
->select()
|
|
|
|
->from('users')
|
|
|
|
->where('id_roles', $role_id)
|
2021-11-06 17:02:40 +03:00
|
|
|
->group_start()
|
2021-10-27 11:04:21 +03:00
|
|
|
->like('first_name', $keyword)
|
|
|
|
->or_like('last_name', $keyword)
|
2022-12-12 11:15:55 +03:00
|
|
|
->or_like('CONCAT_WS(" ", first_name, last_name)', $keyword)
|
2021-10-27 11:04:21 +03:00
|
|
|
->or_like('email', $keyword)
|
|
|
|
->or_like('phone_number', $keyword)
|
|
|
|
->or_like('mobile_number', $keyword)
|
|
|
|
->or_like('address', $keyword)
|
|
|
|
->or_like('city', $keyword)
|
|
|
|
->or_like('state', $keyword)
|
|
|
|
->or_like('zip_code', $keyword)
|
|
|
|
->or_like('notes', $keyword)
|
2021-11-06 17:02:40 +03:00
|
|
|
->group_end()
|
2021-10-27 11:04:21 +03:00
|
|
|
->limit($limit)
|
|
|
|
->offset($offset)
|
|
|
|
->order_by($order_by)
|
|
|
|
->get()
|
|
|
|
->result_array();
|
2021-10-29 13:39:02 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($providers as &$provider) {
|
2021-10-29 13:39:02 +03:00
|
|
|
$this->cast($provider);
|
|
|
|
|
|
|
|
$provider['settings'] = $this->db->get_where('user_settings', ['id_users' => $provider['id']])->row_array();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
unset($provider['settings']['id_users'], $provider['settings']['password'], $provider['settings']['salt']);
|
2021-10-29 13:39:02 +03:00
|
|
|
|
|
|
|
$provider['services'] = [];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
$service_provider_connections = $this->db
|
|
|
|
->get_where('services_providers', ['id_users' => $provider['id']])
|
|
|
|
->result_array();
|
2021-10-29 13:39:02 +03:00
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($service_provider_connections as $service_provider_connection) {
|
|
|
|
$provider['services'][] = (int) $service_provider_connection['id_services'];
|
2021-10-29 13:39:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $providers;
|
2021-10-27 11:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-06 17:40:55 +03:00
|
|
|
* Load related resources to a provider.
|
2021-10-27 11:04:21 +03:00
|
|
|
*
|
|
|
|
* @param array $provider Associative array with the provider data.
|
|
|
|
* @param array $resources Resource names to be attached ("services" supported).
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2021-11-06 17:40:55 +03:00
|
|
|
public function load(array &$provider, array $resources)
|
2021-10-27 11:04:21 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
if (empty($provider) || empty($resources)) {
|
2021-10-27 11:04:21 +03:00
|
|
|
return;
|
2020-12-05 12:38:57 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($resources as $resource) {
|
|
|
|
$provider['services'] = match ($resource) {
|
|
|
|
'services' => $this->db
|
2023-03-13 11:06:18 +03:00
|
|
|
->select('services.*')
|
|
|
|
->from('services')
|
|
|
|
->join('services_providers', 'services_providers.id_services = services.id', 'inner')
|
|
|
|
->where('id_users', $provider['id'])
|
|
|
|
->get()
|
|
|
|
->result_array(),
|
2023-11-29 12:24:09 +03:00
|
|
|
default => throw new InvalidArgumentException(
|
|
|
|
'The requested provider relation is not supported: ' . $resource
|
|
|
|
)
|
2023-03-13 11:06:18 +03:00
|
|
|
};
|
2021-10-27 11:04:21 +03:00
|
|
|
}
|
2020-12-05 12:38:57 +03:00
|
|
|
}
|
2021-11-05 11:32:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the database provider record to the equivalent API resource.
|
|
|
|
*
|
|
|
|
* @param array $provider Provider data.
|
|
|
|
*/
|
|
|
|
public function api_encode(array &$provider)
|
|
|
|
{
|
|
|
|
$encoded_resource = [
|
2023-11-29 12:24:09 +03:00
|
|
|
'id' => array_key_exists('id', $provider) ? (int) $provider['id'] : null,
|
2021-11-05 11:32:34 +03:00
|
|
|
'firstName' => $provider['first_name'],
|
|
|
|
'lastName' => $provider['last_name'],
|
|
|
|
'email' => $provider['email'],
|
|
|
|
'mobile' => $provider['mobile_number'],
|
|
|
|
'phone' => $provider['phone_number'],
|
|
|
|
'address' => $provider['address'],
|
|
|
|
'city' => $provider['city'],
|
|
|
|
'state' => $provider['state'],
|
|
|
|
'zip' => $provider['zip_code'],
|
|
|
|
'notes' => $provider['notes'],
|
2023-11-29 12:24:09 +03:00
|
|
|
'timezone' => $provider['timezone']
|
2021-11-05 11:32:34 +03:00
|
|
|
];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('services', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$encoded_resource['services'] = $provider['services'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('settings', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$encoded_resource['settings'] = [
|
|
|
|
'username' => $provider['settings']['username'],
|
|
|
|
'notifications' => filter_var($provider['settings']['notifications'], FILTER_VALIDATE_BOOLEAN),
|
|
|
|
'calendarView' => $provider['settings']['calendar_view'],
|
|
|
|
'googleSync' => array_key_exists('google_sync', $provider['settings'])
|
|
|
|
? filter_var($provider['settings']['google_sync'], FILTER_VALIDATE_BOOLEAN)
|
2023-11-29 12:24:09 +03:00
|
|
|
: null,
|
2021-11-05 11:32:34 +03:00
|
|
|
'googleCalendar' => array_key_exists('google_calendar', $provider['settings'])
|
|
|
|
? $provider['settings']['google_calendar']
|
2023-11-29 12:24:09 +03:00
|
|
|
: null,
|
2021-11-05 11:32:34 +03:00
|
|
|
'googleToken' => array_key_exists('google_token', $provider['settings'])
|
|
|
|
? $provider['settings']['google_token']
|
2023-11-29 12:24:09 +03:00
|
|
|
: null,
|
2021-11-05 11:32:34 +03:00
|
|
|
'syncFutureDays' => array_key_exists('sync_future_days', $provider['settings'])
|
2023-11-29 12:24:09 +03:00
|
|
|
? (int) $provider['settings']['sync_future_days']
|
|
|
|
: null,
|
2021-11-05 11:32:34 +03:00
|
|
|
'syncPastDays' => array_key_exists('sync_past_days', $provider['settings'])
|
2023-11-29 12:24:09 +03:00
|
|
|
? (int) $provider['settings']['sync_past_days']
|
|
|
|
: null,
|
2021-11-05 11:32:34 +03:00
|
|
|
'workingPlan' => array_key_exists('working_plan', $provider['settings'])
|
2023-11-29 12:24:09 +03:00
|
|
|
? json_decode($provider['settings']['working_plan'], true)
|
|
|
|
: null,
|
2021-11-05 11:32:34 +03:00
|
|
|
'workingPlanExceptions' => array_key_exists('working_plan_exceptions', $provider['settings'])
|
2023-11-29 12:24:09 +03:00
|
|
|
? json_decode($provider['settings']['working_plan_exceptions'], true)
|
|
|
|
: null
|
2021-11-05 11:32:34 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$provider = $encoded_resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the API resource to the equivalent database provider record.
|
|
|
|
*
|
|
|
|
* @param array $provider API resource.
|
|
|
|
* @param array|null $base Base provider data to be overwritten with the provided values (useful for updates).
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
public function api_decode(array &$provider, array $base = null)
|
2021-11-05 11:32:34 +03:00
|
|
|
{
|
|
|
|
$decoded_resource = $base ?: [];
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('id', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['id'] = $provider['id'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('firstName', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['first_name'] = $provider['firstName'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('lastName', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['last_name'] = $provider['lastName'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('email', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['email'] = $provider['email'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('mobile', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['mobile_number'] = $provider['mobile'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('phone', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['phone_number'] = $provider['phone'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('address', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['address'] = $provider['address'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('city', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['city'] = $provider['city'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('state', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['state'] = $provider['state'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('zip', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['zip_code'] = $provider['zip'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('notes', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['notes'] = $provider['notes'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('timezone', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['timezone'] = $provider['timezone'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('services', $provider)) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['services'] = $provider['services'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('settings', $provider)) {
|
|
|
|
if (empty($decoded_resource['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings'] = [];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('username', $provider['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings']['username'] = $provider['settings']['username'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('password', $provider['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings']['password'] = $provider['settings']['password'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('calendarView', $provider['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings']['calendar_view'] = $provider['settings']['calendarView'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('notifications', $provider['settings'])) {
|
|
|
|
$decoded_resource['settings']['notifications'] = filter_var(
|
|
|
|
$provider['settings']['notifications'],
|
|
|
|
FILTER_VALIDATE_BOOLEAN
|
|
|
|
);
|
2021-11-05 11:32:34 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('googleSync', $provider['settings'])) {
|
|
|
|
$decoded_resource['settings']['google_sync'] = filter_var(
|
|
|
|
$provider['settings']['googleSync'],
|
|
|
|
FILTER_VALIDATE_BOOLEAN
|
|
|
|
);
|
2021-11-05 11:32:34 +03:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('googleCalendar', $provider['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings']['google_calendar'] = $provider['settings']['googleCalendar'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('googleToken', $provider['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings']['google_token'] = $provider['settings']['googleToken'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('syncFutureDays', $provider['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings']['sync_future_days'] = $provider['settings']['syncFutureDays'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('syncPastDays', $provider['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings']['sync_past_days'] = $provider['settings']['syncPastDays'];
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('workingPlan', $provider['settings'])) {
|
2021-11-05 11:32:34 +03:00
|
|
|
$decoded_resource['settings']['working_plan'] = json_encode($provider['settings']['workingPlan']);
|
|
|
|
}
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (array_key_exists('workingPlanExceptions', $provider['settings'])) {
|
|
|
|
$decoded_resource['settings']['working_plan_exceptions'] = json_encode(
|
|
|
|
$provider['settings']['workingPlanExceptions']
|
|
|
|
);
|
2021-11-05 11:32:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$provider = $decoded_resource;
|
|
|
|
}
|
2023-04-15 15:34:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Quickly check if a service is assigned to a provider.
|
2023-07-10 09:22:55 +03:00
|
|
|
*
|
2023-04-15 15:34:23 +03:00
|
|
|
* @param int $provider_id
|
|
|
|
* @param int $service_id
|
2023-07-10 09:22:55 +03:00
|
|
|
*
|
2023-04-15 15:34:23 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function is_service_supported(int $provider_id, int $service_id): bool
|
|
|
|
{
|
|
|
|
$provider = $this->find($provider_id);
|
|
|
|
|
|
|
|
return in_array($service_id, $provider['services']);
|
|
|
|
}
|
2016-01-01 16:52:43 +02:00
|
|
|
}
|