You are here

function simplenews_scheduler_submit in Simplenews Scheduler 8

Same name and namespace in other branches
  1. 6.2 simplenews_scheduler.module \simplenews_scheduler_submit()
  2. 7 simplenews_scheduler.module \simplenews_scheduler_submit()
  3. 2.0.x simplenews_scheduler.module \simplenews_scheduler_submit()

Additional submit handler for the node_tab_send_form of simplenews.

1 string reference to 'simplenews_scheduler_submit'
simplenews_scheduler_form_simplenews_node_tab_alter in ./simplenews_scheduler.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function simplenews_scheduler_submit(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $node = $form_state
    ->get('node');
  $nid = $node
    ->id();
  if ($form_state
    ->getValue('enable_scheduler')) {
    $stoptype = $values['stoptype'];
    $start_date = strtotime($values['start_date']);
    $stop_date = $stoptype == 1 ? strtotime($values['stop_date']) : 0;
    $record = array(
      'nid' => $nid,
      'send_interval' => $values['interval'],
      'interval_frequency' => $values['frequency'],
      'start_date' => $start_date,
      'stop_type' => $stoptype,
      'stop_date' => $stop_date,
      'stop_edition' => $values['stop_edition'],
      'title' => $values['title'],
      'activated' => 1,
    );

    // For a new scheduler, add the next_run time.
    if (!isset($values['next_run'])) {
      $record['next_run'] = $start_date;
    }

    // Update scheduler record.
    db_merge('simplenews_scheduler')
      ->key(array(
      'nid' => $nid,
    ))
      ->fields($record)
      ->execute();
  }
  else {

    // The form was submitted with the checkbox unchecked, disable an eventually
    // existing scheduler configuration.
    db_update('simplenews_scheduler')
      ->condition('nid', $nid)
      ->fields([
      'activated' => 0,
    ])
      ->execute();
  }
  \Drupal::entityManager()
    ->getStorage('node')
    ->resetCache([
    $node
      ->id(),
  ]);
  drupal_set_message(t('Newsletter schedule preferences have been saved.'));
}