You are here

class TMGMTTranslator in Translation Management Tool 7

Entity class for the tmgmt_translator entity.

Hierarchy

Expanded class hierarchy of TMGMTTranslator

Related topics

1 string reference to 'TMGMTTranslator'
tmgmt_entity_info in ./tmgmt.module
Implements hook_entity_info().

File

entity/tmgmt.entity.translator.inc, line 13

View source
class TMGMTTranslator extends Entity {

  /**
   * The ID of the translator.
   *
   * @var integer
   */
  public $tid;

  /**
   * Machine readable name of the translator.
   *
   * @var string
   */
  public $name;

  /**
   * Label of the translator.
   *
   * @var string
   */
  public $label;

  /**
   * Description of the translator.
   *
   * @var string
   */
  public $description;

  /**
   * Weight of the translator.
   *
   * @var int
   */
  public $weight;

  /**
   * Plugin name of the translator.
   *
   * @type string
   */
  public $plugin;

  /**
   * Translator type specific settings.
   *
   * @var array
   */
  public $settings;

  /**
   * The supported target languages caches.
   *
   * @var array
   */
  protected $languageCache;

  /**
   * The supported language pairs caches.
   *
   * @var array
   */
  protected $languagePairsCache;

  /**
   * Whether the language cache in the database is outdated.
   *
   * @var boolean
   */
  protected $languageCacheOutdated;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $values = array()) {
    parent::__construct($values, 'tmgmt_translator');
  }

  /**
   * Returns the translator plugin controller of this translator.
   *
   * @return TMGMTTranslatorPluginControllerInterface
   */
  public function getController() {
    if (!empty($this->plugin)) {
      return tmgmt_translator_plugin_controller($this->plugin);
    }
    return FALSE;
  }

  /**
   * Returns the supported target languages for this translator.
   *
   * @return array
   *   An array of supported target languages in ISO format.
   */
  public function getSupportedTargetLanguages($source_language) {
    if ($controller = $this
      ->getController()) {
      if (isset($this->pluginInfo['cache languages']) && empty($this->pluginInfo['cache languages'])) {

        // This plugin doesn't support language caching.
        return $controller
          ->getSupportedTargetLanguages($this, $source_language);
      }
      else {

        // Retrieve the supported languages from the cache.
        if (empty($this->languageCache) && ($cache = cache_get('languages:' . $this->name, 'cache_tmgmt'))) {
          $this->languageCache = $cache->data;
        }

        // Even if we successfully queried the cache it might not have an entry
        // for our source language yet.
        if (!isset($this->languageCache[$source_language])) {
          $this->languageCache[$source_language] = $controller
            ->getSupportedTargetLanguages($this, $source_language);
          $this->languageCacheOutdated = TRUE;
        }
      }
      return $this->languageCache[$source_language];
    }
  }

  /**
   * Gets the supported language pairs for this translator.
   *
   * @return array
   *   List of language pairs where a pair is an associative array of
   *   source_language and target_language.
   *   Example:
   *   array(
   *     array('source_language' => 'en-US', 'target_language' => 'de-DE'),
   *     array('source_language' => 'en-US', 'target_language' => 'de-CH'),
   *   )
   */
  public function getSupportedLanguagePairs() {
    if ($controller = $this
      ->getController()) {
      if (isset($this->pluginInfo['cache languages']) && empty($this->pluginInfo['cache languages'])) {

        // This plugin doesn't support language caching.
        return $controller
          ->getSupportedLanguagePairs($this);
      }
      else {

        // Retrieve the supported languages from the cache.
        if (empty($this->languagePairsCache) && ($cache = cache_get('language_pairs:' . $this->name, 'cache_tmgmt'))) {
          $this->languagePairsCache = $cache->data;
        }

        // Even if we successfully queried the cache data might not be yet
        // available.
        if (empty($this->languagePairsCache)) {
          $this->languagePairsCache = $controller
            ->getSupportedLanguagePairs($this);
          $this->languageCacheOutdated = TRUE;
        }
      }
      return $this->languagePairsCache;
    }
  }

  /**
   * Clears the language cache for this translator.
   */
  public function clearLanguageCache() {
    cache_clear_all('languages:' . $this->name, 'cache_tmgmt');
    cache_clear_all('language_pairs:' . $this->name, 'cache_tmgmt');
  }

  /**
   * Check whether this translator can handle a particular translation job.
   *
   * @param $job
   *   The TMGMTJob entity that should be translated.
   *
   * @return boolean
   *   TRUE if the job can be processed and translated, FALSE otherwise.
   */
  public function canTranslate(TMGMTJob $job) {
    if ($controller = $this
      ->getController()) {
      return $controller
        ->canTranslate($this, $job);
    }
    return FALSE;
  }

  /**
   * Checks whether a translator is available.
   *
   * @return boolean
   *   TRUE if the translator plugin is available, FALSE otherwise.
   */
  public function isAvailable() {
    if ($controller = $this
      ->getController()) {
      return $controller
        ->isAvailable($this);
    }
    return FALSE;
  }

  /**
   * Returns if the plugin has any settings for this job.
   */
  public function hasCheckoutSettings(TMGMTJob $job) {
    if ($controller = $this
      ->getController()) {
      return $controller
        ->hasCheckoutSettings($job);
    }
    return FALSE;
  }

  /**
   * @todo Remove this once http://drupal.org/node/1420364 is done.
   */
  public function getNotAvailableReason() {
    if ($controller = $this
      ->getController()) {
      return $controller
        ->getNotAvailableReason($this);
    }
    return FALSE;
  }

  /**
   * @todo Remove this once http://drupal.org/node/1420364 is done.
   */
  public function getNotCanTranslateReason(TMGMTJob $job) {
    if ($controller = $this
      ->getController()) {
      return $controller
        ->getNotCanTranslateReason($job);
    }
    return FALSE;
  }

  /**
   * Retrieves a setting value from the translator settings. Pulls the default
   * values (if defined) from the plugin controller.
   *
   * @param $name
   *   The name of the setting.
   *
   * @return
   *   The setting value or $default if the setting value is not set. Returns
   *   NULL if the setting does not exist at all.
   */
  public function getSetting($name) {
    if (isset($this->settings[$name])) {
      return $this->settings[$name];
    }
    elseif ($controller = $this
      ->getController()) {
      $defaults = $controller
        ->defaultSettings();
      if (isset($defaults[$name])) {
        return $defaults[$name];
      }
    }
  }

  /**
   * Maps local language to remote language.
   *
   * @param $language
   *   Local language code.
   *
   * @return string
   *   Remote language code.
   *
   * @ingroup tmgmt_remote_languages_mapping
   */
  public function mapToRemoteLanguage($language) {
    return $this
      ->getController()
      ->mapToRemoteLanguage($this, $language);
  }

  /**
   * Maps remote language to local language.
   *
   * @param $language
   *   Remote language code.
   *
   * @return string
   *   Local language code.
   *
   * @ingroup tmgmt_remote_languages_mapping
   */
  public function mapToLocalLanguage($language) {
    return $this
      ->getController()
      ->mapToLocalLanguage($this, $language);
  }

  /**
   * Updates the language cache if it has changed.
   */
  public function __destruct() {
    if ($controller = $this
      ->getController()) {
      $info = $controller
        ->pluginInfo();
      if (!isset($info['language cache']) || !empty($info['language cache']) && !empty($this->languageCacheOutdated)) {
        cache_set('languages:' . $this->name, $this->languageCache, 'cache_tmgmt');
        cache_set('language_pairs:' . $this->name, $this->languagePairsCache, 'cache_tmgmt');
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::save public function Permanently saves the entity. Overrides EntityInterface::save
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.
TMGMTTranslator::$description public property Description of the translator.
TMGMTTranslator::$label public property Label of the translator.
TMGMTTranslator::$languageCache protected property The supported target languages caches.
TMGMTTranslator::$languageCacheOutdated protected property Whether the language cache in the database is outdated.
TMGMTTranslator::$languagePairsCache protected property The supported language pairs caches.
TMGMTTranslator::$name public property Machine readable name of the translator.
TMGMTTranslator::$plugin public property Plugin name of the translator.
TMGMTTranslator::$settings public property Translator type specific settings.
TMGMTTranslator::$tid public property The ID of the translator.
TMGMTTranslator::$weight public property Weight of the translator.
TMGMTTranslator::canTranslate public function Check whether this translator can handle a particular translation job.
TMGMTTranslator::clearLanguageCache public function Clears the language cache for this translator.
TMGMTTranslator::getController public function Returns the translator plugin controller of this translator.
TMGMTTranslator::getNotAvailableReason public function @todo Remove this once http://drupal.org/node/1420364 is done.
TMGMTTranslator::getNotCanTranslateReason public function @todo Remove this once http://drupal.org/node/1420364 is done.
TMGMTTranslator::getSetting public function Retrieves a setting value from the translator settings. Pulls the default values (if defined) from the plugin controller.
TMGMTTranslator::getSupportedLanguagePairs public function Gets the supported language pairs for this translator.
TMGMTTranslator::getSupportedTargetLanguages public function Returns the supported target languages for this translator.
TMGMTTranslator::hasCheckoutSettings public function Returns if the plugin has any settings for this job.
TMGMTTranslator::isAvailable public function Checks whether a translator is available.
TMGMTTranslator::mapToLocalLanguage public function Maps remote language to local language.
TMGMTTranslator::mapToRemoteLanguage public function Maps local language to remote language.
TMGMTTranslator::__construct public function Overrides Entity::__construct
TMGMTTranslator::__destruct public function Updates the language cache if it has changed.