From 3eccb661922ba6e6fce54ba469497b09c275be0a Mon Sep 17 00:00:00 2001 From: "alextselegidis@gmail.com" Date: Sat, 14 Sep 2013 16:10:59 +0000 Subject: [PATCH] * Created the backend/settings page. * Added basic backend_settings.js code structure. * Added User Model and new functions to settings_model.php --- Release Notes.txt | 15 +- src/application/config/config.php | 5 +- src/application/config/constants.php | 3 + src/application/controllers/backend.php | 12 + src/application/controllers/backend_api.php | 27 +++ src/application/models/settings_model.php | 34 +++ src/application/models/user_model.php | 41 ++++ src/application/views/backend/settings.php | 255 +++++++++++++++++++- src/application/views/backend/users.php | 4 +- src/assets/css/backend.css | 5 +- src/assets/js/backend.js | 4 + src/assets/js/backend_settings.js | 148 ++++++++++++ src/assets/js/backend_users.js | 12 +- 13 files changed, 537 insertions(+), 28 deletions(-) create mode 100644 src/application/models/user_model.php create mode 100644 src/assets/js/backend_settings.js diff --git a/Release Notes.txt b/Release Notes.txt index abadfc41..0e76cd5a 100644 --- a/Release Notes.txt +++ b/Release Notes.txt @@ -1,12 +1,11 @@ -VERSION 0.5 +VERSION 0.6 =========== -[Feature] Admin users management. -[Feature] Service provider users management. -[Feature] Customer users management. -[Feature] Secretary users management. -[Feature] Custom error 404 page. -[Enhancement] Display selected service description and price at the appointment book wizard. -[Enhancement] Group services by categories at the appointment book wizard. +[Feature] System and user settings page. +[Feature] Login process included. +[Feature] Applied user privileges through all the system depending their role type. +[Feature] Select existing customer from backend/calendar when trying to add a new appointment. +[Fix] Fix backend/calendar service and provider functionality. + Official Easy!Appointments Website: http://easyappointments.org diff --git a/src/application/config/config.php b/src/application/config/config.php index 641d7f3e..cf4496bc 100644 --- a/src/application/config/config.php +++ b/src/application/config/config.php @@ -1,5 +1,8 @@ load->model('settings_model'); + $this->load->model('user_model'); + + $this->load->library('session'); + + // @task Apply data for testing this page (this must be done during the login process). + $this->session->set_userdata('user_id', 18); + $this->session->set_userdata('user_slug', DB_SLUG_ADMIN); + + $user_id = $this->session->userdata('user_id'); $view['base_url'] = $this->config->item('base_url'); $view['company_name'] = $this->settings_model->get_setting('company_name'); + $view['user_slug'] = $this->session->userdata('user_slug'); + $view['system_settings'] = $this->settings_model->get_settings(); + $view['user_settings'] = $this->user_model->get_settings($user_id); $this->load->view('backend/header', $view); $this->load->view('backend/settings', $view); diff --git a/src/application/controllers/backend_api.php b/src/application/controllers/backend_api.php index 7c154ec8..674441bb 100644 --- a/src/application/controllers/backend_api.php +++ b/src/application/controllers/backend_api.php @@ -793,6 +793,33 @@ class Backend_api extends CI_Controller { )); } } + + /** + * [AJAX] Save a setting or multiple settings in the database. + * + * This method is used to store settings in the database. It can be either system + * or user settings, one or many. Use the $_POST variables accordingly. + * + * @param array $_POST['settings'] Contains an array with settings. + * @param bool $_POST['type'] Determines the settings type, can be either SETTINGS_SYSTEM + * or SETTINGS_USER. + */ + public function ajax_save_settings() { + try { + if ($_POST['type'] == SETTINGS_SYSTEM) { + // @task Implement save settings. + } else if ($_POST['type'] == SETTINGS_USER) { + $this->load->library('session'); + $this->load->model('user_model'); + $user_id = $this->session->userdata('user_id'); + $this->user_model->save_settings($_POST['settings'], $user_id); + } + } catch(Exception $exc) { + echo json_encode(array( + 'exceptions' => array(exceptionToJavaScript($exc)) + )); + } + } } /* End of file backend_api.php */ diff --git a/src/application/models/settings_model.php b/src/application/models/settings_model.php index 2ceefe18..98b3acd3 100644 --- a/src/application/models/settings_model.php +++ b/src/application/models/settings_model.php @@ -90,6 +90,40 @@ class Settings_Model extends CI_Model { return $this->db->delete('ea_settings', array('name' => $name)); } + + /** + * Saves all the system settings into the database. + * + * This method is usefull when trying to save all the system settings at once instead of + * saving them one by one. + * + * @param array $settings Contains all the system settings. + * @return bool Returns the save operation result. + * + * @throws Exception When the update operation won't work for a specific setting. + */ + public function save_settings($settings) { + if (!is_array($settings)) { + throw new Exception('$settings argument is invalid: '. print_r($settings, TRUE)); + } + + foreach($settings as $name=>$value) { + if (!$this->db->update('ea_settings', array('value' => $value), array('name' => $name))) { + throw new Exception('Could not save setting (' . $name . ' - ' . $value . ')'); + } + } + + return TRUE; + } + + /** + * Returns all the system settings at once. + * + * @return array Array of all the system settings stored in the 'ea_settings' table. + */ + public function get_settings() { + return $this->db->get('ea_settings')->result_array(); + } } /* End of file settings_model.php */ diff --git a/src/application/models/user_model.php b/src/application/models/user_model.php new file mode 100644 index 00000000..fbacc999 --- /dev/null +++ b/src/application/models/user_model.php @@ -0,0 +1,41 @@ +db->get_where('ea_user_settings', array('id_users' => $user_id))->row_array(); + unset($settings['id_users']); + return $settings; + } + + /** + * This method saves the user settings into the database. + * + * @param array $settings Contains the current users settings. + * @param numeric $user_id User record id of the settings. + * @return bool Returns the operation result. + */ + public function save_settings($settings, $user_id) { + $settings['id_users'] = $user_id; + $this->db->where('id_users', $user_id); + return $this->db->update('ea_user_settings', $settings); + } +} + +/* End of file user_model.php */ +/* Location: ./application/models/user_model.php */ \ No newline at end of file diff --git a/src/application/views/backend/settings.php b/src/application/views/backend/settings.php index fb95cac4..1ad43951 100644 --- a/src/application/views/backend/settings.php +++ b/src/application/views/backend/settings.php @@ -1,11 +1,248 @@ -
-

