Skip to content
Snippets Groups Projects
Commit 17f4e8af authored by Andy Kirkham's avatar Andy Kirkham
Browse files

HEAD should now be DRUPAL6 compat

parent 5207a2bf
No related branches found
No related tags found
No related merge requests found
name = Scheduler
description = This module allows nodes to be published and unpublished on specified dates.
core = 6.x
\ No newline at end of file
......@@ -2,28 +2,49 @@
// $Id$
function scheduler_install() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("CREATE TABLE {scheduler} (
nid int(10) unsigned NOT NULL,
publish_on int(11) NOT NULL default '0',
unpublish_on int(11) NOT NULL default '0',
timezone int(6) NOT NULL default '0',
PRIMARY KEY (nid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;"
);
break;
case 'pgsql':
db_query("CREATE TABLE {scheduler} (
nid integer NOT NULL default '0',
publish_on integer NOT NULL default '0',
unpublish_on integer NOT NULL default '0',
timezone integer NOT NULL default '0',
PRIMARY KEY (nid));"
);
break;
}
drupal_install_schema('scheduler');
}
function scheduler_schema() {
return array(
'scheduler' => array(
'description' => t('The main table to hold the scheduler data.'),
'fields' => array(
'nid' => array(
'description' => t('The foreign key to node.nid'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'publish_on' => array(
'description' => t('The UNIX UTC timestamp when to publish'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => '0',
),
'unpublish_on' => array(
'description' => t('The UNIX UTC timestamp when to unpublish'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => '0',
),
'timezone' => array(
'description' => t('The offset in seconds due to the timezone'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => '0',
),
),
'indexes' => array(
'scheduler_publish_on' => array('publish_on'),
'scheduler_unpublish_on' => array('unpublish_on'),
),
'primary key' => array('nid'),
),
);
}
function scheduler_update_1() {
......@@ -47,3 +68,9 @@ function scheduler_update_2() {
return $ret;
}
function scheduler_update_3() {
$ret[] = update_sql("CREATE INDEX scheduler_publish_on ON {scheduler} (publish_on)");
$ret[] = update_sql("CREATE INDEX scheduler_unpublish_on ON {scheduler} (unpublish_on)");
return $ret;
}
......@@ -20,35 +20,35 @@ function scheduler_perm() {
/**
* Implementation of hook_menu().
*/
function scheduler_menu($may_cache) {
function scheduler_menu() {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'scheduler/cron',
'type' => MENU_CALLBACK,
'callback' => '_scheduler_run_cron',
'access' => TRUE,
);
$items[] = array(
'path' => 'scheduler/timecheck',
'type' => MENU_CALLBACK,
'callback' => '_scheduler_timecheck',
'access' => user_access('schedule (un)publishing of nodes'),
);
}
$items['scheduler/cron'] = array(
'title' => t('Light weight cron handler'),
'description' => t('A light weight cron handler to allow more frequent runs of Scheduler\'s internal cron system'),
'page callback' => '_scheduler_run_cron',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['scheduler/timecheck'] = array(
'title' => t('Test your servers UTC clock'),
'description' => t('Allow\'s site admin to check their server\'s internal clock'),
'page callback' => '_scheduler_timecheck',
'access arguments' => array('access administration pages'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_form_alter().
*/
function scheduler_form_alter($form_id, &$form) {
function scheduler_form_alter(&$form, $form_state, $form_id) {
//allow scheduling on a per-node-type basis
if ('node_type_form' == $form_id) {
$form['workflow']['scheduler'] = array(
'#type' => 'checkbox',
'#title' => t('Enable scheduled (un)publishing'),
'#type' => 'checkbox',
'#title' => t('Enable scheduled (un)publishing'),
'#default_value' => variable_get('scheduler_'. $form['#node_type']->type, 0),
'#description' => t('Check this box to enable scheduled (un)publishing for this node type.')
);
......@@ -73,62 +73,62 @@ function scheduler_form_alter($form_id, &$form) {
// Show 24-hour clock for JScalendar
$form['#jscalendar_timeFormat'] = '24';
}
$node = $form['#node'];
//only load the values if we are viewing an existing node
if ($node->nid > 0) {
if (isset($node->nid) && $node->nid > 0) {
$defaults = db_fetch_object(db_query('SELECT publish_on, unpublish_on, timezone FROM {scheduler} WHERE nid = %d', $node->nid));
}
else {
else {
// init standard values
$defaults = new StdClass;
$defaults->publish_on = $defaults->unpublish_on = NULL;
}
}
//note don't use format_date() because drupal automatically formats the date to the user's timezone
//this will show the wrong time because scheduler can set nodes to be published in different timezones
$form['scheduler_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Scheduling options'),
'#collapsible' => TRUE,
'#collapsed' => ($defaults->publish_on != 0 || $defaults->unpublish_on != 0) ? FALSE: TRUE,
'#collapsed' => ((isset($defaults->publish_on) && $defaults->publish_on != 0) || (isset($defaults->unpublish_on) && $defaults->unpublish_on != 0)) ? FALSE: TRUE,
'#weight' => 35
);
$form['scheduler_settings']['publish_on'] = array(
'#type' => 'textfield',
'#title' => t('Publish on'),
'#type' => 'textfield',
'#title' => t('Publish on'),
'#maxlength' => 25,
//we subtract the time zone to show the user the correct time they entered
//and below that we show the timezone to adjust this time by
//we store the adjusted timezone value in the database for cron
'#default_value' => $defaults->publish_on ? date('Y-m-d H:i:s', $defaults->publish_on - $defaults->timezone) : '',
'#default_value' => isset($defaults->publish_on) && $defaults->publish_on ? date('Y-m-d H:i:s', $defaults->publish_on - $defaults->timezone) : '',
'#description' => t('Format: %time. Leave blank to publish on the time of form submission.', array('%time' => date('Y-m-d H:i:s'))),
'#attributes' => $jscalendar ? array('class' => 'jscalendar') : array()
);
$form['scheduler_settings']['unpublish_on'] = array(
'#type' => 'textfield',
'#title' => t('Unpublish on'),
'#maxlength' => 25,
'#type' => 'textfield',
'#title' => t('Unpublish on'),
'#maxlength' => 25,
//we subtract the time zone to show the user the correct time they entered
//and below that we show the timezone to adjust this time by
//we store the adjusted timezone value in the database for cron
'#default_value' => $defaults->unpublish_on ? date('Y-m-d H:i:s', $defaults->unpublish_on - $defaults->timezone) : '',
'#default_value' => isset($defaults->unpublish_on) && $defaults->unpublish_on ? date('Y-m-d H:i:s', $defaults->unpublish_on - $defaults->timezone) : '',
'#description' => t('Format: %time. Leave blank to not unpublish this node.', array('%time' => date('Y-m-d H:i:s'))),
'#attributes' => $jscalendar ? array('class' => 'jscalendar') : array()
);
//default to user timezone, if not specified, default to system wide timezone
if (variable_get('configurable_timezones', 1) == 1) {
global $user;
$zones = _system_zonelist();
$form['scheduler_settings']['timezone'] = array(
'#type' => 'select',
'#title' => t('Time zone'),
'#default_value' => $defaults->timezone ? $defaults->timezone : (strlen($user->timezone) ? $user->timezone : variable_get('date_default_timezone', 0)),
'#options' => $zones,
'#type' => 'select',
'#title' => t('Time zone'),
'#default_value' => isset($defaults->timezone) ? $defaults->timezone : (strlen($user->timezone) ? $user->timezone : variable_get('date_default_timezone', 0)),
'#options' => $zones,
'#description' => t('Select the time zone to (un)publish in.')
);
}
......@@ -138,8 +138,8 @@ function scheduler_form_alter($form_id, &$form) {
'#value' => $defaults->timezone ? $defaults->timezone : (strlen($user->timezone) ? $user->timezone : variable_get('date_default_timezone', 0)),
);
}
}
}
}
}
}
}
......@@ -150,62 +150,79 @@ function scheduler_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
if (user_access('schedule (un)publishing of nodes')) {
switch ($op) {
case 'view':
if ($page && $node->unpublish_on) {
if ($page && isset($node->unpublish_on) && $node->unpublish_on) {
$unavailable_after = date ("d-M-Y H:i:s e", $node->unpublish_on);
drupal_set_html_head('<META NAME="GOOGLEBOT" CONTENT="unavailable_after: '. $unavailable_after .'">');
}
break;
case 'load':
if ($node->nid) {
if (isset($node->nid) && $node->nid) {
$result = db_query('SELECT * FROM {scheduler} WHERE nid = %d', $node->nid);
if ($result && db_num_rows($result) > 0) {
if ($result) {
$row = db_fetch_array($result);
if (isset($row['nid'])) {
unset($row['nid']);
$node->publish_on = $row['publish_on'];
$node->unpublish_on = $row['unpublish_on'];
$node->timezone = $row['timezone'];
$row['published'] = $row['publish_on'] ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $row['publish_on']) : NULL;
$row['unpublished'] = $row['unpublish_on'] ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $row['unpublish_on']) : NULL;
$node->scheduler = $row;
$node->publish_on = $row['publish_on'];
$node->unpublish_on = $row['unpublish_on'];
$node->timezone = $row['timezone'];
$row['published'] = $row['publish_on'] ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $row['publish_on']) : NULL;
$row['unpublished'] = $row['unpublish_on'] ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $row['unpublish_on']) : NULL;
$node->scheduler = $row;
}
}
}
break;
case 'submit':
//adjust the entered times for timezone consideration
$node->publish_on = $node->publish_on ? strtotime($node->publish_on) + $node->timezone : NULL;
$node->unpublish_on = $node->unpublish_on ? strtotime($node->unpublish_on) + $node->timezone : NULL;
case 'presave':
// adjust the entered times for timezone consideration.
// Note, we must check to see if the value is numeric,
// if it is, assume we have already done the strtotime
// conversation. This prevents us running strtotime on
// a value we have already converted. This is needed
// because DRUPAL6 removed 'submit' and added 'presave'
// and all this happens at different times.
if (isset($node->publish_on) && !is_numeric($node->publish_on)) {
$node->publish_on = strtotime($node->publish_on) + $node->timezone;
}
if (isset($node->unpublish_on) && !is_numeric($node->unpublish_on)) {
$node->unpublish_on = strtotime($node->unpublish_on) + $node->timezone;
}
//$node->publish_on = isset($node->publish_on) && $node->publish_on ? strtotime($node->publish_on) + $node->timezone : NULL;
//$node->unpublish_on = isset($node->unpublish_on) && $node->unpublish_on ? strtotime($node->unpublish_on) + $node->timezone : NULL;
// right before we save the node, we need to check if a "publish on" value has been set
// if it has been set, we want to make sure the node is unpublished
// since it will be published at a later date (but only if the value is in the future.
if ($node->publish_on != '' && is_numeric($node->publish_on) && $node->publish_on > time()) {
if (isset($node->publish_on) && $node->publish_on != '' && is_numeric($node->publish_on) && $node->publish_on > time()) {
$node->status = 0;
}
break;
case 'insert':
//only insert into database if we need to (un)publish this node at some date
if ($node->publish_on != NULL || $node->unpublish_on != NULL) {
// only insert into database if we need to (un)publish this node at some date
if ((isset($node->publish_on) && $node->publish_on != NULL) || (isset($node->unpublish_on) && $node->unpublish_on != NULL)) {
db_query('INSERT INTO {scheduler} (nid, publish_on, unpublish_on, timezone) VALUES (%d, %d, %d, %d)', $node->nid, $node->publish_on, $node->unpublish_on, $node->timezone);
}
break;
case 'update':
$exists = db_result(db_query('SELECT nid FROM {scheduler} WHERE nid = %d', $node->nid));
//if this node has already been scheduled, update its record
// if this node has already been scheduled, update its record
if ($exists) {
//only update database if we need to (un)publish this node at some date
//otherwise the user probably cleared out the (un)publish dates so we should remove the record
if ($node->publish_on != NULL || $node->unpublish_on != NULL) {
// only update database if we need to (un)publish this node at some date
// otherwise the user probably cleared out the (un)publish dates so we should remove the record
if ((isset($node->publish_on) && $node->publish_on != NULL) || (isset($node->unpublish_on) && $node->unpublish_on != NULL)) {
db_query('UPDATE {scheduler} SET publish_on = %d, unpublish_on = %d, timezone = %d WHERE nid = %d', $node->publish_on, $node->unpublish_on, $node->timezone, $node->nid);
}
else {
db_query('DELETE FROM {scheduler} WHERE nid = %d', $node->nid);
}
}
//node doesn't exist, create a record only if the (un)publish fields are blank
else if ($node->publish_on != NULL || $node->unpublish_on != NULL) {
// node doesn't exist, create a record only if the (un)publish fields are blank
else if ((isset($node->publish_on) && $node->publish_on != NULL) || (isset($node->unpublish_on) && $node->unpublish_on != NULL)) {
db_query('INSERT INTO {scheduler} (nid, publish_on, unpublish_on, timezone) VALUES (%d, %d, %d, %d)', $node->nid, $node->publish_on, $node->unpublish_on, $node->timezone);
}
break;
......@@ -221,41 +238,38 @@ function scheduler_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
*/
function scheduler_cron() {
$clear_cache = FALSE;
//if the time now is greater than the time to publish a node, publish it
// if the time now is greater than the time to publish a node, publish it
$nodes = db_query('SELECT *, (publish_on - timezone) AS utc_publish_on FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid WHERE n.status = 0 AND s.publish_on > 0 AND s.publish_on < %d + s.timezone', time());
while ($node = db_fetch_object($nodes)) {
$n = node_load($node->nid);
$n->changed = $node->utc_publish_on;
if (variable_get('scheduler_touch_'. $n->type, 0) == 1) {
$n->created = $node->utc_publish_on;
}
$n = node_submit($n);
$n->status = 1;
node_save($n);
//if this node is not to be unpublished, then we can delete the record
if ($n->unpublish_on == 0) {
// if this node is not to be unpublished, then we can delete the record
if (isset($n->unpublish_on) && $n->unpublish_on == 0) {
db_query('DELETE FROM {scheduler} WHERE nid = %d', $n->nid);
}
//we need to unpublish this node at some time so clear the publish on since it's been published
// we need to unpublish this node at some time so clear the publish on since it's been published
else {
db_query('UPDATE {scheduler} SET publish_on = 0 WHERE nid = %d', $n->nid);
}
//invoke scheduler API
// invoke scheduler API
_scheduler_scheduler_api($n, 'publish');
watchdog('content', t('@type: scheduled publishing of %title.', array('@type' => $n->type, '%title' => $n->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $n->nid));
watchdog('content', '@type: scheduled publishing of %title.', array('@type' => $n->type, '%title' => $n->title), WATCHDOG_NOTICE, l(t('view'), 'node/'. $n->nid));
$clear_cache = TRUE;
}
//if the time is greater than the time to unpublish a node, unpublish it
// if the time is greater than the time to unpublish a node, unpublish it
$nodes = db_query('SELECT *, (unpublish_on - timezone) AS utc_unpublish_on FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid WHERE n.status = 1 AND s.unpublish_on > 0 AND s.unpublish_on < %d + s.timezone', time());
while ($node = db_fetch_object($nodes)) {
//if this node is to be unpublished, we can update the node and remove the record since it can't be republished
// if this node is to be unpublished, we can update the node and remove the record since it can't be republished
$n = node_load($node->nid);
$n->changed = $node->utc_publish_on;
$n = node_submit($n);
......@@ -263,24 +277,39 @@ function scheduler_cron() {
node_save($n);
db_query('DELETE FROM {scheduler} WHERE nid = %d', $n->nid);
//invoke scheduler API
// invoke scheduler API
_scheduler_scheduler_api($n, 'unpublish');
watchdog('content', t('@type: scheduled unpublishing of %title.', array('@type' => $n->type, '%title' => $n->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $n->nid));
watchdog('content', '@type: scheduled unpublishing of %title.', array('@type' => $n->type, '%title' => $n->title), WATCHDOG_NOTICE, l(t('view'), 'node/'. $n->nid));
$clear_cache = TRUE;
}
if ($clear_cache) {
// clear the cache so an anonymous poster can see the node being published or unpublished
cache_clear_all();
}
}
/**
* Implementation of hook_theme()
*/
function scheduler_theme() {
return array(
'scheduler_timecheck' => array(
'arguments' => array('now' => NULL),
),
);
}
function _scheduler_run_cron() {
watchdog('scheduler', 'Internal scheduler run activated', array(), WATCHDOG_NOTICE);
scheduler_cron();
if (ob_get_level() > 0) {
ob_clean();
$handlers = ob_list_handlers();
if (isset($handlers[0]) && $handlers[0] == 'default output handler') {
ob_clean();
}
}
exit();
}
......@@ -296,7 +325,7 @@ function _scheduler_run_cron() {
function _scheduler_scheduler_api($node, $action) {
foreach (module_implements('scheduler_api') as $module) {
$function = $module .'_scheduler_api';
$function($node, $action);
$function($node, $action);
}
}
......@@ -314,12 +343,12 @@ function theme_scheduler_timecheck($now) {
$lt = ($t['tm_year']+1900) .'-'. ($t['tm_mon']+1) .'-'. $t['tm_mday'] .' '. $t['tm_hour'] .':'. $t['tm_min'] .':'. $t['tm_sec'];
$t_options = array(
'%time' => date("Y-m-d H:i:s", $now),
'%time' => date("Y-m-d H:i:s", $now),
'%lt' => $lt,
);
return
t('Your server reports the UTC time as %time and "localtime" as %lt.', $t_options) .
t('Your server reports the UTC time as %time and "localtime" as %lt.', $t_options) .
'<p />'.
t('If all is well with your server\'s time configuration UTC should match <a target="_blank" href="http://wwp.greenwichmeantime.com/">UTC London Time</a> and the localtime should be the time where you are.') .
'<p />'.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment