From 879f72e60851bdcf288814586db0c829e29bf789 Mon Sep 17 00:00:00 2001 From: azhar bhat Date: Thu, 22 Oct 2026 12:08:05 +0530 Subject: [PATCH 1/4] Fixed: Fixed Google Hotel Connector dashboard widget heading overlapping its content in backoffice --- .../views/css/admin/qlo_ghc_admin.css | 6 ++++++ 1 file changed, 6 insertions(+) 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 From 87c002a99a276f3024c8717ac0edeb68dca42965 Mon Sep 17 00:00:00 2001 From: azhar bhat Date: Thu, 22 Oct 2026 18:51:18 +0530 Subject: [PATCH 2/4] Fixed: Manage Google Hotel Connector API log cleanup cron via Cron Task Manager --- .../classes/QghcApiLog.php | 8 + .../AdminGoogleHotelConfigController.php | 36 +++- .../qlogooglehotelconnector/ghclogcron.php | 24 +-- .../qlogooglehotelconnector.php | 167 ++++++++++++++++++ .../views/templates/admin/config.tpl | 83 +++++++-- 5 files changed, 276 insertions(+), 42 deletions(-) diff --git a/modules/qlogooglehotelconnector/classes/QghcApiLog.php b/modules/qlogooglehotelconnector/classes/QghcApiLog.php index 064dd26a5..74693b150 100644 --- a/modules/qlogooglehotelconnector/classes/QghcApiLog.php +++ b/modules/qlogooglehotelconnector/classes/QghcApiLog.php @@ -95,4 +95,12 @@ class QghcApiLog extends ObjectModel return false; } } + public static function deleteOlderThan($dateTime) + { + if (!Db::getInstance()->delete('qghc_api_log', '`date_add` < \'' . pSQL($dateTime) . '\'')) { + return false; + } + + return (int) Db::getInstance()->Affected_Rows(); + } } diff --git a/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php b/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php index 8cc052e85..800689ddf 100644 --- a/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php +++ b/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php @@ -35,6 +35,8 @@ class AdminGoogleHotelConfigController extends ModuleAdminController const CONF_DISCONNECTED = 102; /** _conf[] code: webservice key regenerated successfully. */ const CONF_KEY_REGENERATED = 103; + /** _conf[] code: log cleanup cron schedule updated successfully. */ + const CONF_CRON_UPDATED = 104; public function __construct() { @@ -50,6 +52,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 schedule updated successfully.'); } /** @@ -72,6 +75,28 @@ class AdminGoogleHotelConfigController extends ModuleAdminController $this->processRegenerateKey(); return; } + + if (Tools::isSubmit('submitGhcCron')) { + $this->processSaveCron(); + return; + } + } + + /** Saves the API log cleanup schedule in the Cron Task Manager. Redirects with a success code. */ + public function processSaveCron() + { + if (!$this->tabAccess['edit']) { + $this->errors[] = Tools::displayError('You do not have permission to edit this.'); + return; + } + + $result = $this->module->updateCronSchedule(Tools::getValue('QGHC_CRON')); + 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 +147,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,9 +163,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( 'configs' => $configs, 'ws_key' => $wsKey, @@ -148,7 +170,11 @@ class AdminGoogleHotelConfigController extends ModuleAdminController 'is_secure' => $this->isSecureRequest(), 'ssl_settings_url' => $this->context->link->getAdminLink('AdminPreferences'), 'action_url' => $this->context->link->getAdminLink('AdminGoogleHotelConfig'), - 'cron_url' => $cronUrl, + 'cron_options' => $this->module->getCronOptionLabels(), + 'cron_task' => $this->module->getCronTaskSchedule(), + 'cron_manager_enabled' => Module::isEnabled('qlocrontaskmanager'), + 'cron_manager_installed' => Module::isInstalled('qlocrontaskmanager'), + 'cron_manager_link' => $this->module->getCronManagerLink(), 'hotel_list_url' => $hotelEditBase, )); } diff --git a/modules/qlogooglehotelconnector/ghclogcron.php b/modules/qlogooglehotelconnector/ghclogcron.php index 9fa7af927..3aa3846d6 100644 --- a/modules/qlogooglehotelconnector/ghclogcron.php +++ b/modules/qlogooglehotelconnector/ghclogcron.php @@ -36,28 +36,12 @@ 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) . '\'' -); +// Delete qghc_api_log rows and daily file logs older than 2 months +$result = Module::getInstanceByName('qlogooglehotelconnector')->cleanupApiLogs(); -if ($deleted) { - $count = Db::getInstance()->Affected_Rows(); - echo 'OK — ' . (int)$count . ' log(s) deleted (older than ' . $cutoff . ').'; +if ($result !== false) { + echo 'OK — ' . (int)$result['count'] . ' log(s) deleted (older than ' . $result['cutoff'] . ').'; } 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/qlogooglehotelconnector.php b/modules/qlogooglehotelconnector/qlogooglehotelconnector.php index 30fb1b909..e49f53d9b 100644 --- a/modules/qlogooglehotelconnector/qlogooglehotelconnector.php +++ b/modules/qlogooglehotelconnector/qlogooglehotelconnector.php @@ -33,10 +33,18 @@ class QloGoogleHotelConnector extends Module const CONFIG_WS_KEY = 'QGHC_WS_KEY'; /** PS Configuration key that stores the module connection status (0 = off, 1 = connected). */ const CONFIG_STATUS = 'QGHC_STATUS'; + const CRON_TASK_CLEANUP_LOGS = 'cleanup_api_logs'; public $debugging = false; public $logger; + /** Schedules offered for the API log cleanup, as cron expressions. */ + protected $cronOptions = array( + 'daily' => '0 0 * * *', + 'weekly' => '0 0 * * 0', + 'monthly' => '0 0 1 * *', + ); + /** Sets up module properties (name, version, author) and initialises the file logger. */ public function __construct() { @@ -192,9 +200,168 @@ class QloGoogleHotelConnector extends Module 'actionObjectHotelOrderRestrictDateUpdateBefore', // Front-end structured data markup 'displayFooterBefore', + // Cron Task Manager + 'registerCronTasks', )); } + /** Tells the Cron Task Manager module which tasks this module provides. */ + public function hookRegisterCronTasks() + { + return array( + array( + 'name' => self::CRON_TASK_CLEANUP_LOGS, + 'description' => $this->l('Delete Google Hotel API logs older than 2 months'), + 'cron' => $this->cronOptions['daily'], + 'callback' => 'cronCleanupApiLogs', + ), + ); + } + + public function cronCleanupApiLogs() + { + if ($this->cleanupApiLogs() === false) { + throw new Exception('Unable to delete the old API logs.'); + } + } + + public function cleanupApiLogs() + { + $cutoff = date('Y-m-d H:i:s', strtotime('-2 months')); + $count = QghcApiLog::deleteOlderThan($cutoff); + + // Daily file logs follow the same retention window as the DB rows. + $logDir = _PS_MODULE_DIR_ . $this->name . '/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 $count === false ? false : array('count' => $count, 'cutoff' => $cutoff); + } + + public function getCronOptionLabels() + { + return array( + 'daily' => $this->l('Daily'), + 'weekly' => $this->l('Weekly'), + 'monthly' => $this->l('Monthly'), + 'never' => $this->l('Never'), + ); + } + + public function updateCronSchedule($option) + { + if ($option !== 'never' && !isset($this->cronOptions[$option])) { + return $this->l('Invalid cron schedule.'); + } + + if (!Module::isEnabled('qlocrontaskmanager')) { + return $this->l('The Cron Task Manager module is not enabled.'); + } + + $cronTask = $this->getCronTask(); + if ($option === 'never') { + if ($cronTask && !$cronTask->delete()) { + return $this->l('Unable to update the cron task.'); + } + + return true; + } + + if (!$cronTask) { + (new QctmCronTask())->registerTasksForModule((int) $this->id, $this->hookRegisterCronTasks()); + $cronTask = $this->getCronTask(); + } + if (!$cronTask) { + return $this->l('Unable to create the cleanup cron task in the Cron Task Manager.'); + } + + $cronTask->cron_expression = $this->cronOptions[$option]; + $cronTask->active = 1; + if (!$cronTask->save()) { + return $this->l('Unable to update the cron task.'); + } + + 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 current cleanup schedule 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( + 'active' => false, + 'expression' => '', + 'readable' => '', + 'option' => 'never', + ); + } + + $readable = $cronTask->cron_expression; + if (\Cron\CronExpression::isValidExpression($cronTask->cron_expression)) { + $readable = (new QctmCronExpressionTranslator())->getCronExpressionTranslation($cronTask->cron_expression); + } + + $option = ''; + if (!$cronTask->active) { + $option = 'never'; + } elseif (($found = array_search($cronTask->cron_expression, $this->cronOptions, true)) !== false) { + $option = $found; + } + + return array( + 'active' => (bool) $cronTask->active, + 'expression' => $cronTask->cron_expression, + 'readable' => $readable, + 'option' => $option, + ); + } + + /** Returns the admin link to the Cron Task Manager, or to its module page when it is not enabled. */ + public function getCronManagerLink() + { + $link = $this->context->link; + if (Module::isEnabled('qlocrontaskmanager')) { + return $link->getAdminLink('AdminCronTaskManager'); + } + + $module = Module::getInstanceByName('qlocrontaskmanager'); + if (!$module) { + return ''; + } + + return $link->getAdminLink('AdminModules') . '&module_name=' . $module->name . '&tab_module=' . $module->tab; + } + /** 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/templates/admin/config.tpl b/modules/qlogooglehotelconnector/views/templates/admin/config.tpl index 3b52dda84..a903e77f0 100644 --- a/modules/qlogooglehotelconnector/views/templates/admin/config.tpl +++ b/modules/qlogooglehotelconnector/views/templates/admin/config.tpl @@ -164,26 +164,75 @@ {/if} {* ── Cron job setting ─────────────────────────────────────────────────── *} -
+
+
-
- - {l s='Cron job setting' mod='qlogooglehotelconnector'} -
- -
-
- - {l s='For automatic cleanup of API logs older than 2 months, set the following cron job to run once every 24 hours:' mod='qlogooglehotelconnector'} - -

