? d7.patch
? dump
Index: scheduler.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/scheduler/Attic/scheduler.info,v
retrieving revision 1.2.2.1
diff -u -p -r1.2.2.1 scheduler.info
--- scheduler.info	13 Feb 2008 00:09:29 -0000	1.2.2.1
+++ scheduler.info	20 Jan 2010 02:10:46 -0000
@@ -1,3 +1,7 @@
 name = Scheduler
 description = This module allows nodes to be published and unpublished on specified dates.
-core = 6.x
+core = 7.x
+files[] = scheduler.install
+files[] = scheduler.module
+files[] = scheduler.views.inc
+files[] = scheduler.test
Index: scheduler.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/scheduler/Attic/scheduler.install,v
retrieving revision 1.5.2.6
diff -u -p -r1.5.2.6 scheduler.install
--- scheduler.install	26 Aug 2009 17:40:43 -0000	1.5.2.6
+++ scheduler.install	20 Jan 2010 02:10:46 -0000
@@ -56,7 +56,7 @@ function scheduler_install() {
  */
 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.35
diff -u -p -r1.50.2.35 scheduler.module
--- scheduler.module	17 Jan 2010 16:24:33 -0000	1.50.2.35
+++ scheduler.module	20 Jan 2010 02:10:46 -0000
@@ -4,10 +4,19 @@
 define('SCHEDULER_DATE_FORMAT', 'Y-m-d H:i:s');
 
 /**
- * Implementation of hook_perm().
+ * Implementation of hook_permission().
  */
-function scheduler_perm() {
-  return array('schedule (un)publishing of nodes', 'administer scheduler');
+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'),
+    ),
+  );
 }
 
 /**
@@ -66,106 +75,94 @@ function scheduler_admin() {
   return system_settings_form($form);
 }
 
-/**
- * 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) {
+function scheduler_form_node_type_form_alter(&$form, $form_state) {
     $form['workflow']['scheduler'] = array(
       '#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.')
+      '#default_value' => variable_get('scheduler_' . $form['#node_type']->type, 0),
+      '#description' => t('Check this box to enable scheduled (un)publishing for this node type.'),
     );
     $form['workflow']['scheduler_touch'] = array(
       '#type' => 'checkbox',
       '#title' => t('Alter published on time'),
-      '#default_value' => variable_get('scheduler_touch_'. $form['#node_type']->type, 0),
-      '#description' => t('Check this box to alter the published on time to match the scheduled time ("touch feature").')
+      '#default_value' => variable_get('scheduler_touch_' . $form['#node_type']->type, 0),
+      '#description' => t('Check this box to alter the published on time to match the scheduled time ("touch feature").'),
     );
-  }
+  
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function scheduler_form_alter(&$form, $form_state) {
+  // Is this a node form an dscheduling has been enabled for this node type
+  if (!empty($form['#node_edit_form']) && user_access('schedule (un)publishing of nodes') && variable_get('scheduler_'. $form['type']['#value'], 0)) {
+    $node = $form['#node'];
 
-  // 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')) {
-      // if scheduling has been enabled for this node type
-      if (variable_get('scheduler_'. $form['type']['#value'], 0) == 1) {
-
-        $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_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);
-        }
-
-        $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',
-          '#attached' => array(
-            'js' => array(
-              'vertical-tabs' => drupal_get_path('module', 'scheduler') . "/scheduler_vertical_tabs.js"
-            ),
-          )
-        );
-
-        $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(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(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']['#type'] = 'date_popup';
-          $form['scheduler_settings']['publish_on']['#date_format'] = $date_format;
-          $form['scheduler_settings']['publish_on']['#date_year_range'] = '0:+10';
-          $form['scheduler_settings']['publish_on']['#description'] = t('Leave blank to disable scheduled publishing.');
-          unset($form['scheduler_settings']['publish_on']['#maxlength']);
-          $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';
-          $form['scheduler_settings']['unpublish_on']['#description'] = t('Leave blank to disable scheduled unpublishing.');
-          unset($form['scheduler_settings']['unpublish_on']['#maxlength']);
-        }
-      }
+    $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 = %d', $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(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(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']['#type'] = 'date_popup';
+      $form['scheduler_settings']['publish_on']['#date_format'] = $date_format;
+      $form['scheduler_settings']['publish_on']['#date_year_range'] = '0:+10';
+      $form['scheduler_settings']['publish_on']['#description'] = t('Leave blank to disable scheduled publishing.');
+      unset($form['scheduler_settings']['publish_on']['#maxlength']);
+      $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';
+      $form['scheduler_settings']['unpublish_on']['#description'] = t('Leave blank to disable scheduled unpublishing.');
+      unset($form['scheduler_settings']['unpublish_on']['#maxlength']);
     }
   }
 }
@@ -176,11 +173,25 @@ function scheduler_form_alter(&$form, $f
  */
 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
