You are here

class TMGMTI18nStringDefaultSourceUIController in Translation Management Tool 7

Class TMGMTI18nStringDefaultSourceUIController

UI Controller fo i18n strings translation jobs.

Hierarchy

Expanded class hierarchy of TMGMTI18nStringDefaultSourceUIController

1 string reference to 'TMGMTI18nStringDefaultSourceUIController'
tmgmt_i18n_string_tmgmt_source_plugin_info in sources/i18n_string/tmgmt_i18n_string.module
Implements hook_tmgmt_source_plugin_info().

File

sources/i18n_string/tmgmt_i18n_string.ui.inc, line 13
Provides the I18nString source controller.

View source
class TMGMTI18nStringDefaultSourceUIController extends TMGMTDefaultSourceUIController {

  /**
   * Gets overview form header.
   *
   * @return array
   *   Header array definition as expected by theme_tablesort().
   */
  public function overviewFormHeader() {
    $languages = array();
    foreach (language_list() as $langcode => $language) {
      $langcode = str_replace('-', '', $langcode);
      $languages['langcode-' . $langcode] = array(
        'data' => check_plain($language->name),
      );
    }
    $header = array(
      'title' => array(
        'data' => t('Label (in source language)'),
      ),
      'type' => array(
        'data' => t('Type'),
      ),
    ) + $languages;
    return $header;
  }

  /**
   * {@inheritdoc}
   */
  public function overviewForm($form, &$form_state, $type) {
    $form += $this
      ->overviewSearchFormPart($form, $form_state, $type);
    $form['items'] = array(
      '#type' => 'tableselect',
      '#header' => $this
        ->overviewFormHeader($type),
      '#empty' => t('No strings matching given criteria have been found.'),
    );
    $search_data = $this
      ->getSearchFormSubmittedParams();
    $i18n_strings = tmgmt_i18n_string_get_strings($type, $search_data['label'], $search_data['target_language'], $search_data['target_status']);
    foreach ($this
      ->getTranslationData($i18n_strings, $form_state['item_type']) as $id => $data) {
      $form['items']['#options'][$id] = $this
        ->overviewRow($type, $data);
    }
    $form['pager'] = array(
      '#markup' => theme('pager', array(
        'tags' => NULL,
      )),
    );
    return $form;
  }

