Clean old versions of a module

master
Jean-Christian Paul Denis 2023-11-14 22:05:41 +01:00
parent ac8516f30c
commit f53bab350b
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
1 changed files with 47 additions and 22 deletions

View File

@ -24,6 +24,14 @@ class BackendBehaviors
{ {
private static bool $cleared = false; private static bool $cleared = false;
/**
* Add "Hide" button on modules list.
*
* @param ModulesList $list The modules list
* @param ModuleDefine $define The current module
*
* @return string The button or empty string
*/
public static function adminModulesListGetActionsV2(ModulesList $list, ModuleDefine $define): string public static function adminModulesListGetActionsV2(ModulesList $list, ModuleDefine $define): string
{ {
return in_array($list->getList(), [ return in_array($list->getList(), [
@ -33,7 +41,11 @@ class BackendBehaviors
} }
/** /**
* Hide selected module on modules list.
*
* @param ModulesList $list Th emodules list
* @param array<int, ModuleDefine> $defines The modules * @param array<int, ModuleDefine> $defines The modules
* @param string $type The modules type (plugins or themes)
*/ */
public static function adminModulesListDoActions(ModulesList $list, array $defines, string $type): void public static function adminModulesListDoActions(ModulesList $list, array $defines, string $type): void
{ {
@ -44,33 +56,61 @@ class BackendBehaviors
} }
$type = str_starts_with($type, 'theme') ? 'themes' : 'plugins'; $type = str_starts_with($type, 'theme') ? 'themes' : 'plugins';
$to_add = key($_POST[My::id()]); $update = key($_POST[My::id()]);
$tmp = explode(' ', $update);
$module = $tmp[0];
$logs = self::getLogs($type); $logs = self::getLogs($type);
if (!$logs->isEmpty()) { if (!$logs->isEmpty()) {
$ids = [];
while ($logs->fetch()) { while ($logs->fetch()) {
if ($to_add == $logs->f('log_msg')) { if ($update == $logs->f('log_msg')) {
// Allready hidden! // Same module version allready hidden
return; return;
} }
if (str_starts_with($logs->f('log_msg'), $module)) {
// Same module but older version
$ids[] = $logs->f('log_id');
}
}
if (!empty($ids)) {
// Delete older versions
App::log()->deLogs($ids);
} }
} }
if (!empty($to_add)) { if (!empty($update)) {
// Add module version
$cur = App::log()->openLogCursor(); $cur = App::log()->openLogCursor();
$cur->setField('log_table', My::id() . '_' . $type); $cur->setField('log_table', My::id() . '_' . $type);
$cur->setField('log_msg', $to_add); $cur->setField('log_msg', $update);
App::log()->addLog($cur); App::log()->addLog($cur);
Notices::AddSuccessNotice(__('Module successfully hidden.')); Notices::AddSuccessNotice(sprintf(__('Module %s successfully hidden.'), $update));
} }
} }
/** /**
* Hide modules from store update list.
*
* @param string $type The modules type (plugins or themes)
* @param ArrayObject<int, ModuleDefine> $defines The modules * @param ArrayObject<int, ModuleDefine> $defines The modules
*/ */
public static function afterCheckStoreUpdate(string $type, ArrayObject $defines): void public static function afterCheckStoreUpdate(string $type, ArrayObject $defines): void
{ {
self::clearLogs($type); if (!self::$cleared && !empty($_GET['nocache'])) {
// Clear (once) modules versions on manual check
$logs = self::getLogs($type);
if (!$logs->isEmpty()) {
$ids = [];
while ($logs->fetch()) {
$ids[] = (int) $logs->f('log_id');
}
App::log()->delLogs($ids);
self::$cleared = true;
return;
}
}
$logs = self::getLogs($type); $logs = self::getLogs($type);
if ($logs->isEmpty()) { if ($logs->isEmpty()) {
@ -89,21 +129,6 @@ class BackendBehaviors
} }
} }
private static function clearLogs(string $type): void
{
if (!self::$cleared && !empty($_GET['nocache'])) {
$logs = self::getLogs($type);
if (!$logs->isEmpty()) {
$ids = [];
while ($logs->fetch()) {
$ids[] = (int) $logs->f('log_id');
}
App::log()->delLogs($ids);
}
self::$cleared = true;
}
}
private static function getLogs(string $type): MetaRecord private static function getLogs(string $type): MetaRecord
{ {
return App::log()->getLogs([ return App::log()->getLogs([