You are here

node_expire.admin.inc in Node expire 6

Module settings.

File

node_expire.admin.inc
View source
<?php

/**
 * @file
 * Module settings.
 */

/**
 * Configuration form for node_expire
 */
function node_expire_settings_form() {

  // Publishing settings section
  $form['general'] = array(
    '#title' => t('General Settings'),
    '#type' => 'fieldset',
    '#tree' => FALSE,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $expireperiod = drupal_map_assoc(array(
    0,
    1,
    86400,
    172800,
    259200,
    345600,
    432000,
    518400,
    604800,
    691200,
    777600,
    864000,
    950400,
    1036800,
    1123200,
    1209600,
  ), 'format_interval');
  $expireperiod[0] = 'Never';
  $expireperiod[1] = 'Instantly';
  $form['general']['node-expire-unpublishtime'] = array(
    '#title' => t('Timeout for automatic unpublishing'),
    '#description' => t('The duration of time when a node is expired for it to become automatically unpublished. Notice: Unpublished documents won\'t trigger any expiration notices.'),
    '#type' => 'select',
    '#options' => $expireperiod,
    '#default_value' => variable_get('node-expire-unpublishtime', 0),
  );

  // Only allow inheritance for book pages.
  if (module_exists('book')) {
    $form['general']['node-expire-book-props'] = array(
      '#title' => t('Book pages - Inheritance'),
      '#description' => t('Enable inheritance/propagation of expiration times amongst child books.'),
      '#type' => 'select',
      '#options' => array(
        1 => 'Yes',
        0 => 'No',
      ),
      '#default_value' => variable_get('node-expire-book-props', 1),
    );
  }

  // Notification settings section
  $form['notify'] = array(
    '#title' => t('Notifications'),
    '#type' => 'fieldset',
    '#tree' => FALSE,
    '#collapsible' => TRUE,
    '#collapsed' => variable_get('node-expire-enable-email', 1) == 0,
  );
  $form['notify']['node-expire-enable-email'] = array(
    '#title' => t('Enable email notifications'),
    '#description' => t('Whether or not to e-mail out about expired nodes.'),
    '#type' => 'select',
    '#options' => array(
      1 => 'Yes',
      0 => 'No',
    ),
    '#default_value' => variable_get('node-expire-enable-email', 1),
  );
  $period = drupal_map_assoc(array(
    86400,
    172800,
    259200,
    345600,
    432000,
    518400,
    604800,
  ), 'format_interval');
  $form['notify']['node-expire-renotify'] = array(
    '#title' => t('Re-notify user every'),
    '#description' => t('The length of time before the user is renotified of old content.'),
    '#type' => 'select',
    '#options' => $period,
    '#default_value' => variable_get('node-expire-renotify', 259200),
  );
  $form['notify']['node-expire-cc'] = array(
    '#title' => t('Carbon Copy Address'),
    '#description' => t('An e-mail address to carbon copy on all notifications.'),
    '#type' => 'textfield',
    '#size' => 50,
    '#default_value' => variable_get('node-expire-cc', ''),
  );

  // E-mail content settings
  $form['notify']['email'] = array(
    '#title' => t('E-mail Content'),
    '#type' => 'fieldset',
    '#tree' => FALSE,
    '#collapsible' => TRUE,
    '#collapsed' => variable_get('node-expire-enable-email', 1) == 0,
  );
  $form['notify']['email']['node-expire-subject'] = array(
    '#title' => t('Subject'),
    '#description' => t('The subject of the automated alerts. Available variables are: !site'),
    '#type' => 'textfield',
    '#size' => 60,
    '#maxlength' => 180,
    '#default_value' => variable_get('node-expire-subject', '!site - Article Update Needed'),
  );
  $form['notify']['email']['node-expire-body'] = array(
    '#title' => t('Body'),
    '#description' => t('The body of the automated alerts. Available variables are: !username !start_section! !stop_section! !section_article !section_timesinceupdate !section_lastupdate !section_expirydate !section_nodelink !section_editlink'),
    '#type' => 'textarea',
    '#rows' => 11,
    '#cols' => 60,
    '#default_value' => variable_get('node-expire-body', "Hello !username,\r\n\r\nThe following article(s) are in need for reviewing and updating. Please update these at your earliest convenience. If no changes are necessary, simply open the editor and press 'Save'.\r\n\r\n!start_section!\r\nArticle: !section_article\r\nTime since update: !section_timesinceupdate\r\nEdit Link: !section_editlink\r\n\r\n!stop_section!\r\n--  !site team"),
  );

  // Use the Drupal's standard
  $form = system_settings_form($form);
  $form['#validate'][] = 'node_expire_settings_form_validate';
  return $form;
}

/**
 * Implementation of hook_form_validate().
 */
function node_expire_settings_form_validate(&$form, &$form_state) {

  // Only validate the form if we're saving changes. We don't care about values if we're just resetting them anyway.
  if ($form_state['values']['op'] == t('Save configuration')) {

    // Is the CC address valid?
    if ($form_state['values']['node-expire-cc'] and !valid_email_address($form_state['values']['node-expire-cc'])) {
      form_set_error('node-expire-cc', t('The entered carbon copy address is invalid.'));
    }

    // Count instances of !start_section!
    $matches = array();
    preg_match_all('/!start_section!/', $form_state['values']['node-expire-body'], $matches);
    if (count($matches[0]) > 1) {
      form_set_error('node-expire-body', t('The tag "!start_section!" can only be used once.'));
    }

    // Make sure instances of !start_section! match !stop_section!
    $matches2 = array();
    preg_match_all('/!stop_section!/', $form_state['values']['node-expire-body'], $matches2);
    if (count($matches[0]) != count($matches2[0])) {
      form_set_error('node-expire-body', t('The tag "!stop_section!" is missing or doesn\'t match with "!start_section!".'));
    }
  }
}

/**
 * Configuration form for default expirations for node_expire
 */
function node_expire_default_settings_form() {

  // Get current settings
  $curdefaults = variable_get('node-expire-node-visibility', array());
  $period = array(
    '+1 day' => t('1 Day'),
    '+2 days' => t('!number Days', array(
      '!number' => 2,
    )),
    '+3 days' => t('!number Days', array(
      '!number' => 3,
    )),
    '+4 days' => t('!number Days', array(
      '!number' => 4,
    )),
    '+5 days' => t('!number Days', array(
      '!number' => 5,
    )),
    '+6 days' => t('!number Days', array(
      '!number' => 6,
    )),
    '+1 week' => t('1 Week'),
    '+2 weeks' => t('!number Weeks', array(
      '!number' => 2,
    )),
    '+3 weeks' => t('!number Weeks', array(
      '!number' => 3,
    )),
    '+1 month' => t('1 Month'),
    '+2 months' => t('!number Months', array(
      '!number' => 2,
    )),
    '+3 months' => t('!number Months', array(
      '!number' => 3,
    )),
    '+4 months' => t('!number Months', array(
      '!number' => 4,
    )),
    '+5 months' => t('!number Months', array(
      '!number' => 5,
    )),
    '+6 months' => t('!number Months', array(
      '!number' => 6,
    )),
    '+7 months' => t('!number Months', array(
      '!number' => 7,
    )),
    '+8 months' => t('!number Months', array(
      '!number' => 8,
    )),
    '+9 months' => t('!number Months', array(
      '!number' => 9,
    )),
    '+10 months' => t('!number Months', array(
      '!number' => 10,
    )),
    '+11 months' => t('!number Months', array(
      '!number' => 11,
    )),
    '+1 Year' => t('1 Year'),
  );

  // Make the options available for each node type.
  $types = node_get_types();
  $form['node-expire-node-visibility'] = array(
    '#tree' => TRUE,
  );
  foreach ($types as $node) {

    // If we don't already have defaults for this node set, use our own.
    if (!$curdefaults[$node->type]) {
      $curdefaults[$node->type] = array(
        'enabled' => FALSE,
        'expiremode' => NODE_EXPIRE_NONE,
        'expire_timefrom' => '+3 months',
        'expire' => '+3 months',
        'isroot' => FALSE,
      );
    }
    $form['node-expire-node-visibility'][$node->type] = array(
      '#type' => 'fieldset',
      '#title' => $node->name,
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => $curdefaults[$node->type]['enabled'] == FALSE,
      '#description' => $node->module == 'book' ? t('These defaults will only be used when no inheritance is available.') : '',
    );
    $form['node-expire-node-visibility'][$node->type]['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable expiration for this node type.'),
      '#default_value' => $curdefaults[$node->type]['enabled'],
    );
    $form['node-expire-node-visibility'][$node->type]['expiremode'] = array(
      '#title' => t('Expiration Type'),
      '#description' => t('What type of node expiration should this node have?'),
      '#type' => 'select',
      '#options' => array(
        NODE_EXPIRE_NONE => t("Doesn't Expire"),
        NODE_EXPIRE_DATE => t('Expire on Date'),
        NODE_EXPIRE_ONUPDATE => t('Expire After Last Update'),
      ),
      '#default_value' => $curdefaults[$node->type]['expiremode'],
    );
    $form['node-expire-node-visibility'][$node->type]['expire_timefrom'] = array(
      '#title' => t('Expiration Time'),
      '#description' => t('Time after last update to consider the node expired.'),
      '#type' => 'select',
      '#options' => $period,
      '#default_value' => $curdefaults[$node->type]['expire_timefrom'],
    );
    $form['node-expire-node-visibility'][$node->type]['expire'] = array(
      '#title' => t('Expiration Date'),
      '#description' => t('Time/date to consider the node expired. Format: %time or PHP <a href="http://www.php.net/strtotime" target="_blank">strtotime format</a>. Note that if the default date entered is in the past at the node post time, and the end-user doesn\'t have access to edit it, the expiration settings for that node will be removed.', array(
        '%time' => format_date(time(), 'large'),
      )),
      '#type' => 'textfield',
      '#default_value' => $curdefaults[$node->type]['expire'],
      '#attributes' => array(
        'class' => 'jscalendar',
      ),
    );

    // As book pages is the only node type that allows inheritance, only show it there.
    if ($node->module == 'book' and variable_get('node-expire-book-props', 1) == 1) {
      $form['node-expire-node-visibility'][$node->type]['isroot'] = array(
        '#title' => t('Block Inheritance'),
        '#description' => t('Whether or not to block inheritance of the above settings from parent nodes.'),
        '#type' => 'checkbox',
        '#default_value' => $curdefaults[$node->type]['isroot'],
      );
    }
  }

  // Use the Drupal's standard
  $form = system_settings_form($form);
  $form['#validate'][] = 'node_expire_default_settings_form_validate';
  $form['#submit'][] = 'node_expire_default_settings_form_submit';
  return $form;
}

