up
This commit is contained in:
@@ -392,6 +392,8 @@ class ApiKernel
|
||||
$gjsComponents = $this->parseHtmlToGjsComponents($topHtml);
|
||||
}
|
||||
|
||||
$usage = $this->calculateUsage($kind, (int)$rowOut['id'], $auth);
|
||||
|
||||
$this->respond([
|
||||
'ok' => true,
|
||||
'kind' => $kind,
|
||||
@@ -400,7 +402,8 @@ class ApiKernel
|
||||
'data' => $rowOut,
|
||||
'html' => $topHtml,
|
||||
'content' => $topContent,
|
||||
'gjs_components' => $gjsComponents
|
||||
'gjs_components' => $gjsComponents,
|
||||
'usage' => $usage,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -680,3 +683,70 @@ class ApiKernel
|
||||
}
|
||||
}
|
||||
}
|
||||
private function lookupTableName(string $key, string $default): string
|
||||
{
|
||||
$tables = $this->conf['tables'] ?? [];
|
||||
if (!empty($tables[$key])) return $tables[$key];
|
||||
$prefix = $this->conf['projectdb']['prefix'] ?? null;
|
||||
if ($prefix && strpos($default, 'emailtemplate_') === 0) {
|
||||
return $prefix . substr($default, strlen('emailtemplate_'));
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
private function countRefsInTable(string $table, string $where, array $params, array $auth): int
|
||||
{
|
||||
try {
|
||||
[$tw, $tp] = $this->tenantWhere($auth);
|
||||
$sql = "SELECT COUNT(*) AS c FROM `$table` WHERE $where" . $tw;
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
foreach ($params as $k => $v) $stmt->bindValue($k, $v);
|
||||
foreach ($tp as $k => $v) $stmt->bindValue($k, $v);
|
||||
$stmt->execute();
|
||||
$row = $stmt->fetch();
|
||||
return (int)($row['c'] ?? 0);
|
||||
} catch (Throwable $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private function calculateUsage(string $kind, int $id, array $auth): array
|
||||
{
|
||||
if ($id <= 0) return ['total' => 0];
|
||||
|
||||
$summary = [];
|
||||
$templateItemsTable = $this->lookupTableName('template_items', 'emailtemplate_template_items');
|
||||
$sectionItemsTable = $this->lookupTableName('section_items', 'emailtemplate_section_items');
|
||||
|
||||
if ($kind === 'sections') {
|
||||
$summary['templates'] = $this->countRefsInTable(
|
||||
$templateItemsTable,
|
||||
"`ref_type` = :rt AND `ref_id` = :rid",
|
||||
[':rt' => 'section', ':rid' => $id],
|
||||
$auth
|
||||
);
|
||||
} elseif ($kind === 'blocks') {
|
||||
$summary['templates'] = $this->countRefsInTable(
|
||||
$templateItemsTable,
|
||||
"`ref_type` = :rt AND `ref_id` = :rid",
|
||||
[':rt' => 'block', ':rid' => $id],
|
||||
$auth
|
||||
);
|
||||
$summary['sections'] = $this->countRefsInTable(
|
||||
$sectionItemsTable,
|
||||
"`ref_id` = :rid",
|
||||
[':rid' => $id],
|
||||
$auth
|
||||
);
|
||||
$summary['snippets'] = $this->countRefsInTable(
|
||||
$this->tableMap['snippets'],
|
||||
"`block_id` = :rid",
|
||||
[':rid' => $id],
|
||||
$auth
|
||||
);
|
||||
}
|
||||
|
||||
$summary = array_filter($summary, fn($v) => (int)$v > 0);
|
||||
$summary['total'] = array_sum($summary);
|
||||
return $summary;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user