<?php
namespace Drupal\tmgmt_locale;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tmgmt\SourcePluginUiBase;
class LocaleSourcePluginUi extends SourcePluginUiBase {
function getStrings($search_label = NULL, $missing_target_language = NULL, $context = NULL) {
$langcodes = array_keys(\Drupal::languageManager()
->getLanguages());
$languages = array_combine($langcodes, $langcodes);
$select = \Drupal::database()
->select('locales_source', 'ls')
->fields('ls', array(
'lid',
'source',
'context',
));
if (!empty($search_label)) {
$select
->condition('ls.source', '%' . \Drupal::database()
->escapeLike($search_label) . '%', 'LIKE');
}
if (!empty($context)) {
$select
->condition('ls.context', $context);
}
if (!empty($missing_target_language) && in_array($missing_target_language, $languages)) {
$select
->isNull("lt_{$missing_target_language}.language");
}
$langcode_to_filed_alias_map = array();
foreach ($languages as $langcode) {
$table_alias = $select
->leftJoin('locales_target', \Drupal::database()
->escapeTable("lt_{$langcode}"), "ls.lid = %alias.lid AND %alias.language = '{$langcode}'");
$langcode_to_filed_alias_map[$langcode] = $select
->addField($table_alias, 'language');
}
unset($field_alias);
$rows = $select
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit(\Drupal::config('tmgmt.settings')
->get('source_list_limit', 20))
->execute()
->fetchAll();
foreach ($rows as $row) {
foreach ($langcode_to_filed_alias_map as $langcode => $field_alias) {
$row->{$langcode} = $row->{$field_alias};
unset($row->{$field_alias});
}
}
unset($row);
return $rows;
}
public function overviewFormHeader() {
$languages = array();
foreach ($this
->getLanguages() as $langcode => $language) {
$languages['langcode-' . $langcode] = array(
'data' => $language
->getName(),
);
}
$header = array(
'source' => array(
'data' => t('Source text'),
),
'context' => array(
'data' => t('Context'),
),
) + $languages;
return $header;
}
public function overviewForm(array $form, FormStateInterface $form_state, $type) {
$form = parent::overviewForm($form, $form_state, $type);
$search_data = $this
->getSearchFormSubmittedParams();
$form['items']['#empty'] = $this
->t('No strings matching given criteria have been found.');
$strings = $this
->getStrings($search_data['label'], $search_data['missing_target_language'], $search_data['context']);
foreach ($this
->getTranslationData($strings, $type) as $id => $data) {
$form['items']['#options'][$id] = $this
->overviewRow($type, $data);
}
$form['pager'] = array(
'#type' => 'pager',
);
return $form;
}
protected function getTranslationData($strings, $type) {
$objects = array();
$source_language = 'en';
foreach ($strings as $string) {
$id = $string->lid;
$current_job_items = tmgmt_job_item_load_latest('locale', $type, $id, $source_language);
$objects[$id] = array(
'id' => $id,
'object' => $string,
);
foreach ($this
->getLanguages() as $langcode => $language) {
$translation_status = 'current';
if ($langcode == $source_language) {
$translation_status = 'original';
}
elseif ($string->{$langcode} === NULL) {
$translation_status = 'missing';
}
$objects[$id]['current_job_items'][$langcode] = isset($current_job_items[$langcode]) ? $current_job_items[$langcode] : NULL;
$objects[$id]['translation_statuses'][$langcode] = $translation_status;
}
}
return $objects;
}
public function overviewSearchFormPart(array $form, FormStateInterface $form_state, $type) {
$form = parent::overviewSearchFormPart($form, $form_state, $type);
$options = array();
foreach (\Drupal::languageManager()
->getLanguages() as $langcode => $language) {
$options[$langcode] = $language
->getName();
}
$default_values = $this
->getSearchFormSubmittedParams();
$contexts = $this
->getContexts();
$form['search_wrapper']['search']['label'] = array(
'#type' => 'textfield',
'#title' => t('Source text'),
'#default_value' => isset($default_values['label']) ? $default_values['label'] : NULL,
);
unset($options['en']);
$form['search_wrapper']['search']['missing_target_language'] = array(
'#type' => 'select',
'#title' => t('Not translated to'),
'#options' => $options,
'#empty_option' => '--',
'#default_value' => isset($default_values['missing_target_language']) ? $default_values['missing_target_language'] : NULL,
);
$form['search_wrapper']['search']['context'] = [
'#type' => 'select',
'#title' => t('Context'),
'#empty_option' => '--',
'#options' => $contexts,
'#default_value' => isset($default_values['context']) ? $default_values['context'] : NULL,
];
return $form;
}
public function getSearchFormSubmittedParams() {
$params = array(
'label' => NULL,
'missing_target_language' => NULL,
'context' => NULL,
);
if (isset($_GET['label'])) {
$params['label'] = $_GET['label'];
}
if (isset($_GET['missing_target_language'])) {
$params['missing_target_language'] = $_GET['missing_target_language'];
}
if (isset($_GET['context'])) {
$params['context'] = $_GET['context'];
}
return $params;
}
public function getContexts() {
$query = \Drupal::database()
->select('locales_source', 'ls');
$query
->fields('ls', [
'context',
'context',
]);
$query
->distinct();
$query
->orderBy('ls.context');
return $query
->execute()
->fetchAllKeyed();
}
public function overviewRow($type, $data) {
$source = $data['object'];
$row = array(
'id' => $data['id'],
'source' => $source->source,
'context' => $source->context,
);
foreach (\Drupal::languageManager()
->getLanguages() as $langcode => $language) {
$build = $this
->buildTranslationStatus($data['translation_statuses'][$langcode], isset($data['current_job_items'][$langcode]) ? $data['current_job_items'][$langcode] : NULL);
$row['langcode-' . $langcode] = \Drupal::service('renderer')
->render($build);
}
return $row;
}
protected function getLanguages() {
$languages = \Drupal::languageManager()
->getLanguages();
$en = $languages['en'];
unset($languages['en']);
$languages = array(
'en' => $en,
) + $languages;
return $languages;
}
}