Index: station_schedule.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/schedule/station_schedule.install,v
retrieving revision 1.17
diff -u -r1.17 station_schedule.install
--- station_schedule.install  3 Nov 2007 21:42:44 -0000 1.17
+++ station_schedule.install  14 Nov 2007 22:48:22 -0000
@@ -13,6 +13,8 @@
         CREATE TABLE {station_schedule} (
           `nid` int unsigned NOT NULL default '0',
           `increment` int unsigned NOT NULL default '0',
+          `start` int unsigned NOT NULL default '0', 
+          `finish` int unsigned NOT NULL default '1440',          
           `streams` longtext,
           `unscheduled_message` VARCHAR(255) NOT NULL default '',
           PRIMARY KEY(`nid`)
@@ -377,3 +379,22 @@
   }
   return $ret;
 }
+
+/**
+ * Add start and end times to schedules.
+ */
+function station_schedule_update_5204() {
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("ALTER TABLE {station_schedule} ADD COLUMN `start` int unsigned AFTER `increment`");      
+      $ret[] = update_sql("ALTER TABLE {station_schedule} ADD COLUMN `finish` int unsigned AFTER `start`");      
+
+      db_query("UPDATE {station_schedule} SET start = '0'");
+      db_query("UPDATE {station_schedule} SET finish = '1440'");
+      
+      break;
+  }
+  return $ret;
+}
Index: station_schedule.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/schedule/station_schedule.module,v
retrieving revision 1.64
diff -u -r1.64 station_schedule.module
--- station_schedule.module 13 Nov 2007 04:02:29 -0000  1.64
+++ station_schedule.module 14 Nov 2007 22:51:50 -0000
@@ -206,6 +206,24 @@
     '#options' => array(15 => t('15 Minutes'), 30 => t('30 Minutes'), 60 => t('1 Hour'), 120 => t('2 Hours')),
     '#description' => t("This is the minimum increment that programs can be scheduled in. <strong>Caution:</strong> Increasing this value on an existing schedule will probably cause wierdness."),
   );
+  $hour_options = array();
+  for ($hour = 0; $hour <= 24; $hour++) {
+    $hour_options[] = $hour;
+  }  
+  $form['settings']['start'] = array(
+    '#type' => 'select',
+    '#title' => t('Schedule start'),
+    '#default_value' => isset($node->settings['start']) ? $node->settings['start']/60 : 0,
+    '#options' => $hour_options,
+    '#description' => t("The hour of the day at which the schedule begins."),
+  );  
+  $form['settings']['finish'] = array(
+    '#type' => 'select',
+    '#title' => t('Schedule finish'),
+    '#default_value' => isset($node->settings['finish']) ? $node->settings['finish']/60 : 24,
+    '#options' => $hour_options,
+    '#description' => t("The hour of the day at which the schedule ends. May be earlier than or equal to the start to wrap around to the next morning."),
+  );  
   $form['settings']['unscheduled_message'] = array(
     '#type' => 'textfield',
     '#title' => t('No scheduled programming message'),
@@ -317,6 +335,11 @@
     }
   }
   $node->settings['streams'] = $streams;
+  
+  // scale up schedule range hours for storage as minutes
+  $node->settings['finish'] *= 60; 
+  $node->settings['start']  *= 60;
+  
 }
 
 
