From e9842a40bcd5af0961903e3e9b2d9603dcdd427f Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Thu, 26 Oct 2023 04:54:13 +0200 Subject: [PATCH] Revert renaming the service-categories to categories (for clarity) --- application/controllers/Booking.php | 2 +- ...{Categories.php => Service_categories.php} | 68 ++-- ...i_v1.php => Service_categories_api_v1.php} | 86 ++-- application/core/EA_Controller.php | 2 +- ...service_categories_table_to_categories.php | 36 ++ ...ce_categories_column_of_services_table.php | 70 ++++ application/models/Categories_model.php | 343 ---------------- .../models/Service_categories_model.php | 369 ++++++++++++++++++ application/models/Services_model.php | 65 ++- .../views/components/backend_header.php | 2 +- .../views/components/booking_type_step.php | 14 +- ...{categories.php => service_categories.php} | 20 +- application/views/pages/services.php | 6 +- ...t.js => service_categories_http_client.js} | 58 +-- .../{categories.js => service_categories.js} | 162 ++++---- assets/js/pages/services.js | 26 +- openapi.yml | 58 +-- 17 files changed, 773 insertions(+), 614 deletions(-) rename application/controllers/{Categories.php => Service_categories.php} (67%) rename application/controllers/api/v1/{Categories_api_v1.php => Service_categories_api_v1.php} (50%) create mode 100644 application/migrations/045_revert_rename_service_categories_table_to_categories.php create mode 100644 application/migrations/046_revert_rename_id_service_categories_column_of_services_table.php delete mode 100644 application/models/Categories_model.php create mode 100644 application/models/Service_categories_model.php rename application/views/pages/{categories.php => service_categories.php} (77%) mode change 100755 => 100644 rename assets/js/http/{categories_http_client.js => service_categories_http_client.js} (56%) rename assets/js/pages/{categories.js => service_categories.js} (52%) diff --git a/application/controllers/Booking.php b/application/controllers/Booking.php index 3d77b2ce..5800dd29 100755 --- a/application/controllers/Booking.php +++ b/application/controllers/Booking.php @@ -33,7 +33,7 @@ class Booking extends EA_Controller { $this->load->model('providers_model'); $this->load->model('admins_model'); $this->load->model('secretaries_model'); - $this->load->model('categories_model'); + $this->load->model('service_categories_model'); $this->load->model('services_model'); $this->load->model('customers_model'); $this->load->model('settings_model'); diff --git a/application/controllers/Categories.php b/application/controllers/Service_categories.php similarity index 67% rename from application/controllers/Categories.php rename to application/controllers/Service_categories.php index 06e78ad0..67168470 100644 --- a/application/controllers/Categories.php +++ b/application/controllers/Service_categories.php @@ -12,21 +12,21 @@ * ---------------------------------------------------------------------------- */ /** - * Categories controller. + * Service-categories controller. * - * Handles the categories related operations. + * Handles the service-categories related operations. * * @package Controllers */ -class Categories extends EA_Controller { +class Service_categories extends EA_Controller { /** - * Categories constructor. + * Service-categories constructor. */ public function __construct() { parent::__construct(); - $this->load->model('categories_model'); + $this->load->model('service_categories_model'); $this->load->model('roles_model'); $this->load->library('accounts'); @@ -35,9 +35,9 @@ class Categories extends EA_Controller { } /** - * Render the backend categories page. + * Render the backend service-categories page. * - * On this page admin users will be able to manage categories, which are eventually selected by customers during the + * On this page admin users will be able to manage service-categories, which are eventually selected by customers during the * booking process. */ public function index() @@ -73,11 +73,11 @@ class Categories extends EA_Controller { 'privileges' => $this->roles_model->get_permissions_by_slug($role_slug), ]); - $this->load->view('pages/categories'); + $this->load->view('pages/service_categories'); } /** - * Filter categories by the provided keyword. + * Filter service-categories by the provided keyword. */ public function search() { @@ -96,9 +96,9 @@ class Categories extends EA_Controller { $offset = 0; - $categories = $this->categories_model->search($keyword, $limit, $offset, $order_by); + $service_categories = $this->service_categories_model->search($keyword, $limit, $offset, $order_by); - json_response($categories); + json_response($service_categories); } catch (Throwable $e) { @@ -107,7 +107,7 @@ class Categories extends EA_Controller { } /** - * Create a category. + * Create a service-category. */ public function create() { @@ -118,22 +118,22 @@ class Categories extends EA_Controller { abort(403, 'Forbidden'); } - $category = request('category'); + $service_category = request('service_category'); - $this->categories_model->only($category, [ + $this->service_categories_model->only($service_category, [ 'name', 'description' ]); - $category_id = $this->categories_model->save($category); + $service_category_id = $this->service_categories_model->save($service_category); - $category = $this->categories_model->find($category_id); + $service_category = $this->service_categories_model->find($service_category_id); - $this->webhooks_client->trigger(WEBHOOK_CATEGORY_SAVE, $category); + $this->webhooks_client->trigger(WEBHOOK_CATEGORY_SAVE, $service_category); json_response([ 'success' => TRUE, - 'id' => $category_id + 'id' => $service_category_id ]); } catch (Throwable $e) @@ -143,7 +143,7 @@ class Categories extends EA_Controller { } /** - * Update a category. + * Update a service-category. */ public function update() { @@ -154,23 +154,23 @@ class Categories extends EA_Controller { abort(403, 'Forbidden'); } - $category = request('category'); + $service_category = request('service_category'); - $this->categories_model->only($category, [ + $this->service_categories_model->only($service_category, [ 'id', 'name', 'description' ]); - $category_id = $this->categories_model->save($category); + $service_category_id = $this->service_categories_model->save($service_category); - $category = $this->categories_model->find($category_id); + $service_category = $this->service_categories_model->find($service_category_id); - $this->webhooks_client->trigger(WEBHOOK_CATEGORY_SAVE, $category); + $this->webhooks_client->trigger(WEBHOOK_CATEGORY_SAVE, $service_category); json_response([ 'success' => TRUE, - 'id' => $category_id + 'id' => $service_category_id ]); } catch (Throwable $e) @@ -180,7 +180,7 @@ class Categories extends EA_Controller { } /** - * Remove a category. + * Remove a service-category. */ public function destroy() { @@ -191,13 +191,13 @@ class Categories extends EA_Controller { abort(403, 'Forbidden'); } - $category_id = request('category_id'); + $service_category_id = request('service_category_id'); - $category = $this->categories_model->find($category_id); + $service_category = $this->service_categories_model->find($service_category_id); - $this->categories_model->delete($category_id); + $this->service_categories_model->delete($service_category_id); - $this->webhooks_client->trigger(WEBHOOK_CATEGORY_DELETE, $category); + $this->webhooks_client->trigger(WEBHOOK_CATEGORY_DELETE, $service_category); json_response([ 'success' => TRUE, @@ -210,7 +210,7 @@ class Categories extends EA_Controller { } /** - * Find a category. + * Find a service-category. */ public function find() { @@ -221,11 +221,11 @@ class Categories extends EA_Controller { abort(403, 'Forbidden'); } - $category_id = request('category_id'); + $service_category_id = request('service_category_id'); - $category = $this->categories_model->find($category_id); + $service_category = $this->service_categories_model->find($service_category_id); - json_response($category); + json_response($service_category); } catch (Throwable $e) { diff --git a/application/controllers/api/v1/Categories_api_v1.php b/application/controllers/api/v1/Service_categories_api_v1.php similarity index 50% rename from application/controllers/api/v1/Categories_api_v1.php rename to application/controllers/api/v1/Service_categories_api_v1.php index 6793b0c4..bf298e69 100644 --- a/application/controllers/api/v1/Categories_api_v1.php +++ b/application/controllers/api/v1/Service_categories_api_v1.php @@ -12,13 +12,13 @@ * ---------------------------------------------------------------------------- */ /** - * Categories API v1 controller. + * Service-categories API v1 controller. * * @package Controllers */ -class Categories_api_v1 extends EA_Controller { +class Service_categories_api_v1 extends EA_Controller { /** - * Categories_api_v1 constructor. + * Service_categories_api_v1 constructor. */ public function __construct() { @@ -28,11 +28,11 @@ class Categories_api_v1 extends EA_Controller { $this->api->auth(); - $this->api->model('categories_model'); + $this->api->model('service_categories_model'); } /** - * Get a category collection. + * Get a service-category collection. */ public function index() { @@ -50,26 +50,26 @@ class Categories_api_v1 extends EA_Controller { $with = $this->api->request_with(); - $categories = empty($keyword) - ? $this->categories_model->get(NULL, $limit, $offset, $order_by) - : $this->categories_model->search($keyword, $limit, $offset, $order_by); + $service_categories = empty($keyword) + ? $this->service_categories_model->get(NULL, $limit, $offset, $order_by) + : $this->service_categories_model->search($keyword, $limit, $offset, $order_by); - foreach ($categories as &$category) + foreach ($service_categories as &$service_category) { - $this->categories_model->api_encode($category); + $this->service_categories_model->api_encode($service_category); if ( ! empty($fields)) { - $this->categories_model->only($category, $fields); + $this->service_categories_model->only($service_category, $fields); } if ( ! empty($with)) { - $this->categories_model->load($category, $with); + $this->service_categories_model->load($service_category, $with); } } - json_response($categories); + json_response($service_categories); } catch (Throwable $e) { @@ -78,9 +78,9 @@ class Categories_api_v1 extends EA_Controller { } /** - * Get a single category. + * Get a single service-category. * - * @param int|null $id Category ID. + * @param int|null $id Service-category ID. */ public function show(int $id = NULL) { @@ -90,28 +90,28 @@ class Categories_api_v1 extends EA_Controller { $with = $this->api->request_with(); - $category = $this->categories_model->find($id); + $service_category = $this->service_categories_model->find($id); - $this->categories_model->api_encode($category); + $this->service_categories_model->api_encode($service_category); if ( ! empty($fields)) { - $this->categories_model->only($category, $fields); + $this->service_categories_model->only($service_category, $fields); } if ( ! empty($with)) { - $this->categories_model->load($category, $with); + $this->service_categories_model->load($service_category, $with); } - if ( ! $category) + if ( ! $service_category) { response('', 404); return; } - json_response($category); + json_response($service_category); } catch (Throwable $e) { @@ -120,28 +120,28 @@ class Categories_api_v1 extends EA_Controller { } /** - * Create a category. + * Create a service-category. */ public function store() { try { - $category = request(); + $service_category = request(); - $this->categories_model->api_decode($category); + $this->service_categories_model->api_decode($service_category); - if (array_key_exists('id', $category)) + if (array_key_exists('id', $service_category)) { - unset($category['id']); + unset($service_category['id']); } - $category_id = $this->categories_model->save($category); + $service_category_id = $this->service_categories_model->save($service_category); - $created_category = $this->categories_model->find($category_id); + $created_service_category = $this->service_categories_model->find($service_category_id); - $this->categories_model->api_encode($created_category); + $this->service_categories_model->api_encode($created_service_category); - json_response($created_category, 201); + json_response($created_service_category, 201); } catch (Throwable $e) { @@ -150,15 +150,15 @@ class Categories_api_v1 extends EA_Controller { } /** - * Update a category. + * Update a service-category. * - * @param int $id Category ID. + * @param int $id Service-category ID. */ public function update(int $id) { try { - $occurrences = $this->categories_model->get(['id' => $id]); + $occurrences = $this->service_categories_model->get(['id' => $id]); if (empty($occurrences)) { @@ -169,17 +169,17 @@ class Categories_api_v1 extends EA_Controller { $original_category = $occurrences[0]; - $category = request(); + $service_category = request(); - $this->categories_model->api_decode($category, $original_category); + $this->service_categories_model->api_decode($service_category, $original_category); - $category_id = $this->categories_model->save($category); + $service_category_id = $this->service_categories_model->save($service_category); - $updated_category = $this->categories_model->find($category_id); + $updated_service_category = $this->service_categories_model->find($service_category_id); - $this->categories_model->api_encode($updated_category); + $this->service_categories_model->api_encode($updated_service_category); - json_response($updated_category); + json_response($updated_service_category); } catch (Throwable $e) { @@ -188,15 +188,15 @@ class Categories_api_v1 extends EA_Controller { } /** - * Delete a category. + * Delete a service-category. * - * @param int $id Category ID. + * @param int $id Service-category ID. */ public function destroy(int $id) { try { - $occurrences = $this->categories_model->get(['id' => $id]); + $occurrences = $this->service_categories_model->get(['id' => $id]); if (empty($occurrences)) { @@ -205,7 +205,7 @@ class Categories_api_v1 extends EA_Controller { return; } - $this->categories_model->delete($id); + $this->service_categories_model->delete($id); response('', 204); } diff --git a/application/core/EA_Controller.php b/application/core/EA_Controller.php index 3def9319..59cdf8d7 100644 --- a/application/core/EA_Controller.php +++ b/application/core/EA_Controller.php @@ -41,7 +41,7 @@ * * @property Admins_model $admins_model * @property Appointments_model $appointments_model - * @property Categories_model $categories_model + * @property Service_categories_model $service_categories_model * @property Consents_model $consents_model * @property Customers_model $customers_model * @property Providers_model $providers_model diff --git a/application/migrations/045_revert_rename_service_categories_table_to_categories.php b/application/migrations/045_revert_rename_service_categories_table_to_categories.php new file mode 100644 index 00000000..220196ca --- /dev/null +++ b/application/migrations/045_revert_rename_service_categories_table_to_categories.php @@ -0,0 +1,36 @@ + + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.5.0 + * ---------------------------------------------------------------------------- */ + +class Migration_Revert_rename_service_categories_table_to_categories extends EA_Migration { + /** + * Upgrade method. + */ + public function up() + { + if ($this->db->table_exists('categories')) + { + $this->dbforge->rename_table('categories', 'service_categories'); + } + } + + /** + * Downgrade method. + */ + public function down() + { + if ($this->db->table_exists('service_categories')) + { + $this->dbforge->rename_table('service_categories', 'categories'); + } + } +} diff --git a/application/migrations/046_revert_rename_id_service_categories_column_of_services_table.php b/application/migrations/046_revert_rename_id_service_categories_column_of_services_table.php new file mode 100644 index 00000000..57a871d8 --- /dev/null +++ b/application/migrations/046_revert_rename_id_service_categories_column_of_services_table.php @@ -0,0 +1,70 @@ + + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.5.0 + * ---------------------------------------------------------------------------- */ + +class Migration_Revert_rename_id_service_categories_column_of_services_table extends EA_Migration { + /** + * Upgrade method. + */ + public function up() + { + if ($this->db->field_exists('id_categories', 'services')) + { + $this->db->query('ALTER TABLE `' . $this->db->dbprefix('services') . '` DROP FOREIGN KEY `services_categories`'); + + $fields = [ + 'id_categories' => [ + 'name' => 'id_service_categories', + 'type' => 'INT', + 'constraint' => '11' + ] + ]; + + $this->dbforge->modify_column('services', $fields); + + $this->db->query(' + ALTER TABLE `' . $this->db->dbprefix('services') . '` + ADD CONSTRAINT `services_service_categories` FOREIGN KEY (`id_service_categories`) REFERENCES `' . $this->db->dbprefix('service_categories') . '` (`id`) + ON DELETE SET NULL + ON UPDATE CASCADE + '); + } + } + + /** + * Downgrade method. + */ + public function down() + { + if ($this->db->field_exists('id_service_categories', 'services')) + { + $this->db->query('ALTER TABLE `' . $this->db->dbprefix('services') . '` DROP FOREIGN KEY `services_service_categories`'); + + $fields = [ + 'id_service_categories' => [ + 'name' => 'id_categories', + 'type' => 'INT', + 'constraint' => '11' + ] + ]; + + $this->dbforge->modify_column('services', $fields); + + $this->db->query(' + ALTER TABLE `' . $this->db->dbprefix('services') . '` + ADD CONSTRAINT `services_categories` FOREIGN KEY (`id_categories`) REFERENCES `' . $this->db->dbprefix('categories') . '` (`id`) + ON DELETE SET NULL + ON UPDATE CASCADE + '); + } + } +} diff --git a/application/models/Categories_model.php b/application/models/Categories_model.php deleted file mode 100644 index 4a096c7e..00000000 --- a/application/models/Categories_model.php +++ /dev/null @@ -1,343 +0,0 @@ - - * @copyright Copyright (c) Alex Tselegidis - * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link https://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -/** - * Categories model. - * - * Handles all the database operations of the category resource. - * - * @package Models - */ -class Categories_model extends EA_Model { - /** - * @var array - */ - protected array $casts = [ - 'id' => 'integer', - ]; - - /** - * @var array - */ - protected array $api_resource = [ - 'id' => 'id', - 'name' => 'name', - 'description' => 'description', - ]; - - /** - * Save (insert or update) a category. - * - * @param array $category Associative array with the category data. - * - * @return int Returns the category ID. - * - * @throws InvalidArgumentException - */ - public function save(array $category): int - { - $this->validate($category); - - if (empty($category['id'])) - { - return $this->insert($category); - } - else - { - return $this->update($category); - } - } - - /** - * Validate the category data. - * - * @param array $category Associative array with the category data. - * - * @throws InvalidArgumentException - */ - public function validate(array $category) - { - // If a category ID is provided then check whether the record really exists in the database. - if ( ! empty($category['id'])) - { - $count = $this->db->get_where('categories', ['id' => $category['id']])->num_rows(); - - if ( ! $count) - { - throw new InvalidArgumentException('The provided category ID does not exist in the database: ' . $category['id']); - } - } - - // Make sure all required fields are provided. - if ( - empty($category['name']) - ) - { - throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($category, TRUE)); - } - } - - /** - * Insert a new category into the database. - * - * @param array $category Associative array with the category data. - * - * @return int Returns the category ID. - * - * @throws RuntimeException - */ - protected function insert(array $category): int - { - $category['create_datetime'] = date('Y-m-d H:i:s'); - $category['update_datetime'] = date('Y-m-d H:i:s'); - - if ( ! $this->db->insert('categories', $category)) - { - throw new RuntimeException('Could not insert category.'); - } - - return $this->db->insert_id(); - } - - /** - * Update an existing category. - * - * @param array $category Associative array with the category data. - * - * @return int Returns the category ID. - * - * @throws RuntimeException - */ - protected function update(array $category): int - { - $category['update_datetime'] = date('Y-m-d H:i:s'); - - if ( ! $this->db->update('categories', $category, ['id' => $category['id']])) - { - throw new RuntimeException('Could not update service categories.'); - } - - return $category['id']; - } - - /** - * Remove an existing category from the database. - * - * @param int $category_id Category ID. - * - * @throws RuntimeException - */ - public function delete(int $category_id): void - { - $this->db->delete('categories', ['id' => $category_id]); - } - - /** - * Get a specific category from the database. - * - * @param int $category_id The ID of the record to be returned. - * - * @return array Returns an array with the category data. - * - * @throws InvalidArgumentException - */ - public function find(int $category_id): array - { - $category = $this->db->get_where('categories', ['id' => $category_id])->row_array(); - - if ( ! $category) - { - throw new InvalidArgumentException('The provided category ID was not found in the database: ' . $category_id); - } - - $this->cast($category); - - return $category; - } - - /** - * Get a specific field value from the database. - * - * @param int $category_id Category ID. - * @param string $field Name of the value to be returned. - * - * @return mixed Returns the selected category value from the database. - * - * @throws InvalidArgumentException - */ - public function value(int $category_id, string $field): mixed - { - if (empty($field)) - { - throw new InvalidArgumentException('The field argument is cannot be empty.'); - } - - if (empty($category_id)) - { - throw new InvalidArgumentException('The category ID argument cannot be empty.'); - } - - // Check whether the service exists. - $query = $this->db->get_where('categories', ['id' => $category_id]); - - if ( ! $query->num_rows()) - { - throw new InvalidArgumentException('The provided category ID was not found in the database: ' . $category_id); - } - - // Check if the required field is part of the category data. - $category = $query->row_array(); - - $this->cast($category); - - if ( ! array_key_exists($field, $category)) - { - throw new InvalidArgumentException('The requested field was not found in the category data: ' . $field); - } - - return $category[$field]; - } - - /** - * Get all services 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 service categories. - */ - public function get(array|string $where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array - { - if ($where !== NULL) - { - $this->db->where($where); - } - - if ($order_by !== NULL) - { - $this->db->order_by($order_by); - } - - $categories = $this->db->get('categories', $limit, $offset)->result_array(); - - foreach ($categories as &$category) - { - $this->cast($category); - } - - return $categories; - } - - /** - * Get the query builder interface, configured for use with the service categories table. - * - * @return CI_DB_query_builder - */ - public function query(): CI_DB_query_builder - { - return $this->db->from('categories'); - } - - /** - * Search service categories 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 service categories. - */ - public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array - { - $categories = $this - ->db - ->select() - ->from('categories') - ->group_start() - ->like('name', $keyword) - ->or_like('description', $keyword) - ->group_end() - ->limit($limit) - ->offset($offset) - ->order_by($order_by) - ->get() - ->result_array(); - - foreach ($categories as &$category) - { - $this->cast($category); - } - - return $categories; - } - - /** - * Load related resources to a category. - * - * @param array $category Associative array with the category data. - * @param array $resources Resource names to be attached. - * - * @throws InvalidArgumentException - */ - public function load(array &$category, array $resources) - { - // Service categories do not currently have any related resources. - } - - /** - * Convert the database category record to the equivalent API resource. - * - * @param array $category Category data. - */ - public function api_encode(array &$category) - { - $encoded_resource = [ - 'id' => array_key_exists('id', $category) ? (int)$category['id'] : NULL, - 'name' => $category['name'], - 'description' => array_key_exists('description', $category) ? $category['description'] : NULL - ]; - - $category = $encoded_resource; - } - - /** - * Convert the API resource to the equivalent database category record. - * - * @param array $category API resource. - * @param array|null $base Base category data to be overwritten with the provided values (useful for updates). - */ - public function api_decode(array &$category, array $base = NULL) - { - $decoded_resource = $base ?: []; - - if (array_key_exists('id', $category)) - { - $decoded_resource['id'] = $category['id']; - } - - if (array_key_exists('name', $category)) - { - $decoded_resource['name'] = $category['name']; - } - - if (array_key_exists('description', $category)) - { - $decoded_resource['description'] = $category['description']; - } - - $category = $decoded_resource; - } -} diff --git a/application/models/Service_categories_model.php b/application/models/Service_categories_model.php new file mode 100644 index 00000000..08209f52 --- /dev/null +++ b/application/models/Service_categories_model.php @@ -0,0 +1,369 @@ + + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Service-Categories model. + * + * Handles all the database operations of the service-category resource. + * + * @package Models + */ +class Service_categories_model extends EA_Model { + /** + * @var array + */ + protected array $casts = [ + 'id' => 'integer', + ]; + + /** + * @var array + */ + protected array $api_resource = [ + 'id' => 'id', + 'name' => 'name', + 'description' => 'description', + ]; + + /** + * Save (insert or update) a service-category. + * + * @param array $service_category Associative array with the service-category data. + * + * @return int Returns the service-category ID. + * + * @throws InvalidArgumentException + */ + public function save(array $service_category): int + { + $this->validate($service_category); + + if (empty($service_category['id'])) + { + return $this->insert($service_category); + } + else + { + return $this->update($service_category); + } + } + + /** + * Validate the service-category data. + * + * @param array $service_category Associative array with the service-category data. + * + * @throws InvalidArgumentException + */ + public function validate(array $service_category) + { + // If a service-category ID is provided then check whether the record really exists in the database. + if ( ! empty($service_category['id'])) + { + $count = $this->db->get_where('service_categories', ['id' => $service_category['id']])->num_rows(); + + if ( ! $count) + { + throw new InvalidArgumentException('The provided service-category ID does not exist in the database: ' . $service_category['id']); + } + } + + // Make sure all required fields are provided. + if ( + empty($service_category['name']) + ) + { + throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($service_category, TRUE)); + } + } + + /** + * Insert a new service-category into the database. + * + * @param array $service_category Associative array with the service-category data. + * + * @return int Returns the service-category ID. + * + * @throws RuntimeException + */ + protected function insert(array $service_category): int + { + $service_category['create_datetime'] = date('Y-m-d H:i:s'); + $service_category['update_datetime'] = date('Y-m-d H:i:s'); + + if ( ! $this->db->insert('service_categories', $service_category)) + { + throw new RuntimeException('Could not insert service-category.'); + } + + return $this->db->insert_id(); + } + + /** + * Update an existing service-category. + * + * @param array $service_category Associative array with the service-category data. + * + * @return int Returns the service-category ID. + * + * @throws RuntimeException + */ + protected function update(array $service_category): int + { + $service_category['update_datetime'] = date('Y-m-d H:i:s'); + + if ( ! $this->db->update('service_categories', $service_category, ['id' => $service_category['id']])) + { + throw new RuntimeException('Could not update service categories.'); + } + + return $service_category['id']; + } + + /** + * Remove an existing service-category from the database. + * + * @param int $category_id Category ID. + * @param bool $force_delete Override soft delete. + * + * @throws RuntimeException + */ + public function delete(int $category_id, bool $force_delete = FALSE) + { + if ($force_delete) + { + $this->db->delete('service_categories', ['id' => $category_id]); + } + else + { + $this->db->update('service_categories', ['delete_datetime' => date('Y-m-d H:i:s')], ['id' => $category_id]); + } + } + + /** + * Get a specific service-category from the database. + * + * @param int $category_id The ID of the record to be returned. + * @param bool $with_trashed + * + * @return array Returns an array with the service-category data. + * + * @throws InvalidArgumentException + */ + public function find(int $category_id, bool $with_trashed = FALSE): array + { + if ( ! $with_trashed) + { + $this->db->where('delete_datetime IS NULL'); + } + + $service_category = $this->db->get_where('service_categories', ['id' => $category_id])->row_array(); + + if ( ! $service_category) + { + throw new InvalidArgumentException('The provided service-category ID was not found in the database: ' . $category_id); + } + + $this->cast($service_category); + + return $service_category; + } + + /** + * Get a specific field value from the database. + * + * @param int $category_id Category ID. + * @param string $field Name of the value to be returned. + * + * @return mixed Returns the selected service-category value from the database. + * + * @throws InvalidArgumentException + */ + public function value(int $category_id, string $field): mixed + { + if (empty($field)) + { + throw new InvalidArgumentException('The field argument is cannot be empty.'); + } + + if (empty($category_id)) + { + throw new InvalidArgumentException('The service-category ID argument cannot be empty.'); + } + + // Check whether the service exists. + $query = $this->db->get_where('service_categories', ['id' => $category_id]); + + if ( ! $query->num_rows()) + { + throw new InvalidArgumentException('The provided service-category ID was not found in the database: ' . $category_id); + } + + // Check if the required field is part of the service-category data. + $service_category = $query->row_array(); + + $this->cast($service_category); + + if ( ! array_key_exists($field, $service_category)) + { + throw new InvalidArgumentException('The requested field was not found in the service-category data: ' . $field); + } + + return $service_category[$field]; + } + + /** + * Get all services 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. + * @param bool $with_trashed + * + * @return array Returns an array of service categories. + */ + public function get(array|string $where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL, bool $with_trashed = FALSE): array + { + if ($where !== NULL) + { + $this->db->where($where); + } + + if ($order_by !== NULL) + { + $this->db->order_by($order_by); + } + + if ( ! $with_trashed) + { + $this->db->where('delete_datetime IS NULL'); + } + + $service_categories = $this->db->get('service_categories', $limit, $offset)->result_array(); + + foreach ($service_categories as &$service_category) + { + $this->cast($service_category); + } + + return $service_categories; + } + + /** + * Get the query builder interface, configured for use with the service categories table. + * + * @return CI_DB_query_builder + */ + public function query(): CI_DB_query_builder + { + return $this->db->from('service_categories'); + } + + /** + * Search service categories 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. + * @param bool $with_trashed + * + * @return array Returns an array of service categories. + */ + public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL, bool $with_trashed = FALSE): array + { + if ( ! $with_trashed) + { + $this->db->where('delete_datetime IS NULL'); + } + + $service_categories = $this + ->db + ->select() + ->from('service_categories') + ->group_start() + ->like('name', $keyword) + ->or_like('description', $keyword) + ->group_end() + ->limit($limit) + ->offset($offset) + ->order_by($order_by) + ->get() + ->result_array(); + + foreach ($service_categories as &$service_category) + { + $this->cast($service_category); + } + + return $service_categories; + } + + /** + * Load related resources to a service-category. + * + * @param array $service_category Associative array with the service-category data. + * @param array $resources Resource names to be attached. + * + * @throws InvalidArgumentException + */ + public function load(array &$service_category, array $resources) + { + // Service categories do not currently have any related resources. + } + + /** + * Convert the database service-category record to the equivalent API resource. + * + * @param array $service_category Category data. + */ + public function api_encode(array &$service_category) + { + $encoded_resource = [ + 'id' => array_key_exists('id', $service_category) ? (int)$service_category['id'] : NULL, + 'name' => $service_category['name'], + 'description' => array_key_exists('description', $service_category) ? $service_category['description'] : NULL + ]; + + $service_category = $encoded_resource; + } + + /** + * Convert the API resource to the equivalent database service-category record. + * + * @param array $service_category API resource. + * @param array|null $base Base service-category data to be overwritten with the provided values (useful for updates). + */ + public function api_decode(array &$service_category, array $base = NULL) + { + $decoded_resource = $base ?: []; + + if (array_key_exists('id', $service_category)) + { + $decoded_resource['id'] = $service_category['id']; + } + + if (array_key_exists('name', $service_category)) + { + $decoded_resource['name'] = $service_category['name']; + } + + if (array_key_exists('description', $service_category)) + { + $decoded_resource['description'] = $service_category['description']; + } + + $service_category = $decoded_resource; + } +} diff --git a/application/models/Services_model.php b/application/models/Services_model.php index e5f95e32..8d9ec8a9 100644 --- a/application/models/Services_model.php +++ b/application/models/Services_model.php @@ -27,7 +27,7 @@ class Services_model extends EA_Model { 'price' => 'float', 'attendants_number' => 'integer', 'is_private' => 'boolean', - 'id_categories' => 'integer', + 'id_service_categories' => 'integer', ]; /** @@ -45,7 +45,7 @@ class Services_model extends EA_Model { 'availabilitiesType' => 'availabilities_type', 'attendantsNumber' => 'attendants_number', 'isPrivate' => 'is_private', - 'categoryId' => 'id_categories', + 'serviceCategoryId' => 'id_service_categories', ]; /** @@ -99,18 +99,18 @@ class Services_model extends EA_Model { throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($service, TRUE)); } - // If a category was provided then make sure it really exists in the database. - if ( ! empty($service['id_categories'])) + // If a category was provided then make sure it really exists in the database. + if ( ! empty($service['id_service_categories'])) { - $count = $this->db->get_where('categories', ['id' => $service['id_categories']])->num_rows(); + $count = $this->db->get_where('categories', ['id' => $service['id_service_categories']])->num_rows(); if ( ! $count) { - throw new InvalidArgumentException('The provided category ID was not found in the database: ' . $service['id_categories']); + throw new InvalidArgumentException('The provided category ID was not found in the database: ' . $service['id_service_categories']); } } - // Make sure the duration value is valid. + // Make sure the duration value is valid. if ( ! empty($service['duration'])) { if ((int)$service['duration'] < EVENT_MINIMUM_DURATION) @@ -136,7 +136,7 @@ class Services_model extends EA_Model { throw new InvalidArgumentException('The provided availabilities type is invalid: ' . $service['availabilities_type']); } - // Validate the attendants number value. + // Validate the attendants number value. if (empty($service['attendants_number']) || (int)$service['attendants_number'] < 1) { throw new InvalidArgumentException('The provided attendants number is invalid: ' . $service['attendants_number']); @@ -190,25 +190,39 @@ class Services_model extends EA_Model { * Remove an existing service from the database. * * @param int $service_id Service ID. + * @param bool $force_delete Override soft delete. * * @throws RuntimeException */ - public function delete(int $service_id): void + public function delete(int $service_id, bool $force_delete = FALSE) { - $this->db->delete('services', ['id' => $service_id]); + if ($force_delete) + { + $this->db->delete('services', ['id' => $service_id]); + } + else + { + $this->db->update('services', ['delete_datetime' => date('Y-m-d H:i:s')], ['id' => $service_id]); + } } /** * Get a specific service from the database. * * @param int $service_id The ID of the record to be returned. + * @param bool $with_trashed * * @return array Returns an array with the service data. * * @throws InvalidArgumentException */ - public function find(int $service_id): array + public function find(int $service_id, bool $with_trashed = FALSE): array { + if ( ! $with_trashed) + { + $this->db->where('delete_datetime IS NULL'); + } + $service = $this->db->get_where('services', ['id' => $service_id])->row_array(); if ( ! $service) @@ -271,10 +285,11 @@ class Services_model extends EA_Model { * @param int|null $limit Record limit. * @param int|null $offset Record offset. * @param string|null $order_by Order by. + * @param bool $with_trashed * * @return array Returns an array of services. */ - public function get(array|string $where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array + public function get(array|string $where = NULL, int $limit = NULL, int $offset = NULL, string $order_by = NULL, bool $with_trashed = FALSE): array { if ($where !== NULL) { @@ -286,6 +301,11 @@ class Services_model extends EA_Model { $this->db->order_by($order_by); } + if ( ! $with_trashed) + { + $this->db->where('delete_datetime IS NULL'); + } + $services = $this->db->get('services', $limit, $offset)->result_array(); foreach ($services as &$service) @@ -313,10 +333,11 @@ class Services_model extends EA_Model { $services = $this ->db ->distinct() - ->select('services.*, categories.name AS category_name, categories.id AS category_id') + ->select('services.*, service_categories.name AS service_category_name, service_categories.id AS service_category_id') ->from('services') ->join('services_providers', 'services_providers.id_services = services.id', 'inner') - ->join('categories', 'categories.id = services.id_categories', 'left') + ->join('service_categories', 'service_categories.id = services.id_service_categories', 'left') + ->where('services.delete_datetime IS NULL') ->order_by('name ASC') ->get() ->result_array(); @@ -346,11 +367,17 @@ class Services_model extends EA_Model { * @param int|null $limit Record limit. * @param int|null $offset Record offset. * @param string|null $order_by Order by. + * @param bool $with_trashed * * @return array Returns an array of services. */ - public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array + public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL, bool $with_trashed = FALSE): array { + if ( ! $with_trashed) + { + $this->db->where('delete_datetime IS NULL'); + } + $services = $this ->db ->select() @@ -395,7 +422,7 @@ class Services_model extends EA_Model { 'category' => $this ->db ->get_where('categories', [ - 'id' => $service['id_categories'] ?? $service['categoryId'] ?? NULL + 'id' => $service['id_service_categories'] ?? $service['serviceCategoryId'] ?? NULL ]) ->row_array(), default => throw new InvalidArgumentException('The requested appointment relation is not supported: ' . $resource), @@ -420,7 +447,7 @@ class Services_model extends EA_Model { 'location' => $service['location'], 'availabilitiesType' => $service['availabilities_type'], 'attendantsNumber' => (int)$service['attendants_number'], - 'categoryId' => $service['id_categories'] !== NULL ? (int)$service['id_categories'] : NULL + 'serviceCategoryId' => $service['id_service_categories'] !== NULL ? (int)$service['id_service_categories'] : NULL ]; $service = $encoded_resource; @@ -481,9 +508,9 @@ class Services_model extends EA_Model { $decoded_resource['attendants_number'] = $service['attendantsNumber']; } - if (array_key_exists('categoryId', $service)) + if (array_key_exists('serviceCategoryId', $service)) { - $decoded_resource['id_categories'] = $service['categoryId']; + $decoded_resource['id_service_categories'] = $service['serviceCategoryId']; } $service = $decoded_resource; diff --git a/application/views/components/backend_header.php b/application/views/components/backend_header.php index a67911b6..951a57c0 100755 --- a/application/views/components/backend_header.php +++ b/application/views/components/backend_header.php @@ -53,7 +53,7 @@ - + diff --git a/application/views/components/booking_type_step.php b/application/views/components/booking_type_step.php index e87ab41c..4e1ba3bc 100644 --- a/application/views/components/booking_type_step.php +++ b/application/views/components/booking_type_step.php @@ -23,7 +23,7 @@ $has_category = FALSE; foreach ($available_services as $service) { - if ( ! empty($service['category_id'])) + if ( ! empty($service['service_category_id'])) { $has_category = TRUE; break; @@ -36,14 +36,14 @@ foreach ($available_services as $service) { - if ( ! empty($service['category_id'])) + if ( ! empty($service['service_category_id'])) { - if ( ! isset($grouped_services[$service['category_name']])) + if ( ! isset($grouped_services[$service['service_category_name']])) { - $grouped_services[$service['category_name']] = []; + $grouped_services[$service['service_category_name']] = []; } - $grouped_services[$service['category_name']][] = $service; + $grouped_services[$service['service_category_name']][] = $service; } } @@ -52,7 +52,7 @@ $grouped_services['uncategorized'] = []; foreach ($available_services as $service) { - if ($service['category_id'] == NULL) + if ($service['service_category_id'] == NULL) { $grouped_services['uncategorized'][] = $service; } @@ -61,7 +61,7 @@ foreach ($grouped_services as $key => $group) { $group_label = $key !== 'uncategorized' - ? $group[0]['category_name'] + ? $group[0]['service_category_name'] : 'Uncategorized'; if (count($group) > 0) diff --git a/application/views/pages/categories.php b/application/views/pages/service_categories.php old mode 100755 new mode 100644 similarity index 77% rename from application/views/pages/categories.php rename to application/views/pages/service_categories.php index 39d6d62d..07c00978 --- a/application/views/pages/categories.php +++ b/application/views/pages/service_categories.php @@ -4,8 +4,8 @@
-
-
+
+
@@ -18,7 +18,7 @@

- +

@@ -29,26 +29,26 @@
- - -
@@ -89,7 +89,7 @@ - - + + diff --git a/application/views/pages/services.php b/application/views/pages/services.php index 5e344db6..1acde33f 100755 --- a/application/views/pages/services.php +++ b/application/views/pages/services.php @@ -94,10 +94,10 @@
-
@@ -174,7 +174,7 @@ - + diff --git a/assets/js/http/categories_http_client.js b/assets/js/http/service_categories_http_client.js similarity index 56% rename from assets/js/http/categories_http_client.js rename to assets/js/http/service_categories_http_client.js index d83c43f0..f2953c4d 100644 --- a/assets/js/http/categories_http_client.js +++ b/assets/js/http/service_categories_http_client.js @@ -10,78 +10,78 @@ * ---------------------------------------------------------------------------- */ /** - * Categories HTTP client. + * Service-categories HTTP client. * - * This module implements the categories related HTTP requests. + * This module implements the service-categories related HTTP requests. */ -App.Http.Categories = (function () { +App.Http.ServiceCategories = (function () { /** - * Save (create or update) a category. + * Save (create or update) a service-category. * - * @param {Object} category + * @param {Object} serviceCategory * * @return {Object} */ - function save(category) { - return category.id ? update(category) : create(category); + function save(serviceCategory) { + return serviceCategory.id ? update(serviceCategory) : create(serviceCategory); } /** - * Create a category. + * Create a service-category. * - * @param {Object} category + * @param {Object} serviceCategory * * @return {Object} */ - function create(category) { - const url = App.Utils.Url.siteUrl('categories/create'); + function create(serviceCategory) { + const url = App.Utils.Url.siteUrl('service_categories/create'); const data = { csrf_token: vars('csrf_token'), - category: category + service_category: serviceCategory }; return $.post(url, data); } /** - * Update a category. + * Update a service-category. * - * @param {Object} category + * @param {Object} serviceCategory * * @return {Object} */ - function update(category) { - const url = App.Utils.Url.siteUrl('categories/update'); + function update(serviceCategory) { + const url = App.Utils.Url.siteUrl('service_categories/update'); const data = { csrf_token: vars('csrf_token'), - category: category + service_category: serviceCategory }; return $.post(url, data); } /** - * Delete a category. + * Delete a service-category. * - * @param {Number} categoryId + * @param {Number} serviceCategoryId * * @return {Object} */ - function destroy(categoryId) { - const url = App.Utils.Url.siteUrl('categories/destroy'); + function destroy(serviceCategoryId) { + const url = App.Utils.Url.siteUrl('service_categories/destroy'); const data = { csrf_token: vars('csrf_token'), - category_id: categoryId + service_category_id: serviceCategoryId }; return $.post(url, data); } /** - * Search categories by keyword. + * Search service-categories by keyword. * * @param {String} keyword * @param {Number} [limit] @@ -91,7 +91,7 @@ App.Http.Categories = (function () { * @return {Object} */ function search(keyword, limit = null, offset = null, orderBy = null) { - const url = App.Utils.Url.siteUrl('categories/search'); + const url = App.Utils.Url.siteUrl('service_categories/search'); const data = { csrf_token: vars('csrf_token'), @@ -105,18 +105,18 @@ App.Http.Categories = (function () { } /** - * Find a category. + * Find a service-category. * - * @param {Number} categoryId + * @param {Number} serviceCategoryId * * @return {Object} */ - function find(categoryId) { - const url = App.Utils.Url.siteUrl('categories/find'); + function find(serviceCategoryId) { + const url = App.Utils.Url.siteUrl('service_categories/find'); const data = { csrf_token: vars('csrf_token'), - category_id: categoryId + service_category_id: serviceCategoryId }; return $.post(url, data); diff --git a/assets/js/pages/categories.js b/assets/js/pages/service_categories.js similarity index 52% rename from assets/js/pages/categories.js rename to assets/js/pages/service_categories.js index 390cc60b..e2f7c2eb 100644 --- a/assets/js/pages/categories.js +++ b/assets/js/pages/service_categories.js @@ -10,13 +10,13 @@ * ---------------------------------------------------------------------------- */ /** - * Categories page. + * Service-categories page. * - * This module implements the functionality of the categories page. + * This module implements the functionality of the service-categories page. */ App.Pages.Categories = (function () { - const $categories = $('#categories'); - const $filterCategories = $('#filter-categories'); + const $serviceCategories = $('#service-categories'); + const $filterCategories = $('#filter-service-categories'); const $id = $('#id'); const $name = $('#name'); const $description = $('#description'); @@ -32,67 +32,67 @@ App.Pages.Categories = (function () { * * @param {jQuery.Event} event */ - $categories.on('submit', '#filter-categories form', (event) => { + $serviceCategories.on('submit', '#filter-service-categories form', (event) => { event.preventDefault(); - const key = $('#filter-categories .key').val(); + const key = $('#filter-service-categories .key').val(); $('.selected').removeClass('selected'); resetForm(); filter(key); }); /** - * Event: Filter Categories Row "Click" + * Event: Filter Service-Categories Row "Click" * * Displays the selected row data on the right side of the page. * * @param {jQuery.Event} event */ - $categories.on('click', '.category-row', (event) => { - if ($('#filter-categories .filter').prop('disabled')) { - $('#filter-categories .results').css('color', '#AAA'); + $serviceCategories.on('click', '.service-category-row', (event) => { + if ($('#filter-service-categories .filter').prop('disabled')) { + $('#filter-service-categories .results').css('color', '#AAA'); return; // exit because we are on edit mode } - const categoryId = $(event.currentTarget).attr('data-id'); + const serviceCategoryId = $(event.currentTarget).attr('data-id'); - const category = filterResults.find((filterResult) => Number(filterResult.id) === Number(categoryId)); + const serviceCategory = filterResults.find((filterResult) => Number(filterResult.id) === Number(serviceCategoryId)); - display(category); - $('#filter-categories .selected').removeClass('selected'); + display(serviceCategory); + $('#filter-service-categories .selected').removeClass('selected'); $(event.currentTarget).addClass('selected'); - $('#edit-category, #delete-category').prop('disabled', false); + $('#edit-service-category, #delete-service-category').prop('disabled', false); }); /** - * Event: Add Category Button "Click" + * Event: Add Service-Category Button "Click" */ - $categories.on('click', '#add-category', () => { + $serviceCategories.on('click', '#add-service-category', () => { resetForm(); - $categories.find('.add-edit-delete-group').hide(); - $categories.find('.save-cancel-group').show(); - $categories.find('.record-details').find('input, select, textarea').prop('disabled', false); - $categories.find('.record-details .form-label span').prop('hidden', false); + $serviceCategories.find('.add-edit-delete-group').hide(); + $serviceCategories.find('.save-cancel-group').show(); + $serviceCategories.find('.record-details').find('input, select, textarea').prop('disabled', false); + $serviceCategories.find('.record-details .form-label span').prop('hidden', false); $filterCategories.find('button').prop('disabled', true); $filterCategories.find('.results').css('color', '#AAA'); }); /** - * Event: Edit Category Button "Click" + * Event: Edit Service-Category Button "Click" */ - $categories.on('click', '#edit-category', () => { - $categories.find('.add-edit-delete-group').hide(); - $categories.find('.save-cancel-group').show(); - $categories.find('.record-details').find('input, select, textarea').prop('disabled', false); - $categories.find('.record-details .form-label span').prop('hidden', false); + $serviceCategories.on('click', '#edit-service-category', () => { + $serviceCategories.find('.add-edit-delete-group').hide(); + $serviceCategories.find('.save-cancel-group').show(); + $serviceCategories.find('.record-details').find('input, select, textarea').prop('disabled', false); + $serviceCategories.find('.record-details .form-label span').prop('hidden', false); $filterCategories.find('button').prop('disabled', true); $filterCategories.find('.results').css('color', '#AAA'); }); /** - * Event: Delete Category Button "Click" + * Event: Delete Service-Category Button "Click" */ - $categories.on('click', '#delete-category', () => { - const categoryId = $id.val(); + $serviceCategories.on('click', '#delete-service-category', () => { + const serviceCategoryId = $id.val(); const buttons = [ { @@ -104,7 +104,7 @@ App.Pages.Categories = (function () { { text: lang('delete'), click: (event, messageModal) => { - remove(categoryId); + remove(serviceCategoryId); messageModal.dispose(); } } @@ -116,27 +116,27 @@ App.Pages.Categories = (function () { /** * Event: Categories Save Button "Click" */ - $categories.on('click', '#save-category', () => { - const category = { + $serviceCategories.on('click', '#save-service-category', () => { + const serviceCategory = { name: $name.val(), description: $description.val() }; if ($id.val() !== '') { - category.id = $id.val(); + serviceCategory.id = $id.val(); } if (!validate()) { return; } - save(category); + save(serviceCategory); }); /** - * Event: Cancel Category Button "Click" + * Event: Cancel Service-Category Button "Click" */ - $categories.on('click', '#cancel-category', () => { + $serviceCategories.on('click', '#cancel-service-category', () => { const id = $id.val(); resetForm(); if (id !== '') { @@ -148,23 +148,23 @@ App.Pages.Categories = (function () { /** * Filter service categories records. * - * @param {String} keyword This key string is used to filter the category records. + * @param {String} keyword This key string is used to filter the service-category records. * @param {Number} selectId Optional, if set then after the filter operation the record with the given * ID will be selected (but not displayed). * @param {Boolean} show Optional (false), if true then the selected record will be displayed on the form. */ function filter(keyword, selectId = null, show = false) { - App.Http.Categories.search(keyword, filterLimit).then((response) => { + App.Http.ServiceCategories.search(keyword, filterLimit).then((response) => { filterResults = response; - $('#filter-categories .results').empty(); + $('#filter-service-categories .results').empty(); - response.forEach((category) => { - $('#filter-categories .results').append(getFilterHtml(category)).append($('
')); + response.forEach((serviceCategory) => { + $('#filter-service-categories .results').append(getFilterHtml(serviceCategory)).append($('
')); }); if (response.length === 0) { - $('#filter-categories .results').append( + $('#filter-service-categories .results').append( $('', { 'text': lang('no_records_found') }) @@ -178,7 +178,7 @@ App.Pages.Categories = (function () { filterLimit += 20; filter(keyword, selectId, show); } - }).appendTo('#filter-categories .results'); + }).appendTo('#filter-service-categories .results'); } if (selectId) { @@ -188,13 +188,13 @@ App.Pages.Categories = (function () { } /** - * Save a category record to the database (via AJAX post). + * Save a service-category record to the database (via AJAX post). * - * @param {Object} category Contains the category data. + * @param {Object} serviceCategory Contains the service-category data. */ - function save(category) { - App.Http.Categories.save(category).then((response) => { - App.Layouts.Backend.displayNotification(lang('category_saved')); + function save(serviceCategory) { + App.Http.ServiceCategories.save(serviceCategory).then((response) => { + App.Layouts.Backend.displayNotification(lang('service_category_saved')); resetForm(); $filterCategories.find('.key').val(''); filter('', response.id, true); @@ -202,42 +202,42 @@ App.Pages.Categories = (function () { } /** - * Delete category record. + * Delete service-category record. * * @param {Number} id Record ID to be deleted. */ function remove(id) { - App.Http.Categories.destroy(id).then(() => { + App.Http.ServiceCategories.destroy(id).then(() => { App.Layouts.Backend.displayNotification(lang('category_deleted')); resetForm(); - filter($('#filter-categories .key').val()); + filter($('#filter-service-categories .key').val()); }); } /** - * Display a category record on the form. + * Display a service-category record on the form. * - * @param {Object} category Contains the category data. + * @param {Object} serviceCategory Contains the service-category data. */ - function display(category) { - $id.val(category.id); - $name.val(category.name); - $description.val(category.description); + function display(serviceCategory) { + $id.val(serviceCategory.id); + $name.val(serviceCategory.name); + $description.val(serviceCategory.description); } /** - * Validate category data before save (insert or update). + * Validate service-category data before save (insert or update). * * @return {Boolean} Returns the validation result. */ function validate() { - $categories.find('.is-invalid').removeClass('is-invalid'); - $categories.find('.form-message').removeClass('alert-danger').hide(); + $serviceCategories.find('.is-invalid').removeClass('is-invalid'); + $serviceCategories.find('.form-message').removeClass('alert-danger').hide(); try { let missingRequired = false; - $categories.find('.required').each((index, fieldEl) => { + $serviceCategories.find('.required').each((index, fieldEl) => { if (!$(fieldEl).val()) { $(fieldEl).addClass('is-invalid'); missingRequired = true; @@ -250,43 +250,43 @@ App.Pages.Categories = (function () { return true; } catch (error) { - $categories.find('.form-message').addClass('alert-danger').text(error.message).show(); + $serviceCategories.find('.form-message').addClass('alert-danger').text(error.message).show(); return false; } } /** - * Bring the category form back to its initial state. + * Bring the service-category form back to its initial state. */ function resetForm() { $filterCategories.find('.selected').removeClass('selected'); $filterCategories.find('button').prop('disabled', false); $filterCategories.find('.results').css('color', ''); - $categories.find('.add-edit-delete-group').show(); - $categories.find('.save-cancel-group').hide(); - $categories.find('.record-details').find('input, select, textarea').val('').prop('disabled', true); - $categories.find('.record-details .form-label span').prop('hidden', true); - $('#edit-category, #delete-category').prop('disabled', true); + $serviceCategories.find('.add-edit-delete-group').show(); + $serviceCategories.find('.save-cancel-group').hide(); + $serviceCategories.find('.record-details').find('input, select, textarea').val('').prop('disabled', true); + $serviceCategories.find('.record-details .form-label span').prop('hidden', true); + $('#edit-service-category, #delete-service-category').prop('disabled', true); - $categories.find('.record-details .is-invalid').removeClass('is-invalid'); - $categories.find('.record-details .form-message').hide(); + $serviceCategories.find('.record-details .is-invalid').removeClass('is-invalid'); + $serviceCategories.find('.record-details .form-message').hide(); } /** * Get the filter results row HTML code. * - * @param {Object} category Contains the category data. + * @param {Object} serviceCategory Contains the service-category data. * * @return {String} Returns the record HTML code. */ - function getFilterHtml(category) { + function getFilterHtml(serviceCategory) { return $('
', { - 'class': 'category-row entry', - 'data-id': category.id, + 'class': 'service-category-row entry', + 'data-id': serviceCategory.id, 'html': [ $('', { - 'text': category.name + 'text': serviceCategory.name }), $('
') ] @@ -296,7 +296,7 @@ App.Pages.Categories = (function () { /** * Select a specific record from the current filter results. * - * If the category ID does not exist in the list then no record will be selected. + * If the service-category ID does not exist in the list then no record will be selected. * * @param {Number} id The record ID to be selected from the filter results. * @param {Boolean} show Optional (false), if true then the method will display the record on the form. @@ -304,14 +304,14 @@ App.Pages.Categories = (function () { function select(id, show = false) { $filterCategories.find('.selected').removeClass('selected'); - $filterCategories.find('.category-row[data-id="' + id + '"]').addClass('selected'); + $filterCategories.find('.service-category-row[data-id="' + id + '"]').addClass('selected'); if (show) { - const category = filterResults.find((category) => Number(category.id) === Number(id)); + const serviceCategory = filterResults.find((serviceCategory) => Number(serviceCategory.id) === Number(id)); - display(category); + display(serviceCategory); - $('#edit-category, #delete-category').prop('disabled', false); + $('#edit-service-category, #delete-service-category').prop('disabled', false); } } diff --git a/assets/js/pages/services.js b/assets/js/pages/services.js index acc74765..3d4cd534 100644 --- a/assets/js/pages/services.js +++ b/assets/js/pages/services.js @@ -21,7 +21,7 @@ App.Pages.Services = (function () { const $duration = $('#duration'); const $price = $('#price'); const $currency = $('#currency'); - const $category = $('#category'); + const $serviceCategoryId = $('#service-category-id'); const $availabilitiesType = $('#availabilities-type'); const $attendantsNumber = $('#attendants-number'); const $isPrivate = $('#is-private'); @@ -107,7 +107,7 @@ App.Pages.Services = (function () { $duration.val('30'); $price.val('0'); $currency.val(''); - $category.val(''); + $serviceCategoryId.val(''); $availabilitiesType.val('flexible'); $attendantsNumber.val('1'); }); @@ -142,7 +142,7 @@ App.Pages.Services = (function () { availabilities_type: $availabilitiesType.val(), attendants_number: $attendantsNumber.val(), is_private: Number($isPrivate.prop('checked')), - id_categories: $category.val() || undefined + id_service_categories: $serviceCategoryId.val() || undefined }; if ($id.val() !== '') { @@ -300,8 +300,8 @@ App.Pages.Services = (function () { $isPrivate.prop('checked', service.is_private); App.Components.ColorSelection.setColor($color, service.color); - const categoryId = service.id_categories !== null ? service.id_categories : ''; - $category.val(categoryId); + const serviceCategoryId = service.id_service_categories !== null ? service.id_service_categories : ''; + $serviceCategoryId.val(serviceCategoryId); } /** @@ -399,19 +399,19 @@ App.Pages.Services = (function () { } /** - * Update the service category list box. + * Update the service-category list box. * * Use this method every time a change is made to the service categories db table. */ - function updateAvailableCategories() { - App.Http.Categories.search('', 999).then((response) => { - $category.empty(); + function updateAvailableServiceCategories() { + App.Http.ServiceCategories.search('', 999).then((response) => { + $serviceCategoryId.empty(); - response.forEach((category) => { - $category.append(new Option(category.name, category.id)); + response.forEach((serviceCategory) => { + $serviceCategoryId.append(new Option(serviceCategory.name, serviceCategory.id)); }); - $category.append(new Option('', '')).val(''); + $serviceCategoryId.append(new Option('', '')).val(''); }); } @@ -422,7 +422,7 @@ App.Pages.Services = (function () { resetForm(); filter(''); addEventListeners(); - updateAvailableCategories(); + updateAvailableServiceCategories(); } document.addEventListener('DOMContentLoaded', initialize); diff --git a/openapi.yml b/openapi.yml index 330ef8a3..284ed689 100644 --- a/openapi.yml +++ b/openapi.yml @@ -21,10 +21,10 @@ tags: - name: admins - name: appointments - name: availabilities - - name: categories - name: customers - name: providers - name: secretaries + - name: service_categories - name: services - name: settings - name: unavailabilities @@ -776,11 +776,11 @@ paths: security: - BearerToken: [ ] - BasicAuth: [ ] - /categories: + /service_categories: get: tags: - - categories - summary: Get all categories + - service_categories + summary: Get all service-categories parameters: - name: page in: query @@ -812,7 +812,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/CategoryCollection' + $ref: '#/components/schemas/ServiceCategoryCollection' '401': description: Unauthorized '500': @@ -826,13 +826,13 @@ paths: - BasicAuth: [ ] post: tags: - - categories - summary: Create a category + - service_categories + summary: Create a service-category requestBody: content: application/json: schema: - $ref: '#/components/schemas/CategoryPayload' + $ref: '#/components/schemas/ServiceCategoryPayload' required: true responses: '201': @@ -840,7 +840,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/CategoryRecord' + $ref: '#/components/schemas/ServiceCategoryRecord' '401': description: Unauthorized '500': @@ -853,13 +853,13 @@ paths: security: - BearerToken: [ ] - BasicAuth: [ ] - /categories/{categoryId}: + /service_categories/{serviceCategoryId}: get: tags: - - categories - summary: Get a category + - service_categories + summary: Get a service-category parameters: - - name: categoryId + - name: serviceCategoryId in: path required: true schema: @@ -870,7 +870,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/CategoryRecord' + $ref: '#/components/schemas/ServiceCategoryRecord' '401': description: Unauthorized '404': @@ -886,10 +886,10 @@ paths: - BasicAuth: [ ] put: tags: - - categories - summary: Update a category + - service_categories + summary: Update a service-category parameters: - - name: categoryId + - name: serviceCategoryId in: path required: true schema: @@ -898,7 +898,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/CategoryPayload' + $ref: '#/components/schemas/ServiceCategoryPayload' required: true responses: '200': @@ -906,7 +906,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/CategoryRecord' + $ref: '#/components/schemas/ServiceCategoryRecord' '401': description: Unauthorized '404': @@ -923,10 +923,10 @@ paths: - BasicAuth: [ ] delete: tags: - - categories - summary: Delete a category + - service_categories + summary: Delete a service-category parameters: - - name: categoryId + - name: serviceCategoryId in: path required: true schema: @@ -1986,7 +1986,7 @@ components: description: This is a test service. availabilitiesType: flexible attendantsNumber: 1 - categoryId: null + serviceCategoryId: null ServicePayload: type: object properties: @@ -2006,7 +2006,7 @@ components: type: string attendantsNumber: type: integer - categoryId: + serviceCategoryId: type: integer example: name: Test Service @@ -2017,12 +2017,12 @@ components: description: This is a test service. availabilitiesType: flexible attendantsNumber: 1 - categoryId: null + serviceCategoryId: null ServiceCollection: type: array items: $ref: '#/components/schemas/ServiceRecord' - CategoryRecord: + ServiceCategoryRecord: type: object properties: id: @@ -2035,7 +2035,7 @@ components: id: 1 name: Test Category description: This is a test category. - CategoryPayload: + ServiceCategoryPayload: type: object properties: name: @@ -2045,10 +2045,10 @@ components: example: name: Test Category description: This is a test category. - CategoryCollection: + ServiceCategoryCollection: type: array items: - $ref: '#/components/schemas/CategoryRecord' + $ref: '#/components/schemas/ServiceCategoryRecord' AdminRecord: type: object properties: