diff --git a/application/controllers/Categories.php b/application/controllers/Categories.php index d26e7610..270a4419 100644 --- a/application/controllers/Categories.php +++ b/application/controllers/Categories.php @@ -52,6 +52,11 @@ class Categories extends EA_Controller { $role_slug = session('role_slug'); + script_vars([ + 'user_id' => $user_id, + 'role_slug' => $role_slug, + ]); + html_vars([ 'page_title' => lang('categories'), 'active_menu' => PRIV_SERVICES, @@ -100,7 +105,7 @@ class Categories extends EA_Controller { { try { - $category = json_decode(request('category'), TRUE); + $category = request('category'); if (cannot('add', PRIV_SERVICES)) { @@ -127,7 +132,7 @@ class Categories extends EA_Controller { { try { - $category = json_decode(request('category'), TRUE); + $category = request('category'); if (cannot('edit', PRIV_SERVICES)) { diff --git a/application/views/pages/categories.php b/application/views/pages/categories.php index ec7a6378..08c3caca 100755 --- a/application/views/pages/categories.php +++ b/application/views/pages/categories.php @@ -87,27 +87,10 @@ - - - + + + + + diff --git a/assets/js/http/categories_http_client.js b/assets/js/http/categories_http_client.js index cb1919b7..b0878e2d 100644 --- a/assets/js/http/categories_http_client.js +++ b/assets/js/http/categories_http_client.js @@ -11,7 +11,18 @@ App.Http.Categories = (function () { /** - * Create an category. + * Save (create or update) a category. + * + * @param {Object} category + * + * @return {Object} + */ + function save(category) { + return category.id ? update(category) : create(category); + } + + /** + * Create a category. * * @param {Object} category * @@ -29,7 +40,7 @@ App.Http.Categories = (function () { } /** - * Update an category. + * Update a category. * * @param {Object} category * @@ -47,7 +58,7 @@ App.Http.Categories = (function () { } /** - * Delete an category. + * Delete a category. * * @param {Number} categoryId * @@ -89,7 +100,7 @@ App.Http.Categories = (function () { } /** - * Find an category. + * Find a category. * * @param {Number} categoryId * @@ -107,6 +118,7 @@ App.Http.Categories = (function () { } return { + save, create, update, destroy, diff --git a/assets/js/pages/backend_categories.js b/assets/js/pages/backend_categories.js deleted file mode 100644 index 75b7f8ba..00000000 --- a/assets/js/pages/backend_categories.js +++ /dev/null @@ -1,62 +0,0 @@ -/* ---------------------------------------------------------------------------- - * Easy!Appointments - Open Source Web Scheduler - * - * @package EasyAppointments - * @author A.Tselegidis - * @copyright Copyright (c) Alex Tselegidis - * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link https://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -window.BackendCategories = window.BackendCategories || {}; - -/** - * Backend Categories - * - * This namespace handles the js functionality of the backend categories page. - * - * @module BackendCategories - */ -(function (exports) { - 'use strict'; - - /** - * Contains the basic record methods for the page. - * - * @type {CategoriesHelper} - */ - var helper; - - var categoriesHelper = new CategoriesHelper(); - - /** - * Default initialize method of the page. - * - * @param {Boolean} [defaultEventHandlers] Optional (true), determines whether to bind the default event handlers. - */ - exports.initialize = function (defaultEventHandlers) { - defaultEventHandlers = defaultEventHandlers || true; - - // Instantiate helper object (category helper by default). - helper = categoriesHelper; - helper.resetForm(); - helper.filter(''); - helper.bindEventHandlers(); - - if (defaultEventHandlers) { - bindEventHandlers(); - } - - Backend.placeFooterToBottom(); - }; - - /** - * Binds the default event handlers of the backend categories page. - * - * Do not use this method if you include the "BackendCategories" namespace on another page. - */ - function bindEventHandlers() { - // - } -})(window.BackendCategories); diff --git a/assets/js/pages/backend_categories_helper.js b/assets/js/pages/categories.js similarity index 57% rename from assets/js/pages/backend_categories_helper.js rename to assets/js/pages/categories.js index bebedea0..1b2e1423 100644 --- a/assets/js/pages/backend_categories_helper.js +++ b/assets/js/pages/categories.js @@ -9,39 +9,26 @@ * @since v1.0.0 * ---------------------------------------------------------------------------- */ -(function () { - 'use strict'; - - /** - * CategoriesHelper Class - * - * This class contains the core method implementations that belong to the categories tab - * of the backend services page. - * - * @class CategoriesHelper - */ - function CategoriesHelper() { - this.filterResults = {}; - this.filterLimit = 20; - } +App.Pages.Categories = (function () { + const $categories = $('#categories'); + let filterResults = {}; + let filterLimit = 20; /** * Binds the default event handlers of the categories tab. */ - CategoriesHelper.prototype.bindEventHandlers = function () { - var instance = this; - + function bindEventHandlers() { /** * Event: Filter Categories Form "Submit" * * @param {jQuery.Event} event */ - $('#categories').on('submit', '#filter-categories form', function (event) { + $categories.on('submit', '#filter-categories form', function (event) { event.preventDefault(); - var key = $('#filter-categories .key').val(); + const key = $('#filter-categories .key').val(); $('.selected').removeClass('selected'); - instance.resetForm(); - instance.filter(key); + resetForm(); + filter(key); }); /** @@ -49,19 +36,19 @@ * * Displays the selected row data on the right side of the page. */ - $('#categories').on('click', '.category-row', function () { + $categories.on('click', '.category-row', function () { if ($('#filter-categories .filter').prop('disabled')) { $('#filter-categories .results').css('color', '#AAA'); return; // exit because we are on edit mode } - var categoryId = $(this).attr('data-id'); + const categoryId = $(this).attr('data-id'); - var category = instance.filterResults.find(function (filterResult) { + const category = filterResults.find(function (filterResult) { return Number(filterResult.id) === Number(categoryId); }); - instance.display(category); + display(category); $('#filter-categories .selected').removeClass('selected'); $(this).addClass('selected'); $('#edit-category, #delete-category').prop('disabled', false); @@ -70,8 +57,8 @@ /** * Event: Add Category Button "Click" */ - $('#categories').on('click', '#add-category', function () { - instance.resetForm(); + $categories.on('click', '#add-category', function () { + resetForm(); $('#categories .add-edit-delete-group').hide(); $('#categories .save-cancel-group').show(); $('#categories .record-details').find('input, select, textarea').prop('disabled', false); @@ -82,7 +69,7 @@ /** * Event: Edit Category Button "Click" */ - $('#categories').on('click', '#edit-category', function () { + $categories.on('click', '#edit-category', function () { $('#categories .add-edit-delete-group').hide(); $('#categories .save-cancel-group').show(); $('#categories .record-details').find('input, select, textarea').prop('disabled', false); @@ -93,10 +80,10 @@ /** * Event: Delete Category Button "Click" */ - $('#categories').on('click', '#delete-category', function () { - var categoryId = $('#category-id').val(); + $categories.on('click', '#delete-category', function () { + const categoryId = $('#category-id').val(); - var buttons = [ + const buttons = [ { text: App.Lang.cancel, click: function () { @@ -106,20 +93,20 @@ { text: App.Lang.delete, click: function () { - instance.delete(categoryId); + remove(categoryId); $('#message-box').dialog('close'); } } ]; - GeneralFunctions.displayMessageBox(App.Lang.delete_category, App.Lang.delete_record_prompt, buttons); + App.Utils.Message.show(App.Lang.delete_category, App.Lang.delete_record_prompt, buttons); }); /** * Event: Categories Save Button "Click" */ - $('#categories').on('click', '#save-category', function () { - var category = { + $categories.on('click', '#save-category', function () { + const category = { name: $('#category-name').val(), description: $('#category-description').val() }; @@ -128,30 +115,30 @@ category.id = $('#category-id').val(); } - if (!instance.validate()) { + if (!validate()) { return; } - instance.save(category); + save(category); }); /** * Event: Cancel Category Button "Click" */ - $('#categories').on('click', '#cancel-category', function () { - var id = $('#category-id').val(); - instance.resetForm(); + $categories.on('click', '#cancel-category', function () { + const id = $('#category-id').val(); + resetForm(); if (id !== '') { - instance.select(id, true); + select(id, true); } }); - }; + } /** * Remove the previously registered event handlers. */ - CategoriesHelper.prototype.unbindEventHandlers = function () { - $('#categories') + function unbindEventHandlers() { + $categories .off('click', '#filter-categories .clear') .off('submit', '#filter-categories form') .off('click', '.category-row') @@ -160,7 +147,7 @@ .off('click', '#delete-category') .off('click', '#save-category') .off('click', '#cancel-category'); - }; + } /** * Filter service categories records. @@ -168,122 +155,93 @@ * @param {String} keyword This key string is used to filter the 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} display Optional (false), if true then the selected record will be displayed on the form. + * @param {Boolean} show Optional (false), if true then the selected record will be displayed on the form. */ - CategoriesHelper.prototype.filter = function (keyword, selectId, display) { - var url = GlobalVariables.baseUrl + '/index.php/categories/search'; + function filter(keyword, selectId = null, show = false) { + App.Http.Categories.search(keyword, filterLimit).then((response) => { + filterResults = response; - var data = { - csrf_token: GlobalVariables.csrfToken, - keyword: keyword, - limit: this.filterLimit - }; + $('#filter-categories .results').empty(); - $.post(url, data).done( - function (response) { - this.filterResults = response; + response.forEach( + function (category) { + $('#filter-categories .results').append(getFilterHtml(category)).append($('
')); + }.bind(this) + ); - $('#filter-categories .results').empty(); - - response.forEach( - function (category) { - $('#filter-categories .results').append(this.getFilterHtml(category)).append($('
')); - }.bind(this) + if (response.length === 0) { + $('#filter-categories .results').append( + $('', { + 'text': App.Lang.no_records_found + }) ); + } else if (response.length === filterLimit) { + $('