From abba52819ab8170c7c988c6408e5a63e90c22916 Mon Sep 17 00:00:00 2001
From: Marco Antonio Villegas Vega <marvil07@gmail.com>
Date: Wed, 4 Nov 2009 16:04:01 -0500
Subject: [PATCH] avoid use unintialized data

---
 theme/theme.inc |  161 +++++++++++++++++++++++++++++--------------------------
 1 files changed, 85 insertions(+), 76 deletions(-)

diff --git a/theme/theme.inc b/theme/theme.inc
index 19e01a3..957b5c5 100644
--- a/theme/theme.inc
+++ b/theme/theme.inc
@@ -293,20 +293,22 @@ function template_preprocess_calendar_day(&$vars) {
   
   // Move all_day items into the right columns and render them.
   $grouped_items = array();
-  foreach ($rows['all_day'] as $item) {
-    if (isset($item->{$grouping_field})) {
-      $column = $item->{$grouping_field};        
-      $item->{$grouping_field} = ''; // Remove the grouping field from the results.
-      if (!in_array($column, $columns)) {
-        $columns[] = $column;
+  if (!empty($rows['all_day'])) {
+    foreach ($rows['all_day'] as $item) {
+      if (isset($item->{$grouping_field})) {
+        $column = $item->{$grouping_field};        
+        $item->{$grouping_field} = ''; // Remove the grouping field from the results.
+        if (!in_array($column, $columns)) {
+          $columns[] = $column;
+        }
       }
+      else {
+        $column = t('Items');
+      }
+      $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
+      $grouped_items[$column][] = theme($theme, $item, $view);
+      $item_count++;
     }
-    else {
-      $column = t('Items');
-    }
-    $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
-    $grouped_items[$column][] = theme($theme, $item, $view);
-    $item_count++;
   }
   $vars['rows']['all_day'] = $grouped_items;
   
@@ -318,31 +320,33 @@ function template_preprocess_calendar_day(&$vars) {
   $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
   
   $grouped_items = array();
-  foreach ($rows['items'] as $time => $items) {
-    foreach ($items as $item) {
-      if (isset($item->{$grouping_field})) {
-        $column = $item->{$grouping_field};
-        $item->{$grouping_field} = ''; // Remove the grouping field from the results.
-        if (!in_array($column, $columns)) {
-          $columns[] = $column;
+  if (!empty($rows['items'])) {
+    foreach ($rows['items'] as $time => $items) {
+      foreach ($items as $item) {
+        if (isset($item->{$grouping_field})) {
+          $column = $item->{$grouping_field};
+          $item->{$grouping_field} = ''; // Remove the grouping field from the results.
+          if (!in_array($column, $columns)) {
+            $columns[] = $column;
+          }
         }
-      }
-      else {
-        $column = t('Items');
-      }
-      // Find the next time slot and fill it. Populate the skipped
-      // slots if the option to show empty times was chosen.
-      while ($time >= $next_start_time && $time < $end_start_time) {
-        if ((!empty($show_empty_times)) && !array_key_exists($start_time, $grouped_items)) {
-          $grouped_items[$start_time]['values'] = array();        
+        else {
+          $column = t('Items');
+        }
+        // Find the next time slot and fill it. Populate the skipped
+        // slots if the option to show empty times was chosen.
+        while ($time >= $next_start_time && $time < $end_start_time) {
+          if ((!empty($show_empty_times)) && !array_key_exists($start_time, $grouped_items)) {
+            $grouped_items[$start_time]['values'] = array();        
+          }
+          $start_time = $next_start_time;
+          $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
         }
-        $start_time = $next_start_time;
-        $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
+        $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
+        $grouped_items[$start_time]['values'][$column][] = theme($theme, $item, $view);
+        $item_count++;
+        $by_hour_count++;
       }
-      $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
-      $grouped_items[$start_time]['values'][$column][] = theme($theme, $item, $view);
-      $item_count++;
-      $by_hour_count++;
     }
   }
   
@@ -421,56 +425,61 @@ function template_preprocess_calendar_week(&$vars) {
   $end_start_time = '23:59:59';
   
   $grouped_items = array();
-  $vars['rows'] = $rows[0];
-  foreach ($rows[0] as $weekno => $row) {
-    $vars['rows'][$weekno] = $row['data'];
-    
-    // If we're not grouping by time, move all items into the 'all day' array.
-    if (empty($view->date_info->style_groupby_times)) {
-      foreach ($row['data']['items'] as $item) {
-        $row['data']['all_day'] += $item;
+  if (is_array($rows[0])) {
+    $vars['rows'] = $rows[0];
+    foreach ($rows[0] as $weekno => $row) {
+      $vars['rows'][$weekno] = $row['data'];
+
+      // If we're not grouping by time, move all items into the 'all day' array.
+      if (empty($view->date_info->style_groupby_times)) {
+        foreach ($row['data']['items'] as $item) {
+          $row['data']['all_day'] += $item;
+        }
+        $row['data']['items'] = array();
       }
-      $row['data']['items'] = array();
-    }
-    
-    $columns[] = $weekno;
-    $start_times = $view->date_info->style_groupby_times;
-    $start_time = array_shift($start_times);
-    $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
-    foreach ($row['data']['all_day'] as $key => $item) {
-      $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
-      $vars['rows'][$weekno]['all_day'][$key] = theme($theme, $item, $view);
-      $item_count++;
-    }
-    foreach ($row['data']['items'] as $time => $items) {
-      foreach ($items as $item) {
-        // Find the next time slot and fill it. Populate the skipped
-        // slots if the option to show empty times was chosen.
-        while ($time >= $next_start_time && $time < $end_start_time) {
-          if (($show_empty_times) && !array_key_exists($start_time, $grouped_items)) {
-            $grouped_items[$start_time]['values'][$weekno] = array();   
+
+      $columns[] = $weekno;
+      $start_times = $view->date_info->style_groupby_times;
+      $start_time = array_shift($start_times);
+      $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
+      foreach ($row['data']['all_day'] as $key => $item) {
+        $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
+        $vars['rows'][$weekno]['all_day'][$key] = theme($theme, $item, $view);
+        $item_count++;
+      }
+      foreach ($row['data']['items'] as $time => $items) {
+        foreach ($items as $item) {
+          // Find the next time slot and fill it. Populate the skipped
+          // slots if the option to show empty times was chosen.
+          while ($time >= $next_start_time && $time < $end_start_time) {
+            if (($show_empty_times) && !array_key_exists($start_time, $grouped_items)) {
+              $grouped_items[$start_time]['values'][$weekno] = array();   
+            }
+            $start_time = $next_start_time;
+            $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
           }
-          $start_time = $next_start_time;
-          $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
+          $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
+          $grouped_items[$start_time]['values'][$weekno][] = theme($theme, $item, $view);
+          $item_count++; 
+          $by_hour_count++; 
         }
-        $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
-        $grouped_items[$start_time]['values'][$weekno][] = theme($theme, $item, $view);
-        $item_count++; 
-        $by_hour_count++; 
       }
-    }
-  
-    // Finish out the day's time values if we want to see empty times.
-    if ($show_empty_times) {
-      while ($start_time < $end_start_time) {
-        if (!array_key_exists($start_time, $grouped_items)) {
-          $grouped_items[$start_time]['values'][$weekno] = array();        
+
+      // Finish out the day's time values if we want to see empty times.
+      if ($show_empty_times) {
+        while ($start_time < $end_start_time) {
+          if (!array_key_exists($start_time, $grouped_items)) {
+            $grouped_items[$start_time]['values'][$weekno] = array();        
+          }
+          $start_time = $next_start_time;
+          $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
         }
-        $start_time = $next_start_time;
-        $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
       }
     }
   }
+  else {
+    $vars['rows'] = array();
+  }
   ksort($grouped_items);
   
   // Do the headers last, once we know what the actual values are.
-- 
1.6.5

