You are here

protected function SmartlingTranslatorUi::checkoutSettingsAddToJobForm in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 src/SmartlingTranslatorUi.php \Drupal\tmgmt_smartling\SmartlingTranslatorUi::checkoutSettingsAddToJobForm()

Returns "Add to job" from part.

Parameters

\Drupal\tmgmt\TranslatorInterface $translator:

array $statuses:

array $add_to_job_values:

Return value

array

1 call to SmartlingTranslatorUi::checkoutSettingsAddToJobForm()
SmartlingTranslatorUi::checkoutSettingsForm in src/SmartlingTranslatorUi.php
Form callback for the checkout settings form.

File

src/SmartlingTranslatorUi.php, line 613
Contains \Drupal\tmgmt_smartling\SmartlingTranslatorUi.

Class

SmartlingTranslatorUi
Smartling translator UI.

Namespace

Drupal\tmgmt_smartling

Code

protected function checkoutSettingsAddToJobForm(TranslatorInterface $translator, array $statuses, array $add_to_job_values) {
  $available_jobs = $translator
    ->getPlugin()
    ->getApiWrapper($translator
    ->getSettings())
    ->listJobs(NULL, $statuses);
  if (empty($available_jobs['items'])) {
    $form = [
      'job_info' => [
        '#type' => 'fieldset',
        '#title' => 'info',
        'message' => [
          '#markup' => t('There are no available jobs'),
        ],
      ],
    ];
  }
  else {
    $options = [];
    $files = [];
    $project_id = $translator
      ->getSetting('project_id');
    foreach ($available_jobs['items'] as $item) {
      $options[$item['translationJobUid']] = $item['jobName'];
    }

    // Default values by first page load.
    if (empty($add_to_job_values)) {
      $selected_job_id = $available_jobs['items'][0]['translationJobUid'];
    }
    else {

      // Get default values from selected job.
      $selected_job_id = $add_to_job_values['container']['job_id'];
    }
    $selected_job = $translator
      ->getPlugin()
      ->getApiWrapper($translator
      ->getSettings())
      ->getJob($selected_job_id);
    $default_description = $selected_job['description'];
    $default_job_state = ucwords(strtolower(str_replace('_', ' ', $selected_job['jobStatus'])));
    $source_files = $selected_job['sourceFiles'];
    foreach ($source_files as $source_file) {
      $file_name = urlencode($source_file['name']);
      $files[] = Link::fromTextAndUrl($source_file['name'], Url::fromUri("https://dashboard.smartling.com/projects/{$project_id}/files/files.htm", [
        'fragment' => "file/{$file_name}",
        'attributes' => [
          'target' => '_blank',
        ],
      ]))
        ->toString();
    }
    $files_markup = empty($files) ? [
      '#markup' => t('There are no files inside this job'),
    ] : [
      '#theme' => 'item_list',
      '#items' => $files,
    ];
    $form = [
      'container' => [
        '#prefix' => '<div id="smartling-job-form-wrapper">',
        '#suffix' => '</div>',
        '#type' => 'container',
        'job_id' => [
          '#type' => 'select',
          '#title' => t('Job'),
          '#options' => $options,
          '#ajax' => [
            'callback' => 'tmgmt_smartling_checkout_settings_add_to_job_form_ajax_callback',
            'wrapper' => 'smartling-job-form-wrapper',
          ],
        ],
        'job_info' => [
          '#type' => 'details',
          '#open' => TRUE,
          '#title' => t('Info'),
          '#collapsible' => TRUE,
          'dashboard_link' => [
            '#type' => 'item',
            '#markup' => Link::fromTextAndUrl($selected_job['jobName'], Url::fromUri("https://dashboard.smartling.com/app/projects/{$project_id}/jobs/{$selected_job_id}", [
              'attributes' => [
                'target' => '_blank',
              ],
            ]))
              ->toString(),
            '#title' => t('Dashboard link'),
          ],
          'state' => [
            '#type' => 'item',
            '#markup' => $default_job_state,
            '#title' => t('State'),
          ],
          'description' => [
            '#type' => 'textarea',
            '#title' => t('Description'),
            '#value' => $default_description,
          ],
          'due_date' => [
            '#type' => 'datetime',
            '#date_year_range' => date('Y') . ':+5',
            '#default_value' => NULL,
            '#title' => t('Due date'),
            '#date_increment' => 60,
          ],
          'utc_due_date_hidden' => [
            '#type' => 'hidden',
            '#value' => $selected_job['dueDate'],
          ],
          'authorize' => [
            '#type' => 'checkbox',
            '#title' => t('Authorize'),
            '#value' => $translator
              ->getSetting('auto_authorize_locales'),
          ],
          'name' => [
            '#type' => 'value',
            '#value' => $selected_job['jobName'],
          ],
          'files_container' => [
            '#type' => 'details',
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
            '#title' => t('Files'),
            'files' => [
              '#markup' => \Drupal::service('renderer')
                ->render($files_markup),
            ],
          ],
        ],
      ],
    ];
  }
  return $form;
}