67 lines
2.4 KiB
PHP
67 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace KupShop\KupShopBundle\Util;
|
|
|
|
use KupShop\I18nBundle\Translations\SettingsTranslation;
|
|
use KupShop\KupShopBundle\Context\ContextManager;
|
|
use KupShop\KupShopBundle\Context\LanguageContext;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
class SettingsUtil
|
|
{
|
|
protected ContextManager $contextManager;
|
|
|
|
protected ?SettingsTranslation $settingsTranslation = null;
|
|
|
|
public function saveValueInLanguage(string $lang, string $key, $value, bool $autoload = true)
|
|
{
|
|
if (Contexts::get(LanguageContext::class)->getDefaultId() == $lang) {
|
|
\Settings::getDefault()->saveToDB([$key => $value], !$autoload ? [$key => true] : []);
|
|
} else {
|
|
$this->settingsTranslation->setLanguageId($lang)->save([$key => $value]);
|
|
}
|
|
}
|
|
|
|
public function loadValueInLanguage(string $lang, string $key, bool $fallback = true)
|
|
{
|
|
return $this->contextManager->activateContexts([LanguageContext::class => $lang], function () use ($lang, $key, $fallback) {
|
|
// fallback to default value is disabled so use settingsTranslation to get value from translations
|
|
if ($this->settingsTranslation && $fallback === false && $lang !== Contexts::get(LanguageContext::class)->getDefaultId()) {
|
|
return $this->settingsTranslation->setLanguageId($lang)->get()[$key] ?? null;
|
|
}
|
|
|
|
return \Settings::getDefault()->loadValue($key);
|
|
});
|
|
}
|
|
|
|
public function isInLanguageDefined(string $lang, array $searchPath): bool
|
|
{
|
|
if (!findModule(\Modules::TRANSLATIONS)) {
|
|
return true;
|
|
}
|
|
|
|
$languageContext = Contexts::get(LanguageContext::class);
|
|
|
|
if ($lang != $languageContext->getDefaultId()) {
|
|
$baseSettingsValue = ArrayUtil::searchInArray((array) \Settings::getFromCache($languageContext->getDefaultId()), $searchPath);
|
|
$languageSettingsValue = ArrayUtil::searchInArray((array) \Settings::getFromCache($lang), $searchPath);
|
|
|
|
return $baseSettingsValue != $languageSettingsValue;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
#[Required]
|
|
final public function setSettingsTranslation(?SettingsTranslation $settingsTranslation = null): void
|
|
{
|
|
$this->settingsTranslation = $settingsTranslation;
|
|
}
|
|
|
|
#[Required]
|
|
final public function setContextManager(ContextManager $contextManager): void
|
|
{
|
|
$this->contextManager = $contextManager;
|
|
}
|
|
}
|