You are here

public function LingotekImportForm::buildForm in Lingotek Translation 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/LingotekImportForm.php, line 46
Contains \Drupal\Lingotek\Form\LingotekManagementForm.

Class

LingotekImportForm
@file Contains \Drupal\Lingotek\Form\LingotekManagementForm.

Namespace

Drupal\lingotek\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  define('ITEMS_PER_PAGE', 10);

  // TODO: use get_projects function to list all project names
  $projects = $this
    ->get_projects();
  $count = 0;
  $total = $this
    ->documentsCount();
  $page = pager_default_initialize($total, ITEMS_PER_PAGE);
  $current_page = pager_find_page();
  $args = array(
    'limit' => ITEMS_PER_PAGE,
    'offset' => $current_page * ITEMS_PER_PAGE,
  );
  $this
    ->downloadDocuments($args);
  foreach ($this->docs as $doc) {
    $unix_upload_time = $doc->properties->upload_date / 1000;
    $upload_date_str = gmdate("m/j/Y", $unix_upload_time);
    $project_name = '-';
    if (array_key_exists($doc->properties->project_id, $projects)) {
      $project_name = $projects[$doc->properties->project_id];
    }
    else {
      $project_name = $doc->properties->project_id;
    }
    $this->table_docs[] = array(
      'id' => $count,
      'title' => $doc->properties->title,
      'extension' => $doc->properties->extension,
      'locale' => $doc->entities[0]->properties->language . " - " . $doc->entities[0]->properties->code,
      'upload_date' => $upload_date_str,
      'doc_id' => $doc->properties->id,
      'project_name' => $project_name,
    );
    $count++;
  }
  $header = array(
    'title' => t('Title'),
    'extension' => t('Extension'),
    'locale' => t('Locale'),
    'upload_date' => t('Upload Date'),
    'project_name' => t('Project Name'),
    'doc_id' => t('ID'),
  );
  $options = array();
  foreach ($this->table_docs as $document) {
    $options[$document['id']] = array(
      'title' => $document['title'],
      'extension' => $document['extension'],
      'locale' => $document['locale'],
      'upload_date' => $document['upload_date'],
      'project_name' => $document['project_name'],
      'doc_id' => $document['doc_id'],
    );
  }
  $form['table'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No documents found'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Import'),
  );
  $form['pager'] = array(
    '#type' => 'pager',
  );
  return $form;
}