@@ -324,15 +347,36 @@
  * Implementation of hook_load().
  */
 function station_schedule_load($node) {
+
+  // Load the settings.
+  $settings = db_fetch_array(db_query('SELECT increment, start, finish, streams, unscheduled_message FROM {station_schedule} WHERE nid = %d', $node->nid));
+  if (isset($settings['streams']) && $streams = unserialize($settings['streams'])) {
+    $settings['streams'] = $streams;
+  }
+  else {
+    $settings['streams'] = array();
+  }
+
+  // Load the schedule.
   $schedule = array();
+  // get the start and end times of the schedule
+  $schedule_start   = $settings['start'];
+  $schedule_finish  = $settings['finish'];
+  // if the schedule finish time comes before start, the schedule day goes on into the next morning
+  if ($schedule_finish <= $schedule_start) {
+      $schedule_finish += 1440;
+  }
+  
   // Use station_day_name() for the day ordering in case Sunday isn't the
   // first day of the week.
   foreach (station_day_name() as $day => $name) {
     $schedule[$day] = array();
 
-    $start = $day * MINUTES_IN_DAY;
-    $finish = $start + MINUTES_IN_DAY;
+    $start = $day * MINUTES_IN_DAY + $schedule_start;
+    $finish = $day * MINUTES_IN_DAY + $schedule_finish;
+    
     $result = db_query('SELECT * FROM {station_schedule_item} i WHERE i.schedule_nid = %d AND i.finish > %d AND i.start < %d ORDER BY i.start', $node->nid, $start, $finish);
+
     while ($s = db_fetch_object($result)) {
       // If a show spans a day, limit its start and finish times to be with-in
       // the day.
@@ -344,15 +388,23 @@
       }
       $schedule[$day][] = $s;
     }
-  }
-
-  // Load the settings.
-  $settings = db_fetch_array(db_query('SELECT increment, streams, unscheduled_message FROM {station_schedule} WHERE nid = %d', $node->nid));
-  if (isset($settings['streams']) && $streams = unserialize($settings['streams'])) {
-    $settings['streams'] = $streams;
-  }
-  else {
-    $settings['streams'] = array();
+        
+    // load Sunday morning to show on the end of Saturday night, if the schedule display goes beyond midnight
+    if ($day == 6 and $finish > 7 * MINUTES_IN_DAY) {
+      $result = db_query('SELECT * FROM {station_schedule_item} i WHERE i.schedule_nid = %d AND i.finish > %d AND i.start < %d ORDER BY i.start', $node->nid, 0, $finish - 7 * MINUTES_IN_DAY);
+
+      while ($s = db_fetch_object($result)) {
+        // spoof the times to appear beyond the end of the week for the schedule layout
+        $s->finish += 7 * MINUTES_IN_DAY;
+        $s->start  += 7 * MINUTES_IN_DAY;
+        
+        // only need to trim the end of the show: a show starting before 0 has already been loaded above
+        if ($s->finish > $finish) {
+          $s->finish = $finish;
+        }
+        $schedule[$day][] = $s;
+      }
+    }    
   }
 
   return array(
@@ -365,7 +417,7 @@
  * Implementation of hook_insert().
  */
 function station_schedule_insert($node) {
-  db_query("INSERT INTO {station_schedule} (nid, increment, streams, unscheduled_message) VALUES (%d, %d, '%s', '%s')", $node->nid, $node->settings['increment'], serialize($node->settings['streams']), $node->settings['unscheduled_message']);
+  db_query("INSERT INTO {station_schedule} (nid, increment, start, finish, streams, unscheduled_message) VALUES (%d, %d, %d, %d, '%s', '%s')", $node->nid, $node->settings['increment'], $node->settings['start'], $node->settings['finish'], serialize($node->settings['streams']), $node->settings['unscheduled_message']);
 }
 
 /**
@@ -381,7 +433,7 @@
  */
 function station_schedule_update($node) {
   db_query("DELETE FROM {station_schedule} WHERE nid = %d", $node->nid);
-  db_query("INSERT INTO {station_schedule} (nid, increment, streams, unscheduled_message) VALUES (%d, %d, '%s', '%s')", $node->nid, $node->settings['increment'], serialize($node->settings['streams']), $node->settings['unscheduled_message']);
+  db_query("INSERT INTO {station_schedule} (nid, increment, start, finish, streams, unscheduled_message) VALUES (%d, %d, %d, %d, '%s', '%s')", $node->nid, $node->settings['increment'], $node->settings['start'], $node->settings['finish'], serialize($node->settings['streams']), $node->settings['unscheduled_message']);
 }
 
 function station_schedule_admin_settings() {
@@ -926,6 +978,14 @@
   drupal_add_css(drupal_get_path('module', 'station_schedule') .'/station_schedule.css');
   drupal_set_title(check_plain($node->title));
 
+  // get the start and end times of the schedule  
+  $schedule_start   = $node->settings['start'];
+  $schedule_finish  = $node->settings['finish'];
+  
+  if ($schedule_finish <= $schedule_start) {
+    $schedule_finish += 1440;
+  } // for wrapping: to come later
+
   $header = array();
   $row = array();
   foreach ($node->schedule as $day => $items) {
@@ -933,8 +993,9 @@
     $row[$day] = '';
 
     // The last finish pointer starts at the beginning of the day.
-    $last_finish = $day * MINUTES_IN_DAY;
-    $day_finish = ($day + 1) * MINUTES_IN_DAY;
+    $last_finish = $day * MINUTES_IN_DAY + $schedule_start;
+    $day_finish  = $day * MINUTES_IN_DAY + $schedule_finish;
+    
     foreach ($items as $item) {
       // Display blocks for unscheduled time periods
       if ($last_finish != $item->start) {
@@ -959,6 +1020,7 @@
 function theme_station_schedule_admin_nonitem($node, $start, $finish) {
   $class = 'station-sch-box station-sch-unscheduled';
   $height = ($finish - $start);
+  
   $link = url("node/{$node->nid}/schedule/add/0/{$start}/{$finish}");
 
   $output = "<div class='{$class}'><a id='schedule-{$start}' href='{$link}' style='height: {$height}px;'>";
@@ -1219,10 +1281,17 @@
     $header[0] = array('data' => t('Time'));
     $row = array();
 
+  // get the start and end times of the schedule
+    $schedule_start   = $node->settings['start'];
+    $schedule_finish  = $node->settings['finish'];
+    if ($schedule_finish <= $schedule_start) {
+      $schedule_finish += 1440; 
+    } // for wrapping: to come later
+
     // First column is hours.
     $row[0]['id'] = 'station-sch-hours';
-    for ($hour = 0; $hour < 24; $hour++) {
-      $row[0]['data'] .= theme('station_schedule_hour', $hour);
+    for ($hour = $schedule_start / 60; $hour < $schedule_finish / 60; $hour++) {
+      $row[0]['data'] .= theme('station_schedule_hour', $hour % 24);
     }
 
     // Then a column for each day of the week.
@@ -1231,8 +1300,8 @@
       $row[$day + 1]['data'] = '';
 
       // The last finish pointer starts at the beginning of the day.
-      $last_finish = $day * MINUTES_IN_DAY;
-      $day_finish = ($day + 1) * MINUTES_IN_DAY;
+      $last_finish = $day * MINUTES_IN_DAY + $schedule_start;
+      $day_finish  = $day * MINUTES_IN_DAY + $schedule_finish;
       foreach ($items as $item) {
         // Display blocks for unscheduled time periods
         if ($last_finish != $item->start) {
