| 1 |
<?php
|
| 2 |
// $Id: node_expire.nodeapi.inc,v 1.3 2009/01/31 00:22:15 brmassa Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Node API integration.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_nodeapi().
|
| 11 |
*/
|
| 12 |
function _node_expire_nodeapi(&$ntypes, &$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 13 |
switch ($op) {
|
| 14 |
case 'load':
|
| 15 |
$query = db_query('SELECT expire, expired
|
| 16 |
FROM {node_expire} WHERE nid = %d', $node->nid);
|
| 17 |
|
| 18 |
// Use the existing expiration data if present.
|
| 19 |
if ($row = db_fetch_object($query)) {
|
| 20 |
$node->expire = $row->expire;
|
| 21 |
$node->expired = $row->expired;
|
| 22 |
}
|
| 23 |
break;
|
| 24 |
|
| 25 |
case 'prepare':
|
| 26 |
if (!isset($node->expire)) {
|
| 27 |
$node->expire = format_date(strtotime($ntypes['default']), 'custom', NODE_EXPIRE_FORMAT);
|
| 28 |
}
|
| 29 |
break;
|
| 30 |
|
| 31 |
case 'validate':
|
| 32 |
// The only restriction we have is that the node can't expire in the past.
|
| 33 |
if ($node->expire == '') {
|
| 34 |
if (!empty($ntypes['required'])) {
|
| 35 |
form_set_error('expire_date', t('You must choose an expiration date.'));
|
| 36 |
}
|
| 37 |
}
|
| 38 |
elseif (!$expire = strtotime($node->expire) or $expire <= 0) {
|
| 39 |
form_set_error('expire_date', t('You have to specify a valid date.'));
|
| 40 |
}
|
| 41 |
elseif ($expire <= time()) {
|
| 42 |
form_set_error('expire_date', t("You can't expire a node in the past!"));
|
| 43 |
}
|
| 44 |
elseif (!empty($ntypes['max']) and $expire > strtotime($ntypes['max'], $node->created)) {
|
| 45 |
form_set_error('expire_date', t('It must expire before %date.',
|
| 46 |
array('%date' => format_date(strtotime($ntypes['max'], $node->created), 'custom', NODE_EXPIRE_FORMAT))));
|
| 47 |
}
|
| 48 |
break;
|
| 49 |
|
| 50 |
case 'update':
|
| 51 |
case 'insert':
|
| 52 |
$update = array();
|
| 53 |
if (isset($node->node_expire)) {
|
| 54 |
$update[] = 'nid';
|
| 55 |
}
|
| 56 |
$node->expire = strtotime($node->expire);
|
| 57 |
$node->expired = FALSE;
|
| 58 |
drupal_write_record('node_expire', $node, $update);
|
| 59 |
break;
|
| 60 |
|
| 61 |
case 'delete':
|
| 62 |
db_query('DELETE FROM {node_expire} WHERE nid = %d', $node->nid);
|
| 63 |
break;
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
function _node_expire_form_alter_nodeform(&$ntypes, &$form, &$form_state, $form_id) {
|
| 68 |
// Check if the Node Expire feature is enabled for the node type
|
| 69 |
$node = isset($form['#node']) ? $form['#node'] : NULL;
|
| 70 |
|
| 71 |
// Convert the timestamp into a human readable date
|
| 72 |
if (is_numeric($node->expire)) {
|
| 73 |
$node->expire = format_date($node->expire, 'custom', NODE_EXPIRE_FORMAT);
|
| 74 |
}
|
| 75 |
|
| 76 |
if (user_access('edit node expire')) {
|
| 77 |
$expire_field = array(
|
| 78 |
'#title' => t('Expiration Date'),
|
| 79 |
'#description' => t('Time date to consider the node expired. Format: %time.',
|
| 80 |
array('%time' => format_date(time(), 'custom', NODE_EXPIRE_FORMAT))),
|
| 81 |
'#type' => 'textfield',
|
| 82 |
'#maxlength' => 25,
|
| 83 |
'#required' => $ntypes['required'],
|
| 84 |
'#default_value' => $node->expire,
|
| 85 |
);
|
| 86 |
|
| 87 |
// In case jQuery UI module is enabled, use it to
|
| 88 |
// get the DatePicker widget.
|
| 89 |
if (module_exists('jquery_ui')) {
|
| 90 |
jquery_ui_add('ui.datepicker');
|
| 91 |
drupal_add_js(drupal_get_path('module', 'node_expire') .'/node_expire.js');
|
| 92 |
$min = empty($node->created) ? time() : $node->created;
|
| 93 |
$min = array(
|
| 94 |
format_date($min, 'custom', 'Y'),
|
| 95 |
format_date($min, 'custom', 'm'),
|
| 96 |
format_date($min, 'custom', 'd'),
|
| 97 |
);
|
| 98 |
if (!empty($ntypes['max'])) {
|
| 99 |
$max = strtotime($ntypes['max'], (empty($node->created) ? time() : $node->created));
|
| 100 |
$max = array(
|
| 101 |
format_date($max, 'custom', 'Y'),
|
| 102 |
format_date($max, 'custom', 'm'),
|
| 103 |
format_date($max, 'custom', 'd'),
|
| 104 |
);
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
$max = NULL;
|
| 108 |
}
|
| 109 |
drupal_add_js(array(
|
| 110 |
'dateFormat' => NODE_EXPIRE_FORMAT_JS,
|
| 111 |
'minDate' => $min,
|
| 112 |
'maxDate' => $max,
|
| 113 |
), 'setting');
|
| 114 |
}
|
| 115 |
}
|
| 116 |
else {
|
| 117 |
$expire_field = array(
|
| 118 |
'#type' => 'value',
|
| 119 |
'#value' => $node->expire
|
| 120 |
);
|
| 121 |
}
|
| 122 |
|
| 123 |
// Put the expire field into 'Publising options' if possible
|
| 124 |
if (user_access('administer nodes')) {
|
| 125 |
$form['options']['expire'] = &$expire_field;
|
| 126 |
}
|
| 127 |
else {
|
| 128 |
$form['expire'] = &$expire_field;
|
| 129 |
}
|
| 130 |
|
| 131 |
if (isset($node->expired)) {
|
| 132 |
$form['node_expire'] = array(
|
| 133 |
'#type' => 'value',
|
| 134 |
'#value' => TRUE,
|
| 135 |
);
|
| 136 |
}
|
| 137 |
}
|