You are here

function tmgmt_local_translation_form_element in Translation Management Tool 7

Builds a translation form element.

1 call to tmgmt_local_translation_form_element()
tmgmt_local_translation_form in translators/tmgmt_local/includes/tmgmt_local.pages.inc
Form callback for translating a job item.

File

translators/tmgmt_local/includes/tmgmt_local.pages.inc, line 272
Provides page and forms callbacks.

Code

function tmgmt_local_translation_form_element($data, TMGMTLocalTaskItem $item, &$zebra) {
  static $flip = array(
    'even' => 'odd',
    'odd' => 'even',
  );
  $form = array();
  $job = $item
    ->getJobItem()
    ->getJob();
  $language_list = language_list();
  foreach (element_children($data) as $key) {
    if (isset($data[$key]['#text']) && _tmgmt_filter_data($data[$key])) {

      // The char sequence '][' confuses the form API so we need to replace it.
      $target_key = str_replace('][', '|', $key);
      $zebra = $flip[$zebra];
      $form[$target_key] = array(
        '#tree' => TRUE,
        '#ajaxid' => drupal_html_id('tmgmt-local-element-' . $key),
        '#theme' => 'tmgmt_local_translation_form_element',
        '#parent_label' => $data[$key]['#parent_label'],
        '#zebra' => $zebra,
      );
      $source_language = $language_list[$job->source_language];
      $target_language = $language_list[$job->target_language];
      $form[$target_key]['source'] = array(
        '#type' => 'textarea',
        '#title' => $source_language->name,
        '#value' => $data[$key]['#text'],
        '#disabled' => TRUE,
        '#allow_focus' => TRUE,
      );
      $form[$target_key]['translation'] = array(
        '#type' => 'textarea',
        '#title' => $target_language->name,
        '#default_value' => $item
          ->getData(tmgmt_ensure_keys_array($key), '#text'),
      );
      $form[$target_key]['actions'] = array(
        '#type' => 'container',
      );
      $status = $item
        ->getData(tmgmt_ensure_keys_array($key), '#status');
      $completed = $status == TMGMT_DATA_ITEM_STATE_TRANSLATED;
      if ($completed) {
        $form[$target_key]['actions']['reject-' . $target_key] = array(
          '#type' => 'submit',
          // Unicode character &#x2717 BALLOT X
          '#value' => '✗',
          '#attributes' => array(
            'title' => t('Reject'),
          ),
          '#name' => 'reject-' . $target_key,
          '#submit' => array(
            'tmgmt_local_translation_form_update_state_submit',
          ),
          '#ajax' => array(
            'callback' => 'tmgmt_local_translation_form_update_state_ajax',
            'wrapper' => $form[$target_key]['#ajaxid'],
          ),
          '#tmgmt_local_action' => 'reject',
          '#tmgmt_local_key' => str_replace('][', '|', $key),
        );
      }
      else {
        $form[$target_key]['actions']['finish-' . $target_key] = array(
          '#type' => 'submit',
          // Unicode character &#x2713 CHECK MARK
          '#value' => '✓',
          '#attributes' => array(
            'title' => t('Finish'),
          ),
          '#name' => 'finish-' . $target_key,
          '#submit' => array(
            'tmgmt_local_translation_form_update_state_submit',
          ),
          '#ajax' => array(
            'callback' => 'tmgmt_local_translation_form_update_state_ajax',
            'wrapper' => $form[$target_key]['#ajaxid'],
          ),
          '#tmgmt_local_action' => 'finish',
          '#tmgmt_local_key' => str_replace('][', '|', $key),
        );
      }
    }
  }
  return $form;
}