  /**
   * Helper function to create translation data list for the sources page list.
   *
   * @param array $i18n_strings
   *   Result of the search query returned by tmgmt_i18n_string_get_strings().
   * @param string $type
   *   I18n object type.
   *
   * @return array
   *   Structured array with translation data.
   */
  protected function getTranslationData($i18n_strings, $type) {
    $objects = array();
    $source_language = variable_get_value('i18n_string_source_language');
    foreach ($i18n_strings as $i18n_string) {
      $wrapper = tmgmt_i18n_string_get_wrapper($type, $i18n_string);
      if ($wrapper instanceof i18n_string_object_wrapper) {
        $id = $i18n_string->job_item_id;

        // Get existing translations and current job items for the entity
        // to determine translation statuses
        $current_job_items = tmgmt_job_item_load_latest('i18n_string', $wrapper
          ->get_type(), $id, $source_language);
        $objects[$id] = array(
          'id' => $id,
          'object' => $wrapper
            ->get_strings(array(
            'empty' => TRUE,
          )),
          'wrapper' => $wrapper,
        );

        // Load entity translation specific data.
        foreach (language_list() as $langcode => $language) {
          $langcode = str_replace('-', '', $langcode);
          $translation_status = 'current';
          if ($langcode == $source_language) {
            $translation_status = 'original';
          }
          elseif ($i18n_string->{'lang_' . $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;
  }

  /**
   * Builds search form for entity sources overview.
   *
   * @param array $form
   *   Drupal form array.
   * @param $form_state
   *   Drupal form_state array.
   * @param $type
   *   Entity type.
   *
   * @return array
   *   Drupal form array.
   */
  public function overviewSearchFormPart($form, &$form_state, $type) {
    $options = array();
    foreach (language_list() as $langcode => $language) {
      $options[$langcode] = $language->name;
    }
    $default_values = $this
      ->getSearchFormSubmittedParams();
    $form['search_wrapper'] = array(
      '#prefix' => '<div class="tmgmt-sources-wrapper tmgmt-i18n_string-sources-wrapper">',
      '#suffix' => '</div>',
      '#weight' => -15,
    );
    $form['search_wrapper']['search'] = array(
      '#tree' => TRUE,
    );
    $form['search_wrapper']['search']['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label in source language'),
      '#default_value' => isset($default_values['label']) ? $default_values['label'] : NULL,
    );

    // Unset the source language as it should not be listed among target
    // languages.
    unset($options[i18n_string_source_language()]);
    $form['search_wrapper']['search']['target_language'] = array(
      '#type' => 'select',
      '#title' => t('Target language'),
      '#options' => $options,
      '#empty_option' => t('Any'),
      '#default_value' => isset($default_values['target_language']) ? $default_values['target_language'] : NULL,
    );
    $form['search_wrapper']['search']['target_status'] = array(
      '#type' => 'select',
      '#title' => t('Target status'),
      '#options' => array(
        'untranslated_or_outdated' => t('Untranslated or outdated'),
        'untranslated' => t('Untranslated'),
        'outdated' => t('Outdated'),
      ),
      '#default_value' => isset($default_values['target_status']) ? $default_values['target_status'] : NULL,
      '#states' => array(
        'invisible' => array(
          ':input[name="search[target_language]"]' => array(
            'value' => '',
          ),
        ),
      ),
    );
    $form['search_wrapper']['search_submit'] = array(
      '#type' => 'submit',
      '#value' => t('Search'),
    );
    return $form;
  }

  /**
   * Gets submitted search params.
   *
   * @return array
   */
  public function getSearchFormSubmittedParams() {
    $params = array(
      'label' => NULL,
      'target_language' => NULL,
      'target_status' => NULL,
    );
    if (isset($_GET['label'])) {
      $params['label'] = $_GET['label'];
    }
    if (isset($_GET['target_language'])) {
      $params['target_language'] = $_GET['target_language'];
    }
    if (isset($_GET['target_status'])) {
      $params['target_status'] = $_GET['target_status'];
    }
    return $params;
  }

  /**
   * Builds a table row for overview form.
   *
   * @param string $type
   *   i18n type.
   * @param array $data
   *   Data needed to build the list row.
   *
   * @return array
   */
  public function overviewRow($type, $data) {

    // Set the default item key, assume it's the first.
    $item_title = reset($data['object']);
    $type_label = i18n_object_info($type, 'title');
    $row = array(
      'id' => $data['id'],
      'title' => $item_title
        ->get_string() ? t('@title (@id)', array(
        '@title' => $item_title
          ->get_string(),
        '@id' => $data['id'],
      )) : $data['id'],
      'type' => empty($type_label) ? t('Unknown') : $type_label,
    );
    foreach (language_list() as $langcode => $language) {
      $langcode = str_replace('-', '', $langcode);
      $row['langcode-' . $langcode] = theme('tmgmt_ui_translation_language_status_single', array(
        'translation_status' => $data['translation_statuses'][$langcode],
        'job_item' => isset($data['current_job_items'][$langcode]) ? $data['current_job_items'][$langcode] : NULL,
      ));
    }
    return $row;
  }

  /**
   * {@inheritdoc}
   */
  public function overviewFormSubmit($form, &$form_state, $type) {

    // Handle search redirect.
    $this
      ->overviewSearchFormRedirect($form, $form_state, $type);
    $items = array_filter($form_state['values']['items']);
    $type = $form_state['item_type'];
    $source_lang = variable_get_value('i18n_string_source_language');

    // Create only single job for all items as the source language is just
    // the same for all.
    $job = tmgmt_job_create($source_lang, NULL, $GLOBALS['user']->uid);

    // Loop through entities and create individual jobs for each source language.
    foreach ($items as $item) {
      $job
        ->addItem('i18n_string', $type, $item);
    }
    $form_state['redirect'] = array(
      'admin/tmgmt/jobs/' . $job->tjid,
      array(
        'query' => array(
          'destination' => current_path(),
        ),
      ),
    );
    drupal_set_message(t('One job needs to be checked out.'));
  }

  /**
   * Performs redirect with search params appended to the uri.
   *
   * In case of triggering element is edit-search-submit it redirects to
   * current location with added query string containing submitted search form
   * values.
   *
   * @param array $form
   *   Drupal form array.
   * @param $form_state
   *   Drupal form_state array.
   * @param $type
   *   Entity type.
   */
  public function overviewSearchFormRedirect($form, &$form_state, $type) {
    if ($form_state['triggering_element']['#id'] == 'edit-search-submit') {
      $query = array();
      foreach ($form_state['values']['search'] as $key => $value) {
        $query[$key] = $value;
      }
      drupal_goto($_GET['q'], array(
        'query' => $query,
      ));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TMGMTDefaultSourceUIController::hook_forms public function Overrides TMGMTSourceUIControllerInterface::hook_forms 1
TMGMTDefaultSourceUIController::hook_menu public function Overrides TMGMTSourceUIControllerInterface::hook_menu 2
TMGMTDefaultSourceUIController::hook_views_default_views public function Overrides TMGMTSourceUIControllerInterface::hook_views_default_views 1
TMGMTDefaultSourceUIController::overviewFormValidate public function 1
TMGMTDefaultSourceUIController::reviewDataItemElement public function Form callback for the data item element form. Overrides TMGMTSourceUIControllerInterface::reviewDataItemElement
TMGMTDefaultSourceUIController::reviewForm public function Form callback for the job item review form. Overrides TMGMTSourceUIControllerInterface::reviewForm
TMGMTDefaultSourceUIController::reviewFormSubmit public function Submit callback for the job item review form. Overrides TMGMTSourceUIControllerInterface::reviewFormSubmit
TMGMTDefaultSourceUIController::reviewFormValidate public function Validation callback for the job item review form. Overrides TMGMTSourceUIControllerInterface::reviewFormValidate
TMGMTI18nStringDefaultSourceUIController::getSearchFormSubmittedParams public function Gets submitted search params.
TMGMTI18nStringDefaultSourceUIController::getTranslationData protected function Helper function to create translation data list for the sources page list.
TMGMTI18nStringDefaultSourceUIController::overviewForm public function Overrides TMGMTDefaultSourceUIController::overviewForm
TMGMTI18nStringDefaultSourceUIController::overviewFormHeader public function Gets overview form header.
TMGMTI18nStringDefaultSourceUIController::overviewFormSubmit public function Overrides TMGMTDefaultSourceUIController::overviewFormSubmit
TMGMTI18nStringDefaultSourceUIController::overviewRow public function Builds a table row for overview form.
TMGMTI18nStringDefaultSourceUIController::overviewSearchFormPart public function Builds search form for entity sources overview.
TMGMTI18nStringDefaultSourceUIController::overviewSearchFormRedirect public function Performs redirect with search params appended to the uri.
TMGMTPluginBase::$pluginInfo protected property
TMGMTPluginBase::$pluginType protected property
TMGMTPluginBase::pluginInfo public function Returns the info of the type of the plugin. Overrides TMGMTPluginBaseInterface::pluginInfo
TMGMTPluginBase::pluginType public function Returns the type of the plugin. Overrides TMGMTPluginBaseInterface::pluginType
TMGMTPluginBase::__construct public function Constructor. Overrides TMGMTPluginBaseInterface::__construct