You are here

function tmgmt_job_form in Translation Management Tool 7

Entity API form the job entity.

File

ui/includes/tmgmt_ui.pages.inc, line 234
Provides page callbacks and form functions for the Translation Management Tool User Interface module.

Code

function tmgmt_job_form($form, &$form_state, TMGMTJob $job, $op = 'edit') {

  // Get the job metadata wrapper so we can display the current status nicely.
  $wrapper = entity_metadata_wrapper('tmgmt_job', $job);

  // Check if the translator entity is completely new or not.
  $old = empty($job->is_new) && $op != 'clone';

  // Handle source language.
  $available['source_language'] = tmgmt_available_languages();
  $job->source_language = isset($form_state['values']['source_language']) ? $form_state['values']['source_language'] : $job->source_language;

  // Handle target language.
  $available['target_language'] = tmgmt_available_languages();
  $job->target_language = isset($form_state['values']['target_language']) ? $form_state['values']['target_language'] : $job->target_language;

  // Remove impossible combinations so we don't end up with the same source and
  // target language in the dropdowns.
  foreach (array(
    'source_language' => 'target_language',
    'target_language' => 'source_language',
  ) as $key => $opposite) {
    if (!empty($job->{$key})) {
      unset($available[$opposite][$job->{$key}]);
    }
  }
  $source = $wrapper->source_language
    ->label();
  if (empty($job->source_language)) {
    $source = '?';
  }
  $target = $wrapper->target_language
    ->label();
  if (empty($job->target_language)) {
    $job->target_language = key($available['target_language']);
    $target = '?';
  }

  // Set the title of the page to the label and the current state of the job.
  drupal_set_title(t('@title (@source to @target, @state)', array(
    '@title' => $job
      ->label(),
    '@source' => $source,
    '@target' => $target,
    '@state' => $wrapper->state
      ->label(),
  )));
  $form['info'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'tmgmt-ui-job-info',
        'clearfix',
      ),
    ),
    '#weight' => 0,
  );

  // Check for label value and set for dynamically change.
  if (isset($form_state['values']['label']) && $form_state['values']['label'] == $job
    ->label()) {
    $job->label = NULL;
    $form_state['values']['label'] = $job->label = $job
      ->label();
  }
  $form['info']['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#description' => t('You can provide a label for this job in order to identify it easily later on. Or leave it empty to use default one.'),
    '#default_value' => $job
      ->label(),
    '#prefix' => '<div id="tmgmt-ui-label">',
    '#suffix' => '</div>',
  );

  // Make the source and target language flexible by showing either a select
  // dropdown or the plain string (if preselected).
  if (!empty($job->source_language) || !$job
    ->isSubmittable()) {
    $form['info']['source_language'] = array(
      '#title' => t('Source language'),
      '#type' => 'item',
      '#markup' => isset($available['source_language'][$job->source_language]) ? $available['source_language'][$job->source_language] : '',
      '#prefix' => '<div id="tmgmt-ui-source-language" class="tmgmt-ui-source-language tmgmt-ui-info-item">',
      '#suffix' => '</div>',
    );
  }
  else {
    $form['info']['source_language'] = array(
      '#title' => t('Source language'),
      '#type' => 'select',
      '#options' => $available['source_language'],
      '#default_value' => $job->source_language,
      '#required' => TRUE,
      '#prefix' => '<div id="tmgmt-ui-source-language" class="tmgmt-ui-source-language tmgmt-ui-info-item">',
      '#suffix' => '</div>',
      '#ajax' => array(
        'callback' => 'tmgmt_ui_ajax_callback_language_select',
      ),
    );
  }
  if (!$job
    ->isSubmittable()) {
    $form['info']['target_language'] = array(
      '#title' => t('Target language'),
      '#type' => 'item',
      '#markup' => isset($available['target_language'][$job->target_language]) ? $available['target_language'][$job->target_language] : '',
      '#prefix' => '<div id="tmgmt-ui-target-language" class="tmgmt-ui-target-language tmgmt-ui-info-item">',
      '#suffix' => '</div>',
    );
  }
  else {
    $form['info']['target_language'] = array(
      '#title' => t('Target language'),
      '#type' => 'select',
      '#options' => $available['target_language'],
      '#default_value' => $job->target_language,
      '#required' => TRUE,
      '#prefix' => '<div id="tmgmt-ui-target-language" class="tmgmt-ui-target-language tmgmt-ui-info-item">',
      '#suffix' => '</div>',
      '#ajax' => array(
        'callback' => 'tmgmt_ui_ajax_callback_language_select',
        'wrapper' => 'tmgmt-ui-target-language',
      ),
    );
  }

  // Display selected translator for already submitted jobs.
  if (!$job
    ->isSubmittable()) {
    $translators = tmgmt_translator_labels();
    $form['info']['translator'] = array(
      '#type' => 'item',
      '#title' => t('Translator'),
      '#markup' => isset($translators[$job->translator]) ? check_plain($translators[$job->translator]) : t('Missing translator'),
      '#prefix' => '<div class="tmgmt-ui-translator tmgmt-ui-info-item">',
      '#suffix' => '</div>',
    );
  }
  $form['info']['word_count'] = array(
    '#type' => 'item',
    '#title' => t('Total word count'),
    '#markup' => number_format($job
      ->getWordCount()),
    '#prefix' => '<div class="tmgmt-ui-word-count tmgmt-ui-info-item">',
    '#suffix' => '</div>',
  );

  // Display created time only for jobs that are not new anymore.
  if (!$job
    ->isUnprocessed()) {
    $form['info']['created'] = array(
      '#type' => 'item',
      '#title' => t('Created'),
      '#markup' => format_date($job->created),
      '#prefix' => '<div class="tmgmt-ui-created tmgmt-ui-info-item">',
      '#suffix' => '</div>',
    );
  }
  if ($view = views_get_view('tmgmt_ui_job_items')) {
    $form['job_items_wrapper'] = array(
      '#type' => 'fieldset',
      '#title' => t('Job items'),
      '#collapsible' => TRUE,
      '#weight' => 10,
      '#prefix' => '<div class="tmgmt-ui-job-checkout-fieldset">',
      '#suffix' => '</div>',
    );

    // Translation jobs.
    $form['job_items_wrapper']['items'] = array(
      '#type' => 'markup',
      '#markup' => $view
        ->preview($job
        ->isSubmittable() ? 'submit' : 'block', array(
        $job->tjid,
      )),
      '#prefix' => '<div class="' . 'tmgmt-ui-job-items ' . ($job
        ->isSubmittable() ? 'tmgmt-ui-job-submit' : 'tmgmt-ui-job-manage') . '">',
      '#attributes' => array(
        'class' => array(
          'tmgmt-ui-job-items',
          $job
            ->isSubmittable() ? 'tmgmt-ui-job-submit' : 'tmgmt-ui-job-manage',
        ),
      ),
      '#suffix' => '</div>',
    );

    // A Wrapper for a button and a table with all suggestions.
    $form['job_items_wrapper']['suggestions'] = array(
      '#type' => 'container',
      '#access' => $job
        ->isSubmittable(),
    );

    // Button to load all translation suggestions with AJAX.
    $form['job_items_wrapper']['suggestions']['load'] = array(
      '#type' => 'submit',
      '#value' => t('Load suggestions'),
      '#submit' => array(
        'tmgmt_ui_ajax_submit_load_suggestions',
      ),
      '#limit_validation_errors' => array(),
      '#attributes' => array(
        'class' => array(
          'tmgmt-ui-job-suggestions-load',
        ),
      ),
      '#ajax' => array(
        'callback' => 'tmgmt_ui_ajax_callback_load_suggestions',
        'wrapper' => 'tmgmt-ui-job-items-suggestions',
        'method' => 'replace',
        'effect' => 'fade',
      ),
    );
    $form['job_items_wrapper']['suggestions']['container'] = array(
      '#type' => 'container',
      '#prefix' => '<div id="tmgmt-ui-job-items-suggestions">',
      '#suffix' => '</div>',
    );

    // Create the suggestions table.
    $suggestions_table = array(
      '#type' => 'tableselect',
      '#header' => array(),
      '#options' => array(),
      '#multiple' => TRUE,
    );

    // If this is an AJAX-Request, load all related nodes and fill the table.
    if ($form_state['rebuild'] && !empty($form_state['rebuild_suggestions'])) {
      _tmgmt_ui_translation_suggestions($suggestions_table, $form_state);

      // A save button on bottom of the table is needed.
      $suggestions_table = array(
        'suggestions_table' => $suggestions_table,
        'suggestions_add' => array(
          '#type' => 'submit',
          '#value' => t('Add suggestions'),
          '#submit' => array(
            'tmgmt_ui_submit_add_suggestions',
          ),
          '#limit_validation_errors' => array(
            array(
              'suggestions_table',
            ),
          ),
          '#attributes' => array(
            'class' => array(
              'tmgmt-ui-job-suggestions-add',
            ),
          ),
          '#access' => !empty($suggestions_table['#options']),
        ),
      );
      $form['job_items_wrapper']['suggestions']['container']['suggestions_list'] = array(
        '#type' => 'fieldset',
        '#title' => t('Suggestions'),
        '#prefix' => '<div id="tmgmt-ui-job-items-suggestions">',
        '#suffix' => '</div>',
      ) + $suggestions_table;
    }
  }

  // Display the checkout settings form if the job can be checked out.
  if ($job
    ->isSubmittable()) {
    $form['translator_wrapper'] = array(
      '#type' => 'fieldset',
      '#title' => t('Configure translator'),
      '#collapsible' => FALSE,
      '#weight' => 20,
    );

    // Show a list of translators tagged by availability for the selected source
    // and target language combination.
    if (!($translators = tmgmt_translator_labels_flagged($job))) {
      drupal_set_message(t('There are no translators available. Before you can checkout you need to !configure at least one translator.', array(
        '!configure' => l(t('configure'), 'admin/config/regional/tmgmt_translator'),
      )), 'warning');
    }
    $preselected_translator = !empty($job->translator) && isset($translators[$job->translator]) ? $job->translator : key($translators);
    $job->translator = isset($form_state['values']['translator']) ? $form_state['values']['translator'] : $preselected_translator;
    $form['translator_wrapper']['translator'] = array(
      '#type' => 'select',
      '#title' => t('Translator'),
      '#description' => t('The configured translator plugin that will process of the translation.'),
      '#options' => $translators,
      '#default_value' => $job->translator,
      '#required' => TRUE,
      '#ajax' => array(
        'callback' => 'tmgmt_ui_ajax_callback_translator_select',
        'wrapper' => 'tmgmt-ui-translator-settings',
      ),
    );
    $settings = tmgmt_ui_checkout_settings_form($form_state, $job);
    if (!is_array($settings)) {
      $settings = array();
    }
    $form['translator_wrapper']['settings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Checkout settings'),
      '#prefix' => '<div id="tmgmt-ui-translator-settings">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
    ) + $settings;
  }
  elseif (!empty($job->translator)) {
    $form['translator_wrapper'] = array(
      '#type' => 'fieldset',
      '#title' => t('Translator information'),
      '#collapsible' => TRUE,
      '#weight' => 20,
    );
    $form['translator_wrapper']['info'] = tmgmt_ui_checkout_info($job);
  }
  if (!$job
    ->isSubmittable() && empty($form['translator_wrapper']['info'])) {
    $form['translator_wrapper']['info'] = array(
      '#type' => 'markup',
      '#markup' => t('The translator does not provide any information.'),
    );
  }
  $form['clearfix'] = array(
    '#markup' => '<div class="clearfix"></div>',
    '#weight' => 45,
  );
  if ($output = tmgmt_ui_embed_view('tmgmt_ui_job_messages', 'block', array(
    $job->tjid,
  ))) {
    $form['messages'] = array(
      '#type' => 'fieldset',
      '#title' => t('Messages'),
      '#collapsible' => TRUE,
      '#weight' => 50,
    );
    $form['messages']['view'] = array(
      '#type' => 'markup',
      '#markup' => $output,
    );
  }

  // Add the buttons and action links.
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save job'),
  );
  if (entity_access('submit', 'tmgmt_job', $job)) {
    $form['actions']['checkout'] = array(
      '#type' => 'submit',
      '#value' => tmgmt_ui_redirect_queue_count() == 0 ? t('Submit to translator') : t('Submit to translator and continue'),
      '#access' => $job
        ->isSubmittable(),
      '#disabled' => empty($job->translator),
    );
    $form['actions']['resubmit_job'] = array(
      '#type' => 'submit',
      '#submit' => array(
        'tmgmt_ui_submit_redirect',
      ),
      '#redirect' => 'admin/tmgmt/jobs/' . $job->tjid . '/resubmit',
      '#value' => t('Resubmit'),
      '#access' => $job
        ->isAborted(),
    );
    $form['actions']['abort_job'] = array(
      '#type' => 'submit',
      '#value' => t('Abort job'),
      '#redirect' => 'admin/tmgmt/jobs/' . $job->tjid . '/abort',
      '#submit' => array(
        'tmgmt_ui_submit_redirect',
      ),
      '#access' => $job
        ->isAbortable(),
    );
  }
  if ($old) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array(
        'tmgmt_ui_submit_redirect',
      ),
      '#redirect' => 'admin/tmgmt/jobs/' . $job->tjid . '/delete',
      // Don't run validations, so the user can always delete the job.
      '#limit_validation_errors' => array(),
      '#access' => user_access('administer tmgmt'),
    );
  }

  // Only show the 'Cancel' button if the job has been submitted to the
  // translator.
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => 'admin/tmgmt/jobs',
    '#weight' => 10,
  );
  $form['#attached']['css'][] = drupal_get_path('module', 'tmgmt_ui') . '/css/tmgmt_ui.admin.css';
  return $form;
}