iflrandevu/assets/js/pages/instance/installation.js

157 lines
4.8 KiB
JavaScript
Raw Normal View History

2015-10-05 00:19:15 +03:00
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
2015-10-05 00:19:15 +03:00
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
$(function () {
2016-04-24 20:13:39 +03:00
'use strict';
var MIN_PASSWORD_LENGTH = 7;
var $install = $('#install');
var $alert = $('.alert');
2015-10-05 00:19:15 +03:00
2018-01-23 12:08:37 +03:00
$(document).ajaxStart(function () {
$('#loading').removeClass('d-none');
2015-10-05 00:19:15 +03:00
});
2018-01-23 12:08:37 +03:00
$(document).ajaxStop(function () {
$('#loading').addClass('d-none');
2015-10-05 00:19:15 +03:00
});
/**
* Event: Install Easy!Appointments Button "Click"
*/
$install.on('click', function () {
2016-04-24 20:13:39 +03:00
if (!validate()) {
return;
}
2015-10-05 00:19:15 +03:00
var url = GlobalVariables.baseUrl + '/index.php/installation/ajax_install';
var data = {
csrfToken: GlobalVariables.csrfToken,
admin: getAdminData(),
company: getCompanyData()
};
2015-10-05 00:19:15 +03:00
$.ajax({
url: url,
2015-10-05 00:19:15 +03:00
type: 'POST',
data: data,
dataType: 'json'
}).done(function (response) {
$alert
.text('Easy!Appointments has been successfully installed!')
.addClass('alert-success')
.prop('hidden', false);
setTimeout(function () {
window.location.href = GlobalVariables.baseUrl + '/index.php/backend';
}, 1000);
});
2015-10-05 00:19:15 +03:00
});
/**
* Validates the user input.
2015-10-05 00:19:15 +03:00
*
* Use this before executing the installation procedure.
*
* @return {Boolean} Returns the validation result.
2015-10-05 00:19:15 +03:00
*/
function validate() {
try {
$alert.removeClass('alert-danger').prop('hidden', true);
$('input').removeClass('is-invalid');
2015-10-05 00:19:15 +03:00
// Check for empty fields.
var missingRequired = false;
$('input').each(function (index, field) {
if (!$(field).val()) {
$(field).addClass('is-invalid');
2015-10-05 00:19:15 +03:00
missingRequired = true;
}
});
2016-04-24 20:13:39 +03:00
if (missingRequired) {
throw new Error('All the page fields are required.');
2016-04-24 20:13:39 +03:00
}
2015-10-05 00:19:15 +03:00
// Validate Passwords
if ($('#password').val() !== $('#retype-password').val()) {
$('#password').addClass('is-invalid');
$('#retype-password').addClass('is-invalid');
throw new Error('Passwords do not match!');
2015-10-05 00:19:15 +03:00
}
if ($('#password').val().length < MIN_PASSWORD_LENGTH) {
$('#password').addClass('is-invalid');
$('#retype-password').addClass('is-invalid');
throw new Error('The password must be at least ' + MIN_PASSWORD_LENGTH + ' characters long.');
2015-10-05 00:19:15 +03:00
}
// Validate Email
if (!GeneralFunctions.validateEmail($('#email').val())) {
$('#email').addClass('is-invalid');
throw new Error('The email address is invalid!');
2015-10-05 00:19:15 +03:00
}
if (!GeneralFunctions.validateEmail($('#company-email').val())) {
$('#company-email').addClass('is-invalid');
throw new Error('The email address is invalid!');
2015-10-05 00:19:15 +03:00
}
return true;
} catch (error) {
$alert.addClass('alert-danger').text(error.message).prop('hidden', false);
2015-10-05 00:19:15 +03:00
return false;
}
}
/**
* Get the admin data as an object.
*
* @return {Object}
2015-10-05 00:19:15 +03:00
*/
function getAdminData() {
return {
2016-04-24 20:13:39 +03:00
first_name: $('#first-name').val(),
last_name: $('#last-name').val(),
email: $('#email').val(),
phone_number: $('#phone-number').val(),
username: $('#username').val(),
password: $('#password').val()
2015-10-05 00:19:15 +03:00
};
}
/**
* Get the company data as an object.
*
* @return {Object}
2015-10-05 00:19:15 +03:00
*/
function getCompanyData() {
return {
2016-04-24 20:13:39 +03:00
company_name: $('#company-name').val(),
company_email: $('#company-email').val(),
company_link: $('#company-link').val()
2015-10-05 00:19:15 +03:00
};
}
// Validate the base URL setting (must not contain any trailing slash).
if (GlobalVariables.baseUrl.slice(-1) === '/') {
GeneralFunctions.displayMessageBox(
'Misconfiguration Detected',
'Please remove any trailing ' +
'slashes from your BASE_URL setting of the root config.php file and try again.'
);
$install.prop('disabled', true).fadeTo('0.4');
}
2015-10-05 00:19:15 +03:00
});