easyappointments/assets/js/pages/installation.js

175 lines
5.1 KiB
JavaScript
Raw Normal View History

2015-10-05 00:19:15 +03:00
/* ----------------------------------------------------------------------------
2022-01-18 15:05:42 +03:00
* Easy!Appointments - Online Appointment Scheduler
2015-10-05 00:19:15 +03:00
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
2021-12-18 19:43:45 +03:00
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
2015-10-05 00:19:15 +03:00
* ---------------------------------------------------------------------------- */
/**
* Installation page.
*
* This module implements the functionality of the installation page.
*/
App.Pages.Installation = (function () {
const MIN_PASSWORD_LENGTH = 7;
const $install = $('#install');
const $alert = $('.alert');
const $loading = $('#loading');
const $firstName = $('#first-name');
const $lastName = $('#last-name');
const $email = $('#email');
const $phoneNumber = $('#phone-number');
const $username = $('#username');
const $password = $('#password');
const $passwordConfirm = $('#password-confirm');
const $companyName = $('#company-name');
const $companyEmail = $('#company-email');
const $companyLink = $('#company-link');
2015-10-05 00:19:15 +03:00
$(document).ajaxStart(() => {
$loading.removeClass('d-none');
2015-10-05 00:19:15 +03:00
});
$(document).ajaxStop(() => {
$loading.addClass('d-none');
2015-10-05 00:19:15 +03:00
});
/**
* Event: Install Easy!Appointments Button "Click"
*/
$install.on('click', () => {
2016-04-24 20:13:39 +03:00
if (!validate()) {
return;
}
2015-10-05 00:19:15 +03:00
const url = App.Utils.Url.siteUrl('installation/perform');
const data = {
csrf_token: vars('csrf_token'),
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(() => {
$alert
.text('Easy!Appointments has been successfully installed!')
.addClass('alert-success')
.prop('hidden', false);
setTimeout(() => {
window.location.href = App.Utils.Url.siteUrl('calendar');
}, 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 {
const $fields = $('input');
$alert.removeClass('alert-danger').prop('hidden', true);
$fields.removeClass('is-invalid');
2015-10-05 00:19:15 +03:00
// Check for empty fields.
let missingRequired = false;
$fields.each((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(lang('fields_are_required'));
2016-04-24 20:13:39 +03:00
}
2015-10-05 00:19:15 +03:00
// Validate Passwords
if ($password.val() !== $passwordConfirm.val()) {
$password.addClass('is-invalid');
$passwordConfirm.addClass('is-invalid');
throw new Error(lang('passwords_mismatch'));
2015-10-05 00:19:15 +03:00
}
if ($password.val().length < MIN_PASSWORD_LENGTH) {
$password.addClass('is-invalid');
$passwordConfirm.addClass('is-invalid');
throw new Error(lang('password_length_notice').replace('$number', MIN_PASSWORD_LENGTH));
2015-10-05 00:19:15 +03:00
}
// Validate Email
if (!App.Utils.Validation.email($email.val())) {
$email.addClass('is-invalid');
throw new Error(lang('invalid_email'));
2015-10-05 00:19:15 +03:00
}
if (!App.Utils.Validation.email($companyEmail.val())) {
$companyEmail.addClass('is-invalid');
throw new Error(lang('invalid_email'));
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 {
first_name: $firstName.val(),
last_name: $lastName.val(),
email: $email.val(),
phone_number: $phoneNumber.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 {
company_name: $companyName.val(),
company_email: $companyEmail.val(),
company_link: $companyLink.val(),
2015-10-05 00:19:15 +03:00
};
}
// Validate the base URL setting (must not contain any trailing slash).
if (vars('base_url').slice(-1) === '/') {
App.Utils.Message.show(
'Invalid Configuration 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');
}
return {};
})();