+
+ // makeGroupByPart builds a combined group by part for sql query for time items using group_by1,
+ // group_by2, and group_by3 values passed in $options.
+ static function makeGroupByPart($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;
+
+ $group_by1 = $options['group_by1'];
+ $group_by2 = $options['group_by2'];
+ $group_by3 = $options['group_by3'];
+
+ switch ($group_by1) {
+ case 'date':
+ $group_by_parts .= ', l.date';
+ break;
+ case 'user':
+ $group_by_parts .= ', u.name';
+ break;
+ case 'client':
+ $group_by_parts .= ', c.name';
+ break;
+ case 'project':
+ $group_by_parts .= ', p.name';
+ break;
+ case 'task':
+ $group_by_parts .= ', t.name';
+ break;
+ case 'cf_1':
+ $group_by_parts .= ', cfo.value';
+ break;
+ }
+ switch ($group_by2) {
+ case 'date':
+ $group_by_parts .= ', l.date';
+ break;
+ case 'user':
+ $group_by_parts .= ', u.name';
+ break;
+ case 'client':
+ $group_by_parts .= ', c.name';
+ break;
+ case 'project':
+ $group_by_parts .= ', p.name';
+ break;
+ case 'task':
+ $group_by_parts .= ', t.name';
+ break;
+ case 'cf_1':
+ $group_by_parts .= ', cfo.value';
+ break;
+ }
+ switch ($group_by3) {
+ case 'date':
+ $group_by_parts .= ', l.date';
+ break;
+ case 'user':
+ $group_by_parts .= ', u.name';
+ break;
+ case 'client':
+ $group_by_parts .= ', c.name';
+ break;
+ case 'project':
+ $group_by_parts .= ', p.name';
+ break;
+ case 'task':
+ $group_by_parts .= ', t.name';
+ break;
+ case 'cf_1':
+ $group_by_parts .= ', cfo.value';
+ break;
+ }
+ // Remove garbage from the beginning.
+ $group_by_parts = ltrim($group_by_parts, ', ');
+ $group_by_part = "group by $group_by_parts";
+ return $group_by_part;
+ }
+
+ // makeGroupByExpensesPart builds a combined group by part for sql query for expense items using
+ // group_by1, group_by2, and group_by3 values passed in $options.
+ static function makeGroupByExpensesPart($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;
+
+ $group_by1 = $options['group_by1'];
+ $group_by2 = $options['group_by2'];
+ $group_by3 = $options['group_by3'];
+
+ switch ($group_by1) {
+ case 'date':
+ $group_by_parts .= ', ei.date';
+ break;
+ case 'user':
+ $group_by_parts .= ', u.name';
+ break;
+ case 'client':
+ $group_by_parts .= ', c.name';
+ break;
+ case 'project':
+ $group_by_parts .= ', p.name';
+ break;
+ }
+ switch ($group_by2) {
+ case 'date':
+ $group_by_parts .= ', ei.date';
+ break;
+ case 'user':
+ $group_by_parts .= ', u.name';
+ break;
+ case 'client':
+ $group_by_parts .= ', c.name';
+ break;
+ case 'project':
+ $group_by_parts .= ', p.name';
+ break;
+ }
+ switch ($group_by3) {
+ case 'date':
+ $group_by_parts .= ', ei.date';
+ break;
+ case 'user':
+ $group_by_parts .= ', u.name';
+ break;
+ case 'client':
+ $group_by_parts .= ', c.name';
+ break;
+ case 'project':
+ $group_by_parts .= ', p.name';
+ break;
+ }
+ // Remove garbage from the beginning.
+ $group_by_parts = ltrim($group_by_parts, ', ');
+ $group_by_part = "group by $group_by_parts";
+ return $group_by_part;
+ }
+
+ // makeConcatPart builds a concatenation part for getSubtotals query (for time items).
+ static function makeConcatPart($options) {
+ $group_by1 = $options['group_by1'];
+ $group_by2 = $options['group_by2'];
+ $group_by3 = $options['group_by3'];
+
+ switch ($group_by1) {
+ case 'date':
+ $what_to_concat .= ", ' - ', l.date";
+ break;
+ case 'user':
+ $what_to_concat .= ", ' - ', u.name";
+ break;
+ case 'client':
+ $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+ break;
+ case 'project':
+ $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+ break;
+ case 'task':
+ $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
+ break;
+ case 'cf_1':
+ $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
+ break;
+ }
+ switch ($group_by2) {
+ case 'date':
+ $what_to_concat .= ", ' - ', l.date";
+ break;
+ case 'user':
+ $what_to_concat .= ", ' - ', u.name";
+ break;
+ case 'client':
+ $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+ break;
+ case 'project':
+ $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+ break;
+ case 'task':
+ $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
+ break;
+ case 'cf_1':
+ $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
+ break;
+ }
+ switch ($group_by3) {
+ case 'date':
+ $what_to_concat .= ", ' - ', l.date";
+ break;
+ case 'user':
+ $what_to_concat .= ", ' - ', u.name";
+ break;
+ case 'client':
+ $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+ break;
+ case 'project':
+ $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+ break;
+ case 'task':
+ $what_to_concat .= ", ' - ', coalesce(t.name, 'Null')";
+ break;
+ case 'cf_1':
+ $what_to_concat .= ", ' - ', coalesce(cfo.value, 'Null')";
+ break;
+ }
+ // Remove garbage from both ends.
+ $what_to_concat = trim($what_to_concat, "', -");
+ $concat_part = "concat($what_to_concat) as group_field";
+ $concat_part = trim($concat_part, ' -');
+ return $concat_part;
+ }
+
+ // makeConcatPart builds a concatenation part for getSubtotals query (for expense items).
+ static function makeConcatExpensesPart($options) {
+ $group_by1 = $options['group_by1'];
+ $group_by2 = $options['group_by2'];
+ $group_by3 = $options['group_by3'];
+
+ switch ($group_by1) {
+ case 'date':
+ $what_to_concat .= ", ' - ', ei.date";
+ break;
+ case 'user':
+ $what_to_concat .= ", ' - ', u.name";
+ break;
+ case 'client':
+ $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+ break;
+ case 'project':
+ $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+ break;
+ }
+ switch ($group_by2) {
+ case 'date':
+ $what_to_concat .= ", ' - ', ei.date";
+ break;
+ case 'user':
+ $what_to_concat .= ", ' - ', u.name";
+ break;
+ case 'client':
+ $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+ break;
+ case 'project':
+ $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+ break;
+ }
+ switch ($group_by3) {
+ case 'date':
+ $what_to_concat .= ", ' - ', ei.date";
+ break;
+ case 'user':
+ $what_to_concat .= ", ' - ', u.name";
+ break;
+ case 'client':
+ $what_to_concat .= ", ' - ', coalesce(c.name, 'Null')";
+ break;
+ case 'project':
+ $what_to_concat .= ", ' - ', coalesce(p.name, 'Null')";
+ break;
+ }
+ // Remove garbage from both ends.
+ $what_to_concat = trim($what_to_concat, "', -");
+ $concat_part = "concat($what_to_concat) as group_field";
+ $concat_part = trim($concat_part, ' -');
+ return $concat_part;
+ }
+
+ // 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?
+
+ $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.
+ $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.
+ $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.
+ $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.
+ $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 ($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)
+ $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.option_id = cfo.id)';
+ }
+ return $join;
+ }
+
+ // 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.
+ $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.
+ $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.
+ $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;
+ }
+
+ // 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;
+ }
+
+ // makeGroupByHeader builds a column header for a totals-only report using group_by1,
+ // group_by2, and group_by3 values passed in $options.
+ static function makeGroupByHeader($options) {
+ global $i18n;
+ global $custom_fields;
+
+ $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_by1 = $options['group_by1'];
+ if ('cf_1' == $group_by1)
+ $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
+ else {
+ $key = 'label.'.$group_by1;
+ $group_by_header .= ' - '.$i18n->get($key);
+ }
+ }
+ if ($options['group_by2'] != null && $options['group_by2'] != 'no_grouping') {
+ // We have group_by2.
+ $group_by2 = $options['group_by2'];
+ if ('cf_1' == $group_by2)
+ $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
+ else {
+ $key = 'label.'.$group_by2;
+ $group_by_header .= ' - '.$i18n->get($key);
+ }
+ }
+ if ($options['group_by3'] != null && $options['group_by3'] != 'no_grouping') {
+ // We have group_by3.
+ $group_by3 = $options['group_by3'];
+ if ('cf_1' == $group_by3)
+ $group_by_header .= ' - '.$custom_fields->fields[0]['label'];
+ else {
+ $key = 'label.'.$group_by3;
+ $group_by_header .= ' - '.$i18n->get($key);
+ }
+ }
+ $group_by_header = ltrim($group_by_header, ' -');
+ return $group_by_header;
+ }