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.
247 lines
7.7 KiB
PHP
247 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace Plugi\Repositories;
|
|
|
|
use Plugi\Contracts\PageContract;
|
|
use Plugi\Contracts\PageRepositoryContract;
|
|
use Plugi\Contracts\PageTranslationContract;
|
|
use Plugi\PageTranslationService;
|
|
|
|
class PageRepository extends BaseRepository implements PageRepositoryContract
|
|
{
|
|
/**
|
|
* The pages database table.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table;
|
|
|
|
/**
|
|
* The class that represents each page.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $class;
|
|
|
|
/**
|
|
* PageRepository constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->table = empty(phpb_config('page.table')) ? 'pages' : phpb_config('page.table');
|
|
parent::__construct();
|
|
$this->class = phpb_instance('page');
|
|
}
|
|
|
|
/**
|
|
* Create a new page.
|
|
*
|
|
* @param array $data
|
|
* @return bool
|
|
* @throws \Throwable
|
|
*/
|
|
public function create(array $data)
|
|
{
|
|
foreach (['name', 'layout'] as $field) {
|
|
if (! isset($data[$field]) || ! is_string($data[$field]) || trim($data[$field]) === '') {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (! isset($data['type']) || ! is_string($data['type']) || ! in_array($data['type'], ['static', 'redirect'], true)) {
|
|
return false;
|
|
}
|
|
|
|
$activeLanguages = phpb_active_languages();
|
|
if (! $this->hasValidTranslationData($data, $activeLanguages)) {
|
|
return false;
|
|
}
|
|
|
|
return $this->db->transaction(function () use ($data) {
|
|
$page = parent::create([
|
|
'name' => $data['name'],
|
|
'layout' => $data['layout'],
|
|
'type' => $data['type'],
|
|
]);
|
|
if (! ($page instanceof PageContract)) {
|
|
return false;
|
|
}
|
|
|
|
return $this->replaceTranslations($page, $data);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Update the given page with the given updated data.
|
|
*
|
|
* @param $page
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
public function update($page, array $data)
|
|
{
|
|
foreach (['name', 'layout'] as $field) {
|
|
if (! isset($data[$field]) || ! is_string($data[$field])) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$activeLanguages = phpb_active_languages();
|
|
if (! $this->hasValidTranslationData($data, $activeLanguages)) {
|
|
return false;
|
|
}
|
|
|
|
$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;
|
|
}
|
|
|
|
return parent::update($page, [
|
|
'name' => $data['name'],
|
|
'layout' => $data['layout'],
|
|
'type' => $data['type'],
|
|
]);
|
|
});
|
|
if ($updateResult) {
|
|
$translationService->invalidateRoutes(array_merge($oldRoutes, array_values($data['route'])));
|
|
}
|
|
return $updateResult;
|
|
}
|
|
|
|
/**
|
|
* Replace the translations of the given page by the given data.
|
|
*
|
|
* @param PageContract $page
|
|
* @param array $data
|
|
* @return bool
|
|
*/
|
|
protected function replaceTranslations(PageContract $page, array $data)
|
|
{
|
|
$activeLanguages = phpb_active_languages();
|
|
if (! $this->hasValidTranslationData($data, $activeLanguages)
|
|
|| $this->hasRouteCollision($data, $activeLanguages, $page->getId())) {
|
|
return false;
|
|
}
|
|
|
|
$pageTranslationRepository = new PageTranslationRepository;
|
|
if (! $pageTranslationRepository->destroyWhere(phpb_config('page.translation.foreign_key'), $page->getId())) {
|
|
return false;
|
|
}
|
|
foreach ($activeLanguages as $languageCode => $languageTranslation) {
|
|
$translation = $pageTranslationRepository->create([
|
|
phpb_config('page.translation.foreign_key') => $page->getId(),
|
|
'locale' => $languageCode,
|
|
'title' => $data['title'][$languageCode],
|
|
'meta_title' => $data['meta_title'][$languageCode],
|
|
'meta_description' => $data['meta_description'][$languageCode],
|
|
'route' => $data['route'][$languageCode],
|
|
'target' => $data['target'][$languageCode],
|
|
]);
|
|
if (! ($translation instanceof PageTranslationContract)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function hasValidTranslationData(array $data, array $activeLanguages): bool
|
|
{
|
|
if (empty($activeLanguages) || ! isset($data['type']) || ! is_string($data['type'])
|
|
|| ! in_array($data['type'], ['static', 'redirect'], true)) {
|
|
return false;
|
|
}
|
|
|
|
foreach (['title', 'meta_title', 'meta_description', 'route', 'target'] as $field) {
|
|
if (! isset($data[$field]) || ! is_array($data[$field])) {
|
|
return false;
|
|
}
|
|
foreach ($activeLanguages as $languageCode => $languageTranslation) {
|
|
if (! array_key_exists($languageCode, $data[$field]) || ! is_string($data[$field][$languageCode])) {
|
|
return false;
|
|
}
|
|
if ($field === 'route' && trim($data[$field][$languageCode]) === '') {
|
|
return false;
|
|
}
|
|
if ($field === 'title' && $data['type'] === 'static' && trim($data[$field][$languageCode]) === '') {
|
|
return false;
|
|
}
|
|
if ($field === 'target' && $data['type'] === 'redirect' && trim($data[$field][$languageCode]) === '') {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function hasRouteCollision(array $data, array $activeLanguages, $pageId = null): bool
|
|
{
|
|
$routes = [];
|
|
foreach ($activeLanguages as $languageCode => $languageTranslation) {
|
|
$route = $data['route'][$languageCode];
|
|
if (array_key_exists($route, $routes)) {
|
|
return true;
|
|
}
|
|
$routes[$route] = true;
|
|
}
|
|
|
|
$pageTranslationRepository = new PageTranslationRepository;
|
|
foreach (array_keys($routes) as $route) {
|
|
if ($pageTranslationRepository->routeExists($route, $pageId)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Update the given page with the given updated page data.
|
|
*
|
|
* @param $page
|
|
* @param array $data
|
|
* @return bool|object|null
|
|
*/
|
|
public function updatePageData($page, array $data)
|
|
{
|
|
$updateResult = parent::update($page, [
|
|
'data' => json_encode($data),
|
|
]);
|
|
if ($updateResult) {
|
|
$page->invalidateCache();
|
|
}
|
|
return $updateResult;
|
|
}
|
|
|
|
/**
|
|
* Remove the given page from the database.
|
|
*
|
|
* @param $id
|
|
* @return bool
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$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);
|
|
});
|
|
|
|
if ($destroyResult) {
|
|
$translationService->invalidateRoutes($routes);
|
|
}
|
|
|
|
return $destroyResult;
|
|
}
|
|
}
|