You are here

function simplenews_scheduler_form_simplenews_node_tab_send_form_alter in Simplenews Scheduler 6.2

Same name and namespace in other branches
  1. 7 simplenews_scheduler.module \simplenews_scheduler_form_simplenews_node_tab_send_form_alter()

Implementation of hook_form_FORM_ID_alter().

Add schedule settings to the newsletter edit form.

File

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

Code

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

  // if this is an edition, then we should be fiddling with it, only the parent.
  if (user_access('send scheduled newsletters') && !isset($node->simplenews_scheduler_edition)) {
    drupal_add_js(drupal_get_path('module', 'simplenews_scheduler') . '/simplenews_scheduler.js', 'module', 'header', FALSE, FALSE, TRUE);

    // Set the default values.
    $form['#submit'][] = "simplenews_scheduler_submit";
    $scheduler = array();
    $result = db_query("SELECT * FROM {simplenews_scheduler} WHERE nid = %d", arg(1));
    $row = db_fetch_array($result);
    if ($row) {
      $scheduler = $row;
    }
    else {
      $scheduler['activated'] = 0;
    }
    $form['#simplenews_scheduler'] = $scheduler;

    // Add further commands to the 'Send action' radio buttons.
    $form['simplenews']['send']['#options'][SIMPLENEWS_COMMAND_SEND_SCHEDULE] = t('Send newsletter according to schedule');
    $form['simplenews']['send']['#options'][SIMPLENEWS_COMMAND_SEND_NONE] = t("Don't send now or stop sending");
    $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' => FALSE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );

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

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

      // and Today + 2 years for stop, that should be enough.
      $date_stop = isset($scheduler['stop_date']) ? $scheduler['stop_date'] : $_SERVER['REQUEST_TIME'] + 2 * 365 * 24 * 60 * 60;
      $form_date = false;
      if (isset($form['#node'])) {

        // 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);
          $form_date = true;
        }
      }
      if (!$form_date) {
        $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_type'] = array(
        '#type' => 'radios',
        '#title' => t('Stop sending'),
        '#default_value' => isset($scheduler['stop_type']) ? $scheduler['stop_type'] : 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' => isset($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']['php_eval'] = array(
        '#type' => 'textarea',
        '#title' => t('Additionally only create newsletter edition if the following code returns true'),
        '#default_value' => isset($scheduler['php_eval']) ? $scheduler['php_eval'] : '',
        '#required' => FALSE,
        '#description' => t('Additionally evaluate the following PHP code and only issue the newsletter edition if it returns true. Do not include &lt;?php ?&gt; tags.'),
        '#access' => user_access('use PHP for scheduling newsletters'),
      );
      $form['simplenews']['scheduler']['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Title pattern for new edition nodes'),
        '#description' => t('New edition nodes will have their title set to the above string, with tokens replaced.'),
        '#required' => TRUE,
        '#default_value' => isset($scheduler['title']) ? $scheduler['title'] : '[title]',
      );
      $form['simplenews']['scheduler']['token_help'] = array(
        '#title' => t('Replacement patterns'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['simplenews']['scheduler']['token_help']['help'] = array(
        '#value' => theme('token_help', array(
          'node',
          'global',
        )),
      );
      $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;
          }
        }
      }
    }
  }
}