/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
var InstallScript = function(opt) {
this.reChecking = false;
if (typeof(opt.action) !== 'undefined') {
var action = opt.action;
this.action = action;
this[action]();
}
if (typeof(opt.langs) !== 'undefined') {
this.langs = opt.langs;
}
if (typeof(opt.modRewriteUrl) !== 'undefined') {
this.modRewriteUrl = opt.modRewriteUrl;
}
if (typeof(opt.serverType) !== 'undefined') {
this.serverType = opt.serverType;
}
if (typeof(opt.OS) !== 'undefined') {
this.OS = opt.OS;
}
this.connSett = {};
this.userSett = {};
this.systemSettings = {};
this.emailSettings = {};
this.checkActions = [
{
'action': 'checkModRewrite',
'break': true,
},
{
'action': 'checkPermission',
'break': true,
},
{
'action': 'applySett',
'break': true,
},
{
'action': 'buildDatabse',
'break': true,
},
{
'action': 'createUser',
'break': true,
},
];
this.checkIndex = 0;
this.checkError = false;
}
InstallScript.prototype.main = function() {
var self = this;
var nextAction = 'step1';
$("#start").click(function(){
$(this).attr('disabled', 'disabled');
self.goTo(nextAction);
})
$('[name="user-lang"]').change(function(){
self.goTo(self.action);
})
}
InstallScript.prototype.step1 = function() {
var self = this;
var backAction = 'main';
var nextAction = 'step2';
$('#back').click(function(){
$(this).attr('disabled', 'disabled');
self.goTo(backAction);
})
$("#next").click(function(){
$(this).attr('disabled', 'disabled');
var licenseAgree = $('#license-agree');
if (licenseAgree.length > 0 && !licenseAgree.is(':checked')) {
$(this).removeAttr('disabled');
self.showMsg({msg: self.getLang('You must agree to the license agreement', 'messages'), error: true});
}
else {
self.goTo(nextAction);
}
})
}
InstallScript.prototype.step2 = function() {
var self = this;
var backAction = 'step1';
var nextAction = 'step3';
$('#back').click(function(){
$(this).attr('disabled', 'disabled');
self.goTo(backAction);
})
$('#test-connection, #next').click(function(){
$(this).attr('disabled', 'disabled');
self.setConnSett();
if (!self.validate()) {
$(this).removeAttr('disabled');
return;
}
self.showLoading();
var btn = $(this);
self.checkSett({
success: function(data) {
if (data.success) {
if (btn.attr('id') == 'next') self.goTo(nextAction);
else self.showMsg({msg: self.getLang('All Settings correct', 'messages')});
}
else {
$('#next').removeAttr('disabled');
}
$('#test-connection').removeAttr('disabled');
self.hideLoading();
}, // success END
error: function() {
$('#next').removeAttr('disabled');
$('#test-connection').removeAttr('disabled');
self.hideLoading();
}, // error END
}) // checkSett END
})
}
InstallScript.prototype.step3 = function() {
var self = this;
var backAction = 'step2';
var nextAction = 'step4';
$('#back').click(function(){
$(this).attr('disabled', 'disabled');
self.goTo(backAction);
})
$("#next").click(function(){
$(this).attr('disabled', 'disabled');
self.setUserSett();
if (!self.validate()) {
$(this).removeAttr('disabled');
return;
}
self.checkPass({
success: function(){
self.showLoading();
self.actionsChecking();
},
error: function(msg) {
$("#next").removeAttr('disabled');
self.showMsg({msg: self.getLang(msg, 'messages'), error: true});
}
});
})
}
InstallScript.prototype.step4 = function() {
var self = this;
var backAction = 'step3';
var nextAction = 'step5';
$('#back').click(function(){
$(this).attr('disabled', 'disabled');
self.goTo(backAction);
})
$("#next").click(function(){
$(this).attr('disabled', 'disabled');
self.setSystemSett();
if (!self.validate()) {
$(this).removeAttr('disabled');
return;
}
var data = self.systemSettings;
data.action = 'setPreferences';
$.ajax({
url: "index.php",
type: "POST",
data: data,
dataType: 'json',
})
.done(function(ajaxData){
if (typeof(ajaxData) != 'undefined' && ajaxData.success) {
self.goTo(nextAction);
}
})
.fail(function(ajaxData){
})
})
}
InstallScript.prototype.step5 = function() {
var self = this;
var backAction = 'step4';
var nextAction = 'finish';
$('#back').click(function(){
$(this).attr('disabled', 'disabled');
self.goTo(backAction);
})
$("#next").click(function(){
$(this).attr('disabled', 'disabled');
self.setEmailSett();
if (!self.validate()) {
$(this).removeAttr('disabled');
return;
}
var data = self.emailSettings;
data.action = 'setEmailSett';
$.ajax({
url: "index.php",
type: "POST",
data: data,
dataType: 'json',
})
.done(function(ajaxData){
if (typeof(ajaxData) != 'undefined' && ajaxData.success) {
self.goTo(nextAction);
}
})
.fail(function(ajaxData){
})
})
$('.field-smtpAuth').find('input[type="checkbox"]').change( function(e){
if ($(this).is(':checked')) {
$('.cell-smtpPassword').removeClass('hide');
$('.cell-smtpUsername').removeClass('hide');
}
else {
$('.cell-smtpPassword').addClass('hide');
$('.cell-smtpUsername').addClass('hide');
$('.cell-smtpUsername').find('input').val('');
$('.cell-smtpPassword').find('input').val('');
}
});
$('[name="smtpSecurity"]').change( function(e){
if ($(this).val() == '') {
$('[name="smtpPort"]').val('25');
}
else {
$('[name="smtpPort"]').val('465');
}
});
}
InstallScript.prototype.errors = function() {
var self = this;
this.reChecking = true;
var nextAction = '';
$("#re-check").click(function(){
$(this).attr('disabled', 'disabled');
self.showLoading();
self.actionsChecking();
})
}
InstallScript.prototype.finish = function() {
var self = this;
var nextAction = '';
$("#start").click(function(){
self.goToEspo();
})
}
InstallScript.prototype.setConnSett = function() {
this.connSett.dbName = $('[name="db-name"]').val();
this.connSett.hostName = $('[name="host-name"]').val();
this.connSett.dbUserName = $('[name="db-user-name"]').val();
this.connSett.dbUserPass = $('[name="db-user-password"]').val();
this.connSett.dbDriver = $('[name="db-driver"]').val();
}
InstallScript.prototype.setUserSett = function() {
this.userSett.name = $('[name="user-name"]').val();
this.userSett.pass = $('[name="user-pass"]').val();
this.userSett.confPass = $('[name="user-confirm-pass"]').val();
}
InstallScript.prototype.setSystemSett = function() {
this.systemSettings.dateFormat = $('[name="dateFormat"]').val();
this.systemSettings.timeFormat = $('[name="timeFormat"]').val();
this.systemSettings.timeZone = $('[name="timeZone"]').val();
this.systemSettings.weekStart = $('[name="weekStart"]').val();
this.systemSettings.defaultCurrency = $('[name="defaultCurrency"]').val();
this.systemSettings.thousandSeparator = $('[name="thousandSeparator"]').val();
this.systemSettings.decimalMark = $('[name="decimalMark"]').val();
this.systemSettings.language = $('[name="language"]').val();
}
InstallScript.prototype.setEmailSett = function() {
this.emailSettings.smtpServer = $('[name="smtpServer"]').val();
this.emailSettings.smtpPort = $('[name="smtpPort"]').val();
this.emailSettings.smtpAuth = $('[name="smtpAuth"]').is(':checked');
this.emailSettings.smtpSecurity = $('[name="smtpSecurity"]').val();
this.emailSettings.smtpUsername = $('[name="smtpUsername"]').val();
this.emailSettings.smtpPassword = $('[name="smtpPassword"]').val();
this.emailSettings.outboundEmailFromName = $('[name="outboundEmailFromName"]').val();
this.emailSettings.outboundEmailFromAddress = $('[name="outboundEmailFromAddress"]').val();
this.emailSettings.outboundEmailIsShared = $('[name="outboundEmailIsShared"]').is(':checked');
}
InstallScript.prototype.checkSett = function(opt) {
var self = this;
this.hideMsg();
var data = this.connSett;
data.action = 'settingsTest';
$.ajax({
url: "index.php",
data: data,
type: "POST",
dataType: 'json',
})
.done(function(data){
if (typeof(data.success) !== 'undefined' && data.success) {
data.success = true;
}
else {
var msg = '';
var rowDelim = '
';
if (typeof(data.errors)) {
var errors = data.errors;
if (typeof(errors.phpVersion) !== 'undefined') {
msg += self.getLang('phpVersion', 'messages').replace('{minVersion}', errors.phpVersion) + rowDelim;
}
if (typeof(errors.exts) !== 'undefined') {
var exts = errors.exts;
var len = exts.length;
for (var index = 0; index < len; index++) {
var temp = self.getLang('The {extName} PHP extension was not found...', 'messages');
temp = temp.replace('{extName}', exts[index]);
msg += temp+rowDelim;
}
}
if (typeof(errors.modRewrite) !== 'undefined') {
msg += errors.modRewrite+rowDelim;
}
if (typeof(errors.dbConnect) !== 'undefined') {
if (typeof(errors.dbConnect.errorCode) !== 'undefined') {
var temp = self.getLang(errors.dbConnect.errorCode, 'messages');
if (temp == errors.dbConnect.errorCode && typeof(errors.dbConnect.errorMsg) !== 'undefined') temp = errors.dbConnect.errorMsg;
}
else if (typeof(errors.dbConnect.errorMsg) !== 'undefined') {
temp = errors.dbConnect.errorMsg;
}
msg += temp+rowDelim;
}
}
if (msg == '') {
msg = self.getLang('Some errors occurred!', 'messages');
}
self.showMsg({msg: msg, error: true});
}
opt.success(data);
})
.fail(function(){
msg = self.getLang('Ajax failed', 'messages');
self.showMsg({msg: msg, error: true});
opt.error();
})
}
InstallScript.prototype.validate = function() {
this.hideMsg();
var valid = true;
var elem = null;
var fieldRequired = [];
switch (this.action) {
case 'step2':
fieldRequired = ['db-name', 'host-name', 'db-user-name', 'db-driver'];
break;
case 'step3':
fieldRequired = ['user-name', 'user-pass', 'user-confirm-pass'];
break;
case 'step4':
fieldRequired = ['decimalMark'];
break;
break;
case 'step5':
fieldRequired = ['smtpUsername'];
break;
}
var len = fieldRequired.length;
for (var index = 0; index < len; index++) {
elem = $('[name="'+fieldRequired[index]+'"]').filter(':visible');
if (elem.length > 0) {
if (elem.val() == '') {
elem.parent().parent().addClass('has-error');
valid = false;
}
else {
elem.parent().parent().removeClass('has-error');
}
}
}
// decimal and group sep
$('[name="thousandSeparator"]').parent().parent().removeClass('has-error');
if (typeof(this.systemSettings.thousandSeparator) !== 'undefined'
&& typeof(this.systemSettings.decimalMark) !== 'undefined'
&& this.systemSettings.thousandSeparator == this.systemSettings.decimalMark
&& valid) {
$('[name="thousandSeparator"]').parent().parent().addClass('has-error');
$('[name="decimalMark"]').parent().parent().addClass('has-error');
msg = this.getLang('Thousand Separator and Decimal Mark equal', 'messages');
this.showMsg({msg: msg, error: true});
valid = false;
}
return valid;
}
InstallScript.prototype.setForm = function(opt) {
var formId = opt.formId || 'nav';
var action = opt.action || 'main';
var desc = opt.desc || '';
var actionField = $('', {'name': 'action', 'value': action, 'type': 'hidden'});
$('#'+formId).append(actionField);
var descField = $('