2021-11-18 07:58:09 +03:00
|
|
|
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-11-18 07:58:09 +03:00
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2021-12-18 19:43:45 +03:00
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
2021-11-18 07:58:09 +03:00
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Providers controller.
|
|
|
|
*
|
|
|
|
* Handles the providers related operations.
|
|
|
|
*
|
|
|
|
* @package Controllers
|
|
|
|
*/
|
2023-11-29 12:24:09 +03:00
|
|
|
class Providers extends EA_Controller
|
|
|
|
{
|
2021-11-18 07:58:09 +03:00
|
|
|
/**
|
|
|
|
* Providers constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->load->model('providers_model');
|
|
|
|
$this->load->model('services_model');
|
|
|
|
$this->load->model('roles_model');
|
|
|
|
|
|
|
|
$this->load->library('accounts');
|
|
|
|
$this->load->library('timezones');
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->load->library('webhooks_client');
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the backend providers page.
|
|
|
|
*
|
2022-01-11 12:54:46 +03:00
|
|
|
* On this page admin users will be able to manage providers, which are eventually selected by customers during the
|
2021-11-18 07:58:09 +03:00
|
|
|
* booking process.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function index(): void
|
2021-11-18 07:58:09 +03:00
|
|
|
{
|
|
|
|
session(['dest_url' => site_url('providers')]);
|
|
|
|
|
2022-01-17 20:24:02 +03:00
|
|
|
$user_id = session('user_id');
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
if (cannot('view', PRIV_USERS)) {
|
|
|
|
if ($user_id) {
|
2022-01-17 20:24:02 +03:00
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
redirect('login');
|
|
|
|
|
|
|
|
return;
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$role_slug = session('role_slug');
|
2023-03-01 10:26:58 +03:00
|
|
|
|
|
|
|
$services = $this->services_model->get();
|
|
|
|
|
2023-11-29 12:24:09 +03:00
|
|
|
foreach ($services as &$service) {
|
2023-03-01 10:26:58 +03:00
|
|
|
$this->services_model->only($service, ['id', 'name']);
|
2022-01-11 12:54:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
script_vars([
|
|
|
|
'user_id' => $user_id,
|
|
|
|
'role_slug' => $role_slug,
|
|
|
|
'company_working_plan' => setting('company_working_plan'),
|
|
|
|
'date_format' => setting('date_format'),
|
|
|
|
'time_format' => setting('time_format'),
|
|
|
|
'first_weekday' => setting('first_weekday'),
|
|
|
|
'min_password_length' => MIN_PASSWORD_LENGTH,
|
|
|
|
'timezones' => $this->timezones->to_array(),
|
2023-12-22 13:35:41 +03:00
|
|
|
'services' => $services,
|
2024-03-27 18:09:15 +03:00
|
|
|
'default_language' => setting('default_language'),
|
|
|
|
'default_timezone' => setting('default_timezone'),
|
2022-01-11 12:54:46 +03:00
|
|
|
]);
|
|
|
|
|
2021-12-18 19:22:40 +03:00
|
|
|
html_vars([
|
2021-11-18 07:58:09 +03:00
|
|
|
'page_title' => lang('providers'),
|
|
|
|
'active_menu' => PRIV_USERS,
|
|
|
|
'user_display_name' => $this->accounts->get_user_display_name($user_id),
|
2022-03-29 11:16:07 +03:00
|
|
|
'grouped_timezones' => $this->timezones->to_grouped_array(),
|
2021-11-18 07:58:09 +03:00
|
|
|
'privileges' => $this->roles_model->get_permissions_by_slug($role_slug),
|
2023-12-22 13:35:41 +03:00
|
|
|
'services' => $this->services_model->get(),
|
2021-11-18 07:58:09 +03:00
|
|
|
]);
|
2021-12-18 19:22:40 +03:00
|
|
|
|
2022-01-19 12:21:05 +03:00
|
|
|
$this->load->view('pages/providers');
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter providers by the provided keyword.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function search(): void
|
2021-11-18 07:58:09 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('view', PRIV_USERS)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$keyword = request('keyword', '');
|
|
|
|
|
2023-05-04 18:55:29 +03:00
|
|
|
$order_by = 'update_datetime DESC';
|
2021-11-18 07:58:09 +03:00
|
|
|
|
|
|
|
$limit = request('limit', 1000);
|
2022-01-11 12:54:46 +03:00
|
|
|
|
2021-11-18 07:58:09 +03:00
|
|
|
$offset = 0;
|
|
|
|
|
|
|
|
$providers = $this->providers_model->search($keyword, $limit, $offset, $order_by);
|
|
|
|
|
|
|
|
json_response($providers);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 07:58:09 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-26 06:41:25 +03:00
|
|
|
* Store a new provider.
|
2021-11-18 07:58:09 +03:00
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function store(): void
|
2021-11-18 07:58:09 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('add', PRIV_USERS)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:50:40 +03:00
|
|
|
$provider = request('provider');
|
|
|
|
|
2022-06-27 11:27:46 +03:00
|
|
|
$this->providers_model->only($provider, [
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
'alt_number',
|
|
|
|
'phone_number',
|
|
|
|
'address',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'zip_code',
|
|
|
|
'notes',
|
2022-10-03 17:05:55 +03:00
|
|
|
'timezone',
|
|
|
|
'language',
|
2022-06-27 11:27:46 +03:00
|
|
|
'is_private',
|
|
|
|
'id_roles',
|
|
|
|
'settings',
|
2023-12-22 13:35:41 +03:00
|
|
|
'services',
|
2022-06-27 11:27:46 +03:00
|
|
|
]);
|
|
|
|
|
2023-01-14 11:38:49 +03:00
|
|
|
$this->providers_model->only($provider['settings'], [
|
|
|
|
'username',
|
|
|
|
'password',
|
2023-03-01 10:26:58 +03:00
|
|
|
'working_plan',
|
|
|
|
'working_plan_exceptions',
|
2023-01-14 11:38:49 +03:00
|
|
|
'notifications',
|
2023-12-22 13:35:41 +03:00
|
|
|
'calendar_view',
|
2023-01-14 11:38:49 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->providers_model->optional($provider, [
|
2023-12-22 13:35:41 +03:00
|
|
|
'services' => [],
|
2023-01-14 11:38:49 +03:00
|
|
|
]);
|
|
|
|
|
2021-11-18 07:58:09 +03:00
|
|
|
$provider_id = $this->providers_model->save($provider);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$provider = $this->providers_model->find($provider_id);
|
|
|
|
|
|
|
|
$this->webhooks_client->trigger(WEBHOOK_PROVIDER_SAVE, $provider);
|
|
|
|
|
2021-11-18 07:58:09 +03:00
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => $provider_id,
|
2021-11-18 07:58:09 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
json_exception($e);
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|
2023-11-29 12:24:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a provider.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function find(): void
|
2023-11-29 12:24:09 +03:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (cannot('view', PRIV_USERS)) {
|
|
|
|
abort(403, 'Forbidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
$provider_id = request('provider_id');
|
|
|
|
|
|
|
|
$provider = $this->providers_model->find($provider_id);
|
|
|
|
|
|
|
|
json_response($provider);
|
|
|
|
} catch (Throwable $e) {
|
2021-11-18 07:58:09 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a provider.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function update(): void
|
2021-11-18 07:58:09 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('edit', PRIV_USERS)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:50:40 +03:00
|
|
|
$provider = request('provider');
|
|
|
|
|
2022-06-27 11:27:46 +03:00
|
|
|
$this->providers_model->only($provider, [
|
|
|
|
'id',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
'alt_number',
|
|
|
|
'phone_number',
|
|
|
|
'address',
|
|
|
|
'city',
|
|
|
|
'state',
|
|
|
|
'zip_code',
|
|
|
|
'notes',
|
2022-10-03 17:05:55 +03:00
|
|
|
'timezone',
|
|
|
|
'language',
|
2022-06-27 11:27:46 +03:00
|
|
|
'is_private',
|
|
|
|
'id_roles',
|
|
|
|
'settings',
|
2023-12-22 13:35:41 +03:00
|
|
|
'services',
|
2022-06-27 11:27:46 +03:00
|
|
|
]);
|
|
|
|
|
2023-03-01 10:26:58 +03:00
|
|
|
$this->providers_model->only($provider['settings'], [
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'working_plan',
|
|
|
|
'working_plan_exceptions',
|
|
|
|
'notifications',
|
2023-12-22 13:35:41 +03:00
|
|
|
'calendar_view',
|
2023-03-01 10:26:58 +03:00
|
|
|
]);
|
|
|
|
|
2023-01-14 11:38:49 +03:00
|
|
|
$this->providers_model->optional($provider, [
|
2023-12-22 13:35:41 +03:00
|
|
|
'services' => [],
|
2023-01-14 11:38:49 +03:00
|
|
|
]);
|
|
|
|
|
2021-11-18 07:58:09 +03:00
|
|
|
$provider_id = $this->providers_model->save($provider);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$provider = $this->providers_model->find($provider_id);
|
|
|
|
|
|
|
|
$this->webhooks_client->trigger(WEBHOOK_PROVIDER_SAVE, $provider);
|
|
|
|
|
2021-11-18 07:58:09 +03:00
|
|
|
json_response([
|
2023-11-29 12:24:09 +03:00
|
|
|
'success' => true,
|
2023-12-22 13:35:41 +03:00
|
|
|
'id' => $provider_id,
|
2021-11-18 07:58:09 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-11-18 07:58:09 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a provider.
|
|
|
|
*/
|
2024-04-26 17:36:14 +03:00
|
|
|
public function destroy(): void
|
2021-11-18 07:58:09 +03:00
|
|
|
{
|
2023-11-29 12:24:09 +03:00
|
|
|
try {
|
|
|
|
if (cannot('delete', PRIV_USERS)) {
|
2021-12-18 19:22:40 +03:00
|
|
|
abort(403, 'Forbidden');
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$provider_id = request('provider_id');
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$provider = $this->providers_model->find($provider_id);
|
|
|
|
|
2021-11-18 07:58:09 +03:00
|
|
|
$this->providers_model->delete($provider_id);
|
|
|
|
|
2022-06-19 20:05:45 +03:00
|
|
|
$this->webhooks_client->trigger(WEBHOOK_PROVIDER_DELETE, $provider);
|
|
|
|
|
2021-11-18 07:58:09 +03:00
|
|
|
json_response([
|
2023-12-22 13:35:41 +03:00
|
|
|
'success' => true,
|
2021-11-18 07:58:09 +03:00
|
|
|
]);
|
2023-11-29 12:24:09 +03:00
|
|
|
} catch (Throwable $e) {
|
2021-12-14 09:18:46 +03:00
|
|
|
json_exception($e);
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 07:58:09 +03:00
|
|
|
}
|