You are here

function simplenews_scheduler_submit in Simplenews Scheduler 6.2

Same name and namespace in other branches
  1. 8 simplenews_scheduler.module \simplenews_scheduler_submit()
  2. 7 simplenews_scheduler.module \simplenews_scheduler_submit()
  3. 2.0.x simplenews_scheduler.module \simplenews_scheduler_submit()
1 string reference to 'simplenews_scheduler_submit'
simplenews_scheduler_form_simplenews_node_tab_send_form_alter in ./simplenews_scheduler.module
Implementation of hook_form_FORM_ID_alter().

File

./simplenews_scheduler.module, line 219
Simplenews Scheduler module allows a schedule to be set for sending (and resending) a Simplenews item.

Code

function simplenews_scheduler_submit($form, &$form_state) {
  $scheduler = $form['#simplenews_scheduler'];
  $nid = $form_state['values']['nid'];
  $node = node_load($nid);

  // Get Scheduler values from Simplenews.
  $send = $form_state['values']['simplenews']['send'];

  // Change activation status if necessary.
  switch ($send) {
    case SIMPLENEWS_COMMAND_SEND_TEST:
    case SIMPLENEWS_COMMAND_SEND_NOW:
      $activated = 0;
      break;
    case SIMPLENEWS_COMMAND_SEND_SCHEDULE:
      $activated = 1;
      break;
  }
  $start_date = $form_state['values']['simplenews']['scheduler']['start_date'];
  $stop_date = $form_state['values']['simplenews']['scheduler']['stop_date'];

  // Convert the user time back to GMT time and use that as our record.
  $start_date = date_convert($start_date, DATE_DATETIME, DATE_UNIX, date_default_timezone_name());
  $stop_date = date_convert($stop_date, DATE_DATETIME, DATE_UNIX, date_default_timezone_name());
  $record = array(
    'nid' => $nid,
    'activated' => $activated,
    'send_interval' => $form_state['values']['simplenews']['scheduler']['send_interval'],
    'start_date' => $start_date,
    'stop_type' => $form_state['values']['simplenews']['scheduler']['stop_type'],
    // number of edition
    'stop_date' => $stop_date,
    'stop_edition' => $form_state['values']['simplenews']['scheduler']['stop_edition'],
    'php_eval' => $form_state['values']['simplenews']['scheduler']['php_eval'],
    'title' => $form_state['values']['simplenews']['scheduler']['title'],
  );

  // Merge in values from the existing scheduler, such as last_run and next_run.
  $record += $scheduler;

  // For a new scheduler, add the next_run time.
  if (!isset($scheduler['next_run'])) {
    $record['next_run'] = $start_date;
  }
  db_query("DELETE FROM {simplenews_scheduler} WHERE nid = %d", $nid);
  $result = drupal_write_record('simplenews_scheduler', $record);
  if (!$result) {
    drupal_set_message(t('Saving or updating schedule settings for <em>@title</em> has been unsuccessful.', array(
      '@title' => $node->title,
    )), 'error');
  }
  else {
    drupal_set_message(t('Newsletter Schedule preferences saved'));
  }
}