You are here

public function QueueForm::buildForm in Notify 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/QueueForm.php \Drupal\notify\Form\QueueForm::buildForm()
  2. 1.0.x src/Form/QueueForm.php \Drupal\notify\Form\QueueForm::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 ConfigFormBase::buildForm

File

src/Form/QueueForm.php, line 76

Class

QueueForm
Defines a form that configures forms module settings.

Namespace

Drupal\notify\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
  $config = $this
    ->config('notify.settings');
  $notify_send_last = $config
    ->get('notify_send_last');

  // If notify_send_last is not set (fresh install), set it to now.
  if (!$notify_send_last) {
    $notify_send_last = \Drupal::time()
      ->getRequestTime();
    $sendlastdate = \Drupal::service('date.formatter')
      ->format($notify_send_last, 'short');
    $config
      ->set('notify_send_last', $notify_send_last);
    \Drupal::messenger()
      ->addMessage($this
      ->t('Fresh install of <strong>Notify</strong>. Setting “Last notification tmestamp” to @last to avoid sending notifications about old content. You may override the timestamp to pick another time.', [
      '@last' => $sendlastdate,
    ]));
  }
  $period = $config
    ->get('notify_period');
  $since = $notify_send_last - $period;
  $lastdate = \Drupal::service('date.formatter')
    ->format($since, 'short');
  $cron_next = $config
    ->get('notify_cron_next') ?? _notify_cron_next(\Drupal::time()
    ->getRequestTime());

  // Set $start to notify_send_start from configuration.
  $start = $config
    ->get('notify_send_start');
  $startdate = \Drupal::service('date.formatter')
    ->format($start, 'short');
  $next_last = _notify_next_notificaton($notify_send_last);
  if ($next_last == -1) {
    $batch_msg = $this
      ->t('No more notifications scheduled');
  }
  elseif ($next_last == 0) {
    $batch_msg = $this
      ->t('The next notification is scheduled for the next cron run');
  }
  else {
    $next = \Drupal::service('date.formatter')
      ->format($next_last, 'short');
    $batch_msg = $this
      ->t('The next notification is scheduled for the first cron run after @next', [
      '@next' => $next,
    ]);
  }
  $form['process'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Notification queue operations'),
    '#default_value' => 0,
    '#options' => [
      $this
        ->t('Send batch now'),
      $this
        ->t('Truncate queue'),
      $this
        ->t('Override timestamp'),
    ],
    '#description' => $this
      ->t('Select “Send batch now” to send next batch of e-mails queued for notifications. Select “Truncate queue” to empty queue of pending notification <em>without</em> sending e-mails. Select “Override timestamp” to override the last notification timestamp. Press “Submit” to execute.'),
  ];
  $send_last = \Drupal::service('date.formatter')
    ->format($notify_send_last, 'custom', 'Y-m-d H:i:s');
  $form['lastdate'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Last notification timestamp'),
    '#default_value' => $send_last,
    '#size' => 20,
    '#maxlength' => 19,
    '#description' => $this
      ->t('To explicitly set the last notification timestamp, change the value of this field and select the “Override timestamp” option above, then press “Submit” to execute.'),
  ];
  $form['batch'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Status'),
    '#description' => $this
      ->t('<em>The values below shows the status of the</em> <strong>Notify</strong> <em>queue. Please incude these values if you post a Bug report.</em>'),
    // FALSE DEBUG.
    '#open' => TRUE,
  ];
  list($np, $cp, $nn, $cn, $nu, $cu) = _notify_count($config);

  // dpm($np . '+' . $cp, 'Published: np+cp');
  // dpm($nn . '+' . $cn, '??Published: nn+cn');
  // dpm($nu . '+' . $cu, 'Unpublished: nu+cu');.
  $npcp = $np + $cp;
  if ($npcp) {
    $queue_msg = $this
      ->t('Notifications about at least @item queued', [
      '@item' => \Drupal::translation()
        ->formatPlural($npcp, '1 post is', '@count posts are'),
    ]);
  }
  else {
    $queue_msg = $this
      ->t('No notifications queued');
  }
  $flagcnt = $this
    ->_Count($config
    ->get('notify_skip_nodes')) + $this
    ->_Count($config
    ->get('notify_skip_comments'));
  if ($flagcnt) {
    $skip_msg = $this
      ->t('@item flagged for skipping', [
      '@item' => \Drupal::translation()
        ->formatPlural($flagcnt, '1 post is', '@count posts are'),
    ]);
  }
  else {
    $skip_msg = $this
      ->t('No posts are flagged for skipping');
  }
  if ($np && $nu || $cp && $cu) {
    $nonew_msg = '';
  }
  else {
    $nonew_msg = $this
      ->t(', no notification about unpublished posts are queued');
  }
  if ($nu + $cu) {
    $unpub_msg = $this
      ->t('Unpublished: @nodeup and @commup', [
      '@nodeup' => \Drupal::translation()
        ->formatPlural($nu, '1 node', '@count nodes'),
      '@commup' => \Drupal::translation()
        ->formatPlural($cu, '1 comment', '@count comments'),
    ]) . $nonew_msg;
  }
  else {
    $unpub_msg = $this
      ->t('No unpublished posts');
  }
  $sent = $config
    ->get('notify_num_sent');
  $fail = $config
    ->get('notify_num_failed');
  $users = $config
    ->get('notify_users');
  $batch_remain = $users ? count($users) : 0;
  $creat_msg = $this
    ->t('There are @nodes and @comms created', [
    '@nodes' => \Drupal::translation()
      ->formatPlural($np, '1 node', '@count nodes'),
    '@comms' => \Drupal::translation()
      ->formatPlural($cp, '1 comment', '@count comments'),
  ]);
  if ($nn + $cn) {
    $publ_msg = $this
      ->t(', and in addition @noderp and @commrp published,', [
      '@noderp' => \Drupal::translation()
        ->formatPlural($nn, '1 node', '@count nodes'),
      '@commrp' => \Drupal::translation()
        ->formatPlural($cn, '1 comment', '@count comments'),
    ]);
  }
  else {
    $publ_msg = '';
  }
  if ($batch_remain) {
    $intrv_msg = $this
      ->t('between @last and @start', [
      '@last' => $lastdate,
      '@start' => $startdate,
    ]);
    $sent_msg = $this
      ->t('Batch not yet complete.  So far @sent has been sent (@fail, @remain to go)', [
      '@sent' => \Drupal::translation()
        ->formatPlural($sent, '1 e-mail', '@count e-mails'),
      '@fail' => \Drupal::translation()
        ->formatPlural($fail, '1 failure', '@count failures'),
      '@remain' => \Drupal::translation()
        ->formatPlural($batch_remain, '1 user', '@count users'),
    ]);
  }
  else {
    $intrv_msg = $this
      ->t('since @last', [
      '@last' => $lastdate,
    ]);
    $sent_msg = $this
      ->t('Last batch:') . ' ';
    if ($sent == 0) {
      $sent_msg = $this
        ->t('No e-mails were sent');
    }
    else {
      $sent_msg .= $this
        ->t('sent @sent', [
        '@sent' => \Drupal::translation()
          ->formatPlural($sent, '1 e-mail', '@count e-mails'),
      ]);
    }
    if ($fail > 0) {
      $sent_msg .= ', ' . $this
        ->t('@fail', [
        '@fail' => \Drupal::translation()
          ->formatPlural($fail, '1 failure', '@count failures'),
      ]);
    }
    elseif ($sent) {
      $sent_msg .= ', ' . $this
        ->t('no failures');
    }
  }
  $mailsystem = $config
    ->get('mail_system') ?? NULL;
  $ms = isset($mailsystem['default-system']) ? $mailsystem['default-system'] : $this
    ->t('system default');
  $form['batch']['schedule'] = [
    '#markup' => $creat_msg . $publ_msg . ' ' . $intrv_msg . '.<br>' . $unpub_msg . '.<br>' . $queue_msg . '.<br>' . $skip_msg . '.<br>' . $sent_msg . '.<br>' . $batch_msg . '.<br>' . $this
      ->t('Default MailSystem: ') . $ms . '.',
  ];
  return parent::buildForm($form, $form_state);
}