openstamanager/modules/stato_servizi/actions.php

419 lines
13 KiB
PHP
Raw Normal View History

2019-05-04 02:03:42 +02:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* 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-05-04 02:03:42 +02:00
include_once __DIR__.'/../../core.php';
use API\Services;
2023-08-04 14:54:28 +02:00
use Carbon\Carbon;
use Models\Cache;
use Models\Module;
use Models\Plugin;
2019-05-04 02:03:42 +02:00
use Util\FileSystem;
$id = post('id');
switch (filter('op')) {
2023-03-09 18:12:38 +01:00
case 'cambia-dimensione':
$result = $dbo->update('zz_widgets', [
2023-08-04 14:54:28 +02:00
'class' => post('valore'),
2023-03-09 18:12:38 +01:00
], [
2023-08-04 14:54:28 +02:00
'id' => post('id'),
2023-03-09 18:12:38 +01:00
]);
2023-08-04 14:54:28 +02:00
2023-03-09 18:12:38 +01:00
echo json_encode([
'result' => $result,
]);
if ($result) {
flash()->info('Impostazione modificata con successo!');
} else {
flash()->error('Errore durante il salvataggio!');
}
break;
case 'rimuovi-modulo':
$id = filter('id');
$is_plugin = filter('tipo') == 'plugin';
if (empty($id)) {
echo json_encode([]);
}
2019-05-04 02:03:42 +02:00
// Ricerca del modulo/plugin tra quelli disponibili
if (!$is_plugin) {
$struttura = Module::where('default', '=', 0)->find($id);
} else {
$struttura = Plugin::where('default', '=', 0)->find($id);
}
2019-05-04 02:03:42 +02:00
// Modulo/plugin non trovato
if (empty($struttura)) {
echo json_encode([]);
}
2019-05-04 02:03:42 +02:00
// Eliminazione del modulo/plugin dal sistema di navigazione
$struttura->delete();
2019-05-04 02:03:42 +02:00
// Esecuzione dello script di disinstallazione (se presente)
$uninstall_script = $struttura->path.'/update/uninstall.php';
if (file_exists($uninstall_script)) {
include_once $uninstall_script;
2019-05-04 02:03:42 +02:00
}
// Eliminazione dei file del modulo/plugin
delete($struttura->path);
// Messaggio informativo
if (!$is_plugin) {
flash()->info(tr('Modulo "_NAME_" disinstallato!', [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura->getTranslation('title'),
]));
} else {
flash()->info(tr('Plugin "_NAME_" disinstallato!', [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura->getTranslation('title'),
]));
}
2019-05-04 02:03:42 +02:00
echo json_encode([]);
2019-05-04 02:03:42 +02:00
break;
case 'disabilita-modulo':
$id = filter('id');
2024-05-29 11:04:10 +02:00
$is_plugin = (filter('tipo') == 'plugin');
// Disabilitazione del modulo indicato
$database->table($is_plugin ? 'zz_plugins' : 'zz_modules')
->where('id', '=', $id)
->update(['enabled' => 0]);
// Cascata in tutti i sotto-moduli
if (!$is_plugin) {
$moduli_interessati = collect([$id]);
while (!$moduli_interessati->isEmpty()) {
$id_modulo = $moduli_interessati->pop();
// Disabilitazione dei sotto-moduli
$database->table('zz_modules')
->where('parent', '=', $id_modulo)
->update(['enabled' => 0]);
// Ricerca sotto-moduli
$sotto_moduli = $database->table('zz_modules')
->where('parent', '=', $id_modulo)
->select('id')
->get()->pluck('id');
$moduli_interessati->concat($sotto_moduli);
}
}
2019-05-04 02:03:42 +02:00
// Disabilitazione modulo/plugin indicato
2024-06-11 16:58:27 +02:00
$moduli_sempre_attivi = [Module::where('name', 'Utenti e permessi')->first()->id, Module::where('name', 'Stato dei servizi')->first()->id];
$database->table('zz_modules')
2024-03-05 16:01:45 +01:00
->whereIn('id', $moduli_sempre_attivi)
->update(['enabled' => 1]);
2024-05-29 11:04:10 +02:00
// Messaggio informativo'
if (!$is_plugin) {
2024-05-29 11:04:10 +02:00
flash()->info(tr('Modulo disabilitato!'));
} else {
2024-05-29 11:04:10 +02:00
flash()->info(tr('Plugin disabilitato!'));
}
echo json_encode([]);
2019-05-04 02:03:42 +02:00
break;
case 'abilita-sotto-modulo':
$id = filter('id');
// Cascata in tutti i sotto-moduli
$moduli_interessati = collect([$id]);
while (!$moduli_interessati->isEmpty()) {
$id_modulo = $moduli_interessati->pop();
// Disabilitazione dei sotto-moduli
$database->table('zz_modules')
->where('parent', '=', $id_modulo)
->update(['enabled' => 1]);
// Ricerca sotto-moduli
$sotto_moduli = $database->table('zz_modules')
->where('parent', '=', $id_modulo)
->select('id')
->get()->pluck('id');
$moduli_interessati->concat($sotto_moduli);
}
2019-05-04 02:03:42 +02:00
// no break
case 'abilita-modulo':
$id = filter('id');
$is_plugin = filter('tipo') == 'plugin';
// Abilitazione del modulo/plugin indicato
$database->table($is_plugin ? 'zz_plugins' : 'zz_modules')
->where('id', '=', $id)
->update(['enabled' => 1]);
// Messaggio informativo
$struttura = $is_plugin ? Plugin::find($id) : Module::find($id);
if (!isset($moduli_interessati)) {
if (!$is_plugin) {
flash()->info(tr('Modulo "_NAME_" abilitato!', [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura->getTranslation('title'),
]));
} else {
flash()->info(tr('Plugin "_NAME_" abilitato!', [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura->getTranslation('title'),
]));
}
} else {
2024-03-05 16:01:45 +01:00
$modulo = Module::find($id);
2024-05-29 11:20:46 +02:00
flash()->info(tr('Sottomoduli di "_NAME_" abilitati!', [
2024-03-20 11:13:28 +01:00
'_NAME_' => $struttura->getTranslation('title'),
]));
}
2019-05-04 02:03:42 +02:00
echo json_encode([]);
2019-05-04 02:03:42 +02:00
break;
case 'disabilita-widget':
$id = filter('id');
2019-05-04 02:03:42 +02:00
2024-08-26 09:13:55 +02:00
$widget = $database->table('zz_widgets_lang')
->where('id', '=', $id)
->first();
// Abilitazione del widget indicato
$database->table('zz_widgets')
2024-08-26 09:13:55 +02:00
->where('id', '=', $widget->id_record)
->update(['enabled' => 0]);
2019-05-04 02:03:42 +02:00
// Messaggio informativo
flash()->info(tr('Widget "_NAME_" disabilitato!', [
2024-08-26 09:13:55 +02:00
'_NAME_' => $widget->title,
2019-05-04 02:03:42 +02:00
]));
echo json_encode([]);
2019-05-04 02:03:42 +02:00
break;
case 'abilita-widget':
$id = filter('id');
2019-05-04 02:03:42 +02:00
// Abilitazione del widget indicato
$database->table('zz_widgets')
->where('id', '=', $id)
->update(['enabled' => 1]);
2019-05-04 02:03:42 +02:00
// Messaggio informativo
2024-08-26 09:13:55 +02:00
$widget = $database->table('zz_widgets_lang')
->where('id_record', '=', $id)
->where('id_lang', '=', Models\Locale::getDefault()->id)
->first();
flash()->info(tr('Widget "_NAME_" abilitato!', [
2024-08-26 09:13:55 +02:00
'_NAME_' => $widget->title,
2019-05-04 02:03:42 +02:00
]));
echo json_encode([]);
2019-05-04 02:03:42 +02:00
break;
case 'sposta-widget':
$id = filter('id');
// Individuazione widget
$widget = $database->table('zz_widgets')
->where('id', '=', $id)
->first();
if (empty($widget)) {
echo json_encode([]);
}
2019-05-04 02:03:42 +02:00
// Individuazione dello spostamento da effettuare
$pieces = explode('_', $widget->location);
$location = $pieces[0].'_'.($pieces[1] == 'right' ? 'top' : 'right');
2019-05-04 02:03:42 +02:00
// Abilitazione del widget indicato
$database->table('zz_widgets')
->where('id', '=', $id)
->update(['location' => $location]);
// Messaggio informativo
flash()->info(tr('Posizione del widget "_NAME_" aggiornata!', [
2024-04-18 17:44:05 +02:00
'_NAME_' => $widget->getTranslation('title'),
2019-05-04 02:03:42 +02:00
]));
echo json_encode([]);
2019-05-04 02:03:42 +02:00
break;
2024-01-15 15:30:45 +01:00
// Ordinamento moduli di primo livello
2019-07-26 17:40:52 +02:00
case 'sort_modules':
$order = explode(',', post('order', true));
2019-05-04 02:03:42 +02:00
foreach ($order as $i => $id) {
2024-03-05 16:01:45 +01:00
$dbo->query('UPDATE `zz_modules` SET `order`='.prepare($i).' WHERE `id`='.prepare($id));
2019-05-04 02:03:42 +02:00
}
break;
2019-07-26 17:40:52 +02:00
case 'sort_widgets':
$order = explode(',', post('order', true));
2019-05-04 02:03:42 +02:00
foreach ($order as $i => $id) {
$dbo->query('UPDATE zz_widgets SET `order`='.prepare($i).' WHERE id='.prepare($id));
2019-05-04 02:03:42 +02:00
}
2019-05-04 02:03:42 +02:00
break;
case 'svuota-cache-hooks':
// Svuota cache hooks
$database->table('zz_cache')
->update(['expire_at' => Carbon::now()->subMinutes(1)]);
// Messaggio informativo
flash()->info(tr('Cache hooks svuotata!', []));
echo json_encode([]);
break;
2023-08-04 14:54:28 +02:00
case 'disabilita-hook':
$id = filter('id');
// Abilitazione del widget indicato
$database->table('zz_hooks')
->where('id', '=', $id)
->update(['enabled' => 0]);
// Messaggio informativo
$hook = $database->table('zz_hooks')
->where('id', '=', $id)
->first();
flash()->info(tr('Hook "_NAME_" disabilitato!', [
2024-04-18 17:44:05 +02:00
'_NAME_' => $hook->getTranslation('title'),
]));
echo json_encode([]);
break;
2023-08-04 14:54:28 +02:00
case 'abilita-hook':
$id = filter('id');
// Abilitazione del widget indicato
$database->table('zz_hooks')
->where('id', '=', $id)
->update(['enabled' => 1]);
// Messaggio informativo
$hook = $database->table('zz_hooks')
->where('id', '=', $id)
->first();
flash()->info(tr('Hook "_NAME_" abilitato!', [
2024-04-18 17:44:05 +02:00
'_NAME_' => $hook->getTranslation('title'),
]));
echo json_encode([]);
break;
2019-05-04 02:03:42 +02:00
case 'sizes':
$results = [];
$backup_dir = App::getConfig()['backup_dir'];
$dirs = [
$backup_dir => tr('Backup'),
base_dir().'/files' => tr('Allegati'),
base_dir().'/logs' => tr('Logs'),
2019-05-04 02:03:42 +02:00
];
foreach ($dirs as $dir => $description) {
2023-08-04 14:54:28 +02:00
$excluded_extensions = ['htaccess', 'gitkeep'];
2024-01-15 15:30:45 +01:00
// Tutte le cartelle che non prevedono log in zz_files
$excluded_dir = [DOCROOT.'\files\impianti', DOCROOT.'\files\importFE', DOCROOT.'\files\exportFE', DOCROOT.'\files\receiptFE', DOCROOT.'\files\temp'];
2023-08-04 14:54:28 +02:00
$size = FileSystem::folderSize($dir, array_merge($excluded_extensions, $excluded_dir));
2019-07-19 15:23:00 +02:00
2019-05-04 02:03:42 +02:00
$results[] = [
'description' => $description,
'size' => $size,
'formattedSize' => FileSystem::formatBytes($size),
2023-08-04 14:54:28 +02:00
'count' => FileSystem::fileCount($dir, array_merge($excluded_extensions, $excluded_dir)) ?: 0,
2022-10-06 13:30:07 +02:00
'dbSize' => ($description == 'Allegati') ? $dbo->fetchOne('SELECT SUM(`size`) AS dbsize FROM zz_files')['dbsize'] : 0,
'dbCount' => ($description == 'Allegati') ? $dbo->fetchOne('SELECT COUNT(`id`) AS dbcount FROM zz_files')['dbcount'] : 0,
'dbExtensions' => ($description == 'Allegati') ? $dbo->fetchArray("SELECT SUBSTRING_INDEX(filename, '.', -1) AS extension, COUNT(*) AS num FROM zz_files GROUP BY extension ORDER BY num DESC LIMIT 10") : 0,
2019-05-04 02:03:42 +02:00
];
}
echo json_encode($results);
break;
case 'informazioni-fe':
2024-06-25 09:40:41 +02:00
$info = Cache::where('name', 'Informazioni su spazio FE')->first();
2024-06-11 16:58:27 +02:00
if (!$info->isValid()) {
$response = Services::request('POST', 'informazioni_fe');
$response = Services::responseBody($response);
$info->set($response['result']);
}
$informazioni = $info->content;
2021-09-17 17:18:17 +02:00
$spazio_totale = floatval($informazioni['maxSize']) * (1024 ** 2);
$avviso_spazio = !empty($spazio_totale) && floatval($informazioni['size']) > 0.9 * $spazio_totale;
$history = (array) $informazioni['history'];
// Restrizione storico agli ultimi 3 anni
//$history = array_slice($history, 0, 3);
2021-10-11 19:16:56 +02:00
$max_number = $informazioni['maxNumber'];
$avviso_numero = !empty($max_number) && floatval($history[0]['number']) > 0.9 * $max_number;
// Formattazione dei contenuti dello storico
foreach ($history as $key => $value) {
2024-01-31 14:23:46 +01:00
$history[$key]['size'] = (($history[$key]['size']) ? FileSystem::formatBytes($value['size']) : '-');
2024-01-15 15:30:45 +01:00
// $history[$key]['invoices_size'] = Filesystem::formatBytes($value['invoices_size']);
// $history[$key]['notifies_size'] = Filesystem::formatBytes($value['notifies_size']);
}
// Formattazione dei contenuti generici
echo json_encode([
2024-01-15 15:30:45 +01:00
// 'invoices_size' => Filesystem::formatBytes($informazioni['invoices_size']),
// 'notifies_size' => Filesystem::formatBytes($informazioni['notifies_size']),
2021-09-17 17:18:17 +02:00
2021-10-11 19:16:56 +02:00
'invoice_number' => $informazioni['invoice_number'],
'maxNumber' => $max_number,
'avviso_numero' => $avviso_numero,
2021-09-17 17:18:17 +02:00
'avviso_spazio' => $avviso_spazio,
2024-01-31 14:23:46 +01:00
'spazio_totale' => FileSystem::formatBytes($spazio_totale),
'spazio_occupato' => FileSystem::formatBytes($informazioni['size']),
2021-09-17 17:18:17 +02:00
2021-03-01 16:47:01 +01:00
'history' => $history,
]);
break;
2019-05-04 02:03:42 +02:00
}