? scheduler.d7.patch.txt Index: scheduler.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/scheduler/Attic/scheduler.info,v retrieving revision 1.2.2.1 diff -u -r1.2.2.1 scheduler.info --- scheduler.info 13 Feb 2008 00:09:29 -0000 1.2.2.1 +++ scheduler.info 7 Jan 2011 08:12:37 -0000 @@ -1,3 +1,7 @@ name = Scheduler -description = This module allows nodes to be published and unpublished on specified dates. -core = 6.x +description = This module allows nodes to be published and unpublished on specified dates and time. +core = 7.x +files[] = scheduler.install +files[] = scheduler.module +files[] = scheduler.views.inc +files[] = scheduler.test \ No newline at end of file Index: scheduler.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/scheduler/Attic/scheduler.install,v retrieving revision 1.5.2.7 diff -u -r1.5.2.7 scheduler.install --- scheduler.install 10 Nov 2010 12:04:45 -0000 1.5.2.7 +++ scheduler.install 7 Jan 2011 08:12:37 -0000 @@ -56,7 +56,7 @@ */ function scheduler_uninstall() { drupal_uninstall_schema('scheduler'); - db_query("DELETE FROM {variable} WHERE name LIKE '%s_%%'", 'scheduler'); +// db_query("DELETE FROM {variable} WHERE name LIKE '%s_%%'", 'scheduler'); } function scheduler_update_2() { Index: scheduler.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/scheduler/Attic/scheduler.module,v retrieving revision 1.50.2.53 diff -u -r1.50.2.53 scheduler.module --- scheduler.module 9 Dec 2010 21:31:42 -0000 1.50.2.53 +++ scheduler.module 7 Jan 2011 08:12:37 -0000 @@ -3,11 +3,20 @@ define('SCHEDULER_DATE_FORMAT', 'Y-m-d H:i:s'); -/** - * Implementation of hook_perm(). - */ -function scheduler_perm() { - return array('schedule (un)publishing of nodes', 'administer scheduler'); + /** + * Implementation of hook_permission(). + */ +function scheduler_permission() { + return array( + 'schedule (un)publishing of nodes' => array( + 'title' => t('schedule (un)publishing of nodes'), + 'description' => t('TODO Add a description for schedule (un)publishing of nodes'), + ), + 'administer scheduler' => array( + 'title' => t('administer scheduler'), + 'description' => t('TODO Add a description for administer scheduler'), + ), + ); } /** @@ -29,15 +38,15 @@ 'access arguments' => array('access administration pages'), 'type' => MENU_CALLBACK, ); - $items['admin/settings/scheduler'] = array( - 'title' => 'Scheduler module settings', + $items['admin/config/content/scheduler'] = array( + 'title' => 'Scheduler module settings', 'description' => 'Allows site admins to configure scheduler.', 'page callback' => 'drupal_get_form', 'page arguments' => array('scheduler_admin'), 'access arguments' => array('administer scheduler'), 'type' => MENU_NORMAL_ITEM, ); - $items['admin/content/node/scheduler'] = array( + $items['admin/content/scheduler'] = array( 'type' => MENU_LOCAL_TASK, 'title' => 'Scheduled', 'page callback' => 'scheduler_list', @@ -106,7 +115,6 @@ '#default_value' => variable_get('scheduler_extra_info', ''), '#description' => t('The text entered into this field will be displayed above the scheduling fields in the node edit form.'), ); - return system_settings_form($form); } @@ -129,15 +137,8 @@ return module_exists('date_popup') && variable_get('scheduler_field_type', 'date_popup') == 'date_popup'; } -/** - * Implementation of hook_form_alter(). - */ -function scheduler_form_alter(&$form, $form_state, $form_id) { - //allow scheduling on a per-node-type basis - - if ('node_type_form' == $form_id) { - - $form['scheduler'] = array( +function scheduler_form_node_type_form_alter(&$form, $form_state) { + $form['scheduler'] = array( '#type' => 'fieldset', '#title' => 'Scheduler settings', '#collapsible' => TRUE, @@ -146,7 +147,7 @@ '#group' => 'additional_settings', ); - $form['scheduler']['publish'] = array( + $form['scheduler']['publish'] = array( '#type' => 'fieldset', '#title' => 'Publishing settings', '#collapsible' => TRUE, @@ -154,32 +155,31 @@ '#weight' => 1, '#group' => 'additional_settings', ); - $form['scheduler']['publish']['scheduler_publish_enable'] = array( + $form['scheduler']['publish']['scheduler_publish_enable'] = array( '#type' => 'checkbox', '#title' => t('Enable scheduled publishing'), - '#default_value' => variable_get('scheduler_publish_enable_'. $form['#node_type']->type, 0), + '#default_value' => variable_get('scheduler_publish_enable_' . $form['#node_type']->type, 0), '#description' => t('Check this box to enable scheduled publishing for this node type.') ); - $form['scheduler']['publish']['scheduler_publish_touch'] = array( + $form['scheduler']['publish']['scheduler_publish_touch'] = array( '#type' => 'checkbox', '#title' => t('Alter published on time'), - '#default_value' => variable_get('scheduler_publish_touch_'. $form['#node_type']->type, 0), + '#default_value' => variable_get('scheduler_publish_touch_' . $form['#node_type']->type, 0), '#description' => t('Check this box to alter the published on time to match the scheduled time ("touch feature").') ); - $form['scheduler']['publish']['scheduler_publish_required'] = array( + $form['scheduler']['publish']['scheduler_publish_required'] = array( '#type' => 'checkbox', '#title' => t('Publishing date/time is required.'), - '#default_value' => variable_get('scheduler_publish_required_'. $form['#node_type']->type, 0), + '#default_value' => variable_get('scheduler_publish_required_' . $form['#node_type']->type, 0), '#description' => t('Check this box to if scheduled publishing is required (e.g. the user must enter a date/time).') ); - $form['scheduler']['publish']['scheduler_publish_revision'] = array( + $form['scheduler']['publish']['scheduler_publish_revision'] = array( '#type' => 'checkbox', '#title' => t('Create a new revision on publishing'), - '#default_value' => variable_get('scheduler_publish_revision_'. $form['#node_type']->type, 0), + '#default_value' => variable_get('scheduler_publish_revision_' . $form['#node_type']->type, 0), '#description' => t('Check this box if you want a new revision created when publishing.') ); - - $form['scheduler']['unpublish'] = array( + $form['scheduler']['unpublish'] = array( '#type' => 'fieldset', '#title' => 'Unpublishing settings', '#collapsible' => TRUE, @@ -187,149 +187,104 @@ '#weight' => 2, '#group' => 'additional_settings', ); - $form['scheduler']['unpublish']['scheduler_unpublish_enable'] = array( + $form['scheduler']['unpublish']['scheduler_unpublish_enable'] = array( '#type' => 'checkbox', '#title' => t('Enable scheduled unpublishing'), - '#default_value' => variable_get('scheduler_unpublish_enable_'. $form['#node_type']->type, 0), + '#default_value' => variable_get('scheduler_unpublish_enable_' . $form['#node_type']->type, 0), '#description' => t('Check this box to enable scheduled unpublishing for this node type.') ); - $form['scheduler']['unpublish']['scheduler_unpublish_required'] = array( + $form['scheduler']['unpublish']['scheduler_unpublish_required'] = array( '#type' => 'checkbox', '#title' => t('Unpublishing date/time is required.'), - '#default_value' => variable_get('scheduler_unpublish_required_'. $form['#node_type']->type, 0), + '#default_value' => variable_get('scheduler_unpublish_required_' . $form['#node_type']->type, 0), '#description' => t('Check this box to if scheduled unpublishing is required (e.g. the user must enter a date/time).') ); - $form['scheduler']['unpublish']['scheduler_unpublish_revision'] = array( + $form['scheduler']['unpublish']['scheduler_unpublish_revision'] = array( '#type' => 'checkbox', '#title' => t('Create a new revision on unpublishing'), - '#default_value' => variable_get('scheduler_unpublish_revision_'. $form['#node_type']->type, 0), + '#default_value' => variable_get('scheduler_unpublish_revision_' . $form['#node_type']->type, 0), '#description' => t('Check this box if you want a new revision created when unpublishing.') ); +} - } - // is this a node form? - elseif (isset($form['type']['#value']) && $form['type']['#value'] .'_node_form' == $form_id) { - if (user_access('schedule (un)publishing of nodes')) { - $publishing_enabled = variable_get('scheduler_publish_enable_'. $form['type']['#value'], 0) == 1; - $unpublishing_enabled = variable_get('scheduler_unpublish_enable_'. $form['type']['#value'], 0) == 1; - - // if scheduling has been enabled for this node type - if ($publishing_enabled || $unpublishing_enabled) { - - $node = $form['#node']; - - $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); - $use_date_popup = _scheduler_use_date_popup(); - - $internal_date_format = $use_date_popup ? SCHEDULER_DATE_FORMAT : $date_format; - - // if this is a preview then get the values from the form, not the db - if (isset($form_state['values']['op']) && $form_state['values']['op'] == 'Preview') { - $defaults = new StdClass; - $defaults->publish_on = $form_state['values']['publish_on']; - $defaults->unpublish_on = $form_state['values']['unpublish_on']; - } - elseif (isset($node->nid) && $node->nid > 0) { - // load the values if we are viewing an existing node - $defaults = db_fetch_object(db_query('SELECT publish_on, unpublish_on FROM {scheduler} WHERE nid = %d', $node->nid)); - } - else { - // init standard values - $defaults = new StdClass; - $defaults->publish_on = $defaults->unpublish_on = NULL; - } - - // if there is a text value then convert it to a unix timestamp - if (isset($defaults->publish_on) && $defaults->publish_on && !is_numeric($defaults->publish_on)) { - $defaults->publish_on = _scheduler_strtotime($defaults->publish_on); - } - if (isset($defaults->unpublish_on) && $defaults->unpublish_on && !is_numeric($defaults->unpublish_on)) { - $defaults->unpublish_on = _scheduler_strtotime($defaults->unpublish_on); - } - - $publishing_required = variable_get('scheduler_publish_required_'. $form['type']['#value'], 0) == 1; - $unpublishing_required = variable_get('scheduler_unpublish_required_'. $form['type']['#value'], 0) == 1; - - $fieldset_extended = ( - (isset($defaults->publish_on) && $defaults->publish_on != 0) - || (isset($defaults->unpublish_on) && $defaults->unpublish_on != 0) - || $publishing_required - || $unpublishing_required - ); - - $form['scheduler_settings'] = array( - '#type' => 'fieldset', - '#title' => t('Scheduling options'), - '#collapsible' => TRUE, - '#collapsed' => !$fieldset_extended, - '#weight' => 35, - '#group' => 'additional_settings', - '#attached' => array( - 'js' => array( - 'vertical-tabs' => drupal_get_path('module', 'scheduler') . "/scheduler_vertical_tabs.js" - ), - ) - ); - - $extra_info = variable_get('scheduler_extra_info', ''); - if ($extra_info && $extra_info != '') { - $form['scheduler_settings']['extra_info'] = array( - '#type' => 'item', - '#value' => $extra_info, - ); - } - - $description_format = t('Format: %time.', array('%time' => format_date(time(), 'custom', $date_format))); - if ($publishing_enabled) { - $description_blank = ''; - if (!$publishing_required) { - $description_blank .= ' '.t('Leave blank to disable scheduled publishing.'); - } - - $form['scheduler_settings']['publish_on'] = array( - '#type' => 'textfield', - '#title' => t('Publish on'), - '#maxlength' => 25, - '#required' => $publishing_required, - '#default_value' => isset($defaults->publish_on) && $defaults->publish_on ? format_date($defaults->publish_on, 'custom', $internal_date_format) : '', - '#description' => $description_format.$description_blank, - ); - } - - if ($unpublishing_enabled) { - $description_blank = ''; - if (!$unpublishing_required) { - $description_blank .= ' '.t('Leave blank to disable scheduled unpublishing.'); - } - $form['scheduler_settings']['unpublish_on'] = array( - '#type' => 'textfield', - '#title' => t('Unpublish on'), - '#maxlength' => 25, - '#required' => $unpublishing_required, - '#default_value' => isset($defaults->unpublish_on) && $defaults->unpublish_on ? format_date($defaults->unpublish_on, 'custom', $internal_date_format) : '', - '#description' => $description_format.$description_blank, - ); - } - - if ($use_date_popup) { - // Make this a popup calendar widget if Date Popup module is enabled. - if ($publishing_enabled) { - $form['scheduler_settings']['publish_on']['#type'] = 'date_popup'; - $form['scheduler_settings']['publish_on']['#date_format'] = $date_format; - $form['scheduler_settings']['publish_on']['#date_year_range'] = '0:+10'; - if (!$publishing_required) $form['scheduler_settings']['publish_on']['#description'] = t('Leave blank to disable scheduled publishing.'); - unset($form['scheduler_settings']['publish_on']['#maxlength']); - } - if ($unpublishing_enabled) { - $form['scheduler_settings']['unpublish_on']['#type'] = 'date_popup'; - $form['scheduler_settings']['unpublish_on']['#date_format'] = $date_format; - $form['scheduler_settings']['unpublish_on']['#date_year_range'] = '0:+10'; - if (!$unpublishing_required) $form['scheduler_settings']['unpublish_on']['#description'] = t('Leave blank to disable scheduled unpublishing.'); - unset($form['scheduler_settings']['unpublish_on']['#maxlength']); - } - } - } +/** + * Implementation of hook_form_alter(). + */ +function scheduler_form_alter(&$form, $form_state) { + // Is this a node form and scheduling has been enabled for this node type + if (!empty($form['#node_edit_form']) && user_access('schedule (un)publishing of nodes') && variable_get('scheduler_publish_enable_' . $form['type']['#value'], 0)) { + $node = $form['#node']; + + $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); + $internal_date_format = module_exists('date_popup') ? SCHEDULER_DATE_FORMAT : $date_format; + + // if this is a preview then get the values from the form, not the db + if (isset($form_state['values']['op']) && $form_state['values']['op'] == 'Preview') { + $defaults = new StdClass; + $defaults->publish_on = $form_state['values']['publish_on']; + $defaults->unpublish_on = $form_state['values']['unpublish_on']; + } + elseif (isset($node->nid) && $node->nid > 0) { + // load the values if we are viewing an existing node + $defaults = db_query('SELECT publish_on, unpublish_on FROM {scheduler} WHERE nid = :nid', array(':nid' => $node->nid))->fetchObject(); + } + else { + // init standard values + $defaults = new StdClass; + $defaults->publish_on = $defaults->unpublish_on = NULL; + } + + // if there is a text value then convert it to a unix timestamp + if (isset($defaults->publish_on) && $defaults->publish_on && !is_numeric($defaults->publish_on)) { + $defaults->publish_on = _scheduler_strtotime($defaults->publish_on); + } + if (isset($defaults->unpublish_on) && $defaults->unpublish_on && !is_numeric($defaults->unpublish_on)) { + $defaults->unpublish_on = _scheduler_strtotime($defaults->unpublish_on); + } + + $form['scheduler_settings'] = array( + '#type' => 'fieldset', + '#title' => t('Scheduling options'), + '#collapsible' => TRUE, + '#collapsed' => ((isset($defaults->publish_on) && $defaults->publish_on != 0) || (isset($defaults->unpublish_on) && $defaults->unpublish_on != 0)) ? FALSE: TRUE, + '#weight' => 35, + '#group' => 'additional_settings', + ); + $form['scheduler_settings']['publish_on'] = array( + '#type' => 'textfield', + '#title' => t('Publish on'), + '#maxlength' => 25, + '#default_value' => isset($defaults->publish_on) && $defaults->publish_on ? format_date($defaults->publish_on, 'custom', $internal_date_format) : '', + '#description' => t('Format: %time. Leave blank to disable scheduled publishing.', array('%time' => format_date(REQUEST_TIME, 'custom', $date_format))), + ); + + $form['scheduler_settings']['unpublish_on'] = array( + '#type' => 'textfield', + '#title' => t('Unpublish on'), + '#maxlength' => 25, + '#default_value' => isset($defaults->unpublish_on) && $defaults->unpublish_on ? format_date($defaults->unpublish_on, 'custom', $internal_date_format) : '', + '#description' => t('Format: %time. Leave blank to disable scheduled unpublishing.', array('%time' => format_date(REQUEST_TIME, 'custom', $date_format))), + ); + + if (module_exists('date_popup')) { + // Make this a popup calendar widget if Date Popup module is enabled. + $form['scheduler_settings']['publish_on'] = array( + '#type' => 'date_popup', + '#date_format' => $date_format, + '#date_year_range' => '0:+10', + '#default_value' => isset($defaults->publish_on) && $defaults->publish_on ? format_date($defaults->publish_on, 'custom', $internal_date_format) : '', + '#description' => t('Leave blank to disable scheduled publishing.'), + ); + unset($form['scheduler_settings']['publish_on']['#maxlength']); + $form['scheduler_settings']['unpublish_on'] = array( + '#type' => 'date_popup', + '#date_format' => $date_format, + '#date_year_range' => '0:+10', + '#default_value' => isset($defaults->unpublish_on) && $defaults->unpublish_on ? format_date($defaults->unpublish_on, 'custom', $internal_date_format) : '', + '#description' => t('Leave blank to disable scheduled unpublishing.'), + ); + unset($form['scheduler_settings']['unpublish_on']['#maxlength']); } } } @@ -340,11 +295,25 @@ */ function scheduler_list() { $header = array( - array('data' => t('Title'), 'field' => 'n.title'), - array('data' => t('Author'), 'field' => 'u.name'), - array('data' => t('Publish on'), 'field' => 's.publish_on'), - array('data' => t('Unpublish on'), 'field' => 's.unpublish_on'), - array('data' => t('Operations')) + array( + 'data' => t('Title'), + 'field' => 'n.title', + ), + array( + 'data' => t('Author'), + 'field' => 'u.name', + ), + array( + 'data' => t('Publish on'), + 'field' => 's.publish_on', + ), + array( + 'data' => t('Unpublish on'), + 'field' => 's.unpublish_on', + ), + array( + 'data' => t('Operations'), + ), ); // Default ordering @@ -353,29 +322,35 @@ $_GET['sort'] = 'ASC'; } - $sql = 'SELECT n.nid, n.uid, n.status, u.name, n.title, s.publish_on, s.unpublish_on FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid LEFT JOIN {users} u ON n.uid = u.uid' . tablesort_sql($header); - $result = pager_query($sql, 50); + $sql = 'SELECT n.nid, n.uid, n.status, u.name, n.title, s.publish_on, s.unpublish_on FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid LEFT JOIN {users} u ON n.uid = u.uid';// . tablesort_sql($header); + $result = db_query($sql); - while ($node = db_fetch_object($result)) { + foreach ($result as $node) { $rows[] = array( l($node->title, "node/$node->nid"), - theme('username', $node), - ($node->publish_on ? format_date($node->publish_on) : ' '), - ($node->unpublish_on ? format_date($node->unpublish_on) : ' '), - l(t('edit'), 'node/'. $node->nid .'/edit', array(), 'destination=admin/content/node/scheduler'), + theme('username', array('account' => $node)), + ($node->publish_on ? format_date($node->publish_on) : ' '), + ($node->unpublish_on ? format_date($node->unpublish_on) : ' '), + l(t('edit'), 'node/' . $node->nid . '/edit', array(), 'destination=admin/content/node/scheduler'), ); } - - if (count($rows)) { - if ($pager = theme('pager', NULL, 50, 0)) { - $rows[] = array(array('data' => $pager, 'colspan' => 6)); - } - - print theme('page', theme('table', $header, $rows)); - } - else { - print theme('page', t('There are no scheduled nodes.')); + /* + if (count($rows) && ($pager = theme('pager', NULL, 50, 0))) { + $rows[] = array( + array( + 'data' => $pager, + 'colspan' => 6, + ), + ); } + */ + $build['scheduler_list'] = array( + '#theme' => 'table', + '#header' => $header, + '#rows' => $rows, + '#empty' => t('There are no scheduled nodes.'), + ); + return $build; } /** @@ -390,18 +365,21 @@ $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); if (_scheduler_use_date_popup()) { - $date_format = SCHEDULER_DATE_FORMAT; + $date_format = SCHEDULER_DATE_FORMAT; } - $time=_scheduler_strptime(trim($str), $date_format); - if ($time!==FALSE) { + $time = _scheduler_strptime(trim($str), $date_format); + if ($time !== FALSE) { // success - $time -= _scheduler_get_user_timezone(); + // $time -= _scheduler_get_user_timezone(); } } else { // $str is empty $time = NULL; } + + $time = strtotime($str); + return $time; } @@ -486,132 +464,147 @@ } /** - * Implementation of hook_nodeapi(). + * Implementation of hook_node_load(). */ -function scheduler_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { - // Run $op == load for any user. - if ($op == 'load') { - $publishing_enabled = variable_get('scheduler_publish_enable_'. $node->type, 0) == 1; - $unpublishing_enabled = variable_get('scheduler_unpublish_enable_'. $node->type, 0) == 1; - if (isset($node->nid) && $node->nid && ($publishing_enabled || $unpublishing_enabled)) { - $result = db_query('SELECT * FROM {scheduler} WHERE nid = %d', $node->nid); - 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']; - $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; - } - } - } +function scheduler_node_load($nodes, $types) { + $nids = array(); + foreach ($nodes as $node) { + $nids[] = $node->nid; } - elseif (user_access('schedule (un)publishing of nodes')) { - switch ($op) { - case 'view': - if (isset($a4) && $a4 && isset($node->unpublish_on) && $node->unpublish_on) { - $unavailable_after = date("d-M-Y H:i:s T", $node->unpublish_on); - drupal_set_html_head(''); - } - break; - case 'validate': - 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 - // conversion. 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. - - $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); - - if (isset($node->publish_on) && $node->publish_on && !is_numeric($node->publish_on)) { - $publishtime = _scheduler_strtotime($node->publish_on); - if ($publishtime === FALSE) { - form_set_error('publish_on', t("The 'publish on' value does not match the expected format of %time", array('%time' => format_date(time(), 'custom', $date_format)))); - } - elseif ($publishtime && $publishtime < time()) { - form_set_error('publish_on', t("The 'publish on' date must be in the future")); - } - else { - $node->publish_on = $publishtime; - } - } - - if (isset($node->unpublish_on) && $node->unpublish_on && !is_numeric($node->unpublish_on)) { - $unpublishtime = _scheduler_strtotime($node->unpublish_on); - if ($unpublishtime === FALSE) { - form_set_error('unpublish_on', t("The 'unpublish on' value does not match the expected format of %time", array('%time' => format_date(time(), 'custom', $date_format)))); - } - elseif ($unpublishtime && $unpublishtime < time()) { - form_set_error('unpublish_on', t("The 'unpublish on' date must be in the future")); - } - else { - $node->unpublish_on = $unpublishtime; - } - } - - if (isset($publishtime) && isset($unpublishtime) && $unpublishtime < $publishtime) { - form_set_error('unpublish_on', t("The 'unpublish on' date must be later than the 'publish on' date.")); - } - - // 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 - if (isset($node->publish_on) && $node->publish_on != '' && is_numeric($node->publish_on) && $node->publish_on > time()) { - $node->status = 0; - drupal_set_message(t('This post is unpublished and will be published @publish_time.', array('@publish_time' => format_date($node->publish_on, 'custom', $date_format))), 'status', FALSE); - } - break; - case 'insert': - // only insert into database if we need to (un)publish this node at some date - if (isset($node->nid) && $node->nid && (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) VALUES (%d, %d, %d)', $node->nid, $node->publish_on, $node->unpublish_on); - } - break; - case 'update': - if (isset($node->nid) && $node->nid) { - $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 ($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->status == 0 && 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 WHERE nid = %d', $node->publish_on, $node->unpublish_on, $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 - elseif ((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) VALUES (%d, %d, %d)', $node->nid, $node->publish_on, $node->unpublish_on); - } - } - break; - case 'delete': - if (isset($node->nid) && $node->nid) { - db_query('DELETE FROM {scheduler} WHERE nid = %d', $node->nid); - } - break; - } + $result = db_query('SELECT * FROM {scheduler} WHERE nid IN (:nids)', array(':nids' => $nids)); + foreach ($result as $record) { + $nid = $record->nid; + unset($record->nid); + $nodes[$nid]->publish_on = $record->publish_on; + $nodes[$nid]->unpublish_on = $record->unpublish_on; + $row = array(); + $row['published'] = $record->publish_on ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $record->publish_on) : NULL; + $row['unpublished'] = $record->unpublish_on ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $record->unpublish_on) : NULL; + $node->scheduler = $row; + } +} + +function scheduler_node_view($node, $view_mode = 'full') { + if (user_access('schedule (un)publishing of nodes') && $view_mode == 'full' && !empty($node->unpublish_on)) { + $unavailable_after = date("d-M-Y H:i:s T", $node->unpublish_on); + drupal_add_html_head(''); + } +} + +function scheduler_node_validate($node, $form) { + $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); + + // 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 + // conversion. 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 (!empty($node->publish_on) && !is_numeric($node->publish_on)) { + $publishtime = _scheduler_strtotime($node->publish_on); + if ($publishtime === FALSE) { + form_set_error('publish_on', t("The 'publish on' value does not match the expected format of %time", array('%time' => format_date(REQUEST_TIME, 'custom', $date_format)))); + } + elseif ($publishtime && $publishtime < REQUEST_TIME) { + form_set_error('publish_on', t("The 'publish on' date must be in the future")); + } + } + if (!empty($node->unpublish_on) && !is_numeric($node->unpublish_on)) { + $unpublishtime = _scheduler_strtotime($node->unpublish_on); + } + if (isset($publishtime) && isset($unpublishtime) && $unpublishtime < $publishtime) { + form_set_error('unpublish_on', t("The 'unpublish on' date must be later than the 'publish on' date.")); + } +} + +function scheduler_node_presave($node) { + foreach (array('publish_on', 'unpublish_on') as $key) { + if (empty($node->$key)) { + // Make sure publish_on and unpublish_on are not empty strings. + $node->$key = 0; + } + elseif (!is_numeric($node->$key)) { + $node->$key= _scheduler_strtotime($node->$key); + } + } + // 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 + if (isset($node->publish_on) && $node->publish_on != '' && is_numeric($node->publish_on) && $node->publish_on > REQUEST_TIME) { + $node->status = 0; + } +} + +function scheduler_node_insert($node) { + // only insert into database if we need to (un)publish this node at some date + if (!empty($node->publish_on) || !empty($node->unpublish_on)) { + db_insert('scheduler') + ->fields(array( + 'nid' => $node->nid, + 'publish_on' => $node->publish_on, + 'unpublish_on' => $node->unpublish_on, + )) + ->execute(); + } +} + +function scheduler_node_update($node) { + // 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->status && !empty($node->publish_on)) || ($node->status && !empty($node->unpublish_on))) { + db_merge('scheduler') + ->key(array('nid' => $node->nid)) + ->fields(array( + 'publish_on' => $node->publish_on, + 'unpublish_on' => $node->unpublish_on, + )) + ->execute(); + } + else { + scheduler_node_delete($node); } } +function scheduler_node_delete($node) { + db_delete('scheduler') + ->condition('nid', $node->nid) + ->execute(); +} + + /** * Implementation of hook_cron(). */ function scheduler_cron() { $clear_cache = FALSE; - $clear_cache |= _scheduler_publish(); - $clear_cache |= _scheduler_unpublish(); - if ($clear_cache) { - // Clear the cache so an anonymous poster can see any changes to nodes - cache_clear_all(); + // if the time now is greater than the time to publish a node, publish it + $nodes = db_query('SELECT * 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 < :now', array(':now' => REQUEST_TIME)); + + foreach ($nodes as $node) { + $n = node_load($node->nid); + $n->changed = $node->publish_on; + if (variable_get('scheduler_touch_' . $n->type, 0) == 1) { + $n->created = $node->publish_on; + } + + // Use the actions system to publish the node. + watchdog('scheduler', '@type: scheduled publishing of %title.', array('@type' => $n->type, '%title' => $n->title), WATCHDOG_NOTICE, l(t('view'), 'node/' . $n->nid)); + $actions = array('node_publish_action', 'node_save_action'); + $context['node'] = $n; + actions_do($actions, $n, $context, NULL, NULL); + + // if this node is not to be unpublished, then we can delete the record + if (isset($n->unpublish_on) && $n->unpublish_on == 0) { + scheduler_node_delete($n); + } + // we need to unpublish this node at some time so clear the publish on since it's been published + else { + db_update('scheduler') + ->fields(array('publish_on' => 0)) + ->condition('nid', $n->nid) + ->execute(); + } } } @@ -626,7 +619,7 @@ $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); // If the time now is greater than the time to publish a node, publish it. - $query_result = db_query('SELECT s.nid AS nid 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 ', time()); + $query_result = db_query('SELECT s.nid AS nid 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 ', REQUEST_TIME); $nids = array(); while ($node = db_fetch_object($query_result)) { $nids[] = $node->nid; @@ -684,7 +677,7 @@ $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); // If the time is greater than the time to unpublish a node, unpublish it. - $query_result = db_query('SELECT s.nid AS nid FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid WHERE s.unpublish_on > 0 AND s.unpublish_on < %d', time()); + $query_result = db_query('SELECT s.nid AS nid FROM {scheduler} s LEFT JOIN {node} n ON s.nid = n.nid WHERE s.unpublish_on > 0 AND s.unpublish_on < %d', REQUEST_TIME); $nids = array(); while ($node = db_fetch_object($query_result)) { $nids[] = $node->nid; @@ -784,19 +777,23 @@ */ function _scheduler_scheduler_api($node, $action) { foreach (module_implements('scheduler_api') as $module) { - $function = $module .'_scheduler_api'; + $function = $module . '_scheduler_api'; $function($node, $action); } } function _scheduler_timecheck() { - $now = time(); - return theme('scheduler_timecheck', $now); + return theme('scheduler_timecheck', array('now' => REQUEST_TIME)); } -function theme_scheduler_timecheck($now) { +function theme_scheduler_timecheck($variables) { + $now = $variables['now']; drupal_set_title(t('Scheduler OS time check')); + + $t = localtime($now, TRUE); + + $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' => gmdate("Y-m-d H:i:s", $now), @@ -833,7 +830,6 @@ 'weight' => 10, ); } - return $fields; } @@ -928,3 +924,4 @@ } return $tokens; } + Index: scheduler.test =================================================================== RCS file: scheduler.test diff -N scheduler.test --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ scheduler.test 7 Jan 2011 08:12:37 -0000 @@ -0,0 +1,66 @@ + 'Scheduler functionality', + 'description' => 'Publish/unpublish on time.', + 'group' => 'Scheduler', + ); + } + + function setUp() { + parent::setUp('scheduler'); + $this->web_user = $this->drupalCreateUser(array('edit own page content', 'create page content', 'administer nodes', 'schedule (un)publishing of nodes')); + // Add scheduler functionality to the page node type. + variable_set('scheduler_page', 1); + } + + function testScheduler() { + // Create node to edit. + $edit = array( + 'title' => 'title', + 'publish_on' => format_date(time() + 3600, 'custom', 'Y-m-d H:i:s'), + 'status' => 1, + 'promote' => 1, + ); + $this->helpTestScheduler($edit); + $edit['unpublish_on'] = $edit['publish_on']; + unset($edit['publish_on']); + $this->helpTestScheduler($edit); + } + + function helpTestScheduler($edit) { + $langcode = LANGUAGE_NONE; + $body = $this->randomName(); + $edit["body[$langcode][0][value]"] = $body; + $this->drupalLogin($this->web_user); + $this->drupalPost('node/add/page', $edit, t('Save')); + $this->drupalLogout(); + $this->drupalGet('node'); + if (isset($edit['publish_on'])) { + $key = 'publish_on'; + $this->assertNoText($body, t('Node is unpublished')); + } + else { + $key = 'unpublish_on'; + $this->assertText($body, t('Node is published')); + } + db_update('scheduler')->fields(array($key => time() - 1))->execute(); + $this->assertTrue(db_query('SELECT COUNT(*) FROM {scheduler}')->fetchField(), 'Scheduler table is not empty'); + $this->cronRun(); + $this->assertFalse(db_query('SELECT COUNT(*) FROM {scheduler}')->fetchField(), 'Scheduler table is empty'); + $this->drupalGet('node'); + if (isset($edit['publish_on'])) { + $this->assertText($body, t('Node is published')); + } + else { + $this->assertNoText($body, t('Node is unpublished')); + } + } +} \ No newline at end of file