You are here

public function DownloadCountExportForm::buildForm in Download Count 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 FormInterface::buildForm

File

src/Form/DownloadCountExportForm.php, line 30

Class

DownloadCountExportForm
Implements the Export form controller.

Namespace

Drupal\download_count\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $download_count_entry = NULL) {
  if ($download_count_entry != NULL) {
    $connection = Database::getConnection();
    $query = $connection
      ->select('download_count', 'dc');
    $query
      ->join('file_managed', 'f', 'dc.fid = f.fid');
    $query
      ->fields('dc', [
      'dcid',
      'fid',
      'uid',
      'type',
      'id',
      'ip_address',
      'referrer',
      'timestamp',
    ]);
    $query
      ->fields('f', [
      'filename',
      'uri',
      'filemime',
      'filesize',
    ]);
    $query
      ->condition('dc.dcid', $download_count_entry);
    $dc_entry = $query
      ->execute()
      ->fetchObject();
  }
  else {
    $dc_entry = 'all';
  }
  $config = $this
    ->config('download_count.settings');
  $form['#attached']['library'][] = 'download_count/export-form-styling';
  if ($dc_entry == 'all') {
    $form['#title'] = $this
      ->t('Download Count Export CSV - All Files');
  }
  else {
    $form['#title'] = $this
      ->t("Download Count Export CSV - '@filename' from '@type' '@id'", [
      '@filename' => $dc_entry->filename,
      '@type' => $dc_entry->type,
      '@id' => $dc_entry->id,
    ]);
  }
  $form['download_count_export_note'] = [
    '#prefix' => '<div id="download-count-export-note">',
    '#suffix' => '</div>',
    '#markup' => Link::fromTextAndUrl($this
      ->t('Back to summary'), Url::fromRoute('download_count.reports', [
      'html' => TRUE,
    ]))
      ->toString() . '<br /><br />' . $this
      ->t('The following data will be exported:') . '<ul><li>' . $this
      ->t('Download count id') . '<li>' . $this
      ->t('File id') . '<li>' . $this
      ->t('File name') . '<li>' . $this
      ->t('File size') . '<li>' . $this
      ->t('Entity type') . '<li>' . $this
      ->t('Entity id') . '<li>' . $this
      ->t('Downloading user id') . '<li>' . $this
      ->t('Downloading username') . '<li>' . $this
      ->t('Downloading user ip address') . '<li>' . $this
      ->t('HTTP referrer') . '<li>' . $this
      ->t('Date - time (YYYY-MM-DD  HH:MM:SS)') . '</ul>',
  ];
  $form['download_count_export_range'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Export Range'),
    '#options' => [
      $this
        ->t('export all data'),
      $this
        ->t('export data for a specified date range'),
    ],
    '#default_value' => $config
      ->get('download_count_export_range') ? 1 : 0,
  ];
  $form['download_count_export_date_range_from'] = [
    '#type' => 'date',
    '#title' => $this
      ->t('Export Range From Date'),
    '#description' => $this
      ->t("This field will be ignored if the Export Range 'export all data' option is selected above."),
  ];
  $form['download_count_export_date_range_to'] = [
    '#type' => 'date',
    '#title' => $this
      ->t('Export Range To Date'),
    '#description' => $this
      ->t("This field will be ignored if the Export Range 'export all data' option is selected above."),
  ];
  $form['download_count_file_info'] = [
    '#type' => 'value',
    '#value' => $dc_entry,
  ];
  $form['download_count_export_submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Export'),
  ];
  $form['download_count_export_cancel'] = [
    '#value' => '<a href="javascript:history.back(-1)">' . $this
      ->t('Cancel') . '</a>',
  ];
  return $form;
}