feat(multi-language): Add multi-language support and routing updates
Add configuration for multiple languages in the Website Manager
This commit is contained in:
@@ -281,10 +281,15 @@ if (! function_exists('phpb_current_relative_url')) {
|
||||
|
||||
if (! function_exists('phpb_current_language')) {
|
||||
/**
|
||||
* Give the current language based on the current URL.
|
||||
* Give the resolved request language or fall back to the current URL and site default.
|
||||
*/
|
||||
function phpb_current_language(): string
|
||||
{
|
||||
global $phpb_request_language;
|
||||
if (isset($phpb_request_language) && is_string($phpb_request_language)) {
|
||||
return $phpb_request_language;
|
||||
}
|
||||
|
||||
$urlComponents = explode('/', phpb_current_relative_url());
|
||||
// remove empty values and reset array key numbering
|
||||
$urlComponents = array_values(array_filter($urlComponents));
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Plugi\Modules\Router;
|
||||
use Plugi\Contracts\PageContract;
|
||||
use Plugi\Contracts\PageTranslationContract;
|
||||
use Plugi\Contracts\RouterContract;
|
||||
use Plugi\PageTranslationService;
|
||||
use Plugi\Repositories\PageRepository;
|
||||
use Plugi\Repositories\PageTranslationRepository;
|
||||
|
||||
@@ -49,23 +50,46 @@ class DatabasePageRouter implements RouterContract
|
||||
*/
|
||||
public function resolve($url)
|
||||
{
|
||||
// strip URL query parameters
|
||||
$url = explode('?', $url, 2)[0];
|
||||
// split URL into segments using / as separator
|
||||
$this->routeParameters = [];
|
||||
$this->routeToPageTranslationIdMapping = [];
|
||||
$translationService = new PageTranslationService();
|
||||
$url = preg_split('/[?#]/', $url, 2)[0] ?? '';
|
||||
$url = preg_replace('#/+#', '/', '/' . trim($url, '/')) ?: '/';
|
||||
$urlSegments = explode('/', $url);
|
||||
|
||||
// request all routes and convert each to its segments using / as separator
|
||||
$pageTranslations = $this->pageTranslationRepository->getAll(['id', 'route']);
|
||||
$activeLanguages = phpb_active_languages();
|
||||
$pageTranslations = $this->pageTranslationRepository->getAll(['id', 'route', 'locale']);
|
||||
$routes = [];
|
||||
foreach ($pageTranslations as $pageTranslation) {
|
||||
$route = $pageTranslation->route;
|
||||
$this->routeToPageTranslationIdMapping[$route] = $pageTranslation->id;
|
||||
$routeSegments = explode('/', $route);
|
||||
$routes[] = $routeSegments;
|
||||
if (! array_key_exists($pageTranslation->locale, $activeLanguages)) {
|
||||
continue;
|
||||
}
|
||||
if (! is_string($pageTranslation->route) || trim($pageTranslation->route) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$route = $translationService->normalizeRoute($pageTranslation->route);
|
||||
$routeKey = $translationService->getRouteKey($route);
|
||||
if (isset($routes[$routeKey])) {
|
||||
$routes[$routeKey] = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
$routes[$routeKey] = [
|
||||
'route' => $route,
|
||||
'translationId' => $pageTranslation->id,
|
||||
];
|
||||
}
|
||||
|
||||
// sort routes into the order for evaluation
|
||||
$orderedRoutes = $this->getRoutesInOrder($routes);
|
||||
$routeSegments = [];
|
||||
foreach ($routes as $route) {
|
||||
if ($route === false) {
|
||||
continue;
|
||||
}
|
||||
$this->routeToPageTranslationIdMapping[$route['route']] = $route['translationId'];
|
||||
$routeSegments[] = explode('/', $route['route']);
|
||||
}
|
||||
$orderedRoutes = $this->getRoutesInOrder($routeSegments);
|
||||
|
||||
// match each route with current URL segments and return the corresponding page once we find a match
|
||||
foreach ($orderedRoutes as $routeSegments) {
|
||||
@@ -186,7 +210,7 @@ class DatabasePageRouter implements RouterContract
|
||||
break;
|
||||
}
|
||||
// the URL segment matches if equal to the route segment
|
||||
if ($urlSegment === $routeSegment) {
|
||||
if (strtolower($urlSegment) === strtolower($routeSegment)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -329,6 +329,15 @@ class WebsiteManager implements WebsiteManagerContract
|
||||
],
|
||||
);
|
||||
}
|
||||
phpb_redirect(
|
||||
phpb_url("website_manager", ["tab" => "settings"]),
|
||||
[
|
||||
"message-type" => "error",
|
||||
"message" => phpb_trans(
|
||||
"website-manager.invalid-default-language",
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ if (isset($page)) {
|
||||
|
||||
$pageTranslations = $page ? $page->getTranslations() : [];
|
||||
$activeLanguages = phpb_active_languages();
|
||||
$routeService = new \Plugi\PageTranslationService();
|
||||
$fieldValue = static function (string $field, ?string $languageCode = null) use ($page, $pageTranslations): string {
|
||||
if ($languageCode !== null) {
|
||||
$postedValues = $_POST[$field] ?? null;
|
||||
@@ -112,6 +113,7 @@ $javascriptValue = static function (string $value): string {
|
||||
<?php
|
||||
$firstLang = true;
|
||||
foreach ($activeLanguages as $languageCode => $languageTranslation):
|
||||
$routePrefix = $routeService->getGeneratedRoutePrefix($languageCode, $firstLang);
|
||||
$savedTranslation = $pageTranslations[$languageCode] ?? null;
|
||||
$hasSavedTranslation = is_array($savedTranslation);
|
||||
$translationType = $fieldValue('type') !== '' ? $fieldValue('type') : ($page?->get('type') ?? 'static');
|
||||
@@ -147,7 +149,7 @@ $javascriptValue = static function (string $value): string {
|
||||
</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="space-y-3" x-data="{route: <?= $javascriptValue($fieldValue('route', $languageCode)) ?>, empty: true, locale: <?= $javascriptValue($routePrefix) ?>}" x-init="empty = route.length === 0">
|
||||
<div class="form-control w-full">
|
||||
<label class="label" for="<?= phpb_e($languageCode) ?>-page-title">
|
||||
<span class="label-text"><?= phpb_trans('website-manager.page-title') ?></span>
|
||||
|
||||
@@ -2,12 +2,20 @@
|
||||
|
||||
use Plugi\Extensions;
|
||||
|
||||
$settingIns = phpb_instance('setting');
|
||||
$activeLanguages = phpb_active_languages();
|
||||
?>
|
||||
|
||||
<form method="post" action="<?= phpb_url('website_manager', ['route' => 'settings', 'action' => 'update', 'tab' => 'settings']) ?>">
|
||||
<form method="post" action="<?= phpb_url("website_manager", [
|
||||
"route" => "settings",
|
||||
"action" => "update",
|
||||
"tab" => "settings",
|
||||
]) ?>">
|
||||
<?php phpb_csrf_field(); ?>
|
||||
<div class="space-y-6" x-data="{activeTab: 0, tabs: ['Website','Language','Theme','Extensions']}">
|
||||
<div class="space-y-6" x-data="{activeTab: 0, tabs: ['Website','Language','Theme','Extensions'], enabledLanguages: <?= str_replace(
|
||||
'"',
|
||||
"'",
|
||||
json_encode(array_keys($activeLanguages)),
|
||||
) ?>}">
|
||||
<header class="space-y-1">
|
||||
<h2 class="text-xl font-bold tracking-tight text-base-content">Site settings</h2>
|
||||
<p class="text-sm text-base-content/60">Configure language, appearance, and extension options for this site.</p>
|
||||
@@ -22,37 +30,53 @@ $settingIns = phpb_instance('setting');
|
||||
</div>
|
||||
<div id="settings-panel-1" class="space-y-6" role="tabpanel" aria-labelledby="settings-tab-1" tabindex="0" x-show="activeTab === 1">
|
||||
<section class="space-y-4 rounded-box border border-base-300 bg-base-100 p-4 sm:p-6">
|
||||
<h2 class="text-lg font-semibold"><?= phpb_trans('website-manager.website-languages') ?></h2>
|
||||
<h2 class="text-lg font-semibold"><?= phpb_trans(
|
||||
"website-manager.website-languages",
|
||||
) ?></h2>
|
||||
<div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
|
||||
<?php
|
||||
foreach (phpb_trans('languages') as $locale => $localeText):
|
||||
?>
|
||||
<?php foreach (
|
||||
phpb_trans("languages")
|
||||
as $locale => $localeText
|
||||
): ?>
|
||||
<div class="form-control">
|
||||
<label class="flex cursor-pointer items-center justify-between gap-4 rounded-box border border-base-300 bg-base-100 px-4 py-3 transition-colors hover:border-primary/40 hover:bg-base-200/50">
|
||||
<span class="label-text"><?= phpb_e($localeText) ?></span>
|
||||
<input type="checkbox" name="languages[]" value="<?= phpb_e($locale) ?>" <?= phpb_e($settingIns::has('languages', $locale)) ? 'checked' : '' ?> class="checkbox checkbox-sm" />
|
||||
<span class="label-text"><?= phpb_e(
|
||||
$localeText,
|
||||
) ?></span>
|
||||
<input type="checkbox" name="languages[]" value="<?= phpb_e(
|
||||
$locale,
|
||||
) ?>" <?= isset($activeLanguages[$locale])
|
||||
? "checked"
|
||||
: "" ?> @change="enabledLanguages = $event.target.checked ? [...enabledLanguages, $event.target.value] : enabledLanguages.filter(language => language !== $event.target.value)" class="checkbox checkbox-sm" />
|
||||
</label>
|
||||
</div>
|
||||
<?php
|
||||
endforeach;
|
||||
?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</section>
|
||||
<section class="max-w-2xl rounded-box border border-base-300 bg-base-100 p-4 sm:p-6">
|
||||
<div class="form-control w-full">
|
||||
<label for="admin_language" class="label">
|
||||
<span class="label-text"><?= phpb_trans('website-manager.admin-language') ?> <strong><?= phpb_current_language() ?></strong></span>
|
||||
<label for="default_language" class="label">
|
||||
<span class="label-text"><?= phpb_trans(
|
||||
"website-manager.default-language",
|
||||
) ?></span>
|
||||
</label>
|
||||
<select class="select select-bordered w-full" id="admin_language" name="admin_language" title="<?= phpb_trans('website-manager.languages-selector-placeholder') ?>" required>
|
||||
<?php
|
||||
$beLanguages = ['de', 'en', 'sv'];
|
||||
foreach ($beLanguages as $locale):
|
||||
?>
|
||||
<option value="<?= phpb_e($locale) ?>" <?= phpb_e(phpb_config('general.language') === $locale ? 'selected' : '') ?>><?= phpb_e(phpb_trans('languages.'.$locale)) ?></option>
|
||||
<?php
|
||||
endforeach;
|
||||
?>
|
||||
<select class="select select-bordered w-full" id="default_language" name="default_language" title="<?= phpb_trans(
|
||||
"website-manager.languages-selector-placeholder",
|
||||
) ?>" required>
|
||||
<?php foreach (
|
||||
phpb_trans("languages")
|
||||
as $locale => $localeText
|
||||
): ?>
|
||||
<option value="<?= phpb_e($locale) ?>" <?= phpb_e(
|
||||
phpb_config("general.language") === $locale ? "selected" : "",
|
||||
) ?> :disabled="!enabledLanguages.includes('<?= phpb_e(
|
||||
$locale,
|
||||
) ?>')"><?= phpb_e($localeText) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<p class="mt-2 text-sm text-base-content/60"><?= phpb_trans(
|
||||
"website-manager.default-language-requires-enabled",
|
||||
) ?></p>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
@@ -60,74 +84,82 @@ $settingIns = phpb_instance('setting');
|
||||
<section class="max-w-2xl rounded-box border border-base-300 bg-base-100 p-4 sm:p-6">
|
||||
<div class="form-control w-full">
|
||||
<label for="selected_theme" class="label">
|
||||
<span class="label-text"><?= phpb_trans('website-manager.selected-theme') ?></span>
|
||||
<span class="label-text"><?= phpb_trans(
|
||||
"website-manager.selected-theme",
|
||||
) ?></span>
|
||||
</label>
|
||||
<select class="select select-bordered w-full" id="selected_theme" name="selected_theme" title="<?= phpb_trans('website-manager.languages-selector-placeholder') ?>" required>
|
||||
<select class="select select-bordered w-full" id="selected_theme" name="selected_theme" title="<?= phpb_trans(
|
||||
"website-manager.languages-selector-placeholder",
|
||||
) ?>" required>
|
||||
<?php
|
||||
$themes = phpb_themes();
|
||||
foreach ($themes as $theme => $themeConfig):
|
||||
?>
|
||||
<option value="<?= phpb_e($theme) ?>" <?= phpb_e(phpb_config('theme.active_theme') === $theme ? 'selected' : '') ?>><?= phpb_e($themeConfig['title']) ?></option>
|
||||
<?php
|
||||
endforeach;
|
||||
?>
|
||||
foreach ($themes as $theme => $themeConfig): ?>
|
||||
<option value="<?= phpb_e($theme) ?>" <?= phpb_e(
|
||||
phpb_config("theme.active_theme") === $theme ? "selected" : "",
|
||||
) ?>><?= phpb_e($themeConfig["title"]) ?></option>
|
||||
<?php endforeach;
|
||||
?>
|
||||
</select>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
<div id="settings-panel-3" class="space-y-6" role="tabpanel" aria-labelledby="settings-tab-3" tabindex="0" x-show="activeTab === 3">
|
||||
<div class="flex flex-col gap-4">
|
||||
<?php
|
||||
foreach (Extensions::getConfigs() as $extKey => $config):
|
||||
if(count(Extensions::getSettings($extKey)) === 0){
|
||||
<?php foreach (Extensions::getConfigs() as $extKey => $config):
|
||||
if (count(Extensions::getSettings($extKey)) === 0) {
|
||||
continue;
|
||||
}
|
||||
?>
|
||||
} ?>
|
||||
<details class="collapse collapse-arrow border border-base-300 bg-base-100">
|
||||
<summary class="collapse-title text-lg font-semibold"><?= phpb_e($config['title']) ?></summary>
|
||||
<summary class="collapse-title text-lg font-semibold"><?= phpb_e(
|
||||
$config["title"],
|
||||
) ?></summary>
|
||||
<div class="collapse-content">
|
||||
<div class="space-y-4">
|
||||
<?php
|
||||
foreach (Extensions::getSettings($extKey) as $setting):
|
||||
?>
|
||||
<?php
|
||||
if ($setting['type'] === 'text'):
|
||||
?>
|
||||
<?php foreach (
|
||||
Extensions::getSettings($extKey)
|
||||
as $setting
|
||||
): ?>
|
||||
<?php if ($setting["type"] === "text"): ?>
|
||||
<label class="form-control w-full gap-2">
|
||||
<span class="label-text"><?= phpb_e($setting['label'] ?: $setting['name']) ?></span>
|
||||
<input type="text" name="<?= phpb_e($setting['key']) ?>" value="<?= phpb_e($setting['value']) ?>" class="input input-bordered w-full" />
|
||||
<span class="label-text"><?= phpb_e(
|
||||
$setting["label"] ?:
|
||||
$setting["name"],
|
||||
) ?></span>
|
||||
<input type="text" name="<?= phpb_e(
|
||||
$setting["key"],
|
||||
) ?>" value="<?= phpb_e(
|
||||
$setting["value"],
|
||||
) ?>" class="input input-bordered w-full" />
|
||||
</label>
|
||||
<?php
|
||||
endif;
|
||||
?>
|
||||
<?php
|
||||
if ($setting['type'] === 'toggle'):
|
||||
?>
|
||||
<div class="form-control" x-data="{state: <?= phpb_e($setting['value']) ?>}">
|
||||
<?php endif; ?>
|
||||
<?php if ($setting["type"] === "toggle"): ?>
|
||||
<div class="form-control" x-data="{state: <?= phpb_e(
|
||||
$setting["value"],
|
||||
) ?>}">
|
||||
<label class="label cursor-pointer">
|
||||
<span class="label-text"><?= phpb_e($setting['label'] ?: $setting['name']) ?></span>
|
||||
<input type='hidden' :value="state" name="<?= phpb_e($setting['key']) ?>">
|
||||
<span class="label-text"><?= phpb_e(
|
||||
$setting["label"] ?:
|
||||
$setting["name"],
|
||||
) ?></span>
|
||||
<input type='hidden' :value="state" name="<?= phpb_e(
|
||||
$setting["key"],
|
||||
) ?>">
|
||||
<input type="checkbox" class="toggle" x-model="state" />
|
||||
</label>
|
||||
</div>
|
||||
<?php
|
||||
endif;
|
||||
?>
|
||||
<?php
|
||||
endforeach;
|
||||
?>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
<?php
|
||||
endforeach;
|
||||
?>
|
||||
endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex justify-end border-t border-base-300 pt-6">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="ph-bold ph-floppy-disk" aria-hidden="true"></i>
|
||||
<?= phpb_trans('website-manager.save-settings'); ?>
|
||||
<?= phpb_trans("website-manager.save-settings") ?>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -138,10 +170,16 @@ $settingIns = phpb_instance('setting');
|
||||
|
||||
|
||||
<!-- <div class="mt-8">-->
|
||||
<!-- --><?php //= phpb_trans('website-manager.pagebuilder-block-images') ?>
|
||||
<!-- --><?php
|
||||
//= phpb_trans('website-manager.pagebuilder-block-images')
|
||||
?>
|
||||
<!-- </div>-->
|
||||
<!-- <a href="--><?php //= phpb_url('website_manager', ['route' => 'settings', 'action' => 'renderBlockThumbs']) ?><!--" class="btn btn-secondary btn-sm mr-1">-->
|
||||
<!-- --><?php //= phpb_trans('website-manager.render-thumbs') ?>
|
||||
<!-- <a href="--><?php
|
||||
//= phpb_url('website_manager', ['route' => 'settings', 'action' => 'renderBlockThumbs'])
|
||||
?><!--" class="btn btn-secondary btn-sm mr-1">-->
|
||||
<!-- --><?php
|
||||
//= phpb_trans('website-manager.render-thumbs')
|
||||
?>
|
||||
<!-- </a>-->
|
||||
|
||||
</form>
|
||||
|
||||
@@ -6,6 +6,30 @@ use Plugi\Repositories\PageTranslationRepository;
|
||||
|
||||
class PageTranslationService
|
||||
{
|
||||
public function normalizeRoute(string $route): string
|
||||
{
|
||||
$route = preg_split('/[?#]/', trim($route), 2)[0] ?? '';
|
||||
$segments = preg_split('#/+#', trim($route, '/'), -1, PREG_SPLIT_NO_EMPTY) ?: [];
|
||||
foreach ($segments as &$segment) {
|
||||
if ($segment !== '*' && ! preg_match('/^\{[^{}]+\}$/', $segment)) {
|
||||
$segment = strtolower($segment);
|
||||
}
|
||||
}
|
||||
unset($segment);
|
||||
|
||||
return $segments === [] ? '/' : '/' . implode('/', $segments);
|
||||
}
|
||||
|
||||
public function getRouteKey(string $route): string
|
||||
{
|
||||
return strtolower($this->normalizeRoute($route));
|
||||
}
|
||||
|
||||
public function getGeneratedRoutePrefix(string $locale, bool $isDefaultLocale): string
|
||||
{
|
||||
return $isDefaultLocale ? '' : '/' . $locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the requested locale, configured default, or first available locale, in that order.
|
||||
*
|
||||
|
||||
@@ -209,7 +209,7 @@ class Plugi
|
||||
$phpb_db = new DB();
|
||||
$theme = Setting::get('selected_theme');
|
||||
if ($theme) $phpb_config['theme']['active_theme'] = $theme;
|
||||
$language = Setting::get('admin_language');
|
||||
$language = Setting::get('default_language') ?? Setting::get('admin_language');
|
||||
if ($language) $phpb_config['general']['language'] = $language;
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ class Plugi
|
||||
global $phpb_config;
|
||||
$theme = Setting::get('selected_theme');
|
||||
if ($theme) $phpb_config['theme']['active_theme'] = $theme;
|
||||
$language = Setting::get('admin_language');
|
||||
$language = Setting::get('default_language') ?? Setting::get('admin_language');
|
||||
if ($language) $phpb_config['general']['language'] = $language;
|
||||
}
|
||||
|
||||
@@ -473,9 +473,16 @@ class Plugi
|
||||
$previewToken = $_GET['previewToken'] ?? null;
|
||||
$isAuthenticatedPreview = $this->isAuthenticatedPreviewRequest($previewToken);
|
||||
|
||||
$pageTranslation = $this->resolvePageLanguageVariantFromUrl(phpb_current_relative_url());
|
||||
if ($pageTranslation) {
|
||||
global $phpb_request_language;
|
||||
$phpb_request_language = $pageTranslation->locale;
|
||||
$this->loadTranslations($pageTranslation->locale);
|
||||
}
|
||||
|
||||
// try to find page in cache
|
||||
$cache = phpb_instance('cache');
|
||||
if (! $hasPreviewToken &&
|
||||
if ($pageTranslation && ! $hasPreviewToken &&
|
||||
phpb_config('cache.enabled') &&
|
||||
! isset($_GET['ignore_cache']) &&
|
||||
! isset($_GET['refresh_cache']) &&
|
||||
@@ -490,11 +497,7 @@ class Plugi
|
||||
}
|
||||
|
||||
// let the page router resolve the current URL
|
||||
$page = null;
|
||||
$pageTranslation = $this->resolvePageLanguageVariantFromUrl(phpb_current_relative_url());
|
||||
if ($pageTranslation) {
|
||||
$page = $pageTranslation->getPage();
|
||||
}
|
||||
$page = $pageTranslation?->getPage();
|
||||
// if the URL cannot be resolved, but the lowercase version of the URL can be resolved, redirect to the lowercase URL
|
||||
if (($page->logic ?? '') === 'page-not-found' && phpb_current_relative_url() !== strtolower(phpb_current_relative_url())) {
|
||||
$pageLowerCaseUrlTranslation = $this->resolvePageLanguageVariantFromUrl(strtolower(phpb_current_relative_url()));
|
||||
|
||||
@@ -121,8 +121,12 @@ class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
]);
|
||||
});
|
||||
if ($updateResult) {
|
||||
$newRoutes = [];
|
||||
foreach ($activeLanguages as $languageCode => $languageTranslation) {
|
||||
$newRoutes[] = $translationService->normalizeRoute($data['route'][$languageCode]);
|
||||
}
|
||||
$translationService->invalidateRoutes(
|
||||
array_merge($oldRoutes, array_values($data["route"])),
|
||||
array_merge($oldRoutes, $newRoutes),
|
||||
);
|
||||
}
|
||||
return $updateResult;
|
||||
@@ -146,26 +150,35 @@ class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
}
|
||||
|
||||
$pageTranslationRepository = new PageTranslationRepository();
|
||||
if (
|
||||
!$pageTranslationRepository->destroyWhere(
|
||||
phpb_config("page.translation.foreign_key"),
|
||||
$page->getId(),
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
$translationService = new PageTranslationService();
|
||||
$existingTranslations = [];
|
||||
foreach ($pageTranslationRepository->getForPages([$page->getId()]) as $existingTranslation) {
|
||||
$existingTranslations[$existingTranslation->locale] = $existingTranslation;
|
||||
}
|
||||
foreach ($activeLanguages as $languageCode => $languageTranslation) {
|
||||
$translation = $pageTranslationRepository->create([
|
||||
$existingTranslation = $existingTranslations[$languageCode] ?? null;
|
||||
$route = $data['route'][$languageCode];
|
||||
if (! $existingTranslation || $route !== $existingTranslation->route) {
|
||||
$route = $translationService->normalizeRoute($route);
|
||||
}
|
||||
$translationData = [
|
||||
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],
|
||||
"route" => $route,
|
||||
"target" => $data["target"][$languageCode],
|
||||
]);
|
||||
if (!($translation instanceof PageTranslationContract)) {
|
||||
return false;
|
||||
];
|
||||
if ($existingTranslation) {
|
||||
if (! $pageTranslationRepository->update($existingTranslation, $translationData)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$translation = $pageTranslationRepository->create($translationData);
|
||||
if (! ($translation instanceof PageTranslationContract)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,17 +246,49 @@ class PageRepository extends BaseRepository implements PageRepositoryContract
|
||||
array $activeLanguages,
|
||||
$pageId = null,
|
||||
): bool {
|
||||
$translationService = new PageTranslationService();
|
||||
$existingTranslations = [];
|
||||
if ($pageId !== null) {
|
||||
foreach ((new PageTranslationRepository())->getForPages([$pageId]) as $translation) {
|
||||
$existingTranslations[$translation->locale] = $translation;
|
||||
}
|
||||
}
|
||||
|
||||
$routes = [];
|
||||
foreach ($activeLanguages as $languageCode => $languageTranslation) {
|
||||
$route = $data["route"][$languageCode];
|
||||
if (array_key_exists($route, $routes)) {
|
||||
return true;
|
||||
$routeKey = $translationService->getRouteKey($route);
|
||||
if (array_key_exists($routeKey, $routes)) {
|
||||
$unchangedRoutes = $pageId !== null;
|
||||
foreach (array_keys($routes[$routeKey]) as $existingLocale) {
|
||||
if (($existingTranslations[$existingLocale]->route ?? null) !== $data['route'][$existingLocale]) {
|
||||
$unchangedRoutes = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (! $unchangedRoutes || ($existingTranslations[$languageCode]->route ?? null) !== $route) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$routes[$routeKey][$languageCode] = $route;
|
||||
}
|
||||
|
||||
foreach ($routes as $routeKey => $localizedRoutes) {
|
||||
foreach ($existingTranslations as $existingLocale => $translation) {
|
||||
if (isset($activeLanguages[$existingLocale]) || $translationService->getRouteKey((string) $translation->route) !== $routeKey) {
|
||||
continue;
|
||||
}
|
||||
foreach ($localizedRoutes as $languageCode => $route) {
|
||||
if (($existingTranslations[$languageCode]->route ?? null) !== $route) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
$routes[$route] = true;
|
||||
}
|
||||
|
||||
$pageTranslationRepository = new PageTranslationRepository();
|
||||
foreach (array_keys($routes) as $route) {
|
||||
foreach ($routes as $localizedRoutes) {
|
||||
$route = reset($localizedRoutes);
|
||||
if ($pageTranslationRepository->routeExists($route, $pageId)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Plugi\Repositories;
|
||||
|
||||
use Plugi\Contracts\PageTranslationRepositoryContract;
|
||||
use Plugi\PageTranslationService;
|
||||
|
||||
class PageTranslationRepository extends BaseRepository implements PageTranslationRepositoryContract
|
||||
{
|
||||
@@ -39,16 +40,23 @@ class PageTranslationRepository extends BaseRepository implements PageTranslatio
|
||||
*/
|
||||
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;
|
||||
$foreignKey = $this->removeNonAlphaNumeric(phpb_config('page.translation.foreign_key'));
|
||||
$routes = $this->db->select("SELECT route, {$foreignKey} FROM {$this->table}", []);
|
||||
$translationService = new PageTranslationService();
|
||||
$routeKey = $translationService->getRouteKey($route);
|
||||
foreach ($routes as $translation) {
|
||||
if ($excludedPageId !== null && (string) $translation[$foreignKey] === (string) $excludedPageId) {
|
||||
continue;
|
||||
}
|
||||
if (! is_string($translation['route']) || trim($translation['route']) === '') {
|
||||
continue;
|
||||
}
|
||||
if ($translationService->getRouteKey((string) $translation['route']) === $routeKey) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
$query .= ' LIMIT 1';
|
||||
|
||||
return ! empty($this->db->select($query, $parameters));
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,27 +21,60 @@ class SettingRepository extends BaseRepository implements SettingRepositoryContr
|
||||
*/
|
||||
public function updateSettings(array $data)
|
||||
{
|
||||
if (! $this->hasValidLanguageSettings($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$isArray = is_array($value) ? 1 : 0;
|
||||
if ($isArray) {
|
||||
$value = implode(',', $value);
|
||||
}
|
||||
$ins = $this->findWhere('setting', $key);
|
||||
if (empty($ins)) {
|
||||
$this->create([
|
||||
return $this->db->transaction(function () use ($data) {
|
||||
foreach ($data as $key => $value) {
|
||||
$isArray = is_array($value) ? 1 : 0;
|
||||
if ($isArray) {
|
||||
$value = implode(',', $value);
|
||||
}
|
||||
$setting = [
|
||||
'setting' => $key,
|
||||
'value' => $value,
|
||||
'is_array' => $isArray,
|
||||
]);
|
||||
} else {
|
||||
$this->update($ins[0], [
|
||||
'setting' => $key,
|
||||
'value' => $value,
|
||||
'is_array' => $isArray,
|
||||
]);
|
||||
];
|
||||
$ins = $this->findWhere('setting', $key);
|
||||
if (empty($ins)) {
|
||||
if (! $this->create($setting)) {
|
||||
return false;
|
||||
}
|
||||
} elseif (! $this->update($ins[0], $setting)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
private function hasValidLanguageSettings(array $data): bool
|
||||
{
|
||||
if (! array_intersect(['languages', 'default_language', 'admin_language'], array_keys($data))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$languages = array_key_exists('languages', $data)
|
||||
? $data['languages']
|
||||
: array_keys(phpb_active_languages());
|
||||
$defaultLanguage = $data['default_language']
|
||||
?? phpb_instance('setting')::get('default_language')
|
||||
?? $data['admin_language']
|
||||
?? phpb_config('general.language');
|
||||
$availableLanguages = phpb_trans('languages');
|
||||
|
||||
if (! is_array($languages) || $languages === [] || ! is_string($defaultLanguage)
|
||||
|| ! is_array($availableLanguages) || ! isset($availableLanguages[$defaultLanguage])
|
||||
|| ! in_array($defaultLanguage, $languages, true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($languages as $language) {
|
||||
if (! is_string($language) || ! isset($availableLanguages[$language])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user