View source
<?php
namespace Drupal\locale\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TranslationStatusForm extends FormBase {
protected $moduleHandler;
protected $state;
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('state'));
}
public function __construct(ModuleHandlerInterface $module_handler, StateInterface $state) {
$this->moduleHandler = $module_handler;
$this->state = $state;
}
public function getFormId() {
return 'locale_translation_status_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$languages = locale_translatable_language_list();
$status = locale_translation_get_status();
$options = array();
$languages_update = array();
$languages_not_found = array();
$projects_update = array();
if ($languages && $status) {
$updates = $this
->prepareUpdateData($status);
foreach ($updates as $langcode => $update) {
$title = $languages[$langcode]
->getName();
$locale_translation_update_info = array(
'#theme' => 'locale_translation_update_info',
);
foreach (array(
'updates',
'not_found',
) as $update_status) {
if (isset($update[$update_status])) {
$locale_translation_update_info['#' . $update_status] = $update[$update_status];
}
}
$options[$langcode] = array(
'title' => array(
'data' => array(
'#title' => $title,
'#plain_text' => $title,
),
),
'status' => array(
'class' => array(
'description',
'priority-low',
),
'data' => $locale_translation_update_info,
),
);
if (!empty($update['not_found'])) {
$languages_not_found[$langcode] = $langcode;
}
if (!empty($update['updates'])) {
$languages_update[$langcode] = $langcode;
}
}
uasort($options, function ($a, $b) {
return strcasecmp($a['title']['data']['#title'], $b['title']['data']['#title']);
});
$languages_not_found = array_diff($languages_not_found, $languages_update);
}
$last_checked = $this->state
->get('locale.translation_last_checked');
$form['last_checked'] = array(
'#theme' => 'locale_translation_last_check',
'#last' => $last_checked,
);
$header = array(
'title' => array(
'data' => $this
->t('Language'),
'class' => array(
'title',
),
),
'status' => array(
'data' => $this
->t('Status'),
'class' => array(
'status',
'priority-low',
),
),
);
if (!$languages) {
$empty = $this
->t('No translatable languages available. <a href=":add_language">Add a language</a> first.', array(
':add_language' => $this
->url('entity.configurable_language.collection'),
));
}
elseif ($status) {
$empty = $this
->t('All translations up to date.');
}
else {
$empty = $this
->t('No translation status available. <a href=":check">Check manually</a>.', array(
':check' => $this
->url('locale.check_translation'),
));
}
$form['projects_update'] = array(
'#type' => 'value',
'#value' => $projects_update,
);
$form['langcodes'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#default_value' => $languages_update,
'#empty' => $empty,
'#js_select' => TRUE,
'#multiple' => TRUE,
'#required' => TRUE,
'#not_found' => $languages_not_found,
'#after_build' => array(
'locale_translation_language_table',
),
);
$form['#attached']['library'][] = 'locale/drupal.locale.admin';
$form['actions'] = array(
'#type' => 'actions',
);
if ($languages_update) {
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Update translations'),
);
}
return $form;
}
protected function prepareUpdateData(array $status) {
$updates = array();
$this->moduleHandler
->loadInclude('locale', 'compare.inc');
$project_data = locale_translation_build_projects();
foreach ($status as $project_id => $project) {
foreach ($project as $langcode => $project_info) {
if (empty($project_info->type)) {
$updates[$langcode]['not_found'][] = array(
'name' => $project_info->name == 'drupal' ? $this
->t('Drupal core') : $project_data[$project_info->name]->info['name'],
'version' => $project_info->version,
'info' => $this
->createInfoString($project_info),
);
}
elseif ($project_info->type == LOCALE_TRANSLATION_LOCAL || $project_info->type == LOCALE_TRANSLATION_REMOTE) {
$local = isset($project_info->files[LOCALE_TRANSLATION_LOCAL]) ? $project_info->files[LOCALE_TRANSLATION_LOCAL] : NULL;
$remote = isset($project_info->files[LOCALE_TRANSLATION_REMOTE]) ? $project_info->files[LOCALE_TRANSLATION_REMOTE] : NULL;
$recent = _locale_translation_source_compare($local, $remote) == LOCALE_TRANSLATION_SOURCE_COMPARE_LT ? $remote : $local;
$updates[$langcode]['updates'][] = array(
'name' => $project_info->name == 'drupal' ? $this
->t('Drupal core') : $project_data[$project_info->name]->info['name'],
'version' => $project_info->version,
'timestamp' => $recent->timestamp,
);
}
}
}
return $updates;
}
protected function createInfoString($project_info) {
$remote_path = isset($project_info->files['remote']->uri) ? $project_info->files['remote']->uri : FALSE;
$local_path = isset($project_info->files['local']->uri) ? $project_info->files['local']->uri : FALSE;
if (locale_translation_use_remote_source() && $remote_path && $local_path) {
return $this
->t('File not found at %remote_path nor at %local_path', array(
'%remote_path' => $remote_path,
'%local_path' => $local_path,
));
}
elseif ($local_path) {
return $this
->t('File not found at %local_path', array(
'%local_path' => $local_path,
));
}
return $this
->t('Translation file location could not be determined.');
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!array_filter($form_state
->getValue('langcodes'))) {
$form_state
->setErrorByName('', $this
->t('Select a language to update.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->moduleHandler
->loadInclude('locale', 'fetch.inc');
$this->moduleHandler
->loadInclude('locale', 'bulk.inc');
$langcodes = array_filter($form_state
->getValue('langcodes'));
$projects = array_filter($form_state
->getValue('projects_update'));
$options = _locale_translation_default_update_options();
$last_checked = $this->state
->get('locale.translation_last_checked');
if ($last_checked < REQUEST_TIME - LOCALE_TRANSLATION_STATUS_TTL) {
locale_translation_clear_status();
$batch = locale_translation_batch_update_build(array(), $langcodes, $options);
batch_set($batch);
}
else {
$batch = locale_translation_batch_fetch_build($projects, $langcodes, $options);
batch_set($batch);
if ($batch = locale_config_batch_update_components($options, $langcodes)) {
batch_set($batch);
}
}
}
}