TranslateFormBase.php 6.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\locale\Form\TranslateFormBase.
 */

namespace Drupal\locale\Form;

use Drupal\Core\Form\FormBase;
11
use Drupal\Core\Language\LanguageManagerInterface;
12
use Drupal\locale\StringStorageInterface;
13
use Drupal\Core\State\StateInterface;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines the locale user interface translation form base.
 *
 * Provides methods for searching and filtering strings.
 */
abstract class TranslateFormBase extends FormBase {

  /**
   * The locale storage.
   *
   * @var \Drupal\locale\StringStorageInterface
   */
  protected $localeStorage;

  /**
   * The state store.
   *
33
   * @var \Drupal\Core\State\StateInterface
34 35 36 37 38 39
   */
  protected $state;

  /**
   * The language manager.
   *
40
   * @var \Drupal\Core\Language\LanguageManagerInterface
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   */
  protected $languageManager;

  /*
   * Filter values. Shared between objects that inherit this class.
   *
   * @var array|null
   */
  protected static $filterValues;

  /**
   * Constructs a new TranslationFormBase object.
   *
   * @param \Drupal\locale\StringStorageInterface $locale_storage
   *   The locale storage.
56
   * @param \Drupal\Core\State\StateInterface $state
57
   *   The state service.
58
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
59 60
   *   The language manager.
   */
61
  public function __construct(StringStorageInterface $locale_storage, StateInterface $state, LanguageManagerInterface $language_manager) {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    $this->localeStorage = $locale_storage;
    $this->state = $state;
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('locale.storage'),
      $container->get('state'),
      $container->get('language_manager')
    );
  }

  /**
   * Builds a string search query and returns an array of string objects.
   *
   * @return \Drupal\locale\TranslationString[]
   *   Array of \Drupal\locale\TranslationString objects.
   */
  protected function translateFilterLoadStrings() {
    $filter_values = $this->translateFilterValues();

    // Language is sanitized to be one of the possible options in
    // translateFilterValues().
    $conditions = array('language' => $filter_values['langcode']);
    $options = array('pager limit' => 30, 'translated' => TRUE, 'untranslated' => TRUE);

    // Add translation status conditions and options.
    switch ($filter_values['translation']) {
      case 'translated':
        $conditions['translated'] = TRUE;
        if ($filter_values['customized'] != 'all') {
          $conditions['customized'] = $filter_values['customized'];
        }
        break;

      case 'untranslated':
        $conditions['translated'] = FALSE;
        break;

    }

    if (!empty($filter_values['string'])) {
      $options['filters']['source'] = $filter_values['string'];
      if ($options['translated']) {
        $options['filters']['translation'] = $filter_values['string'];
      }
    }

    return $this->localeStorage->getTranslations($conditions, $options);
  }

  /**
   * Builds an array out of search criteria specified in request variables.
   *
   * @param bool $reset
   *   If the list of values should be reset.
   *
123
   * @return array
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
   *   The filter values.
   */
  protected function translateFilterValues($reset = FALSE) {
    if (!$reset && static::$filterValues) {
      return static::$filterValues;
    }

    $filter_values = array();
    $filters = $this->translateFilters();
    foreach ($filters as $key => $filter) {
      $filter_values[$key] = $filter['default'];
      // Let the filter defaults be overwritten by parameters in the URL.
      if ($this->getRequest()->query->has($key)) {
        // Only allow this value if it was among the options, or
        // if there were no fixed options to filter for.
        $value = $this->getRequest()->query->get($key);
        if (!isset($filter['options']) || isset($filter['options'][$value])) {
          $filter_values[$key] = $value;
        }
      }
      elseif (isset($_SESSION['locale_translate_filter'][$key])) {
        // Only allow this value if it was among the options, or
        // if there were no fixed options to filter for.
        if (!isset($filter['options']) || isset($filter['options'][$_SESSION['locale_translate_filter'][$key]])) {
          $filter_values[$key] = $_SESSION['locale_translate_filter'][$key];
        }
      }
    }

    return static::$filterValues = $filter_values;
  }

  /**
   * Lists locale translation filters that can be applied.
   */
  protected function translateFilters() {
    $filters = array();

    // Get all languages, except English.
163
    $this->languageManager->reset();
164 165 166 167
    $languages = language_list();
    $language_options = array();
    foreach ($languages as $langcode => $language) {
      if ($langcode != 'en' || locale_translate_english()) {
168
        $language_options[$langcode] = $language->getName();
169 170 171 172
      }
    }

    // Pick the current interface language code for the filter.
173
    $default_langcode = $this->languageManager->getCurrentLanguage()->getId();
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    if (!isset($language_options[$default_langcode])) {
      $available_langcodes = array_keys($language_options);
      $default_langcode = array_shift($available_langcodes);
    }

    $filters['string'] = array(
      'title' => $this->t('String contains'),
      'description' => $this->t('Leave blank to show all strings. The search is case sensitive.'),
      'default' => '',
    );

    $filters['langcode'] = array(
      'title' => $this->t('Translation language'),
      'options' => $language_options,
      'default' => $default_langcode,
    );

    $filters['translation'] = array(
      'title' => $this->t('Search in'),
      'options' => array(
        'all' => $this->t('Both translated and untranslated strings'),
        'translated' => $this->t('Only translated strings'),
        'untranslated' => $this->t('Only untranslated strings'),
      ),
      'default' => 'all',
    );

    $filters['customized'] = array(
      'title' => $this->t('Translation type'),
      'options' => array(
        'all' => $this->t('All'),
        LOCALE_NOT_CUSTOMIZED => $this->t('Non-customized translation'),
        LOCALE_CUSTOMIZED => $this->t('Customized translation'),
      ),
      'states' => array(
        'visible' => array(
          ':input[name=translation]' => array('value' => 'translated'),
        ),
      ),
      'default' => 'all',
    );

    return $filters;
  }

}