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:
@@ -5,6 +5,7 @@ namespace Plugi\Repositories;
|
||||
use Plugi\Contracts\PageContract;
|
||||
use Plugi\Contracts\PageRepositoryContract;
|
||||
use Plugi\Contracts\PageTranslationContract;
|
||||
use Plugi\PageTranslationService;
|
||||
|
||||
class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
{
|
||||
@@ -90,7 +91,10 @@ class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
return false;
|
||||
}
|
||||
|
||||
$updateResult = $this->db->transaction(function () use ($page, $data) {
|
||||
$translationService = new PageTranslationService();
|
||||
$oldRoutes = [];
|
||||
$updateResult = $this->db->transaction(function () use ($page, $data, $translationService, &$oldRoutes) {
|
||||
$oldRoutes = $translationService->getPageRoutes($page->getId());
|
||||
if (! $this->replaceTranslations($page, $data)) {
|
||||
return false;
|
||||
}
|
||||
@@ -102,7 +106,7 @@ class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
]);
|
||||
});
|
||||
if ($updateResult) {
|
||||
$page->invalidateCache();
|
||||
$translationService->invalidateRoutes(array_merge($oldRoutes, array_values($data['route'])));
|
||||
}
|
||||
return $updateResult;
|
||||
}
|
||||
@@ -185,13 +189,9 @@ class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
$routes[$route] = true;
|
||||
}
|
||||
|
||||
$foreignKey = phpb_config('page.translation.foreign_key');
|
||||
$pageTranslationRepository = new PageTranslationRepository;
|
||||
foreach ($pageTranslationRepository->getAll(['route', $foreignKey]) as $translation) {
|
||||
if ($pageId !== null && (string) $translation->$foreignKey === (string) $pageId) {
|
||||
continue;
|
||||
}
|
||||
if (array_key_exists($translation->route, $routes)) {
|
||||
foreach (array_keys($routes) as $route) {
|
||||
if ($pageTranslationRepository->routeExists($route, $pageId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -211,7 +211,9 @@ class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
$updateResult = parent::update($page, [
|
||||
'data' => json_encode($data),
|
||||
]);
|
||||
$page->invalidateCache();
|
||||
if ($updateResult) {
|
||||
$page->invalidateCache();
|
||||
}
|
||||
return $updateResult;
|
||||
}
|
||||
|
||||
@@ -223,8 +225,22 @@ class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$this->findWithId($id)->invalidateCache();
|
||||
$translationService = new PageTranslationService();
|
||||
$routes = [];
|
||||
$destroyResult = $this->db->transaction(function () use ($id, $translationService, &$routes) {
|
||||
$routes = $translationService->getPageRoutes($id);
|
||||
$pageTranslationRepository = new PageTranslationRepository();
|
||||
if (! $pageTranslationRepository->destroyWhere(phpb_config('page.translation.foreign_key'), $id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::destroy($id);
|
||||
return parent::destroy($id);
|
||||
});
|
||||
|
||||
if ($destroyResult) {
|
||||
$translationService->invalidateRoutes($routes);
|
||||
}
|
||||
|
||||
return $destroyResult;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,4 +29,47 @@ class PageTranslationRepository extends BaseRepository implements PageTranslatio
|
||||
parent::__construct();
|
||||
$this->class = phpb_instance('page.translation');
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a route is already used by another page translation.
|
||||
*
|
||||
* @param string $route
|
||||
* @param mixed|null $excludedPageId
|
||||
* @return bool
|
||||
*/
|
||||
public function routeExists(string $route, $excludedPageId = null): bool
|
||||
{
|
||||
$query = "SELECT 1 FROM {$this->table} WHERE route = ?";
|
||||
$parameters = [$route];
|
||||
if ($excludedPageId !== null) {
|
||||
$foreignKey = $this->removeNonAlphaNumeric(phpb_config('page.translation.foreign_key'));
|
||||
$query .= " AND {$foreignKey} != ?";
|
||||
$parameters[] = $excludedPageId;
|
||||
}
|
||||
$query .= ' LIMIT 1';
|
||||
|
||||
return ! empty($this->db->select($query, $parameters));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return translations belonging to the given page IDs.
|
||||
*
|
||||
* @param array $pageIds
|
||||
* @return array
|
||||
*/
|
||||
public function getForPages(array $pageIds): array
|
||||
{
|
||||
if (empty($pageIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$foreignKey = $this->removeNonAlphaNumeric(phpb_config('page.translation.foreign_key'));
|
||||
$placeholders = implode(', ', array_fill(0, count($pageIds), '?'));
|
||||
$records = $this->db->select(
|
||||
"SELECT * FROM {$this->table} WHERE {$foreignKey} IN ({$placeholders})",
|
||||
array_values($pageIds)
|
||||
);
|
||||
|
||||
return $this->createInstances($records);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user