iflrandevu/src/assets/js/installation.js

164 lines
5.2 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>
2016-01-02 15:47:04 +02:00
* @copyright Copyright (c) 2013 - 2016, 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
* ---------------------------------------------------------------------------- */
$(document).ready(function() {
2016-04-24 20:13:39 +03:00
'use strict';
var MIN_PASSWORD_LENGTH = 7,
AJAX_SUCCESS = 'SUCCESS',
AJAX_FAILURE = 'FAILURE';
2015-10-05 00:19:15 +03:00
$(document).ajaxStart(function() {
$('#loading').show();
});
$(document).ajaxStop(function() {
$('#loading').hide();
});
/**
* Event: Install Easy!Appointments Button "Click"
*/
$('#install').click(function() {
2016-04-24 20:13:39 +03:00
if (!validate()) {
return;
}
2015-10-05 00:19:15 +03:00
2016-04-24 20:13:39 +03:00
var postUrl = GlobalVariables.baseUrl + '/index.php/installation/ajax_install',
postData = {
csrfToken: GlobalVariables.csrfToken,
admin: JSON.stringify(getAdminData()),
company: JSON.stringify(getCompanyData())
};
2015-10-05 00:19:15 +03:00
$.ajax({
url: postUrl,
type: 'POST',
data: postData,
datatype: 'json',
success: function(response) {
2016-04-24 20:13:39 +03:00
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
2015-10-05 00:19:15 +03:00
2015-11-05 22:30:54 +03:00
$('.alert').text('Easy!Appointments has been successfully installed!');
$('.alert').addClass('alert-success');
$('.alert').show();
setTimeout(function() {
window.location.href = GlobalVariables.baseUrl + '/index.php/backend';
}, 1000);
2015-10-05 00:19:15 +03:00
},
error: function(jqXHR, textStatus, errorThrown) {
2015-10-05 01:31:06 +03:00
// Treat the error the same way as php exceptions.
var exc = {
exceptions: [
JSON.stringify({
message: 'The installation could not be completed due to an ' +
'unexpected issue. Please check the browser\'s console for ' +
'more information.'
})
]
};
GeneralFunctions.handleAjaxExceptions(exc);
console.log(exc.exceptions[0].message, jqXHR, textStatus, errorThrown);
2015-10-05 00:19:15 +03:00
}
});
});
/**
* Validates the user input. Use this before executing the installation procedure.
*
* @returns {bool} Returns the validation result.
*/
function validate() {
try {
$('.alert').hide();
$('input').css('border', '');
// Check for empty fields.
var missingRequired = false;
$('input').each(function() {
if ($(this).val() == '') {
$(this).css('border', '2px solid red');
missingRequired = true;
}
});
2016-04-24 20:13:39 +03:00
if (missingRequired) {
2015-10-05 00:19:15 +03:00
throw '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').css('border', '2px solid red');
$('#retype-password').css('border', '2px solid red');
throw 'Passwords do not match!';
}
if ($('#password').val().length < MIN_PASSWORD_LENGTH) {
$('#password').css('border', '2px solid red');
$('#retype-password').css('border', '2px solid red');
throw 'The password must be at least ' + MIN_PASSWORD_LENGTH + ' characters long.';
}
// Validate Email
if (!GeneralFunctions.validateEmail($('#email').val())) {
$('#email').css('border', '2px solid red');
throw 'The email address is invalid!';
}
if (!GeneralFunctions.validateEmail($('#company-email').val())) {
$('#company-email').css('border', '2px solid red');
throw 'The email address is invalid!';
}
return true;
} catch (exc) {
$('.alert').text(exc);
$('.alert').show();
return false;
}
}
/**
* Get the admin data as an object.
*
* @returns {object}
*/
function getAdminData() {
var admin = {
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
};
return admin;
}
/**
* Get the company data as an object.
*
* @returns {object}
*/
function getCompanyData() {
var company = {
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
};
return company;
}
});