You are here

node_expire.module in Node expire 6

File

node_expire.module
View source
<?php

/**
 * @file
 * Allows Alerts administrators of possibly outdated materials
 * and optionally unpublishes them.
 */
DEFINE('NODE_EXPIRE_NONE', 0);
DEFINE('NODE_EXPIRE_ONUPDATE', 1);
DEFINE('NODE_EXPIRE_DATE', 2);
DEFINE('NODE_EXPIRE_FORMAT', 'Y-m-d H:i:s');

/**
 * Implementation of hook_cron().
 */
function node_expire_cron() {
  module_load_include('cron.inc', 'node_expire');
  _node_expire_cron();
}

/**
 * Implementation of hook_form_alter().
 *
 * Add expiration options to the node entry forms
 */
function node_expire_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type']) and $form['type']['#value'] . '_node_form' == $form_id) {

    // Check if the Node Expire feature is enabled for the node type
    $node = isset($form['#node']) ? $form['#node'] : NULL;
    $curdefaults = variable_get('node-expire-node-visibility', array());
    if (empty($curdefaults[$node->type])) {
      return;
    }
    if (user_access('edit expirations') and $node->type != 'book') {
      $form['expiration'] = array(
        '#title' => t('Expiration'),
        '#type' => 'fieldset',
        '#tree' => FALSE,
        '#collapsible' => TRUE,
        '#collapsed' => $node->expiremode == NODE_EXPIRE_NONE,
      );
      $form['expiration']['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' => $node->expiremode,
      );
      $form['expiration']['expire_timefrom'] = array(
        '#title' => t('Expiration Time'),
        '#description' => t('Time after last update to consider the node expired.'),
        '#type' => 'select',
        '#options' => 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'),
        ),
        '#default_value' => $node->expire_timefrom,
      );
      if (!is_numeric($node->expire)) {
        $node->expire = strtotime($node->expire);
      }
      $form['expiration']['expire'] = array(
        '#title' => t('Expiration Date'),
        '#description' => t('Time date to consider the node expired. Format: %time.', array(
          '%time' => format_date($node->expire, 'custom', NODE_EXPIRE_FORMAT),
        )),
        '#type' => 'textfield',
        '#maxlength' => 25,
        '#default_value' => format_date($node->expire, 'custom', NODE_EXPIRE_FORMAT),
        '#attributes' => array(
          'class' => 'jscalendar',
        ),
      );

      // As book pages is the only node type that allows inheritance, only show it there.
      if ($form['type']['#value'] == 'book' and variable_get('node-expire-book-props', 1) == 1) {
        $form['expiration']['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' => $node->isroot,
        );
      }
    }
    else {
      $form['expiration']['expiremode'] = array(
        '#type' => 'value',
        '#value' => $node->expiremode,
      );
      $form['expiration']['expire_timefrom'] = array(
        '#type' => 'value',
        '#value' => $node->expire_timefrom,
      );
      $form['expiration']['expire'] = array(
        '#type' => 'value',
        '#value' => $node->expire,
      );
      if ($form['type']['#value'] == 'book' and variable_get('node-expire-book-props', 1) == 1) {
        $form['expiration']['isroot'] = array(
          '#type' => 'value',
          '#value' => $node->isroot,
        );
      }
    }
    $form['expiration']['node_expire'] = array(
      '#type' => 'value',
      '#value' => $node->node_expire,
    );
  }
}

/**
 * Implementation of hook_mail().
 */
function node_expire_mail($key, &$message, $params) {
  $message['subject'] = $params['subject'];
  $message['body'][] = $params['body'];
  $message['headers'] = array_merge($message['headers'], $params['headers']);
}

/**
 * Implementation of hook_menu().
 */
