You are here

public function AuditFilesNotInDatabase::buildForm in Audit Files 8

Same name and namespace in other branches
  1. 8.3 src/Form/AuditFilesNotInDatabase.php \Drupal\auditfiles\Form\AuditFilesNotInDatabase::buildForm()
  2. 8.2 src/Form/AuditFilesNotInDatabase.php \Drupal\auditfiles\Form\AuditFilesNotInDatabase::buildForm()
  3. 4.x src/Form/AuditFilesNotInDatabase.php \Drupal\auditfiles\Form\AuditFilesNotInDatabase::buildForm()

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/AuditFilesNotInDatabase.php, line 67

Class

AuditFilesNotInDatabase

Namespace

Drupal\auditfiles\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $storage =& $form_state
    ->getStorage();
  if (isset($storage['confirm'])) {
    $values = $form_state
      ->getValue('files');
    $form['changelist'] = [
      '#prefix' => '<ul>',
      '#suffix' => '</ul>',
      '#tree' => TRUE,
    ];

    // Prepare the list of items to present to the user.
    if (!empty($values)) {
      foreach ($values as $filename) {
        if (!empty($filename)) {
          if ($storage['op'] == 'add') {
            $message = $this
              ->t('will be added to the database.');
          }
          elseif ($storage['op'] == 'delete') {
            $message = $this
              ->t('will be deleted from the server.');
          }
          $form['changelist'][$filename] = [
            '#type' => 'hidden',
            '#value' => $filename,
            '#prefix' => '<li><strong>' . $filename . '</strong> ' . $message,
            '#suffix' => "</li>\n",
          ];
        }
        else {
          unset($form_state
            ->getValue('files')[$filename]);
        }
      }
    }
    if ($storage['op'] == 'add') {
      $form['#title'] = $this
        ->t('Add these files to the database?');
    }
    elseif ($storage['op'] == 'delete') {
      $form['#title'] = $this
        ->t('Delete these files from the server?');
    }
    $form['#attributes']['class'][] = 'confirmation';
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->getConfirmText(),
      '#button_type' => 'primary',
      '#submit' => [
        '::confirmSubmissionHandler',
      ],
    ];
    $form['actions']['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this
      ->getRequest());

    // By default, render the form using theme_confirm_form().
    if (!isset($form['#theme'])) {
      $form['#theme'] = 'confirm_form';
    }
    return $form;
  }
  $config = \Drupal::config('auditfiles_config.settings');

  //Get the records to display.

  //Check to see if there is saved data, and if so, use that.
  $rows = \Drupal::service('auditfiles.not_in_database')
    ->_auditfiles_not_in_database_get_reports_files();
  if (!empty($rows)) {

    // Set up the pager.
    $items_per_page = $config
      ->get('auditfiles_report_options_items_per_page') ? $config
      ->get('auditfiles_report_options_items_per_page') : 50;
    if (!empty($items_per_page)) {
      $current_page = pager_default_initialize(count($rows), $items_per_page);

      // Break the total data set into page sized chunks.
      $pages = array_chunk($rows, $items_per_page, TRUE);
    }
  }

  // Define the form.
  // Setup the record count and related messages.
  $maximum_records = $config
    ->get('auditfiles_report_options_maximum_records') ? $config
    ->get('auditfiles_report_options_maximum_records') : 250;

  // $form_count = '';
  if (!empty($rows)) {
    if ($maximum_records > 0) {
      $file_count_message = $this
        ->t('Found at least @count files on the server that are not in the database.');
    }
    else {
      $file_count_message = $this
        ->t('Found @count files on the server that are not in the database.');
    }
    $form_count = $this
      ->formatPlural(count($rows), 'Found 1 file on the server that is not in the database.', $file_count_message);
  }
  else {
    $form_count = $this
      ->t('Found no files on the server that are not in the database.');
  }

  // Create the form table.
  $form['files'] = [
    '#type' => 'tableselect',
    '#header' => \Drupal::service('auditfiles.not_in_database')
      ->_auditfiles_not_in_database_get_header(),
    '#empty' => $this
      ->t('No items found.'),
    '#prefix' => '<div><em>' . $form_count . '</em></div>',
  ];

  // Add the data.
  if (!empty($rows) && !empty($pages)) {
    $form['files']['#options'] = $pages[$current_page];
  }
  elseif (!empty($rows)) {
    $form['files']['#options'] = $rows;
  }
  else {
    $form['files']['#options'] = [];
  }
  if (!empty($rows)) {
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['add'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add selected items to the database'),
      '#submit' => [
        '::submissionHandlerAddRecord',
      ],
    ];
    $form['actions']['markup'] = [
      '#markup' => '&nbsp;' . $this
        ->t('or') . '&nbsp;',
    ];
    $form['actions']['delete'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Delete selected items from the server'),
      '#submit' => [
        '::submissionHandlerDeleteRecord',
      ],
    ];
    $form['pager'] = [
      '#type' => 'pager',
    ];
  }
  return $form;
}