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.
206 lines
6.5 KiB
PHP
206 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace Plugi;
|
|
|
|
use Plugi\Repositories\PageTranslationRepository;
|
|
|
|
class PageTranslationService
|
|
{
|
|
/**
|
|
* Return the requested locale, configured default, or first available locale, in that order.
|
|
*
|
|
* @param string|null $locale
|
|
* @param array $availableLocales
|
|
* @return string[]
|
|
*/
|
|
public function getLocaleFallbackOrder(?string $locale, array $availableLocales): array
|
|
{
|
|
$availableLocales = $this->getAvailableLocales($availableLocales);
|
|
|
|
$fallbackLocales = [];
|
|
foreach ([$locale, phpb_config('general.language')] as $preferredLocale) {
|
|
if (is_string($preferredLocale) && in_array($preferredLocale, $availableLocales, true)) {
|
|
$fallbackLocales[] = $preferredLocale;
|
|
}
|
|
}
|
|
|
|
foreach ($availableLocales as $availableLocale) {
|
|
$fallbackLocales[] = $availableLocale;
|
|
}
|
|
|
|
return array_values(array_unique($fallbackLocales));
|
|
}
|
|
|
|
/**
|
|
* Resolve a locale using the shared fallback order.
|
|
*
|
|
* @param string|null $locale
|
|
* @param array $availableLocales
|
|
* @return string|null
|
|
*/
|
|
public function resolveLocale(?string $locale, array $availableLocales): ?string
|
|
{
|
|
return $this->getLocaleFallbackOrder($locale, $availableLocales)[0] ?? null;
|
|
}
|
|
|
|
/**
|
|
* Determine whether a locale is active without applying fallback.
|
|
*
|
|
* @param string|null $locale
|
|
* @param array $availableLocales
|
|
* @return bool
|
|
*/
|
|
public function isAvailableLocale(?string $locale, array $availableLocales): bool
|
|
{
|
|
return $locale !== null && in_array($locale, $this->getAvailableLocales($availableLocales), true);
|
|
}
|
|
|
|
/**
|
|
* Return the setting from the first translation containing a non-null value.
|
|
*
|
|
* @param array $translations
|
|
* @param string $setting
|
|
* @param string|null $locale
|
|
* @return mixed|null
|
|
*/
|
|
public function resolveTranslation(array $translations, string $setting, ?string $locale = null)
|
|
{
|
|
foreach ($this->getLocaleFallbackOrder($locale, array_keys($translations)) as $fallbackLocale) {
|
|
if (array_key_exists($setting, $translations[$fallbackLocale]) && $translations[$fallbackLocale][$setting] !== null) {
|
|
return $translations[$fallbackLocale][$setting];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Return every stored route for a page, including inactive locales.
|
|
*
|
|
* @param mixed $pageId
|
|
* @return string[]
|
|
*/
|
|
public function getPageRoutes($pageId): array
|
|
{
|
|
$translations = (new PageTranslationRepository())->getForPages([$pageId]);
|
|
$routes = [];
|
|
foreach ($translations as $translation) {
|
|
$route = $translation->route ?? null;
|
|
if (is_string($route) && $route !== '') {
|
|
$routes[] = $route;
|
|
}
|
|
}
|
|
|
|
$routes = array_values(array_unique($routes));
|
|
sort($routes, SORT_STRING);
|
|
|
|
return $routes;
|
|
}
|
|
|
|
/**
|
|
* Invalidate routes after their corresponding persistence operation succeeds.
|
|
*
|
|
* @param array $routes
|
|
*/
|
|
public function invalidateRoutes(array $routes): void
|
|
{
|
|
$routes = array_values(array_unique(array_filter($routes, function ($route) {
|
|
return is_string($route) && $route !== '';
|
|
})));
|
|
sort($routes, SORT_STRING);
|
|
if (empty($routes)) {
|
|
return;
|
|
}
|
|
|
|
$cache = phpb_instance('cache');
|
|
if (! $cache) {
|
|
return;
|
|
}
|
|
foreach ($routes as $route) {
|
|
$cache->invalidate($route);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return actual saved translation coverage for the given pages and active locales.
|
|
*
|
|
* @param array $pages
|
|
* @param array $activeLanguages
|
|
* @return array
|
|
*/
|
|
public function getPageCoverage(array $pages, array $activeLanguages): array
|
|
{
|
|
$coverage = [];
|
|
$pageTypes = [];
|
|
$pageIds = [];
|
|
foreach ($pages as $page) {
|
|
$pageId = (string) $page->getId();
|
|
$pageIds[] = $page->getId();
|
|
$pageTypes[$pageId] = $page->get('type');
|
|
$coverage[$pageId] = [
|
|
'completeCount' => 0,
|
|
'localeCount' => count($activeLanguages),
|
|
'locales' => [],
|
|
];
|
|
foreach ($activeLanguages as $locale => $language) {
|
|
$coverage[$pageId]['locales'][$locale] = [
|
|
'exists' => false,
|
|
'complete' => false,
|
|
'title' => null,
|
|
'route' => null,
|
|
];
|
|
}
|
|
}
|
|
|
|
if (empty($pageIds)) {
|
|
return $coverage;
|
|
}
|
|
|
|
$foreignKey = phpb_config('page.translation.foreign_key');
|
|
$translations = (new PageTranslationRepository())->getForPages($pageIds);
|
|
foreach ($translations as $translation) {
|
|
$pageId = (string) ($translation->$foreignKey ?? '');
|
|
$locale = $translation->locale ?? null;
|
|
if (! isset($coverage[$pageId]) || ! is_string($locale) || ! array_key_exists($locale, $activeLanguages)) {
|
|
continue;
|
|
}
|
|
|
|
$title = $translation->title ?? null;
|
|
$route = $translation->route ?? null;
|
|
$target = $translation->target ?? null;
|
|
$hasRequiredValue = $pageTypes[$pageId] === 'redirect'
|
|
? trim((string) $target) !== ''
|
|
: trim((string) $title) !== '';
|
|
$isComplete = trim((string) $route) !== '' && $hasRequiredValue;
|
|
|
|
$coverage[$pageId]['locales'][$locale] = [
|
|
'exists' => true,
|
|
'complete' => $isComplete,
|
|
'title' => $title,
|
|
'route' => $route,
|
|
];
|
|
if ($isComplete) {
|
|
$coverage[$pageId]['completeCount']++;
|
|
}
|
|
}
|
|
|
|
return $coverage;
|
|
}
|
|
|
|
/**
|
|
* Normalize a list or locale-keyed language map to locale codes.
|
|
*
|
|
* @param array $availableLocales
|
|
* @return string[]
|
|
*/
|
|
private function getAvailableLocales(array $availableLocales): array
|
|
{
|
|
if ($availableLocales === [] || array_keys($availableLocales) !== range(0, count($availableLocales) - 1)) {
|
|
$availableLocales = array_keys($availableLocales);
|
|
}
|
|
|
|
return array_values(array_filter($availableLocales, function ($availableLocale) {
|
|
return is_string($availableLocale) && $availableLocale !== '';
|
|
}));
|
|
}
|
|
} |