Fixed ttReportHelper::groupingBy function.
[timetracker.git] / WEB-INF / lib / ttReportHelper.class.php
index 11f8841..d1de3af 100644 (file)
@@ -128,14 +128,14 @@ class ttReportHelper {
     $isClient = $user->isClient();
 
     $grouping = ttReportHelper::grouping($options);
-    $grouping_by_date = ($options['group_by1'] == 'date'|| $options['group_by2'] == 'date' || $options['group_by3'] == 'date');
-    $grouping_by_client = ($options['group_by1'] == 'client'|| $options['group_by2'] == 'client' || $options['group_by3'] == 'client');
-    $grouping_by_project = ($options['group_by1'] == 'project'|| $options['group_by2'] == 'project' || $options['group_by3'] == 'project');
-    $grouping_by_task = ($options['group_by1'] == 'task'|| $options['group_by2'] == 'task' || $options['group_by3'] == 'task');
-    $grouping_by_user = ($options['group_by1'] == 'user'|| $options['group_by2'] == 'user' || $options['group_by3'] == 'user');
-    $grouping_by_cf_1 = ($options['group_by1'] == 'cf_1'|| $options['group_by2'] == 'cf_1' || $options['group_by3'] == 'cf_1');
-
-    $group_by_option = $options['group_by1'];
+    if ($grouping) {
+      $grouping_by_date = ttReportHelper::groupingBy('date', $options);
+      $grouping_by_client = ttReportHelper::groupingBy('client', $options);
+      $grouping_by_project = ttReportHelper::groupingBy('project', $options);
+      $grouping_by_task = ttReportHelper::groupingBy('task', $options);
+      $grouping_by_user = ttReportHelper::groupingBy('user', $options);
+      $grouping_by_cf_1 = ttReportHelper::groupingBy('cf_1', $options);
+    }
     $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($options['show_start'] || $options['show_end']);
 
     // Prepare a query for time items in tt_log table.
@@ -609,8 +609,8 @@ class ttReportHelper {
     $isClient = $user->isClient();
 
     $items = ttReportHelper::getItems($options);
-    $group_by = $options['group_by1'];
-    if ($group_by && 'no_grouping' != $group_by)
+    $grouping = ttReportHelper::grouping($options);
+    if ($grouping)
       $subtotals = ttReportHelper::getSubtotals($options);
     $totals = ttReportHelper::getTotals($options);
 
@@ -643,14 +643,7 @@ class ttReportHelper {
 
     if ($options['show_totals_only']) {
       // Totals only report. Output subtotals.
-
-      // Determine group_by header.
-      if ('cf_1' == $group_by)
-        $group_by_header = htmlspecialchars($custom_fields->fields[0]['label']);
-      else {
-        $key = 'label.'.$group_by;
-        $group_by_header = $i18n->get($key);
-      }
+      $group_by_header = ttReportHelper::makeGroupByHeader($options);
 
       $body .= '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
       $body .= '<tr>';
@@ -743,7 +736,7 @@ class ttReportHelper {
       $body .= '</tr>';
 
       // Initialize variables to print subtotals.
-      if ($items && 'no_grouping' != $group_by) {
+      if ($items && $grouping) {
         $print_subtotals = true;
         $first_pass = true;
         $prev_grouped_by = '';
@@ -765,11 +758,11 @@ class ttReportHelper {
               $body .= '<tr style="'.$rowSubtotal.'">';
               $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
               $subtotal_name = htmlspecialchars($subtotals[$prev_grouped_by]['name']);
-              if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
-              if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
-              if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
-              if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
-              if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
+              if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
+              if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
+              if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
+              if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
+              if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
               if ($options['show_start']) $body .= '<td></td>';
               if ($options['show_end']) $body .= '<td></td>';
               if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['time'].'</td>';
@@ -841,11 +834,11 @@ class ttReportHelper {
         $body .= '<tr style="'.$rowSubtotal.'">';
         $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$i18n->get('label.subtotal').'</td>';
         $subtotal_name = htmlspecialchars($subtotals[$cur_grouped_by]['name']);
-        if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'user' ? $subtotal_name : '').'</td>';
-        if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'client' ? $subtotal_name : '').'</td>';
-        if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'project' ? $subtotal_name : '').'</td>';
-        if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'task' ? $subtotal_name : '').'</td>';
-        if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.($group_by == 'cf_1' ? $subtotal_name : '').'</td>';
+        if ($canViewReports || $isClient) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['user'].'</td>';
+        if ($options['show_client']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['client'].'</td>';
+        if ($options['show_project']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['project'].'</td>';
+        if ($options['show_task']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['task'].'</td>';
+        if ($options['show_custom_field_1']) $body .= '<td style="'.$cellLeftAlignedSubtotal.'">'.$subtotals[$prev_grouped_by]['cf_1'].'</td>';
         if ($options['show_start']) $body .= '<td></td>';
         if ($options['show_end']) $body .= '<td></td>';
         if ($options['show_duration']) $body .= '<td style="'.$cellRightAlignedSubtotal.'">'.$subtotals[$cur_grouped_by]['time'].'</td>';
@@ -1452,7 +1445,10 @@ class ttReportHelper {
 
   // makeJoinPart builds a left join part for getSubtotals query (for time items).
   static function makeJoinPart($options) {
-    global $custom_fields; // TODO: is it safe to assume the object is there when needed?
+    global $user;
+    if (ttReportHelper::groupingBy('cf_1', $options)) {
+      $custom_fields = new CustomFields($user->group_id);
+    }
 
     $group_by_fields = ttReportHelper::makeGroupByFieldsPart($options); // TODO: refactor this, perhaps?
     if (strpos($group_by_fields, 'user') !== false) {
@@ -1544,9 +1540,7 @@ class ttReportHelper {
   // ('user', 'project', etc.) by checking group_by1, group_by2, and group_by3
   // values passed in $options.
   static function groupingBy($what, $options) {
-    $grouping = ($options['group_by1'] != null && $options['group_by1'] != $what) ||
-      ($options['group_by2'] != null && $options['group_by2'] != $what) ||
-      ($options['group_by3'] != null && $options['group_by3'] != $what);
+    $grouping = ($options['group_by1'] == $what) || ($options['group_by2'] == $what) || ($options['group_by3'] == $what);
     return $grouping;
   }