$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.
// Add custom field.
$include_cf_1 = $options['show_custom_field_1'] || $grouping_by_cf_1;
if ($include_cf_1) {
- $custom_fields = new CustomFields($user->group_id);
+ $custom_fields = new CustomFields($user->getActiveGroup());
$cf_1_type = $custom_fields->fields[0]['type'];
if ($cf_1_type == CustomFields::TYPE_TEXT) {
array_push($fields, 'cfl.value as cf_1');
$group_by_part = ttReportHelper::makeGroupByPart($options);
if ($options['show_cost']) {
if (MODE_TIME == $user->tracking_mode) {
- if (!ttReportHelper::groupingByUser($options))
+ if (!ttReportHelper::groupingBy('user', $options))
$left_join = 'left join tt_users u on (l.user_id = u.id)';
$sql = "select $concat_part, sum(time_to_sec(l.duration)) as time";
if ($options['show_work_units']) {
$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);
// Use custom fields plugin if it is enabled.
if ($user->isPluginEnabled('cf'))
- $custom_fields = new CustomFields($user->group_id);
+ $custom_fields = new CustomFields($user->getActiveGroup());
// Define some styles to use in email.
$style_title = 'text-align: center; font-size: 15pt; font-family: Arial, Helvetica, sans-serif;';
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>';
$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 = '';
$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>';
$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>';
// 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;
- $group_by_fields = ttReportHelper::makeGroupByFieldsPart($options); // TODO: refactor this, perhaps?
- if (strpos($group_by_fields, 'user') !== false) {
- // Grouping by user, add a join on tt_users table.
+ if (ttReportHelper::groupingBy('user', $options)) {
$join .= ' left join tt_users u on (l.user_id = u.id)';
}
- if (strpos($group_by_fields, 'client') !== false) {
- // Grouping by client, add a join on tt_clients table.
+ if (ttReportHelper::groupingBy('client', $options)) {
$join .= ' left join tt_clients c on (l.client_id = c.id)';
}
- if (strpos($group_by_fields, 'project') !== false) {
- // Grouping by project, add a join on tt_projects table.
+ if (ttReportHelper::groupingBy('project', $options)) {
$join .= ' left join tt_projects p on (l.project_id = p.id)';
}
- if (strpos($group_by_fields, 'task') !== false) {
- // Grouping by task, add a join on tt_tasks table.
+ if (ttReportHelper::groupingBy('task', $options)) {
$join .= ' left join tt_tasks t on (l.task_id = t.id)';
}
- if (strpos($group_by_fields, 'cf_1') !== false) {
- // Grouping by custom field 1, add a join for it.
- // $custom_fields = new CustomFields($user->group_id);
+ if (ttReportHelper::groupingBy('cf_1', $options)) {
+ $custom_fields = new CustomFields($user->getActiveGroup());
if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
$join .= ' left join tt_custom_field_log cfl on (l.id = cfl.log_id and cfl.status = 1) left join tt_custom_field_options cfo on (cfl.value = cfo.id)';
elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
// makeJoinExpensesPart builds a left join part for getSubtotals query for expense items.
static function makeJoinExpensesPart($options) {
- $group_by_fields = ttReportHelper::makeGroupByFieldsPart($options); // TODO: refactor this, perhaps?
- if (strpos($group_by_fields, 'user') !== false) {
- // Grouping by user, add a join on tt_users table.
+ if (ttReportHelper::groupingBy('user', $options)) {
$join .= ' left join tt_users u on (ei.user_id = u.id)';
}
- if (strpos($group_by_fields, 'client') !== false) {
- // Grouping by client, add a join on tt_clients table.
+ if (ttReportHelper::groupingBy('client', $options)) {
$join .= ' left join tt_clients c on (ei.client_id = c.id)';
}
- if (strpos($group_by_fields, 'project') !== false) {
- // Grouping by project, add a join on tt_projects table.
+ if (ttReportHelper::groupingBy('project', $options)) {
$join .= ' left join tt_projects p on (ei.project_id = p.id)';
}
return $join;
}
-
- // makeGroupByFieldsPart builds a commma-separated list of fields for sql query using group_by1,
- // group_by2, and group_by3 values passed in $options.
- static function makeGroupByFieldsPart($options) {
- $no_grouping = ($options['group_by1'] == null || $options['group_by1'] == 'no_grouping') &&
- ($options['group_by2'] == null || $options['group_by2'] == 'no_grouping') &&
- ($options['group_by3'] == null || $options['group_by3'] == 'no_grouping');
- if ($no_grouping) return null;
-
- if ($options['group_by1'] != null && $options['group_by1'] != 'no_grouping') {
- // We have group_by1.
- $group_by_fields .= ', '.$options['group_by1'];
- }
- if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
- // We have group_by2.
- $group_by_fields .= ', '.$options['group_by2'];
- }
- if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
- // We have group_by3.
- $group_by_fields .= ', '.$options['group_by3'];
- }
- $group_by_fields = ltrim($group_by_fields, ', ');
- return $group_by_fields;
- }
// grouping determines if we are grouping the report by either group_by1,
// group_by2, or group_by3 values passed in $options.
return $grouping;
}
- // groupingByUser determines if we are grouping a report by user.
- static function groupingByUser($options) {
- if ($options['group_by1'] == 'user' || $options['group_by2'] == 'user' || $options['group_by3'] == 'user') return true;
-
- return false;
- }
-
// groupingBy determines if we are grouping a report by a value of $what
- // ('user', 'project', etc.) by checking group_by1, group_by2, and group_by3
- // values passed in $options.
+ // ('date', '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;
}