You are here

function simplenews_scheduler_form_alter in Simplenews Scheduler 6

Same name and namespace in other branches
  1. 5 simplenews_scheduler.module \simplenews_scheduler_form_alter()

Implementation of hook_form_alter().

File

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

Code

function simplenews_scheduler_form_alter(&$form, &$form_state, $form_id) {
  global $user;

  // Add schedule settings to the newsletter edit form.
  if (isset($form['#node']) && in_array($form['#node']->type, variable_get('simplenews_content_types', array(
    'simplenews',
  ))) && user_access('send scheduled newsletters')) {
    drupal_add_js(drupal_get_path('module', 'simplenews_scheduler') . '/simplenews_scheduler.js', 'module', 'header', FALSE, FALSE, TRUE);

    // Set the default values.
    if (isset($form_state['node'])) {
      $scheduler = $form_state['node']['simplenews']['scheduler'];
    }
    else {
      $scheduler = $form['#node']->simplenews_scheduler;
    }
    $form['simplenews']['send']['#options'][SIMPLENEWS_COMMAND_SEND_NONE] = t("Don't send now or stop sending");
    $form['simplenews']['send']['#options'][SIMPLENEWS_COMMAND_SEND_SCHEDULE] = t('Send newsletter according to schedule');
    $form['simplenews']['send']['#default_value'] = $scheduler['activated'] == 1 ? SIMPLENEWS_COMMAND_SEND_SCHEDULE : variable_get('simplenews_send', SIMPLENEWS_COMMAND_SEND_NONE);
    $form['simplenews']['scheduler'] = array(
      '#type' => 'fieldset',
      '#title' => t('Schedule details'),
      '#attributes' => array(
        'class' => 'schedule_info',
      ),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );

    // Display settings only if this is not an edition.
    if (!isset($form['#node']->simplenews_scheduler_edition)) {
      $form['simplenews']['scheduler']['interval'] = array(
        '#type' => 'select',
        '#title' => t('Send once per'),
        '#options' => array(
          'day' => t('Day'),
          'week' => t('Week'),
          'month' => t('Month'),
        ),
        '#description' => t('Interval to send at'),
        '#default_value' => $scheduler['interval'],
      );

      // If there is no default value, use the current time for start.
      $date_start = $scheduler['start_date'] ? $scheduler['start_date'] : time();

      // and Mon, 30 Dec 2013 23:59:59 GMT for stop, that should be enough.
      $date_stop = $scheduler['stop_date'] ? $scheduler['stop_date'] : 1388447999;

      // Convert dates to valid date objects.
      if ($form['#node']->build_mode == 1) {
        $date_start = date_make_date($date_start, NULL, DATE_DATETIME);
        $date_stop = date_make_date($date_stop, NULL, DATE_DATETIME);
      }
      else {
        $date_start = date_make_date($date_start, date_default_timezone_name(), DATE_UNIX);
        $date_stop = date_make_date($date_stop, date_default_timezone_name(), DATE_UNIX);
      }

      // Translate formatted date results.
      $date_str_start = date_format_date($date_start, 'custom', 'Y-m-d H:i');
      $date_str_stop = date_format_date($date_stop, 'custom', 'Y-m-d H:i');
      $form['simplenews']['scheduler']['start_date'] = array(
        '#type' => 'date_select',
        '#title' => t('Start sending on'),
        '#default_value' => $date_str_start,
        '#date_type' => DATE_DATETIME,
        '#date_format' => 'm-d-Y - H:i',
        '#date_timezone' => date_default_timezone_name(),
        '#date_label_position' => 'none',
        '#date_increment' => 1,
        '#date_year_range' => '0:+3',
        '#required' => TRUE,
        '#description' => t('Intervals work by creating a new node at the
                                 desired time and marking this to be sent, ensure
                                 you have your <a href="@site">site timezones</a>
                                 configured and <a href="@user">user timezone</a>
                                 configured.', array(
          '@site' => url('admin/settings/date-time'),
          '@user' => url('user/' . $user->uid . '/edit'),
        )),
      );
      $form['simplenews']['scheduler']['stop'] = array(
        '#type' => 'radios',
        '#title' => t('Stop sending'),
        '#default_value' => $scheduler['stop'] ? $scheduler['stop'] : 0,
        '#options' => array(
          t('Never'),
          t('On a given date'),
          t('After a maximum number of editions'),
        ),
        '#attributes' => array(
          'class' => 'simplenews-command-stop',
        ),
      );
      $form['simplenews']['scheduler']['stop_date'] = array(
        '#type' => 'date_select',
        '#default_value' => $date_str_stop,
        '#date_type' => DATE_DATETIME,
        '#date_format' => 'm-d-Y - H:i',
        '#date_timezone' => date_default_timezone_name(),
        '#date_label_position' => 'none',
        '#date_increment' => 1,
        '#date_year_range' => '2010:+3',
        '#required' => TRUE,
      );
      $form['simplenews']['scheduler']['stop_edition'] = array(
        '#type' => 'textfield',
        '#default_value' => $scheduler['stop_edition'] ? $scheduler['stop_edition'] : 0,
        '#size' => 5,
        '#maxlength' => 5,
        '#required' => TRUE,
        '#description' => t('The maximum number of editions which should be sent.'),
      );
      $form['simplenews']['scheduler']['activated'] = array(
        '#type' => 'hidden',
        '#value' => $scheduler['activated'],
      );
    }
    else {

      // This is a newsletter edition.
      $title .= t('This node is part of a scheduled newsletter configuration. View the original newsletter <a href="@parent">here</a>.', array(
        '@parent' => url('node/' . $form['#node']->simplenews_scheduler_edition['pid']),
      ));
      $form['simplenews']['none']['#title'] = $title;

      // If the node has attachments
      if (count($form['attachments']['wrapper']['files']) && user_access('upload files')) {

        // Disable all attachment form elements and the delete button to avoid the deletion of the parent's attachments.
        $form['attachments']['#description'] = t('Attachments cannot be changed, this is a newsletter edition created by Simplenews Scheduler.');
        $form['attachments']['wrapper']['new']['upload']['#disabled'] = TRUE;
        $form['attachments']['wrapper']['new']['attach']['#disabled'] = TRUE;

        // Disable every file element.
        foreach ($form['attachments']['wrapper']['files'] as $key => $file) {
          if (is_numeric($key)) {
            $form['attachments']['wrapper']['files'][$key]['description']['#disabled'] = TRUE;
            $form['attachments']['wrapper']['files'][$key]['remove']['#disabled'] = TRUE;
            $form['attachments']['wrapper']['files'][$key]['list']['#disabled'] = TRUE;
            $form['attachments']['wrapper']['files'][$key]['weight']['#disabled'] = TRUE;
            $form['buttons']['delete']['#disabled'] = TRUE;
          }
        }
      }
    }
  }
}