@@ -192,26 +203,31 @@ function scheduler_list() {
   $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);
 
-  while ($node = db_fetch_object($result)) {
+  foreach ($result as $node) {
     $rows[] = array(
       ($node->status ? l($node->title, "node/$node->nid") : $node->title),
-      theme('username', $node),
+      theme('username', array('account' => $node)),
       ($node->publish_on ? format_date($node->publish_on) : '&nbsp;'),
       ($node->unpublish_on ? format_date($node->unpublish_on) : '&nbsp;'),
       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;
 }
 
 /**
@@ -321,116 +337,111 @@ function _scheduler_get_user_timezone() 
 }
 
 /**
- * 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') {
-    if (isset($node->nid) && $node->nid && variable_get('scheduler_'. $node->type, 0) == 1) {
-      $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;
-        }
-      }
-    }
-  }
-  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('<meta name="googlebot" content="unavailable_after: '. $unavailable_after .'" />');
-        }
-        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;
-        }
-        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;
+function scheduler_node_load($nodes, $types) {
+  $nids = array();
+  foreach ($nodes as $node) {
+    $nids[] = $node->nid;
+  }
+  $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('<meta name="googlebot" content="unavailable_after: '. $unavailable_after .'" />');
+  }
+}
+
+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(time(), 'custom', $date_format))));
+    }
+    elseif ($publishtime && $publishtime < 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 > 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();
 }
 
 /**
@@ -440,9 +451,9 @@ function scheduler_cron() {
   $clear_cache = FALSE;
 
   // 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 < %d ', time());
+  $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' => time()));
 
-  while ($node = db_fetch_object($nodes)) {
+  foreach ($nodes as $node) {
     $n = node_load($node->nid);
     $n->changed = $node->publish_on;
     if (variable_get('scheduler_touch_'. $n->type, 0) == 1) {
@@ -457,11 +468,14 @@ function scheduler_cron() {
 
     // 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);
+      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_query('UPDATE {scheduler} SET publish_on = 0 WHERE nid = %d', $n->nid);
+      db_update('scheduler')
+        ->fields(array('publish_on' => 0))
+        ->condition('nid', $n->nid)
+        ->execute();
     }
 
     // invoke scheduler API
@@ -471,9 +485,9 @@ function scheduler_cron() {
   }
 
   // if the time is greater than the time to unpublish a node, unpublish it
-  $nodes = db_query('SELECT * 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', time());
+  $nodes = db_query('SELECT * 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 < :now', array(':now' => time()));
 
-  while ($node = db_fetch_object($nodes)) {
+  foreach ($nodes as $node) {
     // 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->unpublish_on;
@@ -483,7 +497,7 @@ function scheduler_cron() {
     $actions = array('node_unpublish_action', 'node_save_action');
     $context['node'] = $n;
     actions_do($actions, $n, $context, NULL, NULL);
-    db_query('DELETE FROM {scheduler} WHERE nid = %d', $n->nid);
+    scheduler_node_delete($n);
 
     // invoke scheduler API
     _scheduler_scheduler_api($n, 'unpublish');
@@ -536,17 +550,16 @@ function _scheduler_scheduler_api($node,
 }
 
 function _scheduler_timecheck() {
-  $now = time();
-  return theme('scheduler_timecheck', $now);
+  return theme('scheduler_timecheck', array('now' => 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'];
+  $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),
Index: scheduler.test
===================================================================
RCS file: scheduler.test
diff -N scheduler.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ scheduler.test	20 Jan 2010 02:10:46 -0000
@@ -0,0 +1,62 @@
+<?php
+// $Id$
+
+class SchedulerTestCase extends DrupalWebTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => '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'));
+    }
+  }
+}
