|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione |
| 4 | + * Copyright (C) DevCode s.r.l. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +namespace Modules\StatoServizi; |
| 21 | + |
| 22 | +use Carbon\Carbon; |
| 23 | +use Hooks\Manager; |
| 24 | +use Models\Cache; |
| 25 | + |
| 26 | +/** |
| 27 | + * Hook specializzato per il controllo dello stato del cron. |
| 28 | + * Segnala quando il cron non è stato eseguito da più di un giorno. |
| 29 | + */ |
| 30 | +class CronStatusHook extends Manager |
| 31 | +{ |
| 32 | + public function response() |
| 33 | + { |
| 34 | + // Recupera l'ultima esecuzione del cron dalla cache |
| 35 | + $ultima_esecuzione = Cache::where('name', 'Ultima esecuzione del cron')->first(); |
| 36 | + |
| 37 | + if (!$ultima_esecuzione || !$ultima_esecuzione->content) { |
| 38 | + $message = tr('Il cron non è stato configurato correttamente'); |
| 39 | + $show = true; |
| 40 | + } else { |
| 41 | + // Converte la data dell'ultima esecuzione |
| 42 | + $data_ultima_esecuzione = Carbon::parse($ultima_esecuzione->content); |
| 43 | + $ora_attuale = Carbon::now(); |
| 44 | + |
| 45 | + // Calcola la differenza in ore |
| 46 | + $ore_trascorse = $data_ultima_esecuzione->diffInHours($ora_attuale); |
| 47 | + |
| 48 | + if ($ore_trascorse > 1) { |
| 49 | + $giorni = floor($ore_trascorse / 24); |
| 50 | + $ore_rimanenti = $ore_trascorse % 24; |
| 51 | + |
| 52 | + if ($giorni > 0) { |
| 53 | + $tempo_trascorso = $giorni . ' ' . ($giorni == 1 ? tr('giorno') : tr('giorni')); |
| 54 | + if ($ore_rimanenti > 0) { |
| 55 | + $tempo_trascorso .= ' e ' . $ore_rimanenti . ' ' . ($ore_rimanenti == 1 ? tr('ora') : tr('ore')); |
| 56 | + } |
| 57 | + } else { |
| 58 | + $tempo_trascorso = $ore_trascorse . ' ' . ($ore_trascorse == 1 ? tr('ora') : tr('ore')); |
| 59 | + } |
| 60 | + |
| 61 | + $message = tr('Attenzione: il cron non viene eseguito da _TIME_', [ |
| 62 | + '_TIME_' => $tempo_trascorso, |
| 63 | + ]); |
| 64 | + $show = true; |
| 65 | + } else { |
| 66 | + $message = tr('Il cron è stato eseguito di recente'); |
| 67 | + $show = false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return [ |
| 72 | + 'icon' => 'fa fa-clock-o text-danger', |
| 73 | + 'message' => $message, |
| 74 | + 'show' => $show, |
| 75 | + 'link' => base_path().'/controller.php?id_module=' . $this->getModuleId(), |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + public function execute() |
| 80 | + { |
| 81 | + // Non è necessaria alcuna esecuzione per questo hook |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + public function needsExecution() |
| 86 | + { |
| 87 | + // Questo hook non ha bisogno di esecuzione, serve solo per la notifica |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Ottiene l'ID del modulo "Stato servizi" |
| 93 | + */ |
| 94 | + private function getModuleId() |
| 95 | + { |
| 96 | + $module = \Models\Module::where('name', 'Stato servizi')->first(); |
| 97 | + return $module?->id; |
| 98 | + } |
| 99 | +} |
0 commit comments