- - 0 0 * * * curl {$cron_url|escape:'html':'UTF-8'} - +
+ + {l s='Cron job setting' mod='qlogooglehotelconnector'}
-
-
+ {if $cron_task} +
+ +
+ +
+
+ {/if} + +
+ {if $cron_manager_enabled} +

{l s='Automatic API log cleanup' mod='qlogooglehotelconnector'}

+
    + {if $cron_task && !$cron_task.active} +
  • {l s='Automatic API log cleanup is turned off.' mod='qlogooglehotelconnector'}
  • +
  • {l s='To turn it on, select Daily, Weekly or Monthly from the API log cleanup schedule.' mod='qlogooglehotelconnector'}
  • + {elseif $cron_task} +
  • {l s='API logs older than 2 months are deleted automatically. Current schedule:' mod='qlogooglehotelconnector'} {$cron_task.readable|escape:'html':'UTF-8'}.
  • +
  • {l s='To change it, select another option from the API log cleanup schedule.' mod='qlogooglehotelconnector'}
  • + {else} +
  • {l s='API logs older than 2 months are deleted automatically. You can configure the schedule in the' mod='qlogooglehotelconnector'} {l s='Cron Task Manager' mod='qlogooglehotelconnector'}.
  • + {/if} +
+ {else} +
    +
  • + {if $cron_manager_installed} + {l s='Automatic:' mod='qlogooglehotelconnector'} {l s='the Cron Task Manager module is disabled, so the API logs are not cleaned up automatically.' mod='qlogooglehotelconnector'} + {l s='Enable the module' mod='qlogooglehotelconnector'} + {l s='to turn on the automatic cleanup.' mod='qlogooglehotelconnector'} + {elseif $cron_manager_link} + {l s='Automatic:' mod='qlogooglehotelconnector'} {l s='the Cron Task Manager module is not installed, so the API logs are not cleaned up automatically.' mod='qlogooglehotelconnector'} + {l s='Install the module' mod='qlogooglehotelconnector'} + {l s='to turn on the automatic cleanup.' mod='qlogooglehotelconnector'} + {else} + {l s='Automatic:' mod='qlogooglehotelconnector'} {l s='the Cron Task Manager module (qlocrontaskmanager) is not present, so the API logs are not cleaned up automatically. Add the module to turn on the automatic cleanup.' mod='qlogooglehotelconnector'} + {/if} +
  • +
