You are here

function notify_admin_queue_submit in Notify 7

Submit for the notify_admin_queue form.

File

./notify.admin.inc, line 334
Administrative pages callbacks for the Notify module.

Code

function notify_admin_queue_submit($form, &$form_state) {
  unset($form);
  $process = $form_state['values']['process'];
  $notify_send_last = variable_get('notify_send_last', 0);
  $frform_send_last = strtotime($form_state['values']['lastdate']);
  if (FALSE === $frform_send_last) {
    form_set_error('notify_admin_queue', t('This does not look like a valid date format.'));
    return;
  }
  $frform_string = format_date($frform_send_last . 'short');
  if ($process < 2) {
    if ($notify_send_last != $frform_send_last) {
      form_set_error('notify_admin_queue', t('You must select “Override timestamp” to override the timestamp.'));
      return;
    }
  }
  elseif ($process == 2) {
    if ($notify_send_last == $frform_send_last) {
      form_set_error('notify_admin_queue', t('You selected “Override timestamp”, but the timestamp is not altered.'));
      return;
    }
  }
  $watchdog_level = variable_get('notify_watchdog', 0);
  if (0 == $form_state['values']['process']) {

    // flush
    list($num_sent, $num_fail) = _notify_send();
    if ($num_fail > 0) {
      drupal_set_message(t('!sent notification !emsent sent successfully, !fail !emfail could not be sent.', array(
        '!sent' => $num_sent,
        '!emsent' => format_plural($num_sent, 'e-mail', 'e-mails'),
        '!fail' => $num_fail,
        '!emfail' => format_plural($num_fail, 'notification', 'notifications'),
      )), 'error');
      $watchdog_status = WATCHDOG_ERROR;
    }
    elseif ($num_sent > 0) {
      drupal_set_message(t('!count pending notification !emails have been sent in this pass.', array(
        '!count' => $num_sent,
        '!emails' => format_plural($num_sent, 'e-mail', 'e-mails'),
      )));
      $watchdog_status = WATCHDOG_INFO;
    }
    if (0 == $num_sent + $num_fail) {
      drupal_set_message(t('No notifications needed to be sent in this pass.'));
    }
    else {
      if ($watchdog_level <= 1) {
        watchdog('notify', 'Notifications sent: !sent, failures: !fail.', array(
          '!sent' => $num_sent,
          '!fail' => $num_fail,
        ), $watchdog_status);
      }
    }
    $num_sent += variable_get('notify_num_sent', 0);
    $num_fail += variable_get('notify_num_failed', 0);
    variable_set('notify_num_sent', $num_sent);
    variable_set('notify_num_failed', $num_fail);
  }
  elseif (1 == $form_state['values']['process']) {

    // truncate
    list($res_nodes, $res_comms, $res_nopub, $res_copub, $res_nounp, $res_counp) = _notify_select_content();
    foreach ($res_nopub as $row) {
      db_query('DELETE FROM {notify_unpublished_queue} WHERE cid = :cid AND nid = :nid', array(
        ':cid' => 0,
        ':nid' => $row->nid,
      ));
    }
    foreach ($res_copub as $row) {
      db_query('DELETE FROM {notify_unpublished_queue} WHERE cid = :cid AND nid = :nid', array(
        ':cid' => $row->cid,
        ':nid' => $row->nid,
      ));
    }
    variable_set('notify_send_start', REQUEST_TIME);
    variable_set('notify_send_last', REQUEST_TIME);
    variable_set('notify_cron_next', 0);

    // Force reset
    variable_set('notify_users', array());
    drupal_set_message(t('The notification queue has been truncated. No e-mail were sent.'));
    if ($watchdog_level <= 1) {
      watchdog('notify', 'Notification queue truncated.', NULL, WATCHDOG_INFO);
    }
    return;
  }
  elseif (2 == $form_state['values']['process']) {

    // override
    $date = strtotime($form_state['values']['lastdate']);
    variable_set('notify_send_last', $date);
  }
  variable_del('notify_skip_nodes');
  variable_del('notify_skip_comments');
}