function total_task_times($report_data = null, $task = null)
 {
     if (!$report_data) {
         $report_data = array_var($_POST, 'report');
         // save selections into session
         $_SESSION['total_task_times_report_data'] = $report_data;
     }
     if (array_var($_GET, 'export') == 'csv') {
         $context = build_context_array(array_var($_REQUEST, 'context'));
         $report_data = json_decode(str_replace("'", '"', $_REQUEST['parameters']), true);
         tpl_assign('context', $context);
     } else {
         $context = active_context();
     }
     $columns = array_var($report_data, 'columns');
     if (!is_array($columns)) {
         $columns = array_var($_POST, 'columns', array());
     }
     asort($columns);
     //sort the array by column order
     foreach ($columns as $column => $order) {
         if ($order > 0) {
             $newColumn = new ReportColumn();
             //$newColumn->setReportId($newReport->getId());
             if (is_numeric($column)) {
                 $newColumn->setCustomPropertyId($column);
             } else {
                 $newColumn->setFieldName($column);
             }
         }
     }
     $user = Contacts::findById(array_var($report_data, 'user'));
     $now = DateTimeValueLib::now();
     $now->advance(logged_user()->getTimezone() * 3600, true);
     switch (array_var($report_data, 'date_type')) {
         case 1:
             //Today
             $st = DateTimeValueLib::make(0, 0, 0, $now->getMonth(), $now->getDay(), $now->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $now->getMonth(), $now->getDay(), $now->getYear());
             break;
         case 2:
             //This week
             $monday = $now->getMondayOfWeek();
             $nextMonday = $now->getMondayOfWeek()->add('w', 1)->add('d', -1);
             $st = DateTimeValueLib::make(0, 0, 0, $monday->getMonth(), $monday->getDay(), $monday->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $nextMonday->getMonth(), $nextMonday->getDay(), $nextMonday->getYear());
             break;
         case 3:
             //Last week
             $monday = $now->getMondayOfWeek()->add('w', -1);
             $nextMonday = $now->getMondayOfWeek()->add('d', -1);
             $st = DateTimeValueLib::make(0, 0, 0, $monday->getMonth(), $monday->getDay(), $monday->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $nextMonday->getMonth(), $nextMonday->getDay(), $nextMonday->getYear());
             break;
         case 4:
             //This month
             $st = DateTimeValueLib::make(0, 0, 0, $now->getMonth(), 1, $now->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $now->getMonth(), 1, $now->getYear())->add('M', 1)->add('d', -1);
             break;
         case 5:
             //Last month
             $now->add('M', -1);
             $st = DateTimeValueLib::make(0, 0, 0, $now->getMonth(), 1, $now->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $now->getMonth(), 1, $now->getYear())->add('M', 1)->add('d', -1);
             break;
         case 6:
             //Date interval
             $st = getDateValue(array_var($report_data, 'start_value'));
             $st = $st->beginningOfDay();
             $et = getDateValue(array_var($report_data, 'end_value'));
             $et = $et->endOfDay();
             break;
     }
     $timeslotType = array_var($report_data, 'timeslot_type', 0);
     $group_by = array();
     for ($i = 1; $i <= 3; $i++) {
         if ($timeslotType == 0) {
             $gb = array_var($report_data, 'group_by_' . $i);
         } else {
             $gb = array_var($report_data, 'alt_group_by_' . $i);
         }
         if ($gb != '0') {
             $group_by[] = $gb;
         }
     }
     $timeslots = Timeslots::getTaskTimeslots($context, null, $user, $st, $et, array_var($report_data, 'task_id', 0), $group_by, null, null, null, $timeslotType);
     $unworkedTasks = null;
     if (array_var($report_data, 'include_unworked') == 'checked') {
         $unworkedTasks = ProjectTasks::getPendingTasks(logged_user(), $workspace);
         tpl_assign('unworkedTasks', $unworkedTasks);
     }
     $gb_criterias = array();
     foreach ($group_by as $text) {
         if (in_array($text, array('contact_id', 'rel_object_id'))) {
             $gb_criterias[] = array('type' => 'column', 'value' => $text);
         } else {
             if (in_array($text, array('milestone_id', 'priority'))) {
                 $gb_criterias[] = array('type' => 'assoc_obj', 'fk' => 'rel_object_id', 'value' => $text);
             } else {
                 if (str_starts_with($text, 'dim_')) {
                     $gb_criterias[] = array('type' => 'dimension', 'value' => str_replace_first('dim_', '', $text));
                 }
             }
         }
     }
     $grouped_timeslots = groupObjects($gb_criterias, $timeslots);
     tpl_assign('columns', $columns);
     tpl_assign('timeslotsArray', array());
     tpl_assign('grouped_timeslots', $grouped_timeslots);
     if (array_var($report_data, 'date_type') == 6) {
         $st->advance(logged_user()->getTimezone() * 3600, true);
         $et->advance(logged_user()->getTimezone() * 3600, true);
     }
     tpl_assign('start_time', $st);
     tpl_assign('end_time', $et);
     tpl_assign('user', $user);
     tpl_assign('post', $report_data);
     tpl_assign('template_name', 'total_task_times');
     tpl_assign('title', lang('task time report'));
     tpl_assign('allow_export', false);
     if (array_var($_GET, 'export') == 'csv') {
         $this->setTemplate('total_task_times_csv');
         ajx_current("empty");
     } else {
         $this->setTemplate('report_wrapper');
     }
 }
 function total_task_times($report_data = null, $task = null, $csv = null)
 {
     if (!$report_data) {
         $report_data = array_var($_POST, 'report');
         set_user_config_option('timeReportDate', $report_data['date_type'], logged_user()->getId());
         $dateStart = getDateValue($report_data['start_value']);
         if ($dateStart instanceof DateTimeValue) {
             set_user_config_option('timeReportDateStart', $dateStart, logged_user()->getId());
         }
         $dateEnd = getDateValue($report_data['end_value']);
         if ($dateEnd instanceof DateTimeValue) {
             set_user_config_option('timeReportDateEnd', $dateEnd, logged_user()->getId());
         }
         set_user_config_option('timeReportShowEstimatedTime', array_var($report_data, 'show_estimated_time') == 'checked', logged_user()->getId());
         set_user_config_option('timeReportPerson', $report_data['user'], logged_user()->getId());
         set_user_config_option('timeReportTimeslotType', $report_data['timeslot_type'], logged_user()->getId());
         set_user_config_option('timeReportShowBilling', isset($report_data['show_billing']) ? 1 : 0, logged_user()->getId());
         $group = $report_data['group_by_1'] . ", " . $report_data['group_by_2'] . ", " . $report_data['group_by_3'];
         $altGroup = $report_data['alt_group_by_1'] . "," . $report_data['alt_group_by_2'] . "," . $report_data['alt_group_by_3'];
         set_user_config_option('timeReportGroupBy', $group, logged_user()->getId());
         set_user_config_option('timeReportAltGroupBy', $altGroup, logged_user()->getId());
         $_SESSION['total_task_times_report_data'] = $report_data;
     }
     if (array_var($_GET, 'export') == 'csv' || isset($csv) && $csv == true) {
         $context = build_context_array(array_var($_REQUEST, 'context'));
         CompanyWebsite::instance()->setContext($context);
         if (!$report_data) {
             if (isset($_REQUEST['parameters'])) {
                 $report_data = json_decode(str_replace("'", '"', $_REQUEST['parameters']), true);
             } else {
                 $report_data = $_REQUEST;
             }
         }
         tpl_assign('context', $context);
         $this->setTemplate('total_task_times_csv');
     } else {
         $context = active_context();
     }
     $columns = array_var($report_data, 'columns');
     if (!is_array($columns)) {
         $columns = array_var($_POST, 'columns', array());
     }
     asort($columns);
     //sort the array by column order
     foreach ($columns as $column => $order) {
         if ($order > 0) {
             $newColumn = new ReportColumn();
             //$newColumn->setReportId($newReport->getId());
             if (is_numeric($column)) {
                 $newColumn->setCustomPropertyId($column);
             } else {
                 $newColumn->setFieldName($column);
             }
         }
     }
     $user = Contacts::findById(array_var($report_data, 'user'));
     $now = DateTimeValueLib::now();
     $now->advance(logged_user()->getTimezone() * 3600, true);
     switch (array_var($report_data, 'date_type')) {
         case 1:
             //Today
             $st = DateTimeValueLib::make(0, 0, 0, $now->getMonth(), $now->getDay(), $now->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $now->getMonth(), $now->getDay(), $now->getYear());
             break;
         case 2:
             //This week
             $monday = $now->getMondayOfWeek();
             $nextMonday = $now->getMondayOfWeek()->add('w', 1)->add('d', -1);
             $st = DateTimeValueLib::make(0, 0, 0, $monday->getMonth(), $monday->getDay(), $monday->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $nextMonday->getMonth(), $nextMonday->getDay(), $nextMonday->getYear());
             break;
         case 3:
             //Last week
             $monday = $now->getMondayOfWeek()->add('w', -1);
             $nextMonday = $now->getMondayOfWeek()->add('d', -1);
             $st = DateTimeValueLib::make(0, 0, 0, $monday->getMonth(), $monday->getDay(), $monday->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $nextMonday->getMonth(), $nextMonday->getDay(), $nextMonday->getYear());
             break;
         case 4:
             //This month
             $st = DateTimeValueLib::make(0, 0, 0, $now->getMonth(), 1, $now->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $now->getMonth(), 1, $now->getYear())->add('M', 1)->add('d', -1);
             break;
         case 5:
             //Last month
             $now->add('M', -1);
             $st = DateTimeValueLib::make(0, 0, 0, $now->getMonth(), 1, $now->getYear());
             $et = DateTimeValueLib::make(23, 59, 59, $now->getMonth(), 1, $now->getYear())->add('M', 1)->add('d', -1);
             break;
         case 6:
             //Date interval
             $st = getDateValue(array_var($report_data, 'start_value'));
             $st = $st->beginningOfDay();
             $et = getDateValue(array_var($report_data, 'end_value'));
             $et = $et->endOfDay();
             break;
     }
     if ($st instanceof DateTimeValue) {
         $st->add('h', -logged_user()->getTimezone());
     }
     if ($et instanceof DateTimeValue) {
         $et->add('h', -logged_user()->getTimezone());
     }
     $timeslotType = array_var($report_data, 'timeslot_type', 0);
     $group_by = array();
     for ($i = 1; $i <= 3; $i++) {
         if ($timeslotType == 0) {
             $gb = array_var($report_data, 'group_by_' . $i);
         } else {
             $gb = array_var($report_data, 'alt_group_by_' . $i);
         }
         if ($gb != '0') {
             $group_by[] = $gb;
         }
     }
     $dateFormat = user_config_option('date_format');
     $date_format_tip = date_format_tip($dateFormat);
     $extra_conditions = "";
     $conditions = array_var($_POST, 'conditions', array());
     foreach ($conditions as $cond) {
         if ($cond['deleted'] > 0) {
             continue;
         }
         if (array_var($cond, 'custom_property_id') > 0) {
             if (!in_array($cond['condition'], array('like', 'not like', '=', '<=', '>=', '<', '>', '<>', '%'))) {
                 continue;
             }
             $cp = CustomProperties::getCustomProperty($cond['custom_property_id']);
             if (!$cp instanceof CustomProperty) {
                 continue;
             }
             $current_condition = ' AND e.rel_object_id IN ( SELECT object_id as id FROM ' . TABLE_PREFIX . 'custom_property_values cpv WHERE cpv.custom_property_id = ' . $cond['custom_property_id'];
             $value = $cond['value'];
             if ($cond['condition'] == 'like' || $cond['condition'] == 'not like') {
                 $value = '%' . $cond['value'] . '%';
             }
             if ($cp->getType() == 'date') {
                 if ($value == $date_format_tip) {
                     continue;
                 }
                 $dtValue = DateTimeValueLib::dateFromFormatAndString($dateFormat, $value);
                 $value = $dtValue->format('Y-m-d H:i:s');
             }
             if ($cond['condition'] != '%') {
                 if ($cp->getType() == 'numeric') {
                     $current_condition .= ' AND cpv.value ' . $cond['condition'] . ' ' . DB::escape($value);
                 } else {
                     if ($cp->getType() == 'boolean') {
                         $current_condition .= ' AND cpv.value ' . $cond['condition'] . ' ' . ($value ? '1' : '0');
                         if (!$value) {
                             $current_condition .= ') OR o.id NOT IN (SELECT object_id as id FROM ' . TABLE_PREFIX . 'custom_property_values cpv2 WHERE cpv2.object_id=o.id AND cpv2.value=1 AND cpv2.custom_property_id = ' . $cp->getId();
                         }
                     } else {
                         $current_condition .= ' AND cpv.value ' . $cond['condition'] . ' ' . DB::escape($value);
                     }
                 }
             } else {
                 $current_condition .= ' AND cpv.value like ' . DB::escape("%{$value}");
             }
             $current_condition .= ')';
             $extra_conditions .= $current_condition;
         }
     }
     $timeslots = Timeslots::getTaskTimeslots($context, null, $user, $st, $et, array_var($report_data, 'task_id', 0), $group_by, null, null, null, $timeslotType, $extra_conditions);
     $unworkedTasks = null;
     if (array_var($report_data, 'include_unworked') == 'checked') {
         $unworkedTasks = ProjectTasks::getPendingTasks(logged_user(), $workspace);
         tpl_assign('unworkedTasks', $unworkedTasks);
     }
     $gb_criterias = array();
     foreach ($group_by as $text) {
         if (in_array($text, array('contact_id', 'rel_object_id'))) {
             $gb_criterias[] = array('type' => 'column', 'value' => $text);
         } else {
             if (in_array($text, array('milestone_id', 'priority'))) {
                 $gb_criterias[] = array('type' => 'assoc_obj', 'fk' => 'rel_object_id', 'value' => $text);
             } else {
                 if (str_starts_with($text, 'dim_')) {
                     $gb_criterias[] = array('type' => 'dimension', 'value' => str_replace_first('dim_', '', $text));
                 }
             }
         }
     }
     $grouped_timeslots = groupObjects($gb_criterias, $timeslots);
     tpl_assign('columns', $columns);
     tpl_assign('timeslotsArray', array());
     tpl_assign('grouped_timeslots', $grouped_timeslots);
     if (array_var($report_data, 'date_type') == 6) {
         $st->advance(logged_user()->getTimezone() * 3600, true);
         $et->advance(logged_user()->getTimezone() * 3600, true);
     }
     tpl_assign('start_time', $st);
     tpl_assign('end_time', $et);
     tpl_assign('user', $user);
     tpl_assign('post', $report_data);
     tpl_assign('title', lang('task time report'));
     tpl_assign('allow_export', false);
     if (array_var($_GET, 'export') == 'csv' || isset($csv) && $csv == true) {
         $filename = $this->total_task_times_csv_export($grouped_timeslots);
         ajx_extra_data(array('filename' => "{$filename}.csv"));
         ajx_current("empty");
     } else {
         tpl_assign('template_name', 'total_task_times');
         $this->setTemplate('report_wrapper');
     }
 }