You are here

public function QueueForm::submitForm in Notify 1.0.x

Same name and namespace in other branches
  1. 8 src/Form/QueueForm.php \Drupal\notify\Form\QueueForm::submitForm()
  2. 2.0.x src/Form/QueueForm.php \Drupal\notify\Form\QueueForm::submitForm()

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 ConfigFormBase::submitForm

File

src/Form/QueueForm.php, line 238

Class

QueueForm
Defines a form that configures forms module settings.

Namespace

Drupal\notify\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('notify.settings');
  $values = $form_state
    ->getValues();
  $process = $values['process'];
  $notify_send_last = $config
    ->get('notify_send_last');
  $frform_send_last = strtotime($values['lastdate']);
  if (FALSE === $frform_send_last) {

    //      form_set_error('notify_queue_settings', $this->t('This does not look like a valid date format.'));
    $this->messenger
      ->addMessage($this
      ->t('This does not look like a valid date format.'), 'error');
    $form_state
      ->setRebuild();
    return;
  }
  if ($process < 2) {
    if ($notify_send_last != $frform_send_last) {
      $this->messenger
        ->addMessage($this
        ->t('You must select &#8220;Override timestamp&#8221; to override the timestamp.'), 'error');
      $form_state
        ->setRebuild();
      return;
    }
  }
  elseif ($process == 2) {
    if ($notify_send_last == $frform_send_last) {
      $this->messenger
        ->addMessage($this
        ->t('You selected &#8220;Override timestamp&#8221;, but the timestamp is not altered.'), 'error');
      $form_state
        ->setRebuild();
      return;
    }
  }
  $watchdog_level = $config
    ->get('notify_watchdog') ?? 0;
  if (0 == $values['process']) {

    // flush
    list($num_sent, $num_fail) = _notify_send();
    if ($num_fail > 0) {
      $this->messenger
        ->addMessage($this
        ->t('@sent notification @emsent sent successfully, @fail @emfail could not be sent.', [
        '@sent' => $num_sent,
        '@emsent' => \Drupal::translation()
          ->formatPlural($num_sent, 'e-mail', 'e-mails'),
        '@fail' => $num_fail,
        '@emfail' => \Drupal::translation()
          ->formatPlural($num_fail, 'notification', 'notifications'),
      ]), 'error');

      //        $watchdog_status = WATCHDOG_ERROR;
    }
    elseif ($num_sent > 0) {
      $this->messenger
        ->addMessage($this
        ->t('@count pending notification @emails have been sent in this pass.', [
        '@count' => $num_sent,
        '@emails' => \Drupal::translation()
          ->formatPlural($num_sent, 'e-mail', 'e-mails'),
      ]));

      //        $watchdog_status = WATCHDOG_INFO;
    }
    if (0 == $num_sent + $num_fail) {
      $this->messenger
        ->addMessage($this
        ->t('No notifications needed to be sent in this pass.'));
    }
    else {
      if ($watchdog_level <= 1) {
        \Drupal::logger('notify')
          ->notice('Notifications sent: @sent, failures: @fail.', [
          '@sent' => $num_sent,
          '@fail' => $num_fail,
        ]);
      }
    }
    $num_sent += $config
      ->get('notify_num_sent');
    $num_fail += $config
      ->get('notify_num_failed');
    \Drupal::configFactory()
      ->getEditable('notify.settings')
      ->set('notify_num_sent', $num_sent)
      ->set('notify_num_failed', $num_fail)
      ->set('notify_skip_nodes', [])
      ->set('notify_skip_comments', [])
      ->save();
  }
  elseif (1 == $values['process']) {

    // truncate
    list($res_nodes, $res_comms, $res_nopub, $res_copub, $res_nounp, $res_counp) = _notify_select_content();
    foreach ($res_nopub as $row) {
      $q = \Drupal::database()
        ->delete('notify_unpublished_queue', 'n');
      $q
        ->condition('n.cid', 0);
      $q
        ->condition('n.nid', $row->nid);
      $q
        ->execute();
    }
    foreach ($res_copub as $row) {
      $q = \Drupal::database()
        ->delete('notify_unpublished_queue', 'n');
      $q
        ->condition('n.cid', $row->cid);
      $q
        ->condition('n.nid', $row->nid);
      $q
        ->execute();
    }
    \Drupal::configFactory()
      ->getEditable('notify.settings')
      ->set('notify_send_start', \Drupal::time()
      ->getRequestTime())
      ->set('notify_send_last', \Drupal::time()
      ->getRequestTime())
      ->set('notify_cron_next', 0)
      ->set('notify_users', [])
      ->set('notify_skip_nodes', [])
      ->set('notify_skip_comments', [])
      ->save();
    $this->messenger
      ->addMessage($this
      ->t('The notification queue has been truncated. No e-mail were sent.'));
    if ($watchdog_level <= 1) {
      \Drupal::logger('notify')
        ->notice('Notification queue truncated.');
    }
    return;
  }
  elseif (2 == $values['process']) {

    // override
    $last_date = strtotime($values['lastdate']);
    \Drupal::configFactory()
      ->getEditable('notify.settings')
      ->set('notify_send_last', $last_date)
      ->set('notify_skip_nodes', [])
      ->set('notify_skip_comments', [])
      ->save();
    $this->messenger
      ->addMessage($this
      ->t('Timestamp overridden'));
  }
}