text changes

This commit is contained in:
azhar bhat
2026-10-27 10:31:29 +05:30
parent 34fe0c6d0d
commit fd99bc041d
6 changed files with 112 additions and 151 deletions

View File

@@ -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);
}
}

View File

@@ -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,
));
}

View File

@@ -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'] . ').';

View File

@@ -0,0 +1,32 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License version 3.0
* that is bundled with this package in the file LICENSE.md
* It is also available through the world-wide-web at this URL:
* https://opensource.org/license/osl-3-0-php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to support@qloapps.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to a newer
* versions in the future. If you wish to customize this module for your needs
* please refer to https://store.webkul.com/customisation-guidelines for more information.
*
* @author Webkul IN
* @copyright Since 2010 Webkul
* @license https://opensource.org/license/osl-3-0-php Open Software License version 3.0
*/
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../../');
exit;

View File

@@ -33,17 +33,15 @@ 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';
/** Cron task name registered in the Cron Task Manager module. */
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 * *',
);
/** 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()
{

View File

@@ -172,62 +172,47 @@
{l s='Cron job setting' mod='qlogooglehotelconnector'}
</div>
{if $cron_task}
<div class="form-group">
<label class="control-label col-lg-3" for="ghc_cron">
<span class="label-tooltip" data-toggle="tooltip" title="{l s='How often should the API logs older than 2 months be deleted automatically?' mod='qlogooglehotelconnector'}">
{l s='API log cleanup schedule' mod='qlogooglehotelconnector'}
</span>
</label>
<div class="col-lg-3">
<select name="QGHC_CRON" id="ghc_cron" class="form-control">
{if !$cron_task.option}
<option value="" selected="selected" disabled="disabled">{l s='Custom' mod='qlogooglehotelconnector'}: {$cron_task.readable|escape:'html':'UTF-8'}</option>
{/if}
{foreach from=$cron_options key=cron_value item=cron_label}
<option value="{$cron_value|escape:'html':'UTF-8'}"{if $cron_value == $cron_task.option} selected="selected"{/if}>{$cron_label|escape:'html':'UTF-8'}</option>
{/foreach}
</select>
</div>
</div>
{/if}
<div class="alert alert-info">
{if $cron_manager_enabled}
<p><strong>{l s='Automatic API log cleanup' mod='qlogooglehotelconnector'}</strong></p>
<ul>
{if $cron_task && !$cron_task.active}
<li>{l s='Automatic API log cleanup is turned off.' mod='qlogooglehotelconnector'}</li>
<li>{l s='To turn it on, select Daily, Weekly or Monthly from the API log cleanup schedule.' mod='qlogooglehotelconnector'}</li>
{elseif $cron_task}
<li>{l s='API logs older than 2 months are deleted automatically. Current schedule:' mod='qlogooglehotelconnector'} <strong>{$cron_task.readable|escape:'html':'UTF-8'}</strong>.</li>
<li>{l s='To change it, select another option from the API log cleanup schedule.' mod='qlogooglehotelconnector'}</li>
{else}
<li>{l s='API logs older than 2 months are deleted automatically. You can configure the schedule in the' mod='qlogooglehotelconnector'} <a href="{$cron_manager_link|escape:'html':'UTF-8'}">{l s='Cron Task Manager' mod='qlogooglehotelconnector'}</a>.</li>
{/if}
</ul>
{else}
<ul>
<li>
{if $cron_manager_installed}
<strong>{l s='Automatic:' mod='qlogooglehotelconnector'}</strong> {l s='the Cron Task Manager module is disabled, so the API logs are not cleaned up automatically.' mod='qlogooglehotelconnector'}
<a href="{$cron_manager_link|escape:'html':'UTF-8'}">{l s='Enable the module' mod='qlogooglehotelconnector'}</a>
{l s='to turn on the automatic cleanup.' mod='qlogooglehotelconnector'}
{elseif $cron_manager_link}
<strong>{l s='Automatic:' mod='qlogooglehotelconnector'}</strong> {l s='the Cron Task Manager module is not installed, so the API logs are not cleaned up automatically.' mod='qlogooglehotelconnector'}
<a href="{$cron_manager_link|escape:'html':'UTF-8'}">{l s='Install the module' mod='qlogooglehotelconnector'}</a>
{l s='to turn on the automatic cleanup.' mod='qlogooglehotelconnector'}
{if $cron_manager_enabled}
{if $cron_task.exists}
<div class="alert alert-info">
<p><strong>{l s='Automatic API log cleanup' mod='qlogooglehotelconnector'}</strong></p>
<ul>
{if $cron_task.active}
<li>{l s='API logs older than 2 months are deleted automatically. Current schedule:' mod='qlogooglehotelconnector'} <strong>{$cron_task.readable|escape:'html':'UTF-8'}</strong>.</li>
{else}
<strong>{l s='Automatic:' mod='qlogooglehotelconnector'}</strong> {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'}
<li>{l s='Automatic API log cleanup is turned off.' mod='qlogooglehotelconnector'}</li>
{/if}
</li>
</ul>
</ul>
</div>
<div class="alert alert-warning">
{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'}
</div>
{else}
<div class="alert alert-info">
<p>{l s='The API log cleanup task is not present in the Cron Task Manager.' mod='qlogooglehotelconnector'}</p>
<button type="submit" name="submitGhcCron" class="btn btn-default">
<i class="icon-plus-sign"></i> {l s='Set up cron job' mod='qlogooglehotelconnector'}
</button>
</div>
<div class="alert alert-info">
<strong>
{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'}
</strong>
<br><br>
<strong>
<code>0 0 * * * curl {$cron_url|escape:'html':'UTF-8'}</code>
</strong>
</div>
{/if}
</div>
{if $cron_task}
<div class="panel-footer">
<button type="submit" name="submitGhcCron" class="btn btn-default pull-right"><i class="process-icon-save"></i> {l s='Save' mod='qlogooglehotelconnector'}</button>
{else}
<div class="alert alert-info">
<strong>
{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'}
</strong>
<br><br>
<strong>
<code>0 0 * * * curl {$cron_url|escape:'html':'UTF-8'}</code>
</strong>
</div>
{/if}