View source
<?php
namespace Drupal\notify\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
class QueueForm extends ConfigFormBase {
protected $messenger;
public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
parent::__construct($config_factory);
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('messenger'));
}
public function getFormId() {
return 'notify_queue_settings';
}
protected function getEditableConfigNames() {
return [
'notify.settings',
];
}
private function _Count($arg) {
return is_countable($arg) ? count($arg) : 0;
}
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$config = $this
->config('notify.settings');
$period = $config
->get('notify_period');
$since = $config
->get('notify_send_last') - $period;
$lastdate = \Drupal::service('date.formatter')
->format($since, 'short');
if (NULL !== $config
->get('notify_send_start')) {
$start = \Drupal::time()
->getRequestTime();
}
else {
$start = $config
->get('notify_send_start');
}
$start = $config
->get('notify_send_start') ?? 0;
$startdate = \Drupal::service('date.formatter')
->format($start, 'short');
$notify_send_last = $config
->get('notify_send_last');
if (!isset($notify_send_last)) {
$notify_send_last = \Drupal::time()
->getRequestTime();
}
$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;
}
$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' => 19,
'#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' => 'fieldset',
'#title' => $this
->t('Status'),
'#collapsible' => TRUE,
];
list($np, $cp, $nn, $cn, $nu, $cu) = _notify_count($config);
$npcp = $np + $cp;
if ($npcp) {
$queue_msg = $this
->t('Notifications about at least @item queued', [
'@item' => \Drupal::translation()
->formatPlural($npcp, '1 item is', '@count items are'),
]);
}
else {
$queue_msg = $this
->t('No notifications queued');
}
$flagcnt = QueueForm::_Count($config
->get('notify_skip_nodes')) + QueueForm::_Count($config
->get('notify_skip_comments'));
if ($flagcnt) {
$skip_msg = $this
->t('@item flagged for skipping', [
'@item' => \Drupal::translation()
->formatPlural($flagcnt, '1 item is', '@count items are'),
]);
}
else {
$skip_msg = $this
->t('No item is flagged for skipping');
}
if ($np && $nu || $cp && $cu) {
$nonew_msg = '';
}
else {
$nonew_msg = $this
->t(', no notification about unpublished items 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 items');
}
$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);
}
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) {
$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 “Override timestamp” 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 “Override timestamp”, but the timestamp is not altered.'), 'error');
$form_state
->setRebuild();
return;
}
}
$watchdog_level = $config
->get('notify_watchdog') ?? 0;
if (0 == $values['process']) {
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');
}
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'),
]));
}
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']) {
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']) {
$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'));
}
}
}