You are here

function scheduler_form_alter in Scheduler 6

Same name and namespace in other branches
  1. 5 scheduler.module \scheduler_form_alter()
  2. 7 scheduler.module \scheduler_form_alter()
  3. 2.x scheduler.module \scheduler_form_alter()

Implementation of hook_form_alter().

File

./scheduler.module, line 221

Code

function scheduler_form_alter(&$form, $form_state, $form_id) {

  //allow scheduling on a per-node-type basis
  if ('node_type_form' == $form_id) {
    $form['scheduler'] = array(
      '#type' => 'fieldset',
      '#title' => t('Scheduler settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 35,
      '#group' => 'additional_settings',
    );
    $form['scheduler']['publish'] = array(
      '#type' => 'fieldset',
      '#title' => t('Publishing settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 1,
      '#group' => 'additional_settings',
    );
    $form['scheduler']['publish']['scheduler_publish_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable scheduled publishing'),
      '#default_value' => variable_get('scheduler_publish_enable_' . $form['#node_type']->type, 0),
      '#description' => t('Check this box to enable scheduled publishing for this node type.'),
    );
    $form['scheduler']['publish']['scheduler_publish_touch'] = array(
      '#type' => 'checkbox',
      '#title' => t('Alter published on time'),
      '#default_value' => variable_get('scheduler_publish_touch_' . $form['#node_type']->type, 0),
      '#description' => t('Check this box to alter the published on time to match the scheduled time ("touch feature").'),
    );
    $form['scheduler']['publish']['scheduler_publish_required'] = array(
      '#type' => 'checkbox',
      '#title' => t('Publishing date/time is required.'),
      '#default_value' => variable_get('scheduler_publish_required_' . $form['#node_type']->type, 0),
      '#description' => t('Check this box to if scheduled publishing is required (e.g. the user must enter a date/time).'),
    );
    $form['scheduler']['publish']['scheduler_publish_revision'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create a new revision on publishing'),
      '#default_value' => variable_get('scheduler_publish_revision_' . $form['#node_type']->type, 0),
      '#description' => t('Check this box if you want a new revision created when publishing.'),
    );
    $form['scheduler']['unpublish'] = array(
      '#type' => 'fieldset',
      '#title' => t('Unpublishing settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 2,
      '#group' => 'additional_settings',
    );
    $form['scheduler']['unpublish']['scheduler_unpublish_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable scheduled unpublishing'),
      '#default_value' => variable_get('scheduler_unpublish_enable_' . $form['#node_type']->type, 0),
      '#description' => t('Check this box to enable scheduled unpublishing for this node type.'),
    );
    $form['scheduler']['unpublish']['scheduler_unpublish_required'] = array(
      '#type' => 'checkbox',
      '#title' => t('Unpublishing date/time is required.'),
      '#default_value' => variable_get('scheduler_unpublish_required_' . $form['#node_type']->type, 0),
      '#description' => t('Check this box to if scheduled unpublishing is required (e.g. the user must enter a date/time).'),
    );
    $form['scheduler']['unpublish']['scheduler_unpublish_revision'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create a new revision on unpublishing'),
      '#default_value' => variable_get('scheduler_unpublish_revision_' . $form['#node_type']->type, 0),
      '#description' => t('Check this box if you want a new revision created when unpublishing.'),
    );
  }
  elseif (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id) {
    if (user_access('schedule (un)publishing of nodes')) {
      $publishing_enabled = variable_get('scheduler_publish_enable_' . $form['type']['#value'], 0) == 1;
      $unpublishing_enabled = variable_get('scheduler_unpublish_enable_' . $form['type']['#value'], 0) == 1;

      // if scheduling has been enabled for this node type
      if ($publishing_enabled || $unpublishing_enabled) {
        $node = $form['#node'];
        $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
        $use_date_popup = _scheduler_use_date_popup();
        $internal_date_format = $use_date_popup ? SCHEDULER_DATE_FORMAT : $date_format;

        // if this is a preview then get the values from the form, not the db
        if (isset($form_state['values']['op']) && $form_state['values']['op'] == t('Preview')) {
          $defaults = new StdClass();
          $defaults->publish_on = $form_state['values']['publish_on'];
          $defaults->unpublish_on = $form_state['values']['unpublish_on'];
        }
        elseif (isset($node->nid) && $node->nid > 0) {

          // load the values if we are viewing an existing node
          $defaults = db_fetch_object(db_query('SELECT publish_on, unpublish_on FROM {scheduler} WHERE nid = %d', $node->nid));
        }
        else {

          // init standard values
          $defaults = new StdClass();
          $defaults->publish_on = $defaults->unpublish_on = NULL;
        }

        // if there is a text value then convert it to a unix timestamp
        if (isset($defaults->publish_on) && $defaults->publish_on && !is_numeric($defaults->publish_on)) {
          $defaults->publish_on = _scheduler_strtotime($defaults->publish_on);
        }
        if (isset($defaults->unpublish_on) && $defaults->unpublish_on && !is_numeric($defaults->unpublish_on)) {
          $defaults->unpublish_on = _scheduler_strtotime($defaults->unpublish_on);
        }
        $publishing_required = variable_get('scheduler_publish_required_' . $form['type']['#value'], 0) == 1;
        $unpublishing_required = variable_get('scheduler_unpublish_required_' . $form['type']['#value'], 0) == 1;
        $fieldset_extended = isset($defaults->publish_on) && $defaults->publish_on != 0 || isset($defaults->unpublish_on) && $defaults->unpublish_on != 0 || $publishing_required || $unpublishing_required;
        $form['scheduler_settings'] = array(
          '#type' => 'fieldset',
          '#title' => t('Scheduling options'),
          '#collapsible' => TRUE,
          '#collapsed' => !$fieldset_extended,
          '#weight' => 35,
          '#group' => 'additional_settings',
          '#attached' => array(
            'js' => array(
              'vertical-tabs' => drupal_get_path('module', 'scheduler') . "/scheduler_vertical_tabs.js",
            ),
          ),
        );
        $extra_info = variable_get('scheduler_extra_info', '');
        if ($extra_info && $extra_info != '') {
          $form['scheduler_settings']['extra_info'] = array(
            '#type' => 'item',
            '#value' => filter_xss_admin($extra_info),
          );
        }
        $description_format = t('Format: %time.', array(
          '%time' => format_date(time(), 'custom', $date_format),
        ));
        if ($publishing_enabled) {
          $description_blank = '';
          if (!$publishing_required) {
            $description_blank .= ' ' . t('Leave blank to disable scheduled publishing.');
          }
          $form['scheduler_settings']['publish_on'] = array(
            '#type' => 'textfield',
            '#title' => t('Publish on'),
            '#maxlength' => 25,
            '#required' => $publishing_required,
            '#default_value' => isset($defaults->publish_on) && $defaults->publish_on ? format_date($defaults->publish_on, 'custom', $internal_date_format) : '',
            '#description' => $description_format . $description_blank,
          );
        }
        if ($unpublishing_enabled) {
          $description_blank = '';
          if (!$unpublishing_required) {
            $description_blank .= ' ' . t('Leave blank to disable scheduled unpublishing.');
          }
          $form['scheduler_settings']['unpublish_on'] = array(
            '#type' => 'textfield',
            '#title' => t('Unpublish on'),
            '#maxlength' => 25,
            '#required' => $unpublishing_required,
            '#default_value' => isset($defaults->unpublish_on) && $defaults->unpublish_on ? format_date($defaults->unpublish_on, 'custom', $internal_date_format) : '',
            '#description' => $description_format . $description_blank,
          );
        }
        if ($use_date_popup) {

          // Make this a popup calendar widget if Date Popup module is enabled.
          if ($publishing_enabled) {
            $form['scheduler_settings']['publish_on']['#type'] = 'date_popup';
            $form['scheduler_settings']['publish_on']['#date_format'] = $date_format;
            $form['scheduler_settings']['publish_on']['#date_year_range'] = '0:+10';
            if (!$publishing_required) {
              $form['scheduler_settings']['publish_on']['#description'] = t('Leave blank to disable scheduled publishing.');
            }
            unset($form['scheduler_settings']['publish_on']['#maxlength']);
          }
          if ($unpublishing_enabled) {
            $form['scheduler_settings']['unpublish_on']['#type'] = 'date_popup';
            $form['scheduler_settings']['unpublish_on']['#date_format'] = $date_format;
            $form['scheduler_settings']['unpublish_on']['#date_year_range'] = '0:+10';
            if (!$unpublishing_required) {
              $form['scheduler_settings']['unpublish_on']['#description'] = t('Leave blank to disable scheduled unpublishing.');
            }
            unset($form['scheduler_settings']['unpublish_on']['#maxlength']);
          }
        }
      }
    }
  }
}