View source
<?php
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');
function node_expire_cron() {
module_load_include('cron.inc', 'node_expire');
_node_expire_cron();
}
function node_expire_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type']) and $form['type']['#value'] . '_node_form' == $form_id) {
$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',
),
);
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,
);
}
}
function node_expire_mail($key, &$message, $params) {
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
$message['headers'] = array_merge($message['headers'], $params['headers']);
}
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,
);
$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;
}
function node_expire_perm() {
return array(
'administer node_expire',
'edit expirations',
);
}
function node_expire_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
$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);
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];
if (empty($node->node_expire)) {
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))) {
$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':
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':
$save = TRUE;
$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;
}
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);
}
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;
}
}
function node_expire_theme() {
return array(
'node_expire_outdated_nodes' => array(
'file' => 'node_expire.inc',
'arguments' => array(
'header' => array(),
'data' => array(),
),
),
);
}