From fd99bc041dd56b3ccd26fd46d8af259939cb4502 Mon Sep 17 00:00:00 2001 From: azhar bhat Date: Tue, 27 Oct 2026 10:31:29 +0530 Subject: [PATCH] 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'}

- - {else} - +
+
+ {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} -