<?php
/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/
namespace Pimcore\TranslationsProviderInterfaceBundle\Service;
use Pimcore\Config\LocationAwareConfigRepository;
use Pimcore\TranslationsProviderInterfaceBundle\TranslationsProviderManager\TranslationsProviderManagerInterface;
class ConfigurationService
{
const CONFIG_ID = 'pimcore_translations_provider_interface';
const CONFIGURATION_DIRECTORY = PIMCORE_CONFIGURATION_DIRECTORY . '/translations-provider';
const DEFAULT_VALUE_AUTO_SUBMIT = true;
const DEFAULT_VALUE_REDELIVERY_THRESHOLD = 60;
const DEFAULT_VALUE_ERROR_THRESHOLD = 10;
const DEFAULT_VALUE_PROJECT_SHORTCODE = 'Pimcore';
const DEFAULT_VALUE_TRANSLATIONS_DEFAULT_FILE_FORMAT = 'PimcoreXML';
const DEFAULT_VALUE_TRANSLATIONS_WSDL_PATH = 'https://gl-connect2.translations.com/PD/services/';
const DEFAULT_VALUE_TRANSLATIONS_USER_AGENT = 'pimcore';
protected ?array $translationsComSettings;
protected ?array $xplanationSettings;
protected ?array $deepLSettings;
protected bool $autoSubmit;
protected ?string $defaultProvider;
protected ?string $notificationRecipients;
protected ?string $allowedLanguages;
protected int $redeliveryThreshold;
protected int $errorThreshold;
protected ?string $projectShortcode;
protected bool $ignorePermissions = false;
protected bool $isWriteable;
protected bool $configLoaded = false;
protected ?LocationAwareConfigRepository $locationAwareConfigRepository;
protected TranslationsProviderManagerInterface $translationsProviderManager;
public function __construct(array $config, TranslationsProviderManagerInterface $translationsProviderManager)
{
$this->translationsProviderManager = $translationsProviderManager;
$containerConfig = [];
if (isset($config['ignore_permissions']) && $config['ignore_permissions'] === true) {
trigger_deprecation('pimcore/translations-provider-interfaces', '2.4', 'Config `ignore_permissions` is used but deprecated. Make sure you set permissions properly to your users/roles. Will be removed in version 3.0');
$this->ignorePermissions = $config['ignore_permissions'];
}
if (!empty($config['settings'])) {
//check if other keys than default keys exist
$hasAdditionalKeys = false;
if (!empty(array_diff(array_keys($config['settings']), ['auto_submit', 'redelivery_threshold', 'error_threshold', 'project_shortcode']))) {
$hasAdditionalKeys = true;
}
//check if default keys have default values
$defaultValuesAreSet = false;
if (
$config['settings']['auto_submit'] !== self::DEFAULT_VALUE_AUTO_SUBMIT ||
$config['settings']['redelivery_threshold'] !== self::DEFAULT_VALUE_REDELIVERY_THRESHOLD ||
$config['settings']['error_threshold'] !== self::DEFAULT_VALUE_ERROR_THRESHOLD ||
$config['settings']['project_shortcode'] !== self::DEFAULT_VALUE_PROJECT_SHORTCODE
) {
$defaultValuesAreSet = true;
}
if ($hasAdditionalKeys || $defaultValuesAreSet) {
$containerConfig[self::CONFIG_ID]['settings'] = $config['settings'];
}
}
if (!empty($config['translations_com'])) {
$containerConfig[self::CONFIG_ID]['translations_com'] = $config['translations_com'];
}
if (!empty($config['xplanation'])) {
$containerConfig[self::CONFIG_ID]['xplanation'] = $config['xplanation'];
}
if (!empty($config['deepl'])) {
$containerConfig[self::CONFIG_ID]['deepl'] = $config['deepl'];
}
$this->locationAwareConfigRepository = new LocationAwareConfigRepository(
$containerConfig,
self::CONFIG_ID,
self::CONFIGURATION_DIRECTORY,
'PIMCORE_WRITE_TARGET_TRANSLATIONS_PROVIDER'
);
}
protected function loadConfig()
{
if ($this->configLoaded === false) {
list($config, $dataSource) = $this->locationAwareConfigRepository->loadConfigByKey(self::CONFIG_ID);
$this->translationsComSettings = $config['translations_com'] ?? null;
$this->xplanationSettings = $config['xplanation'] ?? [];
$this->deepLSettings = $config['deepl'] ?? [];
$this->autoSubmit = $config['settings']['auto_submit'] ?? self::DEFAULT_VALUE_AUTO_SUBMIT;
$this->defaultProvider = $config['settings']['default_provider'] ?? null;
$this->notificationRecipients = $config['settings']['notification_recipients'] ?? null;
$this->allowedLanguages = $config['settings']['allowed_languages'] ?? null;
$this->redeliveryThreshold = $config['settings']['redelivery_threshold'] ?? self::DEFAULT_VALUE_REDELIVERY_THRESHOLD;
$this->errorThreshold = $config['settings']['error_threshold'] ?? self::DEFAULT_VALUE_ERROR_THRESHOLD;
$this->projectShortcode = $config['settings']['project_shortcode'] ?? self::DEFAULT_VALUE_PROJECT_SHORTCODE;
$this->isWriteable = $this->locationAwareConfigRepository->isWriteable(self::CONFIG_ID, $dataSource);
$this->configLoaded = true;
}
}
/**
*
* @throws \Exception
*/
public function saveConfiguration(
array $settings, ?array $translationsComSettings = null, ?array $xplanationSettings = null, ?array $deeplSettings = null
) {
$data = [
'settings' => [
'auto_submit' => $settings['auto_submit'] ?? self::DEFAULT_VALUE_AUTO_SUBMIT,
'default_provider' => $settings['default_provider'] ?? null,
'notification_recipients' => $settings['notification_recipients'] ?? null,
'allowed_languages' => is_array($settings['allowed_languages'] ?? null) ? implode(',', $settings['allowed_languages']) : ($settings['allowed_languages'] ?? ''),
'redelivery_threshold' => $settings['redelivery_threshold'] ?? self::DEFAULT_VALUE_REDELIVERY_THRESHOLD,
'error_threshold' => $settings['error_threshold'] ?? self::DEFAULT_VALUE_ERROR_THRESHOLD,
'project_shortcode' => $settings['project_shortcode'] ?? self::DEFAULT_VALUE_PROJECT_SHORTCODE
],
'translations_com' => $translationsComSettings,
'xplanation' => $xplanationSettings,
'deepl' => $deeplSettings
];
$this->locationAwareConfigRepository->saveConfig(self::CONFIG_ID, $data, function ($key, $data) {
return [
$key => $data
];
});
$this->configLoaded = false;
}
public function getFullConfiguration(): array
{
$this->loadConfig();
return [
'settings' => [
'auto_submit' => $this->autoSubmit,
'default_provider' => $this->defaultProvider,
'notification_recipients' => $this->notificationRecipients,
'allowed_languages' => $this->allowedLanguages,
'redelivery_threshold' => $this->redeliveryThreshold,
'error_threshold' => $this->errorThreshold,
'project_shortcode' => $this->projectShortcode
],
'translations_com' => $this->translationsComSettings,
'xplanation' => $this->xplanationSettings,
'deepl' => $this->deepLSettings
];
}
public function getProviderTypes(): array
{
$classes = [];
foreach ($this->translationsProviderManager->getTranslationsProviders() as $provider) {
$classes[] = get_class($provider);
}
return $classes;
}
public function getAvailableProviders(): array
{
$providers = [];
foreach ($this->translationsProviderManager->getTranslationsProviders() as $id => $provider) {
$providers[] = [$id, $provider->getShortcut()];
}
return $providers;
}
public function isWriteable(): bool
{
$this->loadConfig();
return $this->isWriteable;
}
public function getTranslationsComSettings(): ?array
{
$this->loadConfig();
return $this->translationsComSettings;
}
public function getXplanationSettings(): ?array
{
$this->loadConfig();
return $this->xplanationSettings;
}
public function getDeeplSettings(): ?array
{
$this->loadConfig();
return $this->deepLSettings;
}
public function getAutoSubmit(): bool
{
$this->loadConfig();
return $this->autoSubmit;
}
public function getDefaultProvider(): ?string
{
$this->loadConfig();
return $this->defaultProvider;
}
public function getNotificationRecipients(): ?string
{
$this->loadConfig();
return $this->notificationRecipients;
}
public function getAllowedLanguages(): ?string
{
$this->loadConfig();
return $this->allowedLanguages;
}
public function getRedeliveryThreshold(): int
{
$this->loadConfig();
return $this->redeliveryThreshold;
}
public function getErrorThreshold(): int
{
$this->loadConfig();
return $this->errorThreshold;
}
public function getProjectShortcode(): ?string
{
$this->loadConfig();
return $this->projectShortcode;
}
/**
*
* @deprecated
*/
public function getIgnorePermissions(): bool
{
return $this->ignorePermissions;
}
}