From d171a5b2eebbb8c027bbc023f3b53ce04cccf78e Mon Sep 17 00:00:00 2001 From: alext Date: Sun, 24 Jun 2018 17:27:16 +0200 Subject: [PATCH] Save consent every time the customer updates his data (#480). --- src/application/controllers/Consents.php | 51 +++++++++++ src/application/models/Consents_model.php | 105 ++++++++++++++++++++++ src/assets/js/frontend_book.js | 44 +++++++++ src/assets/js/frontend_book_api.js | 19 ++++ 4 files changed, 219 insertions(+) create mode 100644 src/application/controllers/Consents.php create mode 100644 src/application/models/Consents_model.php diff --git a/src/application/controllers/Consents.php b/src/application/controllers/Consents.php new file mode 100644 index 00000000..22487844 --- /dev/null +++ b/src/application/controllers/Consents.php @@ -0,0 +1,51 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.3.2 + * ---------------------------------------------------------------------------- */ + +/** + * Class Consent + * + * Handles user consent related operations. + */ +class Consents extends CI_Controller { + /** + * Save the user's consent. + */ + public function ajax_save_consent() + { + try + { + $consent = $this->input->post('consent'); + + $this->load->model('consents_model'); + + $consent['ip'] = $this->input->ip_address(); + + $consent['id'] = $this->consents_model->add($consent); + + $this->output + ->set_content_type('application/json') + ->set_output(json_encode([ + 'success' => TRUE, + 'id' => $consent['id'] + ])); + } + catch (Exception $exc) + { + $this->output + ->set_content_type('application/json') + ->set_output(json_encode([ + 'exceptions' => [exceptionToJavaScript($exc)] + ])); + } + } +} diff --git a/src/application/models/Consents_model.php b/src/application/models/Consents_model.php new file mode 100644 index 00000000..399706a2 --- /dev/null +++ b/src/application/models/Consents_model.php @@ -0,0 +1,105 @@ + + * @copyright Copyright (c) 2013 - 2018, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.3.2 + * ---------------------------------------------------------------------------- */ + +/** + * Class Consents_model + * + * @package Models + */ +class Consents_model extends CI_Model { + /** + * Add a consent record to the database. + * + * This method adds a consent to the database. + * + * @param array $consent Associative array with the consent's data. + * + * @return int Returns the consent ID. + */ + public function add($consent) + { + $this->validate($consent); + + if ( ! isset($consent['id'])) + { + $consent['id'] = $this->_insert($consent); + } + else + { + $this->_update($consent); + } + + return $consent['id']; + } + + + /** + * Validate consent data before the insert or update operation is executed. + * + * @param array $consent Contains the consent data. + * + * @throws Exception If customer validation fails. + */ + public function validate($consent) + { + if ( ! isset($consent['first_name']) + || ! isset($consent['last_name']) + || ! isset($consent['email']) + || ! isset($consent['ip']) + || ! isset($consent['type'])) + { + throw new Exception('Not all required fields are provided: ' + . print_r($consent, TRUE)); + } + } + + /** + * Insert a new consent record to the database. + * + * @param array $consent Associative array with the consent's data. + * + * @return int Returns the ID of the new record. + * + * @throws Exception If consent record could not be inserted. + */ + protected function _insert($consent) + { + if ( ! $this->db->insert('ea_consents', $consent)) + { + throw new Exception('Could not insert consent to the database.'); + } + + return (int)$this->db->insert_id(); + } + + /** + * Update an existing consent record in the database. + * + * The consent data argument should already include the record ID in order to process the update operation. + * + * @param array $consent Associative array with the consent's data. + * + * @return int Returns the updated record ID. + * + * @throws Exception If consent record could not be updated. + */ + protected function _update($consent) + { + if ( ! $this->db->update('ea_consents', $consent, ['id' => $consent['id']])) + { + throw new Exception('Could not update consent to the database.'); + } + + return (int)$consent['id']; + } +} diff --git a/src/assets/js/frontend_book.js b/src/assets/js/frontend_book.js index bf12123e..2a0a2d53 100644 --- a/src/assets/js/frontend_book.js +++ b/src/assets/js/frontend_book.js @@ -24,6 +24,20 @@ window.FrontendBook = window.FrontendBook || {}; 'use strict'; + /** + * Contains terms and conditions consent. + * + * @type {Object} + */ + var termsAndConditionsConsent; + + /** + * Contains privacy policy consent. + * + * @type {Object} + */ + var privacyPolicyConsent; + /** * Determines the functionality of the page. * @@ -257,6 +271,36 @@ window.FrontendBook = window.FrontendBook || {}; return; // Validation failed, do not continue. } else { FrontendBook.updateConfirmFrame(); + + var $acceptToTermsAndConditions = $('#accept-to-terms-and-conditions'); + if ($acceptToTermsAndConditions.length && $acceptToTermsAndConditions.prop('checked') === true) { + var newTermsAndConditionsConsent = { + first_name: $('#first-name').val(), + last_name: $('#last-name').val(), + email: $('#email').val(), + type: 'terms-and-conditions' + }; + + if (JSON.stringify(newTermsAndConditionsConsent) !== JSON.stringify(termsAndConditionsConsent)) { + termsAndConditionsConsent = newTermsAndConditionsConsent; + FrontendBookApi.saveConsent(termsAndConditionsConsent); + } + } + + var $acceptToPrivacyPolicy = $('#accept-to-privacy-policy'); + if ($acceptToPrivacyPolicy.length && $acceptToPrivacyPolicy.prop('checked') === true) { + var newPrivacyPolicyConsent = { + first_name: $('#first-name').val(), + last_name: $('#last-name').val(), + email: $('#email').val(), + type: 'privacy-policy' + }; + + if (JSON.stringify(newPrivacyPolicyConsent) !== JSON.stringify(privacyPolicyConsent)) { + privacyPolicyConsent = newPrivacyPolicyConsent; + FrontendBookApi.saveConsent(privacyPolicyConsent); + } + } } } diff --git a/src/assets/js/frontend_book_api.js b/src/assets/js/frontend_book_api.js index 1b96e0e8..40f5c177 100755 --- a/src/assets/js/frontend_book_api.js +++ b/src/assets/js/frontend_book_api.js @@ -273,4 +273,23 @@ window.FrontendBookApi = window.FrontendBookApi || {}; processingUnavailabilities = false; } + /** + * Save the user's consent. + * + * @param {Object} consent Contains user's consents. + */ + exports.saveConsent = function (consent) { + var url = GlobalVariables.baseUrl + '/index.php/consents/ajax_save_consent'; + var data = { + csrfToken: GlobalVariables.csrfToken, + consent: consent + }; + + $.post(url, data, function (response) { + if (!GeneralFunctions.handleAjaxExceptions(response)) { + return; + } + }, 'json').fail(GeneralFunctions.ajaxFailureHandler); + }; + })(window.FrontendBookApi);