Index: scheduler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/scheduler/Attic/scheduler.module,v
retrieving revision 1.50.2.16
diff -u -r1.50.2.16 scheduler.module
--- scheduler.module	19 Apr 2009 11:21:22 -0000	1.50.2.16
+++ scheduler.module	19 Apr 2009 15:06:19 -0000
@@ -80,6 +80,7 @@
  */
 function scheduler_form_alter(&$form, $form_state, $form_id) {
   //allow scheduling on a per-node-type basis
+  global $user;
 
   if ('node_type_form' == $form_id) {
     $form['workflow']['scheduler'] = array(
@@ -102,12 +103,6 @@
       // if scheduling has been enabled for this node type
       if (variable_get('scheduler_'. $form['type']['#value'], 0) == 1) {
 
-        //use JScalendar picker for dates if the module exists and is enabled
-        $jscalendar = FALSE;
-        if (module_exists('jscalendar')) {
-          $jscalendar = TRUE;
-        }
-
         $node = $form['#node'];
 
         $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT);
@@ -116,55 +111,62 @@
         if (isset($node->nid) && $node->nid > 0) {
           $defaults = db_fetch_object(db_query('SELECT publish_on, unpublish_on FROM {scheduler} WHERE nid = %d', $node->nid));
         }
-	    else {
+        else {
           // init standard values
           $defaults = new StdClass;
           $defaults->publish_on = $defaults->unpublish_on = NULL;
-	    }
+        }
 	    
         if (isset($node->publish_on) && $node->publish_on) {
           if (is_numeric($node->publish_on)) {
               $defaults->publish_on = $node->publish_on;
           }
           else {
-            $defaults->publish_on = _scheduler_strtotime($node->publish_on);
-          }
+            $date = 
+            $defaults->publish_on = date_convert($node->publish_on, DATE_DATETIME, DATE_UNIX) - _scheduler_get_user_timezone();
           }
-          if (isset($node->unpublish_on) && $node->unpublish_on) {
+        }
+        if (isset($node->unpublish_on) && $node->unpublish_on) {
           if (is_numeric($node->unpublish_on)) {
               $defaults->unpublish_on = $node->unpublish_on;
           }
           else {
-            $defaults->unpublish_on = _scheduler_strtotime($node->unpublish_on);
+            $defaults->unpublish_on = date_convert($node->unpublish_on, DATE_DATETIME, DATE_UNIX) - _scheduler_get_user_timezone();
           }
         }
         
-
+        $isset_default_publish_on = isset($defaults->publish_on) && $defaults->publish_on != 0;
+        $isset_default_unpublish_on = isset($defaults->unpublish_on) && $defaults->unpublish_on != 0;
+        
         $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,
+          '#collapsed' => ($isset_default_publish_on || $isset_default_unpublish_on) ? FALSE: TRUE,
           '#weight' => 35
         );
 
         $form['scheduler_settings']['publish_on'] = array(
-          '#type' => 'textfield',
+          '#type' => 'date_text',
+          '#date_format' => $date_format,
           '#title' => t('Publish on'),
-          '#maxlength' => 25,
-          '#default_value' => isset($defaults->publish_on) && $defaults->publish_on ? format_date($defaults->publish_on, 'custom', $date_format) : '',
-          '#description' => t('Format: %time. Leave blank to disable scheduled publishing.', array('%time' => format_date(time(), 'custom', $date_format))),
-          '#attributes' => $jscalendar ? array('class' => 'jscalendar') : array()
+          '#default_value' => $isset_default_publish_on ? format_date($defaults->publish_on, 'custom', $date_format) : '',
+          '#description' => t('Leave blank to disable scheduled publishing.'),
         );
 
         $form['scheduler_settings']['unpublish_on'] = array(
-          '#type' => 'textfield',
+          '#type' => 'date_text',
+          '#date_format' => $date_format,
           '#title' => t('Unpublish on'),
-          '#maxlength' => 25,
-          '#default_value' => isset($defaults->unpublish_on) && $defaults->unpublish_on ? format_date($defaults->unpublish_on, 'custom', $date_format) : '',
-          '#description' => t('Format: %time. Leave blank to disable scheduled unpublishing.', array('%time' => format_date(time(), 'custom', $date_format))),
-          '#attributes' => $jscalendar ? array('class' => 'jscalendar') : array()
+          '#default_value' => $isset_default_unpublish_on ? format_date($defaults->unpublish_on, 'custom', $date_format) : '',
+          '#description' => t('Leave blank to disable scheduled unpublishing.'),
         );
+
+        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']['unpublish_on']['#type'] = 'date_popup';
+        }
       }
     }
   }
