<?php
namespace Drupal\tmgmt\Entity;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\Translator\AvailableResult;
use Drupal\tmgmt\Translator\TranslatableResult;
use Drupal\tmgmt\TranslatorInterface;
class Translator extends ConfigEntityBase implements TranslatorInterface {
protected $name;
protected $uuid;
protected $label;
protected $description;
protected $weight;
protected $plugin;
protected $settings = array();
protected $auto_accept;
protected $languageCache;
protected $languagePairsCache;
protected $remoteLanguages = [];
protected $languageCacheOutdated;
protected $remote_languages_mappings = array();
public function id() {
return $this->name;
}
public function getSettings() {
return $this->settings;
}
public function setSettings(array $settings) {
$this->settings = $settings;
return $this;
}
public function getSetting($name) {
if (is_array($name)) {
if (NestedArray::keyExists($this->settings, $name)) {
return NestedArray::getValue($this->settings, $name);
}
elseif ($plugin = $this
->getPlugin()) {
$defaults = $plugin
->defaultSettings();
return NestedArray::getValue($defaults, $name);
}
}
else {
if (isset($this->settings[$name])) {
return $this->settings[$name];
}
elseif ($plugin = $this
->getPlugin()) {
$defaults = $plugin
->defaultSettings();
if (isset($defaults[$name])) {
return $defaults[$name];
}
}
}
}
public function setSetting($setting_name, $value) {
NestedArray::setValue($this->settings, (array) $setting_name, $value);
return $this;
}
public function isAutoAccept() {
return $this->auto_accept;
}
public function setAutoAccept($value) {
$this->auto_accept = $value;
return $this;
}
public function getPluginId() {
return $this->plugin;
}
public function getDescription() {
return $this->description;
}
public function setPluginID($plugin_id) {
$this->plugin = $plugin_id;
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
foreach ($entities as $key => $name) {
$job_ids = \Drupal::entityQuery('tmgmt_job')
->accessCheck(TRUE)
->condition('state', [
Job::STATE_ACTIVE,
Job::STATE_CONTINUOUS,
Job::STATE_UNPROCESSED,
], 'IN')
->condition('translator', $key)
->execute();
$jobs = Job::loadMultiple($job_ids);
foreach ($jobs as $job) {
$job
->aborted('Job has been aborted because the translation provider %provider was deleted.', [
'%provider' => $job
->getTranslatorLabel(),
]);
}
}
parent::preDelete($storage, $entities);
}
public function getPlugin() {
return \Drupal::service('plugin.manager.tmgmt.translator')
->createInstance($this->plugin);
}
public function hasPlugin() {
if (!empty($this->plugin) && \Drupal::service('plugin.manager.tmgmt.translator')
->hasDefinition($this->plugin)) {
return TRUE;
}
return FALSE;
}
public function getSupportedTargetLanguages($source_language) {
if ($plugin = $this
->getPlugin()) {
$info = $plugin
->getPluginDefinition();
if (isset($info['language_cache']) && empty($info['language_cache'])) {
return $this
->mapToLocalLanguages($plugin
->getSupportedTargetLanguages($this, $this
->mapToRemoteLanguage($source_language)));
}
else {
if (empty($this->languageCache) && ($cache = \Drupal::cache('data')
->get('tmgmt_languages:' . $this->name))) {
$this->languageCache = $cache->data;
}
if (!isset($this->languageCache[$source_language])) {
$local_languages = $this
->mapToLocalLanguages($plugin
->getSupportedTargetLanguages($this, $this
->mapToRemoteLanguage($source_language)));
if (empty($local_languages)) {
return [];
}
$this->languageCache[$source_language] = $local_languages;
$this
->updateCache();
}
}
return $this->languageCache[$source_language];
}
}
public function getSupportedLanguagePairs() {
if ($plugin = $this
->getPlugin()) {
$info = $plugin
->getPluginDefinition();
if (isset($info['language_cache']) && empty($info['language_cache'])) {
return $plugin
->getSupportedLanguagePairs($this);
}
else {
if (empty($this->languagePairsCache) && ($cache = \Drupal::cache('data')
->get('tmgmt_language_pairs:' . $this->name))) {
$this->languagePairsCache = $cache->data;
}
if (empty($this->languagePairsCache)) {
$this->languagePairsCache = $plugin
->getSupportedLanguagePairs($this);
$this
->updateCache();
}
}
return $this->languagePairsCache;
}
}
public function getSupportedRemoteLanguages() {
if ($plugin = $this
->getPlugin()) {
if (empty($this->remoteLanguages) && ($cache = \Drupal::cache('data')
->get('tmgmt_remote_languages:' . $this->name))) {
$this->remoteLanguages = $cache->data;
}
if (empty($this->remoteLanguages)) {
$this->remoteLanguages = $plugin
->getSupportedRemoteLanguages($this);
$info = $plugin
->getPluginDefinition();
if (!isset($info['language_cache']) || !empty($info['language_cache'])) {
\Drupal::cache('data')
->set('tmgmt_remote_languages:' . $this->name, $this->remoteLanguages, Cache::PERMANENT, $this
->getCacheTags());
}
}
}
return $this->remoteLanguages;
}
public function clearLanguageCache() {
$this->languageCache = array();
\Drupal::cache('data')
->delete('tmgmt_languages:' . $this->name);
\Drupal::cache('data')
->delete('tmgmt_language_pairs:' . $this->name);
\Drupal::cache('data')
->delete('tmgmt_remote_languages:' . $this->name);
}
public function checkTranslatable(JobInterface $job) {
if ($plugin = $this
->getPlugin()) {
return $plugin
->checkTranslatable($this, $job);
}
return TranslatableResult::no(t('Missing provider plugin'));
}
public function checkAvailable() {
if ($plugin = $this
->getPlugin()) {
return $plugin
->checkAvailable($this);
}
return AvailableResult::no(t('@translator is not available. Make sure it is properly <a href=:configured>configured</a>.', [
'@translator' => $this
->label(),
':configured' => $this
->toUrl()
->toString(),
]));
}
public function hasCheckoutSettings(JobInterface $job) {
if ($plugin = $this
->getPlugin()) {
return $plugin
->hasCheckoutSettings($job);
}
return FALSE;
}
public function getRemoteLanguagesMappings() {
$remote_languages_mappings = [];
foreach (\Drupal::languageManager()
->getLanguages() as $language => $info) {
$remote_languages_mappings[$language] = $this
->mapToRemoteLanguage($language);
}
return $remote_languages_mappings;
}
public function mapToLocalLanguages(array $remote_languages) {
$local_languages = array();
$remote_mappings = $this
->getPlugin()
->getDefaultRemoteLanguagesMappings();
foreach ($remote_languages as $language => $info) {
if (in_array($language, $remote_mappings)) {
$local_language = array_search($language, $remote_mappings);
$local_languages[$local_language] = $local_language;
}
else {
$local_languages[$language] = $this
->mapToRemoteLanguage($language);
}
}
foreach (\Drupal::languageManager()
->getLanguages() as $language => $info) {
$remote_language = $this
->mapToRemoteLanguage($language);
if (isset($remote_languages[$remote_language])) {
$local_languages[$language] = $language;
}
}
return $local_languages;
}
public function mapToRemoteLanguage($language) {
if (!$this
->providesRemoteLanguageMappings()) {
return $language;
}
$mapping = $this
->get('remote_languages_mappings');
$remote_languages = $this
->getSupportedRemoteLanguages();
if (!empty($mapping) && array_key_exists($language, $mapping)) {
if (empty($remote_languages) || array_key_exists($mapping[$language], $remote_languages)) {
return $mapping[$language];
}
}
$default_mappings = $this
->getPlugin()
->getDefaultRemoteLanguagesMappings();
if (isset($default_mappings[$language])) {
return $default_mappings[$language];
}
if ($matching_language = \Drupal::service('tmgmt.language_matcher')
->getMatchingLangcode($language, $remote_languages)) {
return $matching_language;
}
return $language;
}
protected function updateCache() {
if ($plugin = $this
->getPlugin()) {
$info = $plugin
->getPluginDefinition();
if (!isset($info['language_cache']) || !empty($info['language_cache'])) {
\Drupal::cache('data')
->set('tmgmt_languages:' . $this->name, $this->languageCache, Cache::PERMANENT, $this
->getCacheTags());
\Drupal::cache('data')
->set('tmgmt_language_pairs:' . $this->name, $this->languagePairsCache, Cache::PERMANENT, $this
->getCacheTags());
}
}
}
public function providesRemoteLanguageMappings() {
$definition = \Drupal::service('plugin.manager.tmgmt.translator')
->getDefinition($this
->getPluginId());
if (!isset($definition['map_remote_languages'])) {
return TRUE;
}
return $definition['map_remote_languages'];
}
public function hasCustomSettingsHandling() {
$definition = \Drupal::service('plugin.manager.tmgmt.translator')
->getDefinition($this
->getPluginId());
if (isset($definition['job_settings_custom_handling'])) {
return $definition['job_settings_custom_handling'];
}
return FALSE;
}
public function calculateDependencies() {
parent::calculateDependencies();
if ($this
->getPlugin()) {
$this
->addDependency('module', $this
->getPlugin()
->getPluginDefinition()['provider']);
}
return $this;
}
}