2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
|
|
|
* Copyright (C) DevCode s.n.c.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function startHooks() {
|
|
|
|
$.ajax({
|
|
|
|
url: globals.rootdir + "/ajax.php",
|
|
|
|
type: "get",
|
|
|
|
data: {
|
|
|
|
op: "hooks",
|
|
|
|
},
|
2019-08-27 15:42:13 +02:00
|
|
|
success: function (data) {
|
2019-08-26 18:02:05 +02:00
|
|
|
hooks = JSON.parse(data);
|
2019-07-26 17:40:52 +02:00
|
|
|
|
2019-08-26 18:02:05 +02:00
|
|
|
$("#hooks-header").text(globals.translations.hooksExecuting);
|
|
|
|
$("#hooks-number").text(hooks.length);
|
|
|
|
|
|
|
|
if (hooks.length == 0) {
|
|
|
|
$("#hooks-loading").hide();
|
|
|
|
$("#hooks-number").text(0);
|
|
|
|
$("#hooks-header").text(globals.translations.hookNone);
|
|
|
|
}
|
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
hooks.forEach(function (item, index) {
|
|
|
|
renderHook(item, {
|
|
|
|
show: true,
|
|
|
|
message: globals.translations.hookExecuting.replace('_NAME_', item.name)
|
|
|
|
});
|
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
startHook(item, true);
|
2019-08-26 18:02:05 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-29 11:25:13 +02:00
|
|
|
* Richiama l'hook per l'esecuzione.
|
2019-08-26 18:02:05 +02:00
|
|
|
*
|
|
|
|
* @param hook
|
|
|
|
*/
|
2019-08-29 11:25:13 +02:00
|
|
|
function startHook(hook, init) {
|
2019-07-26 17:40:52 +02:00
|
|
|
$.ajax({
|
|
|
|
url: globals.rootdir + "/ajax.php",
|
|
|
|
type: "get",
|
|
|
|
data: {
|
2019-08-29 11:25:13 +02:00
|
|
|
op: "hook-lock",
|
2019-07-26 17:40:52 +02:00
|
|
|
id: hook.id,
|
|
|
|
},
|
2019-08-27 15:42:13 +02:00
|
|
|
success: function (data) {
|
2019-08-29 11:25:13 +02:00
|
|
|
var token = JSON.parse(data);
|
2019-07-26 17:40:52 +02:00
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
if (init) {
|
|
|
|
hookCount("#hooks-counter");
|
|
|
|
|
|
|
|
updateHook(hook);
|
|
|
|
}
|
|
|
|
|
2019-09-16 10:01:01 +02:00
|
|
|
if (token) {
|
2019-08-29 11:25:13 +02:00
|
|
|
executeHook(hook, token);
|
|
|
|
} else {
|
|
|
|
var timeout = 10;
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
startHook(hook);
|
|
|
|
}, timeout * 1000);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Richiama l'hook per l'esecuzione.
|
|
|
|
*
|
|
|
|
* @param hook
|
|
|
|
* @param token
|
|
|
|
*/
|
|
|
|
function executeHook(hook, token) {
|
|
|
|
$.ajax({
|
|
|
|
url: globals.rootdir + "/ajax.php",
|
|
|
|
type: "get",
|
|
|
|
data: {
|
|
|
|
op: "hook-execute",
|
|
|
|
id: hook.id,
|
|
|
|
token: token,
|
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
var result = JSON.parse(data);
|
|
|
|
updateHook(hook);
|
2019-07-26 17:40:52 +02:00
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
var timeout;
|
|
|
|
if (result.execute) {
|
2019-08-29 14:48:14 +02:00
|
|
|
startHook(hook);
|
2019-07-26 17:40:52 +02:00
|
|
|
} else {
|
2019-08-27 15:42:13 +02:00
|
|
|
timeout = 30;
|
2019-07-26 17:40:52 +02:00
|
|
|
|
2019-08-29 14:48:14 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
startHook(hook);
|
|
|
|
}, timeout * 1000);
|
|
|
|
}
|
2019-08-29 11:25:13 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2019-08-26 18:02:05 +02:00
|
|
|
|
2019-08-29 11:25:13 +02:00
|
|
|
/**
|
|
|
|
* Aggiorna le informazioni dell'hook.
|
|
|
|
*
|
|
|
|
* @param hook
|
|
|
|
* @param init
|
|
|
|
*/
|
|
|
|
function updateHook(hook) {
|
|
|
|
$.ajax({
|
|
|
|
url: globals.rootdir + "/ajax.php",
|
|
|
|
type: "get",
|
|
|
|
data: {
|
|
|
|
op: "hook-response",
|
|
|
|
id: hook.id,
|
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
var result = JSON.parse(data);
|
|
|
|
renderHook(hook, result);
|
2019-07-26 17:40:52 +02:00
|
|
|
|
|
|
|
// Rimozione eventuale della rotella di caricamento
|
2019-08-26 18:02:05 +02:00
|
|
|
var counter = $("#hooks-counter").text();
|
2019-08-27 15:42:13 +02:00
|
|
|
var number = $("#hooks > li").length;
|
2020-02-01 11:02:23 +01:00
|
|
|
|
|
|
|
if (number == 0) {
|
|
|
|
$("#hooks-notified").html('<i class="fa fa-check" aria-hidden="true"></i>');
|
2020-02-05 10:28:16 +01:00
|
|
|
$("#hooks-label").removeClass();
|
|
|
|
$("#hooks-label").addClass('label').addClass('label-success');
|
2020-02-01 11:02:23 +01:00
|
|
|
}else{
|
|
|
|
$("#hooks-notified").text(number);
|
2020-02-05 10:28:16 +01:00
|
|
|
$("#hooks-label").removeClass();
|
|
|
|
$("#hooks-label").addClass('label').addClass('label-danger');
|
2020-02-01 11:02:23 +01:00
|
|
|
}
|
2019-08-27 15:42:13 +02:00
|
|
|
|
|
|
|
if (counter == $("#hooks-number").text()) {
|
2019-07-26 17:40:52 +02:00
|
|
|
$("#hooks-loading").hide();
|
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
var hookMessage;
|
|
|
|
if (number > 1) {
|
2019-07-26 17:40:52 +02:00
|
|
|
hookMessage = globals.translations.hookMultiple.replace('_NUM_', number);
|
2019-08-27 15:42:13 +02:00
|
|
|
} else if (number == 1) {
|
2019-07-26 17:40:52 +02:00
|
|
|
hookMessage = globals.translations.hookSingle;
|
2019-08-27 15:42:13 +02:00
|
|
|
} else {
|
2019-07-26 17:40:52 +02:00
|
|
|
hookMessage = globals.translations.hookNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#hooks-header").text(hookMessage);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Aggiunta dell'hook al numero totale.
|
|
|
|
*/
|
2019-08-27 15:42:13 +02:00
|
|
|
function hookCount(id, value) {
|
|
|
|
value = value ? value : 1;
|
|
|
|
|
|
|
|
var element = $(id);
|
|
|
|
var number = parseInt(element.text());
|
2019-08-26 18:02:05 +02:00
|
|
|
number = isNaN(number) ? 0 : number;
|
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
number += value;
|
|
|
|
element.text(number);
|
2019-08-26 18:02:05 +02:00
|
|
|
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-27 15:42:13 +02:00
|
|
|
* Genera l'HTML per la visualizzazione degli hook.
|
2019-08-26 18:02:05 +02:00
|
|
|
*
|
|
|
|
* @param element_id
|
|
|
|
* @param result
|
|
|
|
*/
|
2019-08-27 15:42:13 +02:00
|
|
|
function renderHook(hook, result) {
|
|
|
|
if (result.length == 0) return;
|
|
|
|
|
|
|
|
var element_id = "hook-" + hook.id;
|
|
|
|
|
|
|
|
// Inizializzazione
|
|
|
|
var element = $("#" + element_id);
|
|
|
|
if (element.length == 0) {
|
|
|
|
$("#hooks").append('<li class="hook-element" id="' + element_id + '"></li>');
|
|
|
|
|
|
|
|
element = $("#" + element_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rimozione
|
|
|
|
if (!result.show) {
|
|
|
|
element.remove();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contenuto
|
|
|
|
var content = '<a href="' + (result.link ? result.link : "#") + '"><i class="' + result.icon + '"></i><span class="small"> ' + result.message + '</span>';
|
|
|
|
|
|
|
|
if (result.progress) {
|
|
|
|
var current = result.progress.current;
|
|
|
|
var total = result.progress.total;
|
2019-08-28 11:52:04 +02:00
|
|
|
var percentage = current / total * 100;
|
|
|
|
percentage = isNaN(percentage) ? 100 : percentage;
|
2019-08-27 15:42:13 +02:00
|
|
|
|
2019-08-27 17:25:52 +02:00
|
|
|
percentage = Math.round(percentage * 100) / 100;
|
|
|
|
|
2019-08-27 15:42:13 +02:00
|
|
|
content += '<div class="progress" style="margin-bottom: 0px;"><div class="progress-bar" role="progressbar" aria-valuenow="' + percentage + '" aria-valuemin="0" aria-valuemax="100" style="width:' + percentage + '%">' + percentage + '% (' + current + '/' + total + ')</div></div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
content += '</a>';
|
|
|
|
|
|
|
|
element.html(content);
|
2019-08-26 18:02:05 +02:00
|
|
|
}
|