From b4f903e7246f007fc5cd507b724ea530e9851349 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 6 Nov 2021 15:02:40 +0100 Subject: [PATCH] Various fixes and additions to the model classes --- application/models/Admins_model.php | 4 +- application/models/Appointments_model.php | 73 +++++-------------- application/models/Customers_model.php | 20 +++++ application/models/Providers_model.php | 31 ++++++-- application/models/Secretaries_model.php | 28 +++++-- .../models/Service_categories_model.php | 9 +++ application/models/Services_model.php | 16 ++++ application/models/Settings_model.php | 8 ++ application/models/Users_model.php | 20 +++++ 9 files changed, 143 insertions(+), 66 deletions(-) diff --git a/application/models/Admins_model.php b/application/models/Admins_model.php index ee7883d9..69733296 100644 --- a/application/models/Admins_model.php +++ b/application/models/Admins_model.php @@ -41,9 +41,9 @@ class Admins_model extends EA_Model { 'city' => 'city', 'state' => 'state', 'zip' => 'zip_code', - 'notes' => 'notes', 'timezone' => 'timezone', 'language' => 'language', + 'notes' => 'notes', 'roleId' => 'id_roles', ]; @@ -499,6 +499,7 @@ class Admins_model extends EA_Model { ->select() ->from('users') ->where('id_roles', $role_id) + ->group_start() ->like('first_name', $keyword) ->or_like('last_name', $keyword) ->or_like('email', $keyword) @@ -509,6 +510,7 @@ class Admins_model extends EA_Model { ->or_like('state', $keyword) ->or_like('zip_code', $keyword) ->or_like('notes', $keyword) + ->group_end() ->limit($limit) ->offset($offset) ->order_by($order_by) diff --git a/application/models/Appointments_model.php b/application/models/Appointments_model.php index 114ac8f6..a64743e0 100644 --- a/application/models/Appointments_model.php +++ b/application/models/Appointments_model.php @@ -28,6 +28,22 @@ class Appointments_model extends EA_Model { 'id_services' => 'integer', ]; + /** + * @var array + */ + protected $api_resource = [ + 'id' => 'id', + 'book' => 'book_datetime', + 'start' => 'start_datetime', + 'end' => 'end_datetime', + 'location' => 'location', + 'notes' => 'notes', + 'hash' => 'hash', + 'providerId' => 'id_users_provider', + 'googleCalendarId' => 'id_google_calendar', + ]; + + /** * Save (insert or update) an appointment. * @@ -179,8 +195,6 @@ class Appointments_model extends EA_Model { */ protected function update(array $appointment): int { - $this->db->where('id', $appointment['id']); - if ( ! $this->db->update('appointments', $appointment, ['id' => $appointment['id']])) { throw new RuntimeException('Could not update appointment record.'); @@ -292,7 +306,7 @@ class Appointments_model extends EA_Model { $this->db->order_by($order_by); } - $appointments = $this->db->get('appointments', $limit, $offset)->result_array(); + $appointments = $this->db->get_where('appointments', ['is_unavailable' => FALSE], $limit, $offset)->result_array(); foreach ($appointments as &$appointment) { @@ -302,56 +316,6 @@ class Appointments_model extends EA_Model { return $appointments; } - /** - * Save (insert or update) an unavailable. - * - * @param array $unavailable Associative array with the unavailable data. - * - * @return int Returns the unavailable ID. - * - * @throws InvalidArgumentException - */ - public function save_unavailable(array $unavailable): int - { - // Make sure the start date time is before the end date time. - $start = strtotime($unavailable['start_datetime']); - - $end = strtotime($unavailable['end_datetime']); - - if ($start > $end) - { - throw new InvalidArgumentException('Unavailable period start date time must be before the end date time.'); - } - - // Make sure the provider ID really exists in the database. - $role = $this->db->get_where('roles', ['slug' => DB_SLUG_PROVIDER])->row_array(); - - $count = $this->db->get_where('users', [ - 'id' => $unavailable['id_users_provider'], - 'id_roles' => $role['id'], - ])->num_rows(); - - if ( ! $count) - { - throw new InvalidArgumentException('Provider id was not found in database.'); - } - - if (empty($unavailable['id'])) - { - $unavailable['book_datetime'] = date('Y-m-d H:i:s'); - - $unavailable['is_unavailable'] = TRUE; - - $this->db->insert('appointments', $unavailable); - - return $this->db->insert_id(); - } - else - { - return $this->db->update('appointments', $unavailable, ['id' => $unavailable['id']]); - } - } - /** * Remove all the Google Calendar event IDs from appointment records. * @@ -472,6 +436,8 @@ class Appointments_model extends EA_Model { ->join('services', 'services.id = appointments.id_services', 'left') ->join('users AS providers', 'providers.id = appointments.id_users_provider', 'inner') ->join('users AS customers', 'customers.id = appointment.id_users_customer', 'left') + ->where('is_unavailable', FALSE) + ->group_start() ->like('appointments.start_datetime', $keyword) ->or_like('appointments.end_datetime', $keyword) ->or_like('appointments.location', $keyword) @@ -487,6 +453,7 @@ class Appointments_model extends EA_Model { ->or_like('customers.last_name', $keyword) ->or_like('customers.email', $keyword) ->or_like('customers.phone_number', $keyword) + ->group_end() ->limit($limit) ->offset($offset) ->order_by($order_by) diff --git a/application/models/Customers_model.php b/application/models/Customers_model.php index b31508e3..f330bb80 100644 --- a/application/models/Customers_model.php +++ b/application/models/Customers_model.php @@ -27,6 +27,24 @@ class Customers_model extends EA_Model { 'id_roles' => 'integer', ]; + /** + * @var array + */ + protected $api_resource = [ + 'id' => 'id', + 'firstName' => 'first_name', + 'lastName' => 'last_name', + 'email' => 'email', + 'phone' => 'phone_number', + 'address' => 'address', + 'city' => 'city', + 'state' => 'state', + 'zip' => 'zip_code', + 'timezone' => 'timezone', + 'language' => 'language', + 'notes' => 'notes', + ]; + /** * Save (insert or update) a customer. * @@ -380,6 +398,7 @@ class Customers_model extends EA_Model { ->select() ->from('users') ->where('id_roles', $role_id) + ->group_start() ->like('first_name', $keyword) ->or_like('last_name', $keyword) ->or_like('email', $keyword) @@ -390,6 +409,7 @@ class Customers_model extends EA_Model { ->or_like('state', $keyword) ->or_like('zip_code', $keyword) ->or_like('notes', $keyword) + ->group_end() ->limit($limit) ->offset($offset) ->order_by($order_by) diff --git a/application/models/Providers_model.php b/application/models/Providers_model.php index e72b9315..a507afe1 100755 --- a/application/models/Providers_model.php +++ b/application/models/Providers_model.php @@ -27,6 +27,25 @@ class Providers_model extends EA_Model { 'id_roles' => 'integer', ]; + /** + * @var array + */ + protected $api_resource = [ + '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', + 'roleId' => 'id_roles', + ]; /** * Save (insert or update) a provider. @@ -89,11 +108,7 @@ class Providers_model extends EA_Model { } // Validate provider services. - if (empty($provider['services']) || ! is_array($provider['services'])) - { - throw new InvalidArgumentException('The provided provider services are invalid: ' . print_r($provider, TRUE)); - } - else + if ( ! empty($provider['services'])) { // Make sure the provided service entries are numeric values. foreach ($provider['services'] as $service_id) @@ -464,9 +479,9 @@ class Providers_model extends EA_Model { * * @param int $provider_id Provider ID. * @param string $name Setting name. - * @param string|null $value Setting value. + * @param mixed $value Setting value. */ - public function set_setting(int $provider_id, string $name, string $value = NULL) + public function set_setting(int $provider_id, string $name, $value = NULL) { if ( ! $this->db->update('user_settings', [$name => $value], ['id_users' => $provider_id])) { @@ -665,6 +680,7 @@ class Providers_model extends EA_Model { ->select() ->from('users') ->where('id_roles', $role_id) + ->group_start() ->like('first_name', $keyword) ->or_like('last_name', $keyword) ->or_like('email', $keyword) @@ -675,6 +691,7 @@ class Providers_model extends EA_Model { ->or_like('state', $keyword) ->or_like('zip_code', $keyword) ->or_like('notes', $keyword) + ->group_end() ->limit($limit) ->offset($offset) ->order_by($order_by) diff --git a/application/models/Secretaries_model.php b/application/models/Secretaries_model.php index a07aa68e..ebd89bab 100644 --- a/application/models/Secretaries_model.php +++ b/application/models/Secretaries_model.php @@ -27,6 +27,26 @@ class Secretaries_model extends EA_Model { 'id_roles' => 'integer', ]; + /** + * @var array + */ + protected $api_resource = [ + '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', + 'roleId' => 'id_roles', + ]; + /** * Save (insert or update) a secretary. * @@ -88,11 +108,7 @@ class Secretaries_model extends EA_Model { } // Validate secretary providers. - if (empty($secretary['providers']) || ! is_array($secretary['providers'])) - { - throw new InvalidArgumentException('The provided secretary providers are invalid: ' . print_r($secretary, TRUE)); - } - else + if ( ! empty($secretary['providers'])) { // Make sure the provided provider entries are numeric values. foreach ($secretary['providers'] as $secretary_id) @@ -529,6 +545,7 @@ class Secretaries_model extends EA_Model { ->select() ->from('users') ->where('id_roles', $role_id) + ->group_start() ->like('first_name', $keyword) ->or_like('last_name', $keyword) ->or_like('email', $keyword) @@ -539,6 +556,7 @@ class Secretaries_model extends EA_Model { ->or_like('state', $keyword) ->or_like('zip_code', $keyword) ->or_like('notes', $keyword) + ->group_end() ->limit($limit) ->offset($offset) ->order_by($order_by) diff --git a/application/models/Service_categories_model.php b/application/models/Service_categories_model.php index 2d4139ad..274eb183 100644 --- a/application/models/Service_categories_model.php +++ b/application/models/Service_categories_model.php @@ -26,6 +26,15 @@ class Service_categories_model extends EA_Model { 'id' => 'integer', ]; + /** + * @var array + */ + protected $api_resource = [ + 'id' => 'id', + 'name' => 'name', + 'description' => 'description', + ]; + /** * Save (insert or update) a service category. * diff --git a/application/models/Services_model.php b/application/models/Services_model.php index 59a4faea..a83a2a43 100644 --- a/application/models/Services_model.php +++ b/application/models/Services_model.php @@ -29,6 +29,22 @@ class Services_model extends EA_Model { 'id_service_categories' => 'boolean', ]; + /** + * @var array + */ + protected $api_resource = [ + 'id' => 'id', + 'name' => 'name', + 'duration' => 'duration', + 'price' => 'price', + 'currency' => 'currency', + 'description' => 'description', + 'location' => 'location', + 'availabilitiesType' => 'availabilities_type', + 'attendantsNumber' => 'attendants_number', + 'categoryId' => 'id_service_categories', + ]; + /** * Save (insert or update) a service. * diff --git a/application/models/Settings_model.php b/application/models/Settings_model.php index e24a47d1..ffe2dd14 100644 --- a/application/models/Settings_model.php +++ b/application/models/Settings_model.php @@ -26,6 +26,14 @@ class Settings_model extends EA_Model { 'id' => 'integer', ]; + /** + * @var array + */ + protected $api_resource = [ + 'name' => 'name', + 'value' => 'value', + ]; + /** * Save (insert or update) a setting. * diff --git a/application/models/Users_model.php b/application/models/Users_model.php index 23ed1791..4e7b7125 100644 --- a/application/models/Users_model.php +++ b/application/models/Users_model.php @@ -26,6 +26,26 @@ class Users_model extends EA_Model { 'id' => 'integer', 'id_roles' => 'integer', ]; + + /** + * @var array + */ + protected $api_resource = [ + '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', + 'roleId' => 'id_roles', + ]; /** * Save (insert or update) a user.