This page is not implemented yet!

-

- It will be included in the next release of Easy!Appointments (v0.6). -
- Check the Google+ - Community for development updates or visit the - Support Group for help. + + + -

+
+ + + +
+
+
+ + General Settings + + + + + + Company name will be displayed everywhere on the system + (required). + +
+ + + + This will be the company email address. It will be used + as the sender and the reply address of the system emails (required). + +
+ + + + Company link should point to the official website of + the company (optional). +
+
+
+ + + + + + +
\ No newline at end of file diff --git a/src/application/views/backend/users.php b/src/application/views/backend/users.php index a2a0ecdd..49914914 100644 --- a/src/application/views/backend/users.php +++ b/src/application/views/backend/users.php @@ -111,7 +111,7 @@
@@ -205,7 +205,7 @@

diff --git a/src/assets/css/backend.css b/src/assets/css/backend.css index b6d9c0f8..a6b2efac 100644 --- a/src/assets/css/backend.css +++ b/src/assets/css/backend.css @@ -363,9 +363,10 @@ body .modal-header h3 { #users-page #secretary-providers, #users-page #provider-services { border: 2px solid #ccc; - width: 300px; - height: 100px; + width: 320px; + height: 140px; overflow-y: scroll; + padding: 7px; } diff --git a/src/assets/js/backend.js b/src/assets/js/backend.js index a11ba188..99b32ed7 100644 --- a/src/assets/js/backend.js +++ b/src/assets/js/backend.js @@ -29,6 +29,10 @@ var Backend = { EXCEPTIONS_MESSAGE: 'The operation could not complete due to unexpected issues. ', WARNINGS_TITLE: 'Unexpected Warnings', WARNINGS_MESSAGE: 'The operation completed but some warnings appeared. ', + DB_SLUG_ADMIN: 'admin', + DB_SLUG_PROVIDER: 'provider', + DB_SLUG_SECRETARY: 'secretary', + DB_SLUG_CUSTOMER: 'customer', /** * Place the backend footer always on the bottom of the page. diff --git a/src/assets/js/backend_settings.js b/src/assets/js/backend_settings.js new file mode 100644 index 00000000..46686cc8 --- /dev/null +++ b/src/assets/js/backend_settings.js @@ -0,0 +1,148 @@ +/** + * Contains the functionality of the backend settings page. Can either work for + * system or user settings, but the actions allowed to the user are restricted to + * his role (only admin has full privileges). + * + * @namespace BackendSettings + */ +var BackendSettings = { + SETTINGS_SYSTEM: 'SETTINGS_SYSTEM', + SETTINGS_USER: 'SETTINGS_USER', + + /** + * Tab settings object. + * + * @type {object} + */ + settings: {}, + + /** + * Initialize Page + * + * @param {bool} bindEventHandlers (OPTIONAL)Determines whether to bind the default event + * handlers (default = true). + * @returns {undefined} + */ + initialize: function(bindEventHandlers) { + if (bindEventHandlers == undefined) bindEventHandlers = true; + + // Apply values from database. + $.each(GlobalVariables.settings.system, function(index, setting) { + $('#general input[data-field="' + setting.name + '"]').val(setting.value); + }); + + if (bindEventHandlers) { + BackendSettings.bindEventHandlers(); + } + }, + + bindEventHandlers: function() { + /** + * Event: Tab "Click" + * + * Change the visible tab contents. + */ + $('.tab').click(function() { + $('.active').removeClass('active'); + $(this).addClass('active'); + $('.tab-content').hide(); + + if ($(this).hasClass('general-tab')) { + $('#general').show(); + BackendSettings.settings = new SystemSettings(); + } else if ($(this).hasClass('business-logic-tab')) { + $('#business-logic').show(); + BackendSettings.settings = new SystemSettings(); + } else if ($(this).hasClass('user-tab')) { + $('#user').show(); + BackendSettings.settings = new UserSettings(); + } + }); + + /** + * Event: Save Settings Button "Click" + * + * Store the setting changes into the database. + */ + $('.save-settings').click(function() { + var settings = BackendSettings.settings.get(); + BackendSettings.settings.save(); + }); + } +}; + +/** + * "System Settings" Tab Helper + * @class SystemSettings + */ +var SystemSettings = function() {}; + +/** + * Save the system settings. This method is run after changes are detected on the + * tab input fields. + * + * @param {array} settings Contains the system settings data. + */ +SystemSettings.prototype.save = function(settings) { + var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_save_settings'; + var postData = { + 'settings': JSON.stringify(settings), + 'type': BackendSettings.SETTINGS_SYSTEM + }; + + $.post(postUrl, postData, function(response) { + /////////////////////////////////////////////////////////// + console.log('Save General Settings Response:', response); + /////////////////////////////////////////////////////////// + + if (!Backend.handleAjaxExceptions(response)) return; + + Backend.displayNotification('Settings saved successfully!'); + }, 'json'); +}; + +/** + * Prepare the system settings array. This method uses the DOM elements of the + * backend/settings page, so it can't be used in another page. + * + * @returns {array} Returns the system settings array. + */ +SystemSettings.prototype.get = function() { + var settings = [] + var tmpSetting = {}; + + // General Settings Tab + $('#general input').each(function() { + tmpSetting[$(this).attr('data-field')] = $(this).val(); + settings.push(tmpSetting); + }); + + // Business Logic Tab + // @task Get business logic settings. + + return settings; +}; + +/** + * "User Settings" Tab Helper + * @class UserSettings + */ +var UserSettings = function() {}; + +/** + * Get the settings data for the user settings. + * + * @returns {array} Returns the user settings array. + */ +UserSettings.prototype.get = function() { + +} + +/** + * Store the user settings into the database. + * + * @param {array} settings Contains the user settings. + */ +UserSettings.prototype.save = function(settings) { + +} diff --git a/src/assets/js/backend_users.js b/src/assets/js/backend_users.js index a074d48c..6944bc2b 100644 --- a/src/assets/js/backend_users.js +++ b/src/assets/js/backend_users.js @@ -28,14 +28,14 @@ var BackendUsers = { // Fill the services and providers list boxes. $.each(GlobalVariables.services, function(index, service) { - var html = '' - + service.name + '
'; + var html = ''; $('#provider-services').append(html); }); $.each(GlobalVariables.providers, function(index, provider) { - var html = '' - + provider.first_name + ' ' + provider.last_name + '
'; + var html = ''; $('#secretary-providers').append(html); }); @@ -82,8 +82,8 @@ var BackendUsers = { $('#secretary-providers').html(''); $.each(GlobalVariables.providers, function(index, provider) { - var html = '' - + provider.first_name + ' ' + provider.last_name + '
'; + var html = ''; $('#secretary-providers').append(html); }); $('#secretary-providers input[type="checkbox"]').prop('disabled', true);