/**
 * Implementation of hook_form_validate().
 */
function node_expire_default_settings_form_validate(&$form, &$form_state) {

  // Only validate the form if we're saving changes. We don't care about values if we're just resetting them anyway.
  if ($form_state['values']['op'] == t('Save configuration')) {

    // The only field we have to check is the expiration date
    foreach ($form_state['values'] as $key => $val) {
      if (is_array($val) and isset($val['expire']) and $val['expiremode'] != NODE_EXPIRE_ONUPDATE) {
        if (($thetime = strtotime($val['expire'])) === FALSE) {
          form_set_error($key . '][expire', t('The entered expiration date is invalid.'));
        }
        elseif ($thetime <= time()) {
          form_set_error($key . '][expire', t('The entered expiration date occurrs in the past.'));
        }
      }
    }
  }
}

/**
 * Implementation of hook_form_submit().
 */
function node_expire_default_settings_form_submit(&$form, &$form_state) {

  // Do we want to reset to the defaults?
  if ($form_state['clicked_button']['#value'] == t('Save configuration')) {
    foreach ($form_state['values'] as $key => $val) {
      if (is_array($val) and isset($val['enabled']) and $val['enabled'] == TRUE) {

        //         $node_visibility[$key] = array(
        //           'enabled'         => TRUE,
        //           'expiremode' => $val['expiremode'],
        //           'expire_timefrom' => $val['expire_timefrom'],
        //           'expire'          => $val['expiremode'] == NODE_EXPIRE_ONUPDATE ? 0 : $val['expire'],
        //           'isroot'          => isset($val['isroot']) ? $val['isroot'] : FALSE,
        //         );
        $node_visibility[] = $key;
        $node_visibility_sql[] = "'%s'";
      }
    }

    // Delete expirations from database if they don't longer pertain to this module
    if (empty($node_visibility)) {
      db_query('DELETE FROM {node_expire}');
    }
    else {
      $query = db_query('SELECT a.nid
        FROM {node_expire} a
        LEFT JOIN {node} b on a.nid = b.nid
        WHERE b.type NOT IN (' . implode(', ', $node_visibility_sql) . ')', $node_visibility);
      while ($row = db_fetch_array($query)) {
        $delete[] = $row['nid'];
      }
      if (!empty($delete)) {

        // Using the normal db_query method, drupal escapes the quotes necessary
        // for the query. This data should be safe anyways since it's pulling
        // from the validated node types.
        db_query('DELETE FROM {node_expire}
          WHERE nid IN(' . implode(', ', $delete) . ')');
      }
    }
  }
}

Functions

Namesort descending Description
node_expire_default_settings_form Configuration form for default expirations for node_expire
node_expire_default_settings_form_submit Implementation of hook_form_submit().
node_expire_default_settings_form_validate Implementation of hook_form_validate().
node_expire_settings_form Configuration form for node_expire
node_expire_settings_form_validate Implementation of hook_form_validate().