+ {/if} +
+ + {if $cron_task} + + {/if} + +
+
{* ── Connected Properties ───────────────────────────────────────────────── *}
From fd99bc041dd56b3ccd26fd46d8af259939cb4502 Mon Sep 17 00:00:00 2001 From: azhar bhat Date: Tue, 27 Oct 2026 10:31:29 +0530 Subject: [PATCH 3/4] text changes --- .../classes/QghcApiLog.php | 19 +++- .../AdminGoogleHotelConfigController.php | 12 +- .../qlogooglehotelconnector/ghclogcron.php | 3 +- .../qlogooglehotelconnector/logs/index.php | 32 ++++++ .../qlogooglehotelconnector.php | 106 ++++-------------- .../views/templates/admin/config.tpl | 91 +++++++-------- 6 files changed, 112 insertions(+), 151 deletions(-) create mode 100644 modules/qlogooglehotelconnector/logs/index.php diff --git a/modules/qlogooglehotelconnector/classes/QghcApiLog.php b/modules/qlogooglehotelconnector/classes/QghcApiLog.php index 74693b150..4c1a6c072 100644 --- a/modules/qlogooglehotelconnector/classes/QghcApiLog.php +++ b/modules/qlogooglehotelconnector/classes/QghcApiLog.php @@ -95,12 +95,25 @@ class QghcApiLog extends ObjectModel return false; } } - public static function deleteOlderThan($dateTime) + public static function cleanupApiLogs() { - if (!Db::getInstance()->delete('qghc_api_log', '`date_add` < \'' . pSQL($dateTime) . '\'')) { + $cutoff = date('Y-m-d H:i:s', strtotime('-2 months')); + if (!Db::getInstance()->delete('qghc_api_log', '`date_add` < \'' . pSQL($cutoff) . '\'')) { return false; } + $count = (int) Db::getInstance()->Affected_Rows(); - return (int) Db::getInstance()->Affected_Rows(); + // 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 array('count' => $count, 'cutoff' => $cutoff); } } diff --git a/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php b/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php index 800689ddf..eeed2d074 100644 --- a/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php +++ b/modules/qlogooglehotelconnector/controllers/admin/AdminGoogleHotelConfigController.php @@ -35,7 +35,6 @@ class AdminGoogleHotelConfigController extends ModuleAdminController const CONF_DISCONNECTED = 102; /** _conf[] code: webservice key regenerated successfully. */ const CONF_KEY_REGENERATED = 103; - /** _conf[] code: log cleanup cron schedule updated successfully. */ const CONF_CRON_UPDATED = 104; public function __construct() @@ -52,7 +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 schedule updated successfully.'); + $this->_conf[self::CONF_CRON_UPDATED] = $this->l('Cron job set up successfully.'); } /** @@ -82,7 +81,6 @@ class AdminGoogleHotelConfigController extends ModuleAdminController } } - /** Saves the API log cleanup schedule in the Cron Task Manager. Redirects with a success code. */ public function processSaveCron() { if (!$this->tabAccess['edit']) { @@ -90,7 +88,7 @@ class AdminGoogleHotelConfigController extends ModuleAdminController return; } - $result = $this->module->updateCronSchedule(Tools::getValue('QGHC_CRON')); + $result = $this->module->setupCronTask(); if ($result !== true) { $this->errors[] = $result; return; @@ -163,6 +161,8 @@ class AdminGoogleHotelConfigController extends ModuleAdminController } } + $cronUrl = Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . 'modules/qlogooglehotelconnector/ghclogcron.php?token=' . Tools::encrypt('qlogooglehotelconnector'); + $this->context->smarty->assign(array( 'configs' => $configs, 'ws_key' => $wsKey, @@ -170,11 +170,9 @@ class AdminGoogleHotelConfigController extends ModuleAdminController 'is_secure' => $this->isSecureRequest(), 'ssl_settings_url' => $this->context->link->getAdminLink('AdminPreferences'), 'action_url' => $this->context->link->getAdminLink('AdminGoogleHotelConfig'), - 'cron_options' => $this->module->getCronOptionLabels(), + 'cron_url' => $cronUrl, 'cron_task' => $this->module->getCronTaskSchedule(), 'cron_manager_enabled' => Module::isEnabled('qlocrontaskmanager'), - 'cron_manager_installed' => Module::isInstalled('qlocrontaskmanager'), - 'cron_manager_link' => $this->module->getCronManagerLink(), 'hotel_list_url' => $hotelEditBase, )); } diff --git a/modules/qlogooglehotelconnector/ghclogcron.php b/modules/qlogooglehotelconnector/ghclogcron.php index 3aa3846d6..87269090a 100644 --- a/modules/qlogooglehotelconnector/ghclogcron.php +++ b/modules/qlogooglehotelconnector/ghclogcron.php @@ -37,7 +37,8 @@ if ($token !== Tools::encrypt('qlogooglehotelconnector')) { } // Delete qghc_api_log rows and daily file logs older than 2 months -$result = Module::getInstanceByName('qlogooglehotelconnector')->cleanupApiLogs(); +Module::getInstanceByName('qlogooglehotelconnector'); // loads the module classes +$result = QghcApiLog::cleanupApiLogs(); if ($result !== false) { echo 'OK — ' . (int)$result['count'] . ' log(s) deleted (older than ' . $result['cutoff'] . ').'; 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 @@ + '0 0 * * *', - 'weekly' => '0 0 * * 0', - 'monthly' => '0 0 1 * *', - ); + /** Default schedule of the API log cleanup task (daily at midnight), as a cron expression. */ + protected $cronExpression = '0 0 * * *'; /** Sets up module properties (name, version, author) and initialises the file logger. */ public function __construct() @@ -212,81 +210,40 @@ class QloGoogleHotelConnector extends Module array( 'name' => self::CRON_TASK_CLEANUP_LOGS, 'description' => $this->l('Delete Google Hotel API logs older than 2 months'), - 'cron' => $this->cronOptions['daily'], + 'cron' => $this->cronExpression, 'callback' => 'cronCleanupApiLogs', ), ); } + /** Cron Task Manager callback for the API log cleanup. */ public function cronCleanupApiLogs() { - if ($this->cleanupApiLogs() === false) { + if (QghcApiLog::cleanupApiLogs() === false) { throw new Exception('Unable to delete the old API logs.'); } } - public function cleanupApiLogs() + /** + * 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() { - $cutoff = date('Y-m-d H:i:s', strtotime('-2 months')); - $count = QghcApiLog::deleteOlderThan($cutoff); - - // Daily file logs follow the same retention window as the DB rows. - $logDir = _PS_MODULE_DIR_ . $this->name . '/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 $count === false ? false : array('count' => $count, 'cutoff' => $cutoff); - } - - public function getCronOptionLabels() - { - return array( - 'daily' => $this->l('Daily'), - 'weekly' => $this->l('Weekly'), - 'monthly' => $this->l('Monthly'), - 'never' => $this->l('Never'), - ); - } - - public function updateCronSchedule($option) - { - if ($option !== 'never' && !isset($this->cronOptions[$option])) { - return $this->l('Invalid cron schedule.'); - } - if (!Module::isEnabled('qlocrontaskmanager')) { return $this->l('The Cron Task Manager module is not enabled.'); } - $cronTask = $this->getCronTask(); - if ($option === 'never') { - if ($cronTask && !$cronTask->delete()) { - return $this->l('Unable to update the cron task.'); - } - + if ($this->getCronTask()) { return true; } - if (!$cronTask) { - (new QctmCronTask())->registerTasksForModule((int) $this->id, $this->hookRegisterCronTasks()); - $cronTask = $this->getCronTask(); - } - if (!$cronTask) { + (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.'); } - $cronTask->cron_expression = $this->cronOptions[$option]; - $cronTask->active = 1; - if (!$cronTask->save()) { - return $this->l('Unable to update the cron task.'); - } - return true; } @@ -309,7 +266,7 @@ class QloGoogleHotelConnector extends Module return Validate::isLoadedObject($cronTask) ? $cronTask : false; } - /** Returns the current cleanup schedule for display, false when the Cron Task Manager is not enabled. */ + /** Returns the cleanup task state for display, false when the Cron Task Manager is not enabled. */ public function getCronTaskSchedule() { if (!Module::isEnabled('qlocrontaskmanager')) { @@ -319,10 +276,9 @@ class QloGoogleHotelConnector extends Module $cronTask = $this->getCronTask(); if (!$cronTask) { return array( + 'exists' => false, 'active' => false, - 'expression' => '', 'readable' => '', - 'option' => 'never', ); } @@ -331,37 +287,13 @@ class QloGoogleHotelConnector extends Module $readable = (new QctmCronExpressionTranslator())->getCronExpressionTranslation($cronTask->cron_expression); } - $option = ''; - if (!$cronTask->active) { - $option = 'never'; - } elseif (($found = array_search($cronTask->cron_expression, $this->cronOptions, true)) !== false) { - $option = $found; - } - return array( + 'exists' => true, 'active' => (bool) $cronTask->active, - 'expression' => $cronTask->cron_expression, 'readable' => $readable, - 'option' => $option, ); } - /** Returns the admin link to the Cron Task Manager, or to its module page when it is not enabled. */ - public function getCronManagerLink() - { - $link = $this->context->link; - if (Module::isEnabled('qlocrontaskmanager')) { - return $link->getAdminLink('AdminCronTaskManager'); - } - - $module = Module::getInstanceByName('qlocrontaskmanager'); - if (!$module) { - return ''; - } - - return $link->getAdminLink('AdminModules') . '&module_name=' . $module->name . '&tab_module=' . $module->tab; - } - /** 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/templates/admin/config.tpl b/modules/qlogooglehotelconnector/views/templates/admin/config.tpl index a903e77f0..b5892be89 100644 --- a/modules/qlogooglehotelconnector/views/templates/admin/config.tpl +++ b/modules/qlogooglehotelconnector/views/templates/admin/config.tpl @@ -172,62 +172,47 @@ {l s='Cron job setting' mod='qlogooglehotelconnector'}
- {if $cron_task} -
- -
- -
-
- {/if} - -
- {if $cron_manager_enabled} -

{l s='Automatic API log cleanup' mod='qlogooglehotelconnector'}

-
    - {if $cron_task && !$cron_task.active} -
  • {l s='Automatic API log cleanup is turned off.' mod='qlogooglehotelconnector'}
  • -
  • {l s='To turn it on, select Daily, Weekly or Monthly from the API log cleanup schedule.' mod='qlogooglehotelconnector'}
  • - {elseif $cron_task} -
  • {l s='API logs older than 2 months are deleted automatically. Current schedule:' mod='qlogooglehotelconnector'} {$cron_task.readable|escape:'html':'UTF-8'}.
  • -
  • {l s='To change it, select another option from the API log cleanup schedule.' mod='qlogooglehotelconnector'}
  • - {else} -
  • {l s='API logs older than 2 months are deleted automatically. You can configure the schedule in the' mod='qlogooglehotelconnector'} {l s='Cron Task Manager' mod='qlogooglehotelconnector'}.
  • - {/if} -
- {else} -
    -
  • - {if $cron_manager_installed} - {l s='Automatic:' mod='qlogooglehotelconnector'} {l s='the Cron Task Manager module is disabled, so the API logs are not cleaned up automatically.' mod='qlogooglehotelconnector'} - {l s='Enable the module' mod='qlogooglehotelconnector'} - {l s='to turn on the automatic cleanup.' mod='qlogooglehotelconnector'} - {elseif $cron_manager_link} - {l s='Automatic:' mod='qlogooglehotelconnector'} {l s='the Cron Task Manager module is not installed, so the API logs are not cleaned up automatically.' mod='qlogooglehotelconnector'} - {l s='Install the module' mod='qlogooglehotelconnector'} - {l s='to turn on the automatic cleanup.' mod='qlogooglehotelconnector'} + {if $cron_manager_enabled} + {if $cron_task.exists} +
    +

    {l s='Automatic API log cleanup' mod='qlogooglehotelconnector'}

    +
      + {if $cron_task.active} +
    • {l s='API logs older than 2 months are deleted automatically. Current schedule:' mod='qlogooglehotelconnector'} {$cron_task.readable|escape:'html':'UTF-8'}.
    • {else} - {l s='Automatic:' mod='qlogooglehotelconnector'} {l s='the Cron Task Manager module (qlocrontaskmanager) is not present, so the API logs are not cleaned up automatically. Add the module to turn on the automatic cleanup.' mod='qlogooglehotelconnector'} +
    • {l s='Automatic API log cleanup is turned off.' mod='qlogooglehotelconnector'}
    • {/if} - -
    +
+
+
+ {l s='If you have set up a cron job for the API log cleanup manually on your server, we recommend removing that cron job and using the Cron Task Manager from now on.' mod='qlogooglehotelconnector'} +
+ {else} +
+

{l s='The API log cleanup task is not present in the Cron Task Manager.' mod='qlogooglehotelconnector'}

+ +
+
+ + {l s='Or, for automatic cleanup of API logs older than 2 months, set the following cron job to run once every 24 hours:' mod='qlogooglehotelconnector'} + +

+ + 0 0 * * * curl {$cron_url|escape:'html':'UTF-8'} + +
{/if} -
- - {if $cron_task} -