You are here

public function LogRescheduleActionForm::submitForm in Log entity 2.x

Form submission handler.

Parameters

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

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

Overrides LogActionFormBase::submitForm

File

src/Form/LogRescheduleActionForm.php, line 123

Class

LogRescheduleActionForm
Provides a log reschedule confirmation form.

Namespace

Drupal\log\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Filter out logs the user doesn't have access to.
  $inaccessible_logs = [];
  $accessible_logs = [];
  $current_user = $this
    ->currentUser();
  foreach ($this->logs as $log) {
    if (!$log
      ->get('timestamp')
      ->access('edit', $current_user) || !$log
      ->get('status')
      ->access('edit', $current_user) || !$log
      ->access('update', $current_user)) {
      $inaccessible_logs[] = $log;
      continue;
    }
    $accessible_logs[] = $log;
  }
  if ($form_state
    ->getValue('confirm') && !empty($accessible_logs)) {
    $count = count($accessible_logs);
    $type_of_date = $form_state
      ->getValue('type_of_date');
    if ($type_of_date) {
      $amount = $form_state
        ->getValue('amount');
      $time = $form_state
        ->getValue('time');
      $sign = $amount >= 0 ? '+' : '';
      foreach ($accessible_logs as $log) {
        $new_date = new DrupalDateTime();
        $new_date
          ->setTimestamp($log
          ->get('timestamp')->value);
        $new_date
          ->modify("{$sign}{$amount} {$time}");
        if ($log
          ->get('status')
          ->first()
          ->isTransitionAllowed('to_pending')) {
          $log
            ->get('status')
            ->first()
            ->applyTransitionById('to_pending');
        }
        $log
          ->set('timestamp', $new_date
          ->getTimestamp());
        $log
          ->setNewRevision(TRUE);
        $log
          ->save();
      }
    }
    else {

      /** @var \Drupal\Core\Datetime\DrupalDateTime $new_date */
      $new_date = $form_state
        ->getValue('date');
      foreach ($accessible_logs as $log) {
        if ($log
          ->get('status')
          ->first()
          ->isTransitionAllowed('to_pending')) {
          $log
            ->get('status')
            ->first()
            ->applyTransitionById('to_pending');
        }
        $log
          ->set('timestamp', $new_date
          ->getTimestamp());
        $log
          ->setNewRevision(TRUE);
        $log
          ->save();
      }
    }
    $this
      ->messenger()
      ->addMessage($this
      ->formatPlural($count, 'Rescheduled 1 log.', 'Rescheduled @count logs.'));
  }

  // Add warning message if there were inaccessible logs.
  if (!empty($inaccessible_logs)) {
    $inaccessible_count = count($inaccessible_logs);
    $this
      ->messenger()
      ->addWarning($this
      ->formatPlural($inaccessible_count, 'Could not reschedule @count log because you do not have the necessary permissions.', 'Could not reschedule @count logs because you do not have the necessary permissions.'));
  }
  parent::submitForm($form, $form_state);
}