You are here

function _tmgmt_ui_review_form_element in Translation Management Tool 7

Build form elements for the review form using flatened data items.

@todo Mention in the api documentation that the char '|' is not allowed in field names.

Parameters

array $form_state: Drupal form form_state object.

$data: Flatened array of translation data items.

$job_item: The job item related to this data.

$zebra: String containing either odd or even. This is used to style the each translation item with alternating colors.

$parent_key: The key for $data.

1 call to _tmgmt_ui_review_form_element()
tmgmt_ui_translation_review_form_defaults in ui/tmgmt_ui.module
Form wrapper callback for the job item review form.

File

ui/tmgmt_ui.module, line 714
Common Translation managment UI.

Code

function _tmgmt_ui_review_form_element(&$form_state, $data, TMGMTJobItem $job_item, &$zebra, $parent_key) {
  $flip = array(
    'even' => 'odd',
    'odd' => 'even',
  );
  $form = array(
    '#theme' => 'tmgmt_ui_translator_review_form',
    '#ajaxid' => tmgmt_ui_review_form_element_ajaxid($parent_key),
  );
  foreach (element_children($data) as $key) {

    // The char sequence '][' confuses the form API so we need to replace it.
    $target_key = str_replace('][', '|', $key);
    if (isset($data[$key]['#text']) && _tmgmt_filter_data($data[$key])) {
      $zebra = $flip[$zebra];
      $form[$target_key] = array(
        '#tree' => TRUE,
        '#theme' => 'tmgmt_ui_translator_review_form_element',
        '#parent_label' => $data[$key]['#parent_label'],
        '#zebra' => $zebra,
      );
      $form[$target_key]['status'] = array(
        '#theme' => 'tmgmt_ui_translator_review_form_element_status',
        '#value' => $job_item
          ->isAccepted() ? TMGMT_DATA_ITEM_STATE_ACCEPTED : $data[$key]['#status'],
      );
      $form[$target_key]['actions'] = array(
        '#type' => 'container',
      );

      // Display data item actions unless the job item is accepted or aborted.
      if (!$job_item
        ->isAccepted() && !$job_item
        ->isAborted()) {
        if ($data[$key]['#status'] != TMGMT_DATA_ITEM_STATE_REVIEWED) {
          $form[$target_key]['actions']['reviewed'] = array(
            '#type' => 'submit',
            // Unicode character &#x2713 CHECK MARK
            '#value' => '✓',
            '#attributes' => array(
              'title' => t('Reviewed'),
            ),
            '#name' => 'reviewed-' . $target_key,
            '#submit' => array(
              'tmgmt_ui_translation_review_form_update_state',
              'tmgmt_ui_translation_review_form_submit',
            ),
            '#ajax' => array(
              'callback' => 'tmgmt_ui_translation_review_form_ajax',
              'wrapper' => $form['#ajaxid'],
            ),
          );
        }
        else {
          $form[$target_key]['actions']['unreviewed'] = array(
            '#type' => 'submit',
            // Unicode character &#x2713 CHECK MARK
            '#value' => '✓',
            '#attributes' => array(
              'title' => t('Not reviewed'),
              'class' => array(
                'unreviewed',
              ),
            ),
            '#name' => 'unreviewed-' . $target_key,
            '#submit' => array(
              'tmgmt_ui_translation_review_form_update_state',
              'tmgmt_ui_translation_review_form_submit',
            ),
            '#ajax' => array(
              'callback' => 'tmgmt_ui_translation_review_form_ajax',
              'wrapper' => $form['#ajaxid'],
            ),
          );
        }
        if ($job_item
          ->getTranslatorController() instanceof TMGMTTranslatorRejectDataItem && $data[$key]['#status'] != TMGMT_DATA_ITEM_STATE_PENDING) {
          $form[$target_key]['actions']['reject'] = array(
            '#type' => 'submit',
            // Unicode character &#x2717 BALLOT X
            '#value' => '✗',
            '#attributes' => array(
              'title' => t('Reject'),
            ),
            '#name' => 'reject-' . $target_key,
            '#submit' => array(
              'tmgmt_ui_translation_review_form_update_state',
            ),
          );
        }
        if (!empty($data[$key]['#translation']['#text_revisions'])) {
          $form[$target_key]['actions']['revert'] = array(
            '#type' => 'submit',
            // Unicode character U+21B6 ANTICLOCKWISE TOP SEMICIRCLE ARROW
            '#value' => '↶',
            '#attributes' => array(
              'title' => t('Revert to previous revision'),
            ),
            '#name' => 'revert-' . $target_key,
            '#data_item_key' => $key,
            '#submit' => array(
              'tmgmt_ui_translation_review_form_revert',
            ),
            '#ajax' => array(
              'callback' => 'tmgmt_ui_translation_review_form_ajax',
              'wrapper' => $form['#ajaxid'],
            ),
          );
        }
      }
      if (!empty($data[$key]['#translation']['#text_revisions'])) {
        $revisions = array();
        foreach ($data[$key]['#translation']['#text_revisions'] as $revision) {
          $revisions[] = t('Origin: %origin, Created: %created<br />%text', array(
            '%origin' => $revision['#origin'],
            '%created' => format_date($revision['#timestamp']),
            '%text' => filter_xss($revision['#text']),
          ));
        }
        $form[$target_key]['below']['revisions_wrapper'] = array(
          '#type' => 'fieldset',
          '#title' => t('Translation revisions'),
          '#collapsed' => TRUE,
          '#collapsible' => TRUE,
        );
        $form[$target_key]['below']['revisions_wrapper']['revisions'] = array(
          '#theme' => 'item_list',
          '#items' => $revisions,
        );
      }
      $form[$target_key]['translation'] = array(
        '#type' => 'textarea',
        '#default_value' => isset($data[$key]['#translation']['#text']) ? $data[$key]['#translation']['#text'] : NULL,
        '#title' => t('Translation'),
        '#disabled' => $job_item
          ->isAccepted(),
      );
      $form[$target_key]['source'] = array(
        '#type' => 'textarea',
        '#default_value' => $data[$key]['#text'],
        '#title' => t('Source'),
        '#disabled' => TRUE,
        '#allow_focus' => TRUE,
      );

      // Give the translator ui controller a chance to affect the data item element.
      if ($translator = $job_item
        ->getTranslator()) {
        $form[$target_key] = tmgmt_translator_ui_controller($translator->plugin)
          ->reviewDataItemElement($form[$target_key], $form_state, $key, $parent_key, $data[$key], $job_item);
      }

      // Give the source ui controller a chance to affect the data item element.
      $form[$target_key] = tmgmt_source_ui_controller($job_item->plugin)
        ->reviewDataItemElement($form[$target_key], $form_state, $key, $parent_key, $data[$key], $job_item);
    }
  }
  return $form;
}