l10n_server-2.x-dev/l10n_packager/src/Form/L10nPackagerForm.php
l10n_packager/src/Form/L10nPackagerForm.php
<?php namespace Drupal\l10n_packager\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\l10n_packager\L10nPackager; /** * Provides a Localization packager form. */ class L10nPackagerForm extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'l10n_packager_l10n_packager'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $projects = \Drupal::entityTypeManager() ->getStorage('l10n_server_project') ->loadMultiple(); $project = reset($projects); $form['projects'] = [ '#type' => 'fieldset', '#title' => t('Project translations'), '#description' => t('Repackage all translations of a single project or release.'), ]; $form['projects']['project'] = [ '#title' => t('Project'), '#required' => TRUE, ]; if (($count = count($projects)) <= 0) { // Radio box widget for as much as 5 projects, select widget for 5-30 // projects. $form['projects']['project']['#type'] = ($count <= 5 ? 'radios' : 'select'); $form['projects']['project']['#options'] = []; foreach ($projects as $project) { // Title used to conform to the autocomplete behavior. $form['projects']['project']['#options'][$project->id()] = $project->label(); } } else { // Autocomplete field for more then 30 projects. $form['projects']['project'] += [ '#type' => 'entity_autocomplete', '#target_type' => 'l10n_server_project', ]; } $form['projects']['release'] = [ '#type' => 'textfield', '#title' => t('Release'), '#description' => t('Optionally select a release name like <em>6.x-1.0-beta1</em> or a partial release name like <em>6.x%</em>.'), ]; $languages = \Drupal::languageManager()->getLanguages(); foreach ($languages as $langcode => $language) { $options[$langcode] = $language->getName(); } $form['projects']['languages'] = [ '#type' => 'select', '#title' => t('Only for these languages'), '#description' => t('Select none for all languages. Otherwise pick the languages you want repackaged.'), '#default_value' => [], '#multiple' => TRUE, '#options' => $options, ]; $form['actions'] = [ '#type' => 'actions', ]; $form['actions']['repackage'] = [ '#type' => 'submit', '#value' => $this->t('Repackage now'), '#name' => 'repackage', ]; $form['actions']['mark'] = [ '#type' => 'submit', '#value' => $this->t('Mark for repackaging'), '#name' => 'mark', ]; return $form; } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { if (!$project_id = $form_state->getValue('project')) { $form_state->setErrorByName('project', t('Invalid project name.')); } if ($release_pattern = $form_state->getValue('release')) { $query = \Drupal::database() ->select('l10n_server_release') ->condition('version', $release_pattern, 'LIKE'); $query ->addExpression('COUNT(rid)'); if (!empty($project_id)) { $query->condition('pid', $project_id, '='); } if (!$query->execute()->fetchField()) { $form_state->setErrorByName('release', t('Invalid release name.')); } } if (!$form_state->getValue('project') && !$form_state->getValue('release')) { $form_state->setErrorByName('project', t('You must select a project and optionally a release.')); } } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); $op = $triggering_element['#name']; $languages = array_filter($form_state->getValue('languages')); $project_id = $form_state->getValue('project'); $release_pattern = $form_state->getValue('release'); // Build the query that will be used in different ways depending on the op. $query = \Drupal::database() ->select('l10n_server_release', 'r') ->fields('r', ['rid']); if ($project_id) { $query = $query->condition('pid', $project_id); } if ($release_pattern) { $query = $query->condition('version', $release_pattern, 'LIKE'); } // Now check results and run operations. if ($op === 'repackage') { $rids = $query->execute()->fetchCol(); if (!empty($rids)) { // @todo Fix batch handling. // $batch = l10n_packager_release_batch($rids, $languages); // batch_set($batch); \Drupal::messenger()->addError(t('@todo Running batch repackaging.')); } else { \Drupal::messenger()->addError(t('No releases found for repackaging.')); } } elseif ($op === 'mark') { \Drupal::messenger()->addError($query); $affected_rows = \Drupal::database() ->update('l10n_packager_release') ->fields([ 'changed' => 0, 'checked' => 0, 'status' => L10nPackager::ACTIVE, ]) ->condition('rid', $query, 'IN') ->execute(); \Drupal::messenger()->addStatus(t("Marked %count releases for repackaging.", ['%count' => $affected_rows])); } } }