Created a new services resource controller

This commit is contained in:
alextselegidis 2021-11-18 05:13:13 +01:00
parent af42c7c532
commit 8dd509d4dc
5 changed files with 191 additions and 36 deletions

View file

@ -0,0 +1,173 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @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);
}
}
}

View file

@ -42,7 +42,7 @@
<?php $hidden = ($privileges[PRIV_SERVICES]['view'] == TRUE) ? '' : 'd-none' ?>
<?php $active = ($active_menu == PRIV_SERVICES) ? 'active' : '' ?>
<li class="nav-item <?= $active . $hidden ?>">
<a href="<?= site_url('backend/services') ?>" class="nav-link"
<a href="<?= site_url('services') ?>" class="nav-link"
data-tippy-content="<?= lang('manage_services_hint') ?>">
<i class="fas fa-business-time mr-2"></i>
<?= lang('services') ?>

View file

@ -1,15 +1,6 @@
<?php
/**
* @var array $categories
* @var array $services
* @var string $date_format
* @var string $time_format
* @var string $base_url
* @var string $timezones
* @var string $user_id
* @var string $user_email
* @var string $timezone
* @var string $role_slug
* @var array $privileges
*/
?>
@ -24,17 +15,15 @@
<script>
var GlobalVariables = {
csrfToken: <?= json_encode($this->security->get_csrf_hash()) ?>,
baseUrl: <?= json_encode($base_url) ?>,
dateFormat: <?= json_encode($date_format) ?>,
timeFormat: <?= json_encode($time_format) ?>,
services: <?= json_encode($services) ?>,
categories: <?= json_encode($categories) ?>,
baseUrl: <?= json_encode(config('base_url')) ?>,
dateFormat: <?= json_encode(setting('date_format')) ?>,
timeFormat: <?= json_encode(setting('time_format')) ?>,
timezones: <?= json_encode($timezones) ?>,
user: {
id: <?= $user_id ?>,
email: <?= json_encode($user_email) ?>,
timezone: <?= json_encode($timezone) ?>,
role_slug: <?= json_encode($role_slug) ?>,
id: <?= session('user_id') ?>,
email: <?= json_encode(session('user_email')) ?>,
timezone: <?= json_encode(session('timezone')) ?>,
role_slug: <?= json_encode(session('role_slug')) ?>,
privileges: <?= json_encode($privileges) ?>
}
};

View file

@ -39,15 +39,6 @@ window.BackendServices = window.BackendServices || {};
exports.initialize = function (defaultEventHandlers) {
defaultEventHandlers = defaultEventHandlers || true;
// Fill available service categories listbox.
GlobalVariables.categories.forEach(function (category) {
$('#service-category').append(new Option(category.name, category.id));
});
$('#service-category')
.append(new Option('- ' + EALang.no_category + ' -', null))
.val('null');
// Instantiate helper object (service helper by default).
helper = servicesHelper;
helper.resetForm();
@ -57,6 +48,8 @@ window.BackendServices = window.BackendServices || {};
if (defaultEventHandlers) {
bindEventHandlers();
}
exports.updateAvailableCategories();
};
/**

View file

@ -209,7 +209,7 @@
* then the update operation is going to be executed.
*/
ServicesHelper.prototype.save = function (service) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_service';
var url = GlobalVariables.baseUrl + '/index.php/services/' + (service.id ? 'update' : 'create');
var data = {
csrfToken: GlobalVariables.csrfToken,
@ -232,7 +232,7 @@
* @param {Number} id Record ID to be deleted.
*/
ServicesHelper.prototype.delete = function (id) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_service';
var url = GlobalVariables.baseUrl + '/index.php/services/destroy';
var data = {
csrfToken: GlobalVariables.csrfToken,
@ -326,21 +326,21 @@
};
/**
* Filters service records depending a string key.
* Filters service records depending a string keyword.
*
* @param {String} key This is used to filter the service records of the database.
* @param {String} keyword This is used to filter the service records of the database.
* @param {Number} selectId Optional, if set then after the filter operation the record with this
* ID will be selected (but not displayed).
* @param {Boolean} display Optional (false), if true then the selected record will be displayed on the form.
*/
ServicesHelper.prototype.filter = function (key, selectId, display) {
ServicesHelper.prototype.filter = function (keyword, selectId, display) {
display = display || false;
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_services';
var url = GlobalVariables.baseUrl + '/index.php/services/search';
var data = {
csrfToken: GlobalVariables.csrfToken,
key: key,
keyword: keyword,
limit: this.filterLimit
};
@ -369,7 +369,7 @@
'text': EALang.load_more,
'click': function () {
this.filterLimit += 20;
this.filter(key, selectId, display);
this.filter(keyword, selectId, display);
}.bind(this)
}).appendTo('#filter-services .results');
}