fix localized page management and builder synchronization

Validate locales and route uniqueness, report page deletion failures, and keep translation CRUD and route caches consistent. Show translation completeness in page settings and listings, and preserve locale-specific block data while synchronizing shared structure. Add translation service and page builder synchronization tests.
This commit is contained in:
2026-11-25 14:53:35 +01:00
parent 9d192c2141
commit a07e3ad8d9
19 changed files with 983 additions and 122 deletions
@@ -5,9 +5,9 @@ namespace Plugi\Modules\WebsiteManager;
use Plugi\Contracts\PageContract;
use Plugi\Contracts\WebsiteManagerContract;
use Plugi\Extensions;
use Plugi\PageTranslationService;
use Plugi\Repositories\MenuRepository;
use Plugi\Repositories\PageRepository;
use Plugi\Repositories\PageTranslationRepository;
use Plugi\Repositories\SettingRepository;
use Plugi\Repositories\UploadRepository;
@@ -127,7 +127,12 @@ class WebsiteManager implements WebsiteManagerContract
public function handleDestroy(PageContract $page)
{
$pageRepository = new PageRepository;
$pageRepository->destroy($page->getId());
if (! $pageRepository->destroy($page->getId())) {
phpb_redirect(phpb_url('website_manager'), [
'message-type' => 'error',
'message' => phpb_trans('website-manager.page-delete-failed')
]);
}
phpb_redirect(phpb_url('website_manager'), [
'message-type' => 'success',
'message' => phpb_trans('website-manager.page-deleted')
@@ -194,8 +199,8 @@ class WebsiteManager implements WebsiteManagerContract
return ($a->getRoute() < $b->getRoute()) ? -1 : 1;
});
$pageTranslationRepository = new PageTranslationRepository;
$pageTranslations = $pageTranslationRepository->getAll();
$activeLanguages = phpb_active_languages();
$pageTranslationCoverage = (new PageTranslationService())->getPageCoverage($pages, $activeLanguages);
$filesRepository = new UploadRepository;
$files = $filesRepository->getAll();
@@ -104,7 +104,7 @@ $javascriptValue = static function (string $value): string {
<div class="flex flex-col gap-1 sm:flex-row sm:items-end sm:justify-between">
<div>
<h2 class="text-lg font-semibold text-base-content">Localized page details</h2>
<p class="text-sm text-base-content/60">Titles, routes, and metadata can vary by language.</p>
<p class="text-sm text-base-content/60">Each language shows its own saved values; missing translations are left blank instead of using fallback text.</p>
</div>
<span class="badge badge-outline"><?= count($activeLanguages) ?> languages</span>
</div>
@@ -112,13 +112,41 @@ $javascriptValue = static function (string $value): string {
<?php
$firstLang = true;
foreach ($activeLanguages as $languageCode => $languageTranslation):
$savedTranslation = $pageTranslations[$languageCode] ?? null;
$hasSavedTranslation = is_array($savedTranslation);
$translationType = $fieldValue('type') !== '' ? $fieldValue('type') : ($page?->get('type') ?? 'static');
$hasRequiredTranslationValues = $hasSavedTranslation
&& trim((string) ($savedTranslation['route'] ?? '')) !== ''
&& ($translationType === 'redirect'
? trim((string) ($savedTranslation['target'] ?? '')) !== ''
: trim((string) ($savedTranslation['title'] ?? '')) !== '');
$localeStatus = $action === 'create'
? 'New translation'
: (! $hasSavedTranslation ? 'Missing' : ($hasRequiredTranslationValues ? 'Complete' : 'Needs attention'));
$localeStatusClass = $action === 'create'
? 'badge-outline'
: (! $hasSavedTranslation ? 'badge-error' : ($hasRequiredTranslationValues ? 'badge-success' : 'badge-warning'));
?>
<details class="collapse collapse-arrow border border-base-300 bg-base-100" <?= phpb_e($firstLang ? 'open' : '') ?>>
<summary class="collapse-title flex min-h-14 items-center text-base font-semibold sm:text-lg">
<summary class="collapse-title flex min-h-14 flex-wrap items-center gap-2 text-base font-semibold sm:text-lg">
<span><?= phpb_trans('languages.' . $languageCode) ?></span>
<span class="badge badge-ghost ml-3"><?= phpb_e($languageCode) ?></span>
<span class="badge badge-sm <?= $localeStatusClass ?>"><?= $localeStatus ?></span>
</summary>
<div class="collapse-content space-y-5">
<?php if ($action === 'create' || ! $hasSavedTranslation || ! $hasRequiredTranslationValues): ?>
<div class="alert <?= $hasSavedTranslation ? 'alert-warning' : 'alert-info' ?>" role="status">
<span>
<?php if ($action === 'create'): ?>
Enter the actual values for this language; other locales will not be used to fill these fields.
<?php elseif (! $hasSavedTranslation): ?>
No saved translation exists for this language. Enter its actual values; fields are not filled from another locale.
<?php else: ?>
This saved translation is missing a required title, route, or redirect target. Complete these fields before saving.
<?php endif; ?>
</span>
</div>
<?php endif; ?>
<div class="space-y-3" x-data="{route: <?= $javascriptValue($fieldValue('route', $languageCode)) ?>, empty: true, locale: <?= $javascriptValue($firstLang ? '' : '/' . $languageCode) ?>}" x-init="empty = route.length === 0">
<div class="form-control w-full">
<label class="label" for="<?= phpb_e($languageCode) ?>-page-title">
@@ -148,7 +176,7 @@ $javascriptValue = static function (string $value): string {
<label class="label" for="<?= phpb_e($languageCode) ?>-page-target-redirect">
<span class="label-text"><?= phpb_trans('website-manager.target-redirect') ?></span>
</label>
<input type="text" class="input input-bordered w-full" id="<?= phpb_e($languageCode) ?>-page-target-redirect" name="target[<?= phpb_e($languageCode) ?>]" value="<?= phpb_e($fieldValue('target', $languageCode)) ?>" />
<input type="text" class="input input-bordered w-full" id="<?= phpb_e($languageCode) ?>-page-target-redirect" name="target[<?= phpb_e($languageCode) ?>]" value="<?= phpb_e($fieldValue('target', $languageCode)) ?>" :required="type==='redirect'" />
</div>
<div class="form-control w-full">
@@ -10,19 +10,20 @@
</a>
</header>
<div class="overflow-x-auto rounded-box border border-base-300 bg-base-100">
<table class="table table-zebra w-full min-w-[48rem]">
<table class="table table-zebra w-full min-w-[64rem]">
<thead>
<tr>
<th class="w-16">ID</th>
<th><?= phpb_trans('website-manager.name') ?></th>
<th><?= phpb_trans('website-manager.route') ?></th>
<th>Localized titles and routes</th>
<th>Coverage</th>
<th class="w-44"><?= phpb_trans('website-manager.actions') ?></th>
</tr>
</thead>
<tbody>
<?php if (empty($pages)): ?>
<tr>
<td colspan="4" class="py-12 text-center">
<td colspan="5" class="py-12 text-center">
<span class="mx-auto mb-3 grid size-11 place-items-center rounded-2xl bg-info/10 text-info">
<i class="text-2xl ph-duotone ph-files" aria-hidden="true"></i>
</span>
@@ -32,18 +33,60 @@
</tr>
<?php endif; ?>
<?php foreach ($pages as $page): ?>
<?php
$pageRoute = $page->getRoute();
$canViewPage = $page->get('type') !== 'redirect' && is_string($pageRoute) && $pageRoute !== '';
?>
<tr>
<td><?= phpb_e($page->getId()) ?></td>
<td class="font-medium"><?= phpb_e($page->getName()) ?></td>
<td>
<div class="hidden pl-4 pl-8"></div>
<div class="pl-<?= phpb_e((count(explode('/', $page->getRoute()))-2)*4) ?>">
<?= phpb_e($page->getRoute()) ?>
<div class="grid gap-2 sm:grid-cols-2">
<?php foreach ($activeLanguages as $languageCode => $languageTranslation): ?>
<?php
$localeCoverage = $pageTranslationCoverage[$page->getId()]['locales'][$languageCode] ?? null;
$localeExists = $localeCoverage['exists'] ?? false;
$localeComplete = $localeCoverage['complete'] ?? false;
$localeStatus = $localeComplete ? 'Complete' : ($localeExists ? 'Needs attention' : 'Missing');
$localeBadgeClass = $localeComplete ? 'badge-success' : ($localeExists ? 'badge-warning' : 'badge-error');
$localeTitle = $localeCoverage['title'] ?? null;
$localeRoute = $localeCoverage['route'] ?? null;
$localeTitleLabel = is_string($localeTitle) && trim($localeTitle) !== ''
? phpb_e($localeTitle)
: ($page->get('type') === 'redirect' ? 'Redirect page' : 'Title not set');
?>
<div class="min-w-0 rounded-box border border-base-300 p-3">
<div class="mb-1 flex flex-wrap items-center justify-between gap-2">
<span class="text-xs font-semibold text-base-content/70">
<?= phpb_trans('languages.' . $languageCode) ?>
<span class="font-mono">(<?= phpb_e($languageCode) ?>)</span>
</span>
<span class="badge badge-sm <?= $localeBadgeClass ?>"><?= $localeStatus ?></span>
</div>
<p class="truncate text-sm font-medium" title="<?= phpb_e($localeTitle) ?>">
<?= $localeTitleLabel ?>
</p>
<p class="break-all font-mono text-xs text-base-content/60" title="<?= phpb_e($localeRoute) ?>">
<?= is_string($localeRoute) && trim($localeRoute) !== '' ? phpb_e($localeRoute) : 'Route not set' ?>
</p>
</div>
<?php endforeach; ?>
</div>
</td>
<td>
<?php
$pageCoverage = $pageTranslationCoverage[$page->getId()] ?? ['completeCount' => 0, 'localeCount' => count($activeLanguages)];
$coverageClass = $pageCoverage['localeCount'] > 0 && $pageCoverage['completeCount'] === $pageCoverage['localeCount']
? 'badge-success'
: ($pageCoverage['completeCount'] > 0 ? 'badge-warning' : 'badge-error');
?>
<span class="badge <?= $coverageClass ?>">
<?= phpb_e($pageCoverage['completeCount']) ?> / <?= phpb_e($pageCoverage['localeCount']) ?> complete
</span>
</td>
<td>
<div class="flex items-center gap-1">
<a href="<?= phpb_e(phpb_full_url($page->getRoute())) ?>" title="<?= phpb_trans('website-manager.view') ?>" aria-label="<?= phpb_e(phpb_trans('website-manager.view')) ?>" target="_blank" rel="noopener noreferrer" class="btn btn-ghost btn-square btn-sm <?= $page->get('type') === 'redirect' ? 'btn-disabled' : '' ?>" <?= $page->get('type') === 'redirect' ? 'tabindex="-1" aria-disabled="true"' : '' ?>>
<a href="<?= phpb_e($canViewPage ? phpb_full_url($pageRoute) : '#') ?>" title="<?= phpb_trans('website-manager.view') ?>" aria-label="<?= phpb_e(phpb_trans('website-manager.view')) ?>" target="_blank" rel="noopener noreferrer" class="btn btn-ghost btn-square btn-sm <?= $canViewPage ? '' : 'btn-disabled' ?>" <?= $canViewPage ? '' : 'tabindex="-1" aria-disabled="true"' ?>>
<i class="text-xl ph-duotone ph-eye" aria-hidden="true"></i>
</a>
<a href="<?= phpb_url('pagebuilder', ['page' => $page->getId()]) ?>" class="btn btn-primary btn-square btn-sm <?= $page->get('type') === 'redirect' ? 'btn-disabled' : '' ?>" title="<?= phpb_trans('website-manager.edit') ?>" aria-label="<?= phpb_e(phpb_trans('website-manager.edit')) ?>" <?= $page->get('type') === 'redirect' ? 'tabindex="-1" aria-disabled="true"' : '' ?>>