From f938dd262b73ec6a180602bb7a591a9a3ab0bf71 Mon Sep 17 00:00:00 2001
From: Lazarus-Long <Lazarus-Long@698858.no-reply.drupal.org>
Date: Wed, 23 May 2012 08:11:15 -0700
Subject: [PATCH 3/3] Separated processing from themeing for display formatter

---
 office_hours.module    |   30 ++++++++++-----
 office_hours.theme.inc |   97 ++++++++++++++++++++++++++++--------------------
 2 files changed, 77 insertions(+), 50 deletions(-)

diff --git a/office_hours.module b/office_hours.module
index 38d2fd9..c2149b3 100644
--- a/office_hours.module
+++ b/office_hours.module
@@ -17,6 +17,7 @@ function office_hours_theme ($existing, $type, $theme, $path) {
   );
   $themes = array(
     'office_hours_formatter_default' => $base + array('render element' => 'element'),
+    'office_hours_time_range' => $base + array('render element' => 'element'),
     'office_hours' => $base + array('render element' => 'element'),
     'field_multiple_value_form' => $base + array('render element' => 'element'),
     'office_hours_select' => $base + array('render element' => 'element'),
@@ -400,6 +401,26 @@ function office_hours_views_api() {
   );
 }
 
+/**
+ * Theme function for formatting time ranges
+ */
+function theme_office_hours_time_range($vars = array()) {
+  $vars += array(
+    'times' => array(
+      'start' => '',
+      'end' => '',
+    ),
+    'format' => 'G:i',
+    'separator' => ' - ',
+  );
+    
+  $startDT = new DateObject(_office_hours_mil_to_tf($vars['times']['start']));
+  $endDT   = new DateObject(_office_hours_mil_to_tf($vars['times']['end']));
+  
+  return $startDT->format($vars['format']) . $vars['separator'] . $endDT->format($vars['format']);
+}
+
+
 function _office_hours_arrange_items($items) {
   $days = array();
   foreach ($items as $item) {
@@ -447,15 +468,6 @@ function _office_hours_convert_to_ampm($hour) {
   }
   return $hr . ':' . $min . $ampm;
 }
-/**
- * Helper function for formatting time ranges
- */
-function _office_hours_format_time_range($times, $format, $separator = ' - ') {
-  $startDT = new DateObject(_office_hours_mil_to_tf($times['start']));
-  $endDT   = new DateObject(_office_hours_mil_to_tf($times['end']));
-  
-  return $startDT->format($format) . $separator . $endDT->format($format);
-}
 
 function _office_hours_tf_to_mil($hour) {
   if (strstr($hour, ':') == FALSE || is_null($hour)) {
diff --git a/office_hours.theme.inc b/office_hours.theme.inc
index 522125e..50c34a4 100644
--- a/office_hours.theme.inc
+++ b/office_hours.theme.inc
@@ -9,30 +9,13 @@
  * Theme function for 'default' text field formatter.
  */
 function office_hours_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
-  $element = array();
-  if ($items) {
-    $element[0] = array('#markup' => theme($field['type'] . '_formatter_default',
-                                           array('element' => $items,
-                                                 //'field' => $instance,
-                                                 'display' => $display,
-                                                 )),);
+  if (!$items) {
+    return array();
   }
-  return $element;
-}
-
-/**
- * Theme function for field formatter.
- * Adapted for 2 types:
- * - default, 'normal', tabular format
- * - inline format, adapted for use in interfaces such as Google Places.
- * This is put in 1 function, since there is a lot of pre-processing before the actual formatting,
- * So you'll find 2 parts: part1 = Preprocessing, part2 = Formatting, depending on the choosen formatter.
- * @TODO: use e.g. strtotime() / Date_module http://php.net/manual/en/function.date.php functions for proper time formatting.
- */
-function theme_office_hours_formatter_default($vars) {
-  $element = $vars['element'];
-  $settings = _office_hours_field_formatter_defaults($vars['display']['settings']);
 
+  $element = array();
+  $settings = _office_hours_field_formatter_defaults($display['settings']);
+  
   switch ($settings['daysformat']) {
     case 'number':
       $weekdays = range(1,7);
@@ -50,30 +33,30 @@ function theme_office_hours_formatter_default($vars) {
   }
 
   // Populate days and times
-  $items = array();
+  $lines = array();
   for ($i = 0; $i < 7; $i++) {
-    $items[$i] = array(
+    $lines[$i] = array(
       'startday'    => $i,
       'endday'      => null,
       'times'       => array(),
     );
   }
-  
-  foreach (element_children($element) as $key => $arraykey) {
-    $el  = $element[$arraykey];
+
+  foreach (element_children($items) as $key => $arraykey) {
+    $el  = $items[$arraykey];
     $day = (int)($el['day'] / 2);
     $alt = (int)($el['day'] % 2);
     
     $start  = check_plain($el['starthours']);
     $end    = check_plain($el['endhours']);
     
-    $items[$day]['times'][$alt]['start']  = $start;
-    $items[$day]['times'][$alt]['end']    = $end;
+    $lines[$day]['times'][$alt]['start']  = $start;
+    $lines[$day]['times'][$alt]['end']    = $end;
   }
   
   // Check if we're compressing times
   if ($settings['compress']) {
-    foreach ($items as $day => &$info) {
+    foreach ($lines as $day => &$info) {
       if (isset($info['times'][0]) && isset($info['times'][1])) {
         $info['times'][0]['start'] = min($info['times'][0]['start'], $info['times'][1]['start']);
         $info['times'][0]['end']   = max($info['times'][0]['end'], $info['times'][1]['end']);
@@ -85,23 +68,52 @@ function theme_office_hours_formatter_default($vars) {
   // Resort array to for the correct starting day
   $i = 0;
   $firstDay = variable_get('date_first_day', 0);
-  while ($i++ < $firstDay) array_push($items, array_shift($items));
+  while ($i++ < $firstDay) array_push($lines, array_shift($lines));
   
   // Check if we're grouping days
   if ($settings['grouped']) {
-    $times = $items[0]['times'];
+    $lines = $lines[0]['times'];
     
     for ($i = 1; $i < 7; $i++) {
-      if ($times != $items[$i]['times']) {
-        $times = $items[$i]['times'];
+      if ($lines != $lines[$i]['times']) {
+        $lines = $lines[$i]['times'];
       }
       else {
-        $items[$i]['endday']      = $items[$i]['startday'];
-        $items[$i]['startday']    = $items[$i-1]['startday'];
-        unset($items[$i-1]);
+        $lines[$i]['endday']      = $lines[$i]['startday'];
+        $lines[$i]['startday']    = $lines[$i-1]['startday'];
+        unset($lines[$i-1]);
       }
     }
   }
+
+  $element[] = array(
+    '#markup' => theme(
+      $field['type'] . '_formatter_default',
+      array(
+        'element' => $items,
+        'display' => $display,
+        'lines' => $lines,
+        'settings' => $settings,
+        'weekdays' => $weekdays,
+      )
+    ),
+  );
+
+  return $element;
+}
+
+/**
+ * Theme function for field formatter.
+ * Adapted for 2 types:
+ * - default, 'normal', tabular format
+ * - inline format, adapted for use in interfaces such as Google Places.
+ * This is put in 1 function, since there is a lot of pre-processing before the actual formatting,
+ * So you'll find 2 parts: part1 = Preprocessing, part2 = Formatting, depending on the choosen formatter.
+ */
+function theme_office_hours_formatter_default($vars) {
+  $items = $vars['lines'];
+  $settings = $vars['settings'];
+  $weekdays = $vars['weekdays'];
   
   // Display results
   $HTML = '';
@@ -122,10 +134,13 @@ function theme_office_hours_formatter_default($vars) {
       $times = array();
       for ($i = 0; $i < 2; $i++) {
         if (isset($info['times'][$i])) {
-          $times[] = _office_hours_format_time_range(
-                      $info['times'][$i],
-                      empty($settings['hourformat']) ? 'G:i' : 'g:i',
-                      $settings['separator_hours_hours']
+          $times[] = theme(
+                      'office_hours_time_range',
+                      array(
+                        'times'       => $info['times'][$i],
+                        'format'      => empty($settings['hourformat']) ? 'G:i' : 'g:i',
+                        'separator'   => $settings['separator_hours_hours'],
+                      )
                     );
         }
       }
-- 
1.7.10.msysgit.1

