Save consent every time the customer updates his data (#480).

This commit is contained in:
alext 2018-06-24 17:27:16 +02:00
parent 2ddf7b118a
commit d171a5b2ee
4 changed files with 219 additions and 0 deletions

View file

@ -0,0 +1,51 @@
<?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 - 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)]
]));
}
}
}

View file

@ -0,0 +1,105 @@
<?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 - 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'];
}
}

View file

@ -24,6 +24,20 @@ window.FrontendBook = window.FrontendBook || {};
'use strict'; '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. * Determines the functionality of the page.
* *
@ -257,6 +271,36 @@ window.FrontendBook = window.FrontendBook || {};
return; // Validation failed, do not continue. return; // Validation failed, do not continue.
} else { } else {
FrontendBook.updateConfirmFrame(); 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);
}
}
} }
} }

View file

@ -273,4 +273,23 @@ window.FrontendBookApi = window.FrontendBookApi || {};
processingUnavailabilities = false; 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); })(window.FrontendBookApi);