/**
  * Execute report
  *
  * @param User $user
  * @param TimeReport $report
  * @param Project $project
  * @return array
  */
 function executeReport($user, $report, $project = null)
 {
     $conditions = $report->prepareConditions($user, $project);
     if (empty($conditions)) {
         return null;
     }
     // if
     if ($report->getSumByUser()) {
         $rows = db_execute_all('SELECT SUM(float_field_1) AS total_time, integer_field_1 AS user_id FROM ' . TABLE_PREFIX . 'project_objects WHERE ' . $conditions . ' GROUP BY integer_field_1');
         if (is_foreachable($rows)) {
             $result = array();
             foreach ($rows as $row) {
                 $user = Users::findById($row['user_id']);
                 if (instance_of($user, 'User')) {
                     $result[] = array('user' => $user, 'total_time' => float_format($row['total_time'], 2));
                 }
                 // if
             }
             // foreach
             return $result;
         } else {
             return null;
         }
         // if
     } else {
         return TimeRecords::findBySQL('SELECT * FROM ' . TABLE_PREFIX . 'project_objects WHERE ' . $conditions . ' ORDER BY date_field_1');
     }
     // if
 }
 /**
  * Show a single report
  *
  * @param void
  * @return null
  */
 function report()
 {
     if ($this->active_report->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_report->canView($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $report_records = TimeReports::executeReport($this->logged_user, $this->active_report, $this->active_project);
     $total_time = 0;
     if (is_foreachable($report_records)) {
         if ($this->active_report->getSumByUser()) {
             foreach ($report_records as $report_record) {
                 $total_time += $report_record['total_time'];
             }
             // foreach
         } else {
             foreach ($report_records as $report_record) {
                 $total_time += $report_record->getValue();
             }
             // foreach
         }
         // if
     }
     // if
     $this->smarty->assign(array('grouped_reports' => TimeReports::findGrouped(), 'report_records' => $report_records, 'total_time' => $total_time, 'show_project' => false));
     $this->setTemplate(array('module' => TIMETRACKING_MODULE, 'controller' => 'global_timetracking', 'template' => 'report'));
 }
 /**
  * Update existing report
  *
  * @param void
  * @return null
  */
 function edit()
 {
     $this->wireframe->print_button = false;
     if ($this->active_report->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     if (!$this->active_report->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $active_project = null;
     $project_id = $this->request->getId('project_id');
     if ($project_id) {
         $active_project = Projects::findById($project_id);
     }
     // if
     $report_data = $this->request->post('report');
     if (empty($report_data)) {
         $report_data = array('name' => $this->active_report->getName(), 'group_name' => $this->active_report->getGroupName(), 'user_filter' => $this->active_report->getUserFilter(), 'user_filter_data' => $this->active_report->getUserFilterData(), 'billable_filter' => $this->active_report->getBillableFilter(), 'date_filter' => $this->active_report->getDateFilter(), 'date_from' => $this->active_report->getDateFrom(), 'date_to' => $this->active_report->getDateTo(), 'sum_by_user' => $this->active_report->getSumByUser());
     }
     // if
     $this->smarty->assign(array('report_data' => $report_data, 'active_project' => $active_project));
     if ($this->request->isSubmitted()) {
         $old_name = $this->active_report->getName();
         $this->active_report->setAttributes($report_data);
         $this->active_report->setUserFilterData(array_var($report_data, 'user_filter_data'));
         $save = $this->active_report->save();
         if ($save && !is_error($save)) {
             flash_success("Report ':name' has been updated", array('name' => $old_name));
             $this->redirectToUrl($this->active_report->getUrl($active_project));
         } else {
             $this->smarty->assign('errors', $save);
         }
     }
     // if
 }