Some more refactoring.
[timetracker.git] / WEB-INF / lib / ttReportHelper.class.php
index d76b027..6412827 100644 (file)
@@ -124,11 +124,11 @@ class ttReportHelper {
     $mdb2 = getConnection();
 
     // Determine these once as they are used in multiple places in this function.
-    $canViewReports = $user->can('view_reports');
+    $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
     $isClient = $user->isClient();
 
-    $group_by_option = $bean->getAttribute('group_by');
-    $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($bean->getAttribute('chstart') || $bean->getAttribute('chfinish'));
+    $group_by_option = $options['group_by'];
+    $convertTo12Hour = ('%I:%M %p' == $user->time_format) && ($options['show_start'] || $options['show_end']);
 
     // Prepare a query for time items in tt_log table.
     $fields = array(); // An array of fields for database query.
@@ -138,16 +138,16 @@ class ttReportHelper {
     if($canViewReports || $isClient)
       array_push($fields, 'u.name as user');
     // Add client name if it is selected.
-    if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+    if ($options['show_client'] || 'client' == $group_by_option)
       array_push($fields, 'c.name as client');
     // Add project name if it is selected.
-    if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+    if ($options['show_project'] || 'project' == $group_by_option)
       array_push($fields, 'p.name as project');
     // Add task name if it is selected.
-    if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
+    if ($options['show_task'] || 'task' == $group_by_option)
       array_push($fields, 't.name as task');
     // Add custom field.
-    $include_cf_1 = $bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option;
+    $include_cf_1 = $options['show_custom_field_1'] || 'cf_1' == $group_by_option;
     if ($include_cf_1) {
       $custom_fields = new CustomFields($user->group_id);
       $cf_1_type = $custom_fields->fields[0]['type'];
@@ -158,28 +158,28 @@ class ttReportHelper {
       }
     }
     // Add start time.
-    if ($bean->getAttribute('chstart')) {
+    if ($options['show_start']) {
       array_push($fields, "l.start as unformatted_start");
       array_push($fields, "TIME_FORMAT(l.start, '%k:%i') as start");
     }
     // Add finish time.
-    if ($bean->getAttribute('chfinish'))
+    if ($options['show_end'])
       array_push($fields, "TIME_FORMAT(sec_to_time(time_to_sec(l.start) + time_to_sec(l.duration)), '%k:%i') as finish");
     // Add duration.
-    if ($bean->getAttribute('chduration'))
+    if ($options['show_duration'])
       array_push($fields, "TIME_FORMAT(l.duration, '%k:%i') as duration");
     // Add work units.
-    if ($bean->getAttribute('chunits')) {
+    if ($options['show_work_units']) {
       if ($user->unit_totals_only)
         array_push($fields, "null as units");
       else
-       array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units");
+        array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units");
     }
     // Add note.
-    if ($bean->getAttribute('chnote'))
+    if ($options['show_note'])
       array_push($fields, 'l.comment as note');
     // Handle cost.
-    $includeCost = $bean->getAttribute('chcost');
+    $includeCost = $options['show_cost'];
     if ($includeCost) {
       if (MODE_TIME == $user->tracking_mode)
         array_push($fields, "cast(l.billable * coalesce(u.rate, 0) * time_to_sec(l.duration)/3600 as decimal(10,2)) as cost");   // Use default user rate.
@@ -188,31 +188,30 @@ class ttReportHelper {
       array_push($fields, "null as expense"); 
     }
     // Add paid status.
-    if ($canViewReports && $bean->getAttribute('chpaid'))
+    if ($canViewReports && $options['show_paid'])
       array_push($fields, 'l.paid as paid');
     // Add IP address.
-    if ($canViewReports && $bean->getAttribute('chip')) {
+    if ($canViewReports && $options['show_ip']) {
       array_push($fields, 'l.created as created');
       array_push($fields, 'l.created_ip as created_ip');
       array_push($fields, 'l.modified as modified');
       array_push($fields, 'l.modified_ip as modified_ip');
     }
-
     // Add invoice name if it is selected.
-    if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
+    if (($canViewReports || $isClient) && $options['show_invoice'])
       array_push($fields, 'i.name as invoice');
 
     // Prepare sql query part for left joins.
     $left_joins = null;
-    if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+    if ($options['show_client'] || 'client' == $group_by_option)
       $left_joins .= " left join tt_clients c on (c.id = l.client_id)";
-    if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
+    if (($canViewReports || $isClient) && $options['show_invoice'])
       $left_joins .= " left join tt_invoices i on (i.id = l.invoice_id and i.status = 1)";
     if ($canViewReports || $isClient || $user->isPluginEnabled('ex'))
        $left_joins .= " left join tt_users u on (u.id = l.user_id)";
-    if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+    if ($options['show_project'] || 'project' == $group_by_option)
       $left_joins .= " left join tt_projects p on (p.id = l.project_id)";
-    if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
+    if ($options['show_task'] || 'task' == $group_by_option)
       $left_joins .= " left join tt_tasks t on (t.id = l.task_id)";
     if ($include_cf_1) {
       if ($cf_1_type == CustomFields::TYPE_TEXT)
@@ -233,7 +232,7 @@ class ttReportHelper {
     // with an exception of sorting part, that is added in the end.
 
     // However, when we have expenses, we need to do a union with a separate query for expense items from tt_expense_items table.
-    if ($bean->getAttribute('chcost') && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
+    if ($options['show_cost'] && $user->isPluginEnabled('ex')) { // if ex(penses) plugin is enabled
 
       $fields = array(); // An array of fields for database query.
       array_push($fields, 'ei.id');
@@ -242,55 +241,53 @@ class ttReportHelper {
       if($canViewReports || $isClient)
         array_push($fields, 'u.name as user');
       // Add client name if it is selected.
-      if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+      if ($options['show_client'] || 'client' == $group_by_option)
         array_push($fields, 'c.name as client');
       // Add project name if it is selected.
-      if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+      if ($options['show_project'] || 'project' == $group_by_option)
         array_push($fields, 'p.name as project');
-      if ($bean->getAttribute('chtask') || 'task' == $group_by_option)
+      if ($options['show_task'] || 'task' == $group_by_option)
         array_push($fields, 'null'); // null for task name. We need to match column count for union.
-      if ($bean->getAttribute('chcf_1') || 'cf_1' == $group_by_option)
+      if ($options['show_custom_field_1'] || 'cf_1' == $group_by_option)
         array_push($fields, 'null'); // null for cf_1.
-      if ($bean->getAttribute('chstart')) {
+      if ($options['show_start']) {
         array_push($fields, 'null'); // null for unformatted_start.
         array_push($fields, 'null'); // null for start.
       }
-      if ($bean->getAttribute('chfinish'))
+      if ($options['show_end'])
         array_push($fields, 'null'); // null for finish.
-      if ($bean->getAttribute('chduration'))
+      if ($options['show_duration'])
         array_push($fields, 'null'); // null for duration.
-      // Add work units.
-      if ($bean->getAttribute('chunits'))
+      if ($options['show_work_units'])
         array_push($fields, 'null as units'); // null for work units.
       // Use the note field to print item name.
-      if ($bean->getAttribute('chnote'))
+      if ($options['show_note'])
         array_push($fields, 'ei.name as note');
       array_push($fields, 'ei.cost as cost');
       array_push($fields, 'ei.cost as expense');
       // Add paid status.
-      if ($canViewReports && $bean->getAttribute('chpaid'))
+      if ($canViewReports && $options['show_paid'])
         array_push($fields, 'ei.paid as paid');
       // Add IP address.
-      if ($canViewReports && $bean->getAttribute('chip')) {
+      if ($canViewReports && $options['show_ip']) {
         array_push($fields, 'ei.created as created');
         array_push($fields, 'ei.created_ip as created_ip');
         array_push($fields, 'ei.modified as modified');
         array_push($fields, 'ei.modified_ip as modified_ip');
       }
-
       // Add invoice name if it is selected.
-      if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
+      if (($canViewReports || $isClient) && $options['show_invoice'])
         array_push($fields, 'i.name as invoice');
 
       // Prepare sql query part for left joins.
       $left_joins = null;
       if ($canViewReports || $isClient)
         $left_joins .= " left join tt_users u on (u.id = ei.user_id)";
-      if ($bean->getAttribute('chclient') || 'client' == $group_by_option)
+      if ($options['show_client'] || 'client' == $group_by_option)
         $left_joins .= " left join tt_clients c on (c.id = ei.client_id)";
-      if ($bean->getAttribute('chproject') || 'project' == $group_by_option)
+      if ($options['show_project'] || 'project' == $group_by_option)
         $left_joins .= " left join tt_projects p on (p.id = ei.project_id)";
-      if (($canViewReports || $isClient) && $bean->getAttribute('chinvoice'))
+      if (($canViewReports || $isClient) && $options['show_invoice'])
         $left_joins .= " left join tt_invoices i on (i.id = ei.invoice_id and i.status = 1)";
 
       $where = ttReportHelper::getExpenseWhere($options);
@@ -304,13 +301,13 @@ class ttReportHelper {
 
     // Determine sort part.
     $sort_part = ' order by ';
-    if ('no_grouping' == $group_by_option || 'date' == $group_by_option)
+    if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option)
       $sort_part .= 'date';
     else
       $sort_part .= $group_by_option.', date';
-    if (($canViewReports || $isClient) && is_array($bean->getAttribute('users')) && 'user' != $group_by_option)
+    if (($canViewReports || $isClient) && $options['users'] && 'user' != $group_by_option)
       $sort_part .= ', user, type';
-    if ($bean->getAttribute('chstart'))
+    if ($options['show_start'])
       $sort_part .= ', unformatted_start';
     $sort_part .= ', id';
 
@@ -399,7 +396,7 @@ class ttReportHelper {
     $mdb2 = getConnection();
 
     // Determine these once as they are used in multiple places in this function.
-    $canViewReports = $user->can('view_reports');
+    $canViewReports = $user->can('view_reports') || $user->can('view_all_reports');
     $isClient = $user->isClient();
 
     $group_by_option = $options['group_by'];
@@ -450,7 +447,6 @@ class ttReportHelper {
       else
         array_push($fields, "if(l.billable = 0 or time_to_sec(l.duration)/60 < $user->first_unit_threshold, 0, ceil(time_to_sec(l.duration)/60/$user->minutes_in_unit)) as units");
     }
-
     // Add note.
     if ($options['show_note'])
       array_push($fields, 'l.comment as note');
@@ -577,11 +573,11 @@ class ttReportHelper {
 
     // Determine sort part.
     $sort_part = ' order by ';
-    if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option) // TODO: fix DB for NULL values in group_by field.
+    if ($group_by_option == null || 'no_grouping' == $group_by_option || 'date' == $group_by_option)
       $sort_part .= 'date';
     else
       $sort_part .= $group_by_option.', date';
-    if (($canViewReports || $isClient) /*&& is_array($bean->getAttribute('users'))*/ && 'user' != $group_by_option)
+    if (($canViewReports || $isClient) && $options['users'] && 'user' != $group_by_option)
       $sort_part .= ', user, type';
     if ($options['show_start'])
       $sort_part .= ', unformatted_start';
@@ -1804,28 +1800,21 @@ class ttReportHelper {
     $options['period'] = $bean->getAttribute('period');
     $options['period_start'] = $bean->getAttribute('start_date');
     $options['period_end'] = $bean->getAttribute('end_date');
-
-/*
- * TODO: remaining fields to fill in...
-  `show_client` tinyint(4) NOT NULL default 0,           # whether to show client column
-  `show_invoice` tinyint(4) NOT NULL default 0,          # whether to show invoice column
-  `show_paid` tinyint(4) NOT NULL default 0,             # whether to show paid column
-  `show_ip` tinyint(4) NOT NULL default 0,               # whether to show ip column
-  `show_project` tinyint(4) NOT NULL default 0,          # whether to show project column
-  `show_start` tinyint(4) NOT NULL default 0,            # whether to show start field
-  `show_duration` tinyint(4) NOT NULL default 0,         # whether to show duration field
-  `show_cost` tinyint(4) NOT NULL default 0,             # whether to show cost field
-  `show_task` tinyint(4) NOT NULL default 0,             # whether to show task column
-  `show_end` tinyint(4) NOT NULL default 0,              # whether to show end field
-  `show_note` tinyint(4) NOT NULL default 0,             # whether to show note column
-  `show_custom_field_1` tinyint(4) NOT NULL default 0,   # whether to show custom field 1
-  `show_work_units` tinyint(4) NOT NULL default 0,       # whether to show work units
-  `show_totals_only` tinyint(4) NOT NULL default 0,      # whether to show totals only
-  `group_by` varchar(20) default NULL,                   # group by field
-  `status` tinyint(4) default 1,                         # favorite report status
-  PRIMARY KEY (`id`)
-);
-     */
+    $options['show_client'] = $bean->getAttribute('chclient');
+    $options['show_invoice'] = $bean->getAttribute('chinvoice');
+    $options['show_paid'] = $bean->getAttribute('chpaid');
+    $options['show_ip'] = $bean->getAttribute('chip');
+    $options['show_project'] = $bean->getAttribute('chproject');
+    $options['show_start'] = $bean->getAttribute('chstart');
+    $options['show_duration'] = $bean->getAttribute('chduration');
+    $options['show_cost'] = $bean->getAttribute('chcost');
+    $options['show_task'] = $bean->getAttribute('chtask');
+    $options['show_end'] = $bean->getAttribute('chfinish');
+    $options['show_note'] = $bean->getAttribute('chnote');
+    $options['show_custom_field_1'] = $bean->getAttribute('chcf_1');
+    $options['show_work_units'] = $bean->getAttribute('chunits');
+    $options['show_totals_only'] = $bean->getAttribute('chtotalsonly');
+    $options['group_by'] = $bean->getAttribute('group_by');
     return $options;
   }