aktive/inaktiv list templates

This commit is contained in:
2026-02-03 03:49:42 +01:00
parent aaddd33e3b
commit d10614dc0a
4 changed files with 163 additions and 35 deletions

View File

@@ -2337,7 +2337,7 @@ class ApiKernel
private function findTemplateReferences(int $customerId, int $templateId, array &$debug = []): array
{
$out = [];
$seen = [];
$byId = [];
$debug = [
'time' => date(DATE_ATOM),
'customer_id' => $customerId,
@@ -2347,6 +2347,31 @@ class ApiKernel
'matched_rows' => [],
'template_items_matches' => [],
];
$addRef = function (int $id, string $name) use (&$out, &$byId) {
if ($id <= 0) return;
if (!isset($byId[$id])) {
$entry = [
'id' => $id,
'name' => $name,
'versions' => [],
];
$byId[$id] = count($out);
$out[] = $entry;
} elseif ($name !== '') {
$out[$byId[$id]]['name'] = $name;
}
};
$addVersion = function (int $id, ?int $ver) use (&$out, &$byId) {
if ($id <= 0 || $ver === null || $ver <= 0) return;
if (!isset($byId[$id])) {
return;
}
$idx = $byId[$id];
if (!in_array($ver, $out[$idx]['versions'], true)) {
$out[$idx]['versions'][] = $ver;
sort($out[$idx]['versions']);
}
};
$matches = function (?string $html, array $libKinds = []) use ($templateId): bool {
if (!$html) return false;
$id = preg_quote((string)$templateId, '/');
@@ -2463,12 +2488,8 @@ class ApiKernel
}
if ($found) {
$id = (int)($row['id'] ?? 0);
if ($id <= 0 || isset($seen[$id])) continue;
$seen[$id] = true;
$out[] = [
'id' => $id,
'name' => (string)($row['name'] ?? ''),
];
if ($id <= 0) continue;
$addRef($id, (string)($row['name'] ?? ''));
if (count($debug['matched_rows']) < 50) {
$debug['matched_rows'][] = [
'id' => $id,
@@ -2478,6 +2499,62 @@ class ApiKernel
}
}
}
if ($this->tableExists($versionsTable)) {
$vCols = $this->tableColumns($versionsTable);
$vHtml = $versionHtmlCol;
$vJson = $versionJsonCol;
$vCraft = $versionCraftCol;
$vSettings = $versionSettingsCol;
$vNo = $this->firstExisting($vCols, ['version_no', 'version', 'ver', 'version_nr']);
$vContentId = $this->firstExisting($vCols, ['content_id', 'content']);
$vCustomerId = $this->firstExisting($vCols, ['customer_id', 'customer']);
$vSectionId = $this->firstExisting($vCols, ['section_id', 'section']);
if ($vContentId && $vNo) {
$select = "v.`$vContentId` AS content_id, v.`$vNo` AS version_no, i.`name` AS name";
if ($vHtml) $select .= ", v.`$vHtml` AS version_html";
if ($vJson) $select .= ", v.`$vJson` AS version_json";
if ($vCraft) $select .= ", v.`$vCraft` AS version_craft";
if ($vSettings) $select .= ", v.`$vSettings` AS version_settings";
$join = "LEFT JOIN `$itemsTable` i ON i.`id` = v.`$vContentId`";
$where = [];
$params = [];
if ($vCustomerId) {
$where[] = "v.`$vCustomerId` = :cid";
$params[':cid'] = $customerId;
}
if ($vSectionId) {
$where[] = "v.`$vSectionId` = :sid";
$params[':sid'] = (int)$section['id'];
}
$where[] = "v.`$vContentId` <> :id";
$params[':id'] = $templateId;
$sql = "SELECT $select FROM `$versionsTable` v $join WHERE " . implode(' AND ', $where);
$stmt = $this->pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll() ?: [];
foreach ($rows as $row) {
$blobs = [
(string)($row['version_html'] ?? ''),
(string)($row['version_json'] ?? ''),
(string)($row['version_craft'] ?? ''),
(string)($row['version_settings'] ?? ''),
];
$found = false;
foreach ($blobs as $blob) {
if ($matches($blob, $libKinds)) {
$found = true;
break;
}
}
if ($found) {
$cid = (int)($row['content_id'] ?? 0);
$addRef($cid, (string)($row['name'] ?? ''));
$addVersion($cid, (int)($row['version_no'] ?? 0));
}
}
}
}
}
if (!$this->useUnifiedContent()) {
$table = $this->tableMap['templates'] ?? null;
@@ -2520,12 +2597,8 @@ class ApiKernel
}
if ($found) {
$id = (int)($row['id'] ?? 0);
if ($id <= 0 || isset($seen[$id])) continue;
$seen[$id] = true;
$out[] = [
'id' => $id,
'name' => (string)($row['name'] ?? ''),
];
if ($id <= 0) continue;
$addRef($id, (string)($row['name'] ?? ''));
if (count($debug['matched_rows']) < 50) {
$debug['matched_rows'][] = [
'id' => $id,
@@ -2559,12 +2632,8 @@ class ApiKernel
$rows = $stmt->fetchAll() ?: [];
foreach ($rows as $row) {
$id = (int)($row['id'] ?? 0);
if ($id <= 0 || isset($seen[$id])) continue;
$seen[$id] = true;
$out[] = [
'id' => $id,
'name' => (string)($row['name'] ?? ''),
];
if ($id <= 0) continue;
$addRef($id, (string)($row['name'] ?? ''));
}
}
} else {
@@ -2588,12 +2657,8 @@ class ApiKernel
$rows = $stmt->fetchAll() ?: [];
foreach ($rows as $row) {
$id = (int)($row['id'] ?? 0);
if ($id <= 0 || isset($seen[$id])) continue;
$seen[$id] = true;
$out[] = [
'id' => $id,
'name' => (string)($row['name'] ?? ''),
];
if ($id <= 0) continue;
$addRef($id, (string)($row['name'] ?? ''));
}
}
}