function node_expire_menu() {
  $items['admin/settings/node_expire'] = array(
    'access arguments' => array(
      'administer node_expire',
    ),
    'description' => 'Configure Node Expire',
    'file' => 'node_expire.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_expire_settings_form',
    ),
    'title' => 'Node Expire',
  );
  $items['admin/settings/node_expire/settings'] = array(
    'access arguments' => array(
      'administer node_expire',
    ),
    'file' => 'node_expire.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_expire_settings_form',
    ),
    'title' => 'Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/settings/node_expire/defaults'] = array(
    'access arguments' => array(
      'administer node_expire',
    ),
    'file' => 'node_expire.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'node_expire_default_settings_form',
    ),
    'title' => 'Defaults',
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );

  // If pages are set to expire instantly, this page is of absolutely no use.
  // Let's just hide it.
  $items['admin/content/node/outdated'] = array(
    'access arguments' => array(
      'administer nodes',
    ),
    'file' => 'node_expire.inc',
    'page callback' => 'node_expire_outdated_nodes',
    'type' => MENU_LOCAL_TASK,
    'title' => 'Outdated Documents',
    'weight' => 10,
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function node_expire_perm() {
  return array(
    'administer node_expire',
    'edit expirations',
  );
}

/**
 * Prepare and parse the data from our node entry forms.
 *
 * @todo Propegate to child nodes.
 */
function node_expire_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  // Check if the Node Expire feature is enabled for the node type
  $curdefaults = variable_get('node-expire-node-visibility', array());
  if (empty($curdefaults[$node->type])) {
    return;
  }
  switch ($op) {
    case 'load':
      $query = db_query('SELECT expire, expire_timefrom, expiremode, isroot
        FROM {node_expire} WHERE nid = %d', $node->nid);

      // Use the existing expiration data if present.
      if ($row = db_fetch_object($query)) {
        $node->expire = $row->expire;
        $node->expiremode = $row->expiremode;
        $node->expire_timefrom = $row->expire_timefrom;
        $node->isroot = $row->isroot;
        $node->node_expire = 'nid';
      }
      break;
    case 'prepare':
      $curdefaults = $curdefaults[$node->type];

      // Use the existing expiration data if present.
      if (empty($node->node_expire)) {

        // If this is a new book page, and inheritance is enabled, lets inherit the data.
        if ($node->type == 'book' and variable_get('node-expire-book-props', 1) and arg(4) > 0 and $query = db_query('SELECT expire, expire_timefrom, expiremode FROM {node_expire} WHERE nid = %d', arg(4))) {

          // If, for whatever reason, no parent data is available, use the defaults
          $row = db_fetch_object($query);
          $node->expire = $row->expire;
          $node->expire_timefrom = $row->expire_timefrom;
          $node->expiremode = $row->expiremode;
          $node->isroot = 0;
        }
        else {
          $node->expire_timefrom = $curdefaults['expire_timefrom'];
          $node->isroot = $curdefaults['isroot'];
          $node->expiremode = $curdefaults['expiremode'];
          if ($curdefaults['expiremode'] == NODE_EXPIRE_ONUPDATE) {
            $node->expire = format_date(strtotime($curdefaults['expire_timefrom']), 'custom', NODE_EXPIRE_FORMAT);
          }
          else {
            $node->expire = format_date(strtotime($curdefaults['expire']), 'custom', NODE_EXPIRE_FORMAT);
          }
        }
      }
      break;
    case 'validate':

      // The only restriction we have is that the node can't expire in the past.
      if ($node->expiremode == NODE_EXPIRE_DATE) {
        if ($node->expire <= 0) {
          form_set_error('expire_date', t('You have to specify a valid date.'));
        }
        elseif (strtotime($node->expire) <= time()) {
          form_set_error('expire_date', t("You can't expire a node in the past!"));
        }
      }
      break;
    case 'update':
    case 'insert':

      // Do we need to save data in the database?
      $save = TRUE;

      // We only care about the defaults for this node type
      $curdefaults = $curdefaults[$node->type];
      if ($node->expiremode == NODE_EXPIRE_DATE) {
        $node->expire = strtotime($node->expire);
        $node->expire_timefrom = '';
      }
      elseif ($node->expiremode == NODE_EXPIRE_ONUPDATE) {
        $node->expire = strtotime($node->expire_timefrom);
      }
      else {
        $node->expiremode = NODE_EXPIRE_NONE;
        $node->expire = 0;
        $node->expire_timefrom = 0;
      }

      // Propagate the settings to the child nodes, only if enabled. Notice
      // this is in the section for people with access to edit these settings.
      // It's a waste of resources to perform the recursion task as nothing
      // will be changed.
      if ($node->type == 'book' and variable_get('node-expire-book-props', 1)) {
        module_load_include('book.inc', 'node_expire');
        _node_expire_propagate_book($node->nid, $node->expire, $node);
      }

      // To keep track of inheritances and other such things, every node records
      // its expiration settings, not just ones set to expire.
      if ($save) {
        drupal_write_record('node_expire', $node, $node->node_expire);
      }
      break;
    case 'delete':
      db_query('DELETE FROM {node_expire} WHERE nid = %d', $node->nid);
      break;
  }
}

/**
 * Implementation of hook_theme().
 */
function node_expire_theme() {
  return array(
    'node_expire_outdated_nodes' => array(
      'file' => 'node_expire.inc',
      'arguments' => array(
        'header' => array(),
        'data' => array(),
      ),
    ),
  );
}

Functions

Namesort descending Description
node_expire_cron Implementation of hook_cron().
node_expire_form_alter Implementation of hook_form_alter().
node_expire_mail Implementation of hook_mail().
node_expire_menu Implementation of hook_menu().
node_expire_nodeapi Prepare and parse the data from our node entry forms.
node_expire_perm Implementation of hook_perm().
node_expire_theme Implementation of hook_theme().