Version 1
[yaffs-website] / web / core / modules / locale / src / Form / TranslateFormBase.php
1 <?php
2
3 namespace Drupal\locale\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Language\LanguageManagerInterface;
7 use Drupal\locale\StringStorageInterface;
8 use Drupal\Core\State\StateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Defines the locale user interface translation form base.
13  *
14  * Provides methods for searching and filtering strings.
15  */
16 abstract class TranslateFormBase extends FormBase {
17
18   /**
19    * The locale storage.
20    *
21    * @var \Drupal\locale\StringStorageInterface
22    */
23   protected $localeStorage;
24
25   /**
26    * The state store.
27    *
28    * @var \Drupal\Core\State\StateInterface
29    */
30   protected $state;
31
32   /**
33    * The language manager.
34    *
35    * @var \Drupal\Core\Language\LanguageManagerInterface
36    */
37   protected $languageManager;
38
39   /*
40    * Filter values. Shared between objects that inherit this class.
41    *
42    * @var array|null
43    */
44   protected static $filterValues;
45
46   /**
47    * Constructs a new TranslationFormBase object.
48    *
49    * @param \Drupal\locale\StringStorageInterface $locale_storage
50    *   The locale storage.
51    * @param \Drupal\Core\State\StateInterface $state
52    *   The state service.
53    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
54    *   The language manager.
55    */
56   public function __construct(StringStorageInterface $locale_storage, StateInterface $state, LanguageManagerInterface $language_manager) {
57     $this->localeStorage = $locale_storage;
58     $this->state = $state;
59     $this->languageManager = $language_manager;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public static function create(ContainerInterface $container) {
66     return new static(
67       $container->get('locale.storage'),
68       $container->get('state'),
69       $container->get('language_manager')
70     );
71   }
72
73   /**
74    * Builds a string search query and returns an array of string objects.
75    *
76    * @return \Drupal\locale\TranslationString[]
77    *   Array of \Drupal\locale\TranslationString objects.
78    */
79   protected function translateFilterLoadStrings() {
80     $filter_values = $this->translateFilterValues();
81
82     // Language is sanitized to be one of the possible options in
83     // translateFilterValues().
84     $conditions = ['language' => $filter_values['langcode']];
85     $options = ['pager limit' => 30, 'translated' => TRUE, 'untranslated' => TRUE];
86
87     // Add translation status conditions and options.
88     switch ($filter_values['translation']) {
89       case 'translated':
90         $conditions['translated'] = TRUE;
91         if ($filter_values['customized'] != 'all') {
92           $conditions['customized'] = $filter_values['customized'];
93         }
94         break;
95
96       case 'untranslated':
97         $conditions['translated'] = FALSE;
98         break;
99
100     }
101
102     if (!empty($filter_values['string'])) {
103       $options['filters']['source'] = $filter_values['string'];
104       if ($options['translated']) {
105         $options['filters']['translation'] = $filter_values['string'];
106       }
107     }
108
109     return $this->localeStorage->getTranslations($conditions, $options);
110   }
111
112   /**
113    * Builds an array out of search criteria specified in request variables.
114    *
115    * @param bool $reset
116    *   If the list of values should be reset.
117    *
118    * @return array
119    *   The filter values.
120    */
121   protected function translateFilterValues($reset = FALSE) {
122     if (!$reset && static::$filterValues) {
123       return static::$filterValues;
124     }
125
126     $filter_values = [];
127     $filters = $this->translateFilters();
128     foreach ($filters as $key => $filter) {
129       $filter_values[$key] = $filter['default'];
130       // Let the filter defaults be overwritten by parameters in the URL.
131       if ($this->getRequest()->query->has($key)) {
132         // Only allow this value if it was among the options, or
133         // if there were no fixed options to filter for.
134         $value = $this->getRequest()->query->get($key);
135         if (!isset($filter['options']) || isset($filter['options'][$value])) {
136           $filter_values[$key] = $value;
137         }
138       }
139       elseif (isset($_SESSION['locale_translate_filter'][$key])) {
140         // Only allow this value if it was among the options, or
141         // if there were no fixed options to filter for.
142         if (!isset($filter['options']) || isset($filter['options'][$_SESSION['locale_translate_filter'][$key]])) {
143           $filter_values[$key] = $_SESSION['locale_translate_filter'][$key];
144         }
145       }
146     }
147
148     return static::$filterValues = $filter_values;
149   }
150
151   /**
152    * Lists locale translation filters that can be applied.
153    */
154   protected function translateFilters() {
155     $filters = [];
156
157     // Get all languages, except English.
158     $this->languageManager->reset();
159     $languages = $this->languageManager->getLanguages();
160     $language_options = [];
161     foreach ($languages as $langcode => $language) {
162       if (locale_is_translatable($langcode)) {
163         $language_options[$langcode] = $language->getName();
164       }
165     }
166
167     // Pick the current interface language code for the filter.
168     $default_langcode = $this->languageManager->getCurrentLanguage()->getId();
169     if (!isset($language_options[$default_langcode])) {
170       $available_langcodes = array_keys($language_options);
171       $default_langcode = array_shift($available_langcodes);
172     }
173
174     $filters['string'] = [
175       'title' => $this->t('String contains'),
176       'description' => $this->t('Leave blank to show all strings. The search is case sensitive.'),
177       'default' => '',
178     ];
179
180     $filters['langcode'] = [
181       'title' => $this->t('Translation language'),
182       'options' => $language_options,
183       'default' => $default_langcode,
184     ];
185
186     $filters['translation'] = [
187       'title' => $this->t('Search in'),
188       'options' => [
189         'all' => $this->t('Both translated and untranslated strings'),
190         'translated' => $this->t('Only translated strings'),
191         'untranslated' => $this->t('Only untranslated strings'),
192       ],
193       'default' => 'all',
194     ];
195
196     $filters['customized'] = [
197       'title' => $this->t('Translation type'),
198       'options' => [
199         'all' => $this->t('All'),
200         LOCALE_NOT_CUSTOMIZED => $this->t('Non-customized translation'),
201         LOCALE_CUSTOMIZED => $this->t('Customized translation'),
202       ],
203       'states' => [
204         'visible' => [
205           ':input[name=translation]' => ['value' => 'translated'],
206         ],
207       ],
208       'default' => 'all',
209     ];
210
211     return $filters;
212   }
213
214 }