@@ -215,89 +217,6 @@
 }
 
 /**
- * Converts an english time string ('Y-m-d H:i:s') from the users timezone into an unix timestamp
- * @param string $str the time string ('Y-m-d H:i:s')
- * @return the time in unix timestamp representation (utc);
- * NULL, if $str is NULL, FALSE, empty, or contains only white spaces;
- * FALSE, if $str is malformed
- */
-function _scheduler_strtotime($str) {
-  if ($str && trim($str) != "" ) {
-    $time=_scheduler_strptime(trim($str), variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT));
-    if ($time!==FALSE) {
-      // success
-      $time -= _scheduler_get_user_timezone();
-    }
-  } else {
-    // $str is empty
-    $time = NULL;
-  }
-  return $time;
-}
-
-/**
- * Parse a time/date as UTC time
- *
- * @param string $date The string to parse
- * @param string $format The format used in $date. See date() (http://www.php.net/manual/en/function.date.php) 
- * specification for format options. Right now only dHhmiaAsyY are supported. 
- * @return the parsed time as a UTC timestamp
- * @see date()
- */
-function _scheduler_strptime($date, $format) {
-  # we need to build a regex pattern for the date format
-  $date_entities = array('d', 'H', 'h', 'm', 'i', 'a', 'A', 's', 'y', 'Y');
-  $date_regex_replacements = array('(\d{2})', '(\d{2})', '(\d{2})', '(\d{2})', '(\d{2})', '([ap]m)', '([AP]M)', '(\d{2})', '(\d{2})', '(\d{4})'); 
-  $custom_pattern = str_replace($date_entities, $date_regex_replacements, $format);
-  if (!preg_match("#$custom_pattern#", $date, $value_matches)) {
-    return FALSE;
-  }
-  
-  if (!preg_match_all("/(\w)/", $format, $entity_matches)) {
-    return FALSE;
-  }
-
-  $results = array('day' => 0, 'hour' => 0, 'month' => 0, 'minute' => 0, 'meridiem' => NULL, 'second' => 0, 'year' => 0);
-  $index = 1;
-  foreach ($entity_matches[1] as $entity) {
-    $value = intval($value_matches[$index]);
-    switch ($entity) {
-      case 'd':
-        $results['day'] = $value;
-        break;
-      case 'H':
-      case 'h':
-        $results['hour'] = $value; 
-        break;
-      case 'm':
-        $results['month'] = $value; 
-        break;
-      case 'i':
-        $results['minute'] = $value; 
-        break;
-      case 'a':
-      case 'A':
-        $results['meridiem'] = $value_matches[$index]; 
-        break;
-      case 's':
-        $results['second'] = $value; 
-        break;
-      case 'y':
-      case 'Y':
-        $results['year'] = $value; 
-        break;
-    }    
-    $index++;
-  }
-  if ((strncasecmp($results['meridiem'], "pm", 2) == 0) && ($results['hour'] <= 12)) {
-    $results['hour'] += 12;
-  }
-
-  $time = gmmktime( $results['hour'], $results['minute'], $results['second'], $results['month'], $results['day'], $results['year'] );
-  return $time;
-}
-
-/**
  * Gets the users timezone if configurable timezones are enabled or otherwise the default timezone of the site
  *
  * @return the offset of the users timezone in seconds
@@ -316,6 +235,7 @@
  */
 function scheduler_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
   // Run $op == load for any user.
+  global $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);
@@ -350,21 +270,23 @@
         // because DRUPAL6 removed 'submit' and added 'presave'
         // and all this happens at different times.
         if (isset($node->publish_on) && !is_numeric($node->publish_on)) {
-          $publishtime = _scheduler_strtotime($node->publish_on);
+          $publishtime = date_convert($node->publish_on, DATE_DATETIME, DATE_UNIX);
           if ($publishtime === FALSE) {
             form_set_error('publish_on', t('The entered publication date is invalid.'));
           }
           else {
+            $publishtime -= _scheduler_get_user_timezone();
             $node->publish_on = $publishtime;
           }
         }
         
         if (isset($node->unpublish_on) && !is_numeric($node->unpublish_on)) {
-          $unpublishtime = _scheduler_strtotime($node->unpublish_on);
+          $unpublishtime = date_convert($node->unpublish_on, DATE_DATETIME, DATE_UNIX);
           if ($unpublishtime === FALSE) {
             form_set_error('unpublish_on', t('The entered expiration date is invalid.'));
           }
           else {
+            $unpublishtime -= _scheduler_get_user_timezone();
             $node->unpublish_on = $unpublishtime;
           }
         }
