From 8dd509d4dcb0e8be2012a6c0d3d372590481aa94 Mon Sep 17 00:00:00 2001 From: alextselegidis Date: Thu, 18 Nov 2021 05:13:13 +0100 Subject: [PATCH] Created a new services resource controller --- application/controllers/Services.php | 173 ++++++++++++++++++ .../views/layouts/backend/backend_header.php | 2 +- .../views/pages/services/services_page.php | 25 +-- assets/js/backend_services.js | 11 +- assets/js/backend_services_helper.js | 16 +- 5 files changed, 191 insertions(+), 36 deletions(-) create mode 100644 application/controllers/Services.php diff --git a/application/controllers/Services.php b/application/controllers/Services.php new file mode 100644 index 00000000..061dba62 --- /dev/null +++ b/application/controllers/Services.php @@ -0,0 +1,173 @@ + + * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Services controller. + * + * Handles the services related operations. + * + * @package Controllers + */ +class Services extends EA_Controller { + /** + * Services constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->model('services_model'); + $this->load->model('roles_model'); + + $this->load->library('accounts'); + $this->load->library('timezones'); + } + + /** + * Render the backend services page. + * + * On this page admin users will be able to manage services, which are eventually selected by customers during the + * booking process. + */ + public function index() + { + session(['dest_url' => site_url('services')]); + + if (cannot('view', 'services')) + { + show_error('Forbidden', 403); + } + + $user_id = session('user_id'); + + $role_slug = session('role_slug'); + + $this->load->view('pages/services/services_page', [ + 'page_title' => lang('services'), + 'active_menu' => PRIV_SERVICES, + 'user_display_name' => $this->accounts->get_user_display_name($user_id), + 'timezones' => $this->timezones->to_array(), + 'privileges' => $this->roles_model->get_permissions_by_slug($role_slug), + ]); + } + + /** + * Filter services by the provided keyword. + */ + public function search() + { + try + { + if (cannot('view', 'services')) + { + show_error('Forbidden', 403); + } + + $keyword = request('keyword', ''); + + $order_by = 'name ASC'; + + $limit = request('limit', 1000); + + $offset = 0; + + $services = $this->services_model->search($keyword, $limit, $offset, $order_by); + + json_response($services); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Create a service. + */ + public function create() + { + try + { + $service = json_decode(request('service'), TRUE); + + if (cannot('add', 'services')) + { + show_error('Forbidden', 403); + } + + $service_id = $this->services_model->save($service); + + json_response([ + 'success' => TRUE, + 'id' => $service_id + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Update a service. + */ + public function update() + { + try + { + $service = json_decode(request('service'), TRUE); + + if (cannot('edit', 'services')) + { + show_error('Forbidden', 403); + } + + $service_id = $this->services_model->save($service); + + json_response([ + 'success' => TRUE, + 'id' => $service_id + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } + + /** + * Remove a service. + */ + public function destroy() + { + try + { + if (cannot('delete', 'services')) + { + show_error('Forbidden', 403); + } + + $service_id = request('service_id'); + + $this->services_model->delete($service_id); + + json_response([ + 'success' => TRUE, + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } +} diff --git a/application/views/layouts/backend/backend_header.php b/application/views/layouts/backend/backend_header.php index 18c2b2df..2df71256 100755 --- a/application/views/layouts/backend/backend_header.php +++ b/application/views/layouts/backend/backend_header.php @@ -42,7 +42,7 @@