diff --git a/modules/qlogooglehotelconnector/classes/QghcApiLog.php b/modules/qlogooglehotelconnector/classes/QghcApiLog.php index 064dd26a5..a4641420d 100644 --- a/modules/qlogooglehotelconnector/classes/QghcApiLog.php +++ b/modules/qlogooglehotelconnector/classes/QghcApiLog.php @@ -95,4 +95,24 @@ class QghcApiLog extends ObjectModel return false; } } + public static function cleanupApiLogs() + { + $cutoff = date('Y-m-d H:i:s', strtotime('-2 months')); + if (!Db::getInstance()->delete('qghc_api_log', '`date_add` < \'' . pSQL($cutoff) . '\'')) { + return false; + } + + // Daily file logs follow the same retention window as the DB rows. + $logDir = _PS_MODULE_DIR_ . 'qlogooglehotelconnector/logs/'; + if (is_dir($logDir)) { + foreach (glob($logDir . '*.log') as $file) { + $fileDate = basename($file, '.log'); + if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $fileDate) && strtotime($fileDate) < strtotime($cutoff)) { + @unlink($file); + } + } + } + + return true; + } } diff --git a/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php b/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php index 8cc052e85..eeed2d074 100644 --- a/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php +++ b/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php @@ -35,6 +35,7 @@ class AdminGoogleHotelConfigController extends ModuleAdminController const CONF_DISCONNECTED = 102; /** _conf[] code: webservice key regenerated successfully. */ const CONF_KEY_REGENERATED = 103; + const CONF_CRON_UPDATED = 104; public function __construct() { @@ -50,6 +51,7 @@ class AdminGoogleHotelConfigController extends ModuleAdminController $this->_conf[self::CONF_CONNECTED] = $this->l('Connection created successfully.'); $this->_conf[self::CONF_DISCONNECTED] = $this->l('Connection disconnected successfully.'); $this->_conf[self::CONF_KEY_REGENERATED] = $this->l('Webservice key regenerated successfully.'); + $this->_conf[self::CONF_CRON_UPDATED] = $this->l('Cron job set up successfully.'); } /** @@ -72,6 +74,27 @@ class AdminGoogleHotelConfigController extends ModuleAdminController $this->processRegenerateKey(); return; } + + if (Tools::isSubmit('submitGhcCron')) { + $this->processSaveCron(); + return; + } + } + + public function processSaveCron() + { + if (!$this->tabAccess['edit']) { + $this->errors[] = Tools::displayError('You do not have permission to edit this.'); + return; + } + + $result = $this->module->setupCronTask(); + if ($result !== true) { + $this->errors[] = $result; + return; + } + + Tools::redirectAdmin($this->context->link->getAdminLink('AdminGoogleHotelConfig') . '&conf=' . self::CONF_CRON_UPDATED); } /** True when the current admin request came over HTTPS (directly or via a TLS-terminating proxy). */ @@ -122,7 +145,7 @@ class AdminGoogleHotelConfigController extends ModuleAdminController $rows = QghcProperty::getEnrolledProperties($idLang); $configs = array(); $hotelEditBase = $this->context->link->getAdminLink('AdminAddHotel'); - + if ($rows) { foreach ($rows as $row) { $badge = isset($statusBadges[(int)$row['google_status']]) ? $statusBadges[(int)$row['google_status']] : $pendingBadge; @@ -138,7 +161,6 @@ class AdminGoogleHotelConfigController extends ModuleAdminController } } - // Token must match the check in ghclogcron.php $cronUrl = Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . 'modules/qlogooglehotelconnector/ghclogcron.php?token=' . Tools::encrypt('qlogooglehotelconnector'); $this->context->smarty->assign(array( @@ -149,6 +171,8 @@ class AdminGoogleHotelConfigController extends ModuleAdminController 'ssl_settings_url' => $this->context->link->getAdminLink('AdminPreferences'), 'action_url' => $this->context->link->getAdminLink('AdminGoogleHotelConfig'), 'cron_url' => $cronUrl, + 'cron_task' => $this->module->getCronTaskSchedule(), + 'cron_manager_enabled' => Module::isEnabled('qlocrontaskmanager'), 'hotel_list_url' => $hotelEditBase, )); } diff --git a/modules/qlogooglehotelconnector/ghclogcron.php b/modules/qlogooglehotelconnector/ghclogcron.php index 9fa7af927..a00912974 100644 --- a/modules/qlogooglehotelconnector/ghclogcron.php +++ b/modules/qlogooglehotelconnector/ghclogcron.php @@ -36,28 +36,11 @@ if ($token !== Tools::encrypt('qlogooglehotelconnector')) { die('Invalid token.'); } -// Delete qghc_api_log rows older than 2 months -$cutoff = date('Y-m-d H:i:s', strtotime('-2 months')); -$deleted = Db::getInstance()->execute( - 'DELETE FROM `' . _DB_PREFIX_ . 'qghc_api_log` - WHERE `date_add` < \'' . pSQL($cutoff) . '\'' -); - -if ($deleted) { - $count = Db::getInstance()->Affected_Rows(); - echo 'OK — ' . (int)$count . ' log(s) deleted (older than ' . $cutoff . ').'; +// Delete qghc_api_log rows and daily file logs older than 2 months +Module::getInstanceByName('qlogooglehotelconnector'); // loads the module classes +if (QghcApiLog::cleanupApiLogs()) { + echo 'OK — logs older than 2 months deleted.'; } else { header('HTTP/1.1 500 Internal Server Error'); echo 'ERROR — failed to delete old logs.'; } - -// Delete daily file logs older than the same retention window used for the DB rows above. -$logDir = _PS_MODULE_DIR_ . 'qlogooglehotelconnector/logs/'; -if (is_dir($logDir)) { - foreach (glob($logDir . '*.log') as $file) { - $fileDate = basename($file, '.log'); - if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $fileDate) && strtotime($fileDate) < strtotime($cutoff)) { - @unlink($file); - } - } -} diff --git a/modules/qlogooglehotelconnector/logs/index.php b/modules/qlogooglehotelconnector/logs/index.php new file mode 100644 index 000000000..ecafa50cf --- /dev/null +++ b/modules/qlogooglehotelconnector/logs/index.php @@ -0,0 +1,32 @@ + self::CRON_TASK_CLEANUP_LOGS, + 'description' => $this->l('Delete Google Hotel API logs older than 2 months'), + 'cron' => $this->cronExpression, + 'callback' => 'cronCleanupApiLogs', + ), + ); + } + + /** Cron Task Manager callback for the API log cleanup. */ + public function cronCleanupApiLogs() + { + if (QghcApiLog::cleanupApiLogs() === false) { + throw new Exception('Unable to delete the old API logs.'); + } + } + + /** + * Creates the API log cleanup task in the Cron Task Manager when it does not exist. + * + * @return string|bool Error message, true on success. + */ + public function setupCronTask() + { + if (!Module::isEnabled('qlocrontaskmanager')) { + return $this->l('The Cron Task Manager module is not enabled.'); + } + + if ($this->getCronTask()) { + return true; + } + + (new QctmCronTask())->registerTasksForModule((int) $this->id, $this->hookRegisterCronTasks()); + if (!$this->getCronTask()) { + return $this->l('Unable to create the cleanup cron task in the Cron Task Manager.'); + } + + return true; + } + + /** Returns the log cleanup task of the Cron Task Manager, false when it does not exist. */ + protected function getCronTask() + { + if (!Module::isEnabled('qlocrontaskmanager')) { + return false; + } + + require_once _PS_MODULE_DIR_ . 'qlocrontaskmanager/classes/QctmRequiredClasses.php'; + + $idTask = QctmCronTask::getByModuleAndName((int) $this->id, self::CRON_TASK_CLEANUP_LOGS); + if (!$idTask) { + return false; + } + + $cronTask = new QctmCronTask((int) $idTask); + + return Validate::isLoadedObject($cronTask) ? $cronTask : false; + } + + /** Returns the cleanup task state for display, false when the Cron Task Manager is not enabled. */ + public function getCronTaskSchedule() + { + if (!Module::isEnabled('qlocrontaskmanager')) { + return false; + } + + $cronTask = $this->getCronTask(); + if (!$cronTask) { + return array( + 'exists' => false, + 'active' => false, + 'readable' => '', + ); + } + + $readable = $cronTask->cron_expression; + if (\Cron\CronExpression::isValidExpression($cronTask->cron_expression)) { + $readable = (new QctmCronExpressionTranslator())->getCronExpressionTranslation($cronTask->cron_expression); + } + + return array( + 'exists' => true, + 'active' => (bool) $cronTask->active, + 'readable' => $readable, + ); + } + /** Adds the Google Hotel Connector menu group with Configuration and API Logs sub-tabs to the admin sidebar. */ public function callInstallTab() { diff --git a/modules/qlogooglehotelconnector/views/css/admin/qlo_ghc_admin.css b/modules/qlogooglehotelconnector/views/css/admin/qlo_ghc_admin.css index 6f7763d8c..259f2cee3 100644 --- a/modules/qlogooglehotelconnector/views/css/admin/qlo_ghc_admin.css +++ b/modules/qlogooglehotelconnector/views/css/admin/qlo_ghc_admin.css @@ -37,6 +37,12 @@ text-align: left; } +#ghc-dashboard-notification h3 { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + /* ── Prevent scrollbar jitter on GHC admin pages ───────────────── Scoped to exactly the controllers this stylesheet is loaded on (see hookActionAdminControllerSetMedia) via QloApps admin's own diff --git a/modules/qlogooglehotelconnector/views/templates/admin/config.tpl b/modules/qlogooglehotelconnector/views/templates/admin/config.tpl index 3b52dda84..b5892be89 100644 --- a/modules/qlogooglehotelconnector/views/templates/admin/config.tpl +++ b/modules/qlogooglehotelconnector/views/templates/admin/config.tpl @@ -164,26 +164,60 @@ {/if} {* ── Cron job setting ─────────────────────────────────────────────────── *} -