2 // +----------------------------------------------------------------------+
 
   3 // | Anuko Time Tracker
 
   4 // +----------------------------------------------------------------------+
 
   5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
 
   6 // +----------------------------------------------------------------------+
 
   7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
 
   8 // | by anyone for any purpose, and freely redistributed alone or in
 
   9 // | combination with other software, provided that the license is obeyed.
 
  11 // | There are only two ways to violate the license:
 
  13 // | 1. To redistribute this code in source form, with the copyright
 
  14 // |    notice or license removed or altered. (Distributing in compiled
 
  15 // |    forms without embedded copyright notices is permitted).
 
  17 // | 2. To redistribute modified versions of this code in *any* form
 
  18 // |    that bears insufficient indications that the modifications are
 
  19 // |    not the work of the original author(s).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 require_once('initialize.php');
 
  31 import('form.ActionForm');
 
  32 import('DateAndTime');
 
  33 import('ttTeamHelper');
 
  34 import('ttGroupHelper');
 
  36 import('ttProjectHelper');
 
  37 import('ttFavReportHelper');
 
  38 import('ttClientHelper');
 
  39 import('ttReportHelper');
 
  42 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports'))) {
 
  43   header('Location: access_denied.php');
 
  47 // Use custom fields plugin if it is enabled.
 
  48 if ($user->isPluginEnabled('cf')) {
 
  49   require_once('plugins/CustomFields.class.php');
 
  50   $custom_fields = new CustomFields();
 
  51   $smarty->assign('custom_fields', $custom_fields);
 
  54 $form = new Form('reportForm');
 
  56 // Get saved favorite reports for user.
 
  57 $report_list = ttFavReportHelper::getReports();
 
  58 $form->addInput(array('type'=>'combobox',
 
  59   'name'=>'favorite_report',
 
  60   'onchange'=>'this.form.fav_report_changed.value=1;this.form.submit();',
 
  61   'style'=>'width: 250px;',
 
  63   'datakeys'=>array('id','name'),
 
  64   'empty'=>array('-1'=>$i18n->get('dropdown.no'))));
 
  65 $form->addInput(array('type'=>'hidden','name'=>'fav_report_changed'));
 
  66 // Generate and Delete buttons.
 
  67 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
 
  68 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete'),'onclick'=>"return confirm('".$i18n->get('form.reports.confirm_delete')."')"));
 
  70 // Dropdown for clients if the clients plugin is enabled.
 
  71 if ($user->isPluginEnabled('cl') && !$user->isClient()) {
 
  72   if ($user->can('view_reports') || $user->can('view_all_reports')) {
 
  73     $client_list = ttClientHelper::getClients(); // TODO: improve getClients for "view_reports"
 
  74                                                  // by filtering out not relevant clients.
 
  76     $client_list = ttClientHelper::getClientsForUser();
 
  77   $form->addInput(array('type'=>'combobox',
 
  79     'style'=>'width: 250px;',
 
  81     'datakeys'=>array('id', 'name'),
 
  82     'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
  85 // If we have a TYPE_DROPDOWN custom field - add control to select an option.
 
  86 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
  87     $form->addInput(array('type'=>'combobox','name'=>'option',
 
  88       'style'=>'width: 250px;',
 
  90       'data'=>$custom_fields->options,
 
  91       'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
  94 // Add controls for projects and tasks.
 
  95 if ($user->can('view_reports') || $user->can('view_all_reports')) {
 
  96   $project_list = ttProjectHelper::getProjects(); // All active and inactive projects.
 
  97 } elseif ($user->isClient()) {
 
  98   $project_list = ttProjectHelper::getProjectsForClient();
 
 100   $project_list = ttProjectHelper::getAssignedProjects($user->id);      
 
 102 $form->addInput(array('type'=>'combobox',
 
 103   'onchange'=>'fillTaskDropdown(this.value);selectAssignedUsers(this.value);',
 
 105   'style'=>'width: 250px;',
 
 106   'data'=>$project_list,
 
 107   'datakeys'=>array('id','name'),
 
 108   'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 109 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
 
 110   $task_list = ttGroupHelper::getActiveTasks();
 
 111   $form->addInput(array('type'=>'combobox',
 
 113     'style'=>'width: 250px;',
 
 115     'datakeys'=>array('id','name'),
 
 116     'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 119 // Add include records control.
 
 120 $include_options = array('1'=>$i18n->get('form.reports.include_billable'),
 
 121   '2'=>$i18n->get('form.reports.include_not_billable'));
 
 122 $form->addInput(array('type'=>'combobox',
 
 123   'name'=>'include_records',
 
 124   'style'=>'width: 250px;',
 
 125   'data'=>$include_options,
 
 126   'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 128 // Add invoiced / not invoiced selector.
 
 129 if ($user->can('manage_invoices')) {
 
 130   $invoice_options = array('1'=>$i18n->get('form.reports.include_invoiced'),
 
 131     '2'=>$i18n->get('form.reports.include_not_invoiced'));
 
 132   $form->addInput(array('type'=>'combobox',
 
 134     'style'=>'width: 250px;',
 
 135     'data'=>$invoice_options,
 
 136     'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 139 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps')) {
 
 140   $form->addInput(array('type'=>'combobox',
 
 141    'name'=>'paid_status',
 
 142    'style'=>'width: 250px;',
 
 143    'data'=>array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid')),
 
 144    'empty'=>array(''=>$i18n->get('dropdown.all'))
 
 148 $user_list = array();
 
 149 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
 
 150   // Prepare user and assigned projects arrays.
 
 151   if ($user->can('view_reports') || $user->can('view_all_reports')) {
 
 152     $max_rank = $user->rank-1;
 
 153     if ($user->can('view_all_reports')) $max_rank = MAX_RANK;
 
 154     if ($user->can('view_own_reports'))
 
 155       $options = array('max_rank'=>$max_rank,'include_self'=>true);
 
 157       $options = array('max_rank'=>$max_rank);
 
 158     $users = $user->getUsers($options); // Active and inactive users.
 
 160   elseif ($user->isClient())
 
 161     $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
 
 163   foreach ($users as $single_user) {
 
 164     $user_list[$single_user['id']] = $single_user['name'];
 
 165     $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
 
 167       foreach ($projects as $single_project) {
 
 168         $assigned_projects[$single_user['id']][] = $single_project['id'];
 
 172   $row_count = ceil(count($user_list)/3);
 
 173   $form->addInput(array('type'=>'checkboxgroup',
 
 177     'groupin'=>$row_count,
 
 178     'style'=>'width: 100%;'));
 
 181 // Add control for time period.
 
 182 $form->addInput(array('type'=>'combobox',
 
 184   'style'=>'width: 250px;',
 
 185   'data'=>array(INTERVAL_THIS_MONTH=>$i18n->get('dropdown.current_month'),
 
 186     INTERVAL_LAST_MONTH=>$i18n->get('dropdown.previous_month'),
 
 187     INTERVAL_THIS_WEEK=>$i18n->get('dropdown.current_week'),
 
 188     INTERVAL_LAST_WEEK=>$i18n->get('dropdown.previous_week'),
 
 189     INTERVAL_THIS_DAY=>$i18n->get('dropdown.current_day'),
 
 190     INTERVAL_LAST_DAY=>$i18n->get('dropdown.previous_day')),
 
 191   'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 192 // Add controls for start and end dates.
 
 193 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
 
 194 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
 
 196 // Add checkboxes for fields.
 
 197 if ($user->isPluginEnabled('cl'))
 
 198   $form->addInput(array('type'=>'checkbox','name'=>'chclient'));
 
 199 if (($user->can('manage_invoices') || $user->isClient()) && $user->isPluginEnabled('iv'))
 
 200   $form->addInput(array('type'=>'checkbox','name'=>'chinvoice'));
 
 201 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))
 
 202   $form->addInput(array('type'=>'checkbox','name'=>'chpaid'));
 
 203 if ($user->can('view_reports') || $user->can('view_all_reports'))
 
 204   $form->addInput(array('type'=>'checkbox','name'=>'chip'));
 
 205 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
 
 206   $form->addInput(array('type'=>'checkbox','name'=>'chproject'));
 
 207 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
 
 208   $form->addInput(array('type'=>'checkbox','name'=>'chtask'));
 
 209 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
 
 210   $form->addInput(array('type'=>'checkbox','name'=>'chstart'));
 
 211   $form->addInput(array('type'=>'checkbox','name'=>'chfinish'));
 
 213 $form->addInput(array('type'=>'checkbox','name'=>'chduration'));
 
 214 $form->addInput(array('type'=>'checkbox','name'=>'chnote'));
 
 215 $form->addInput(array('type'=>'checkbox','name'=>'chcost'));
 
 216 // If we have a custom field - add a checkbox for it.
 
 217 if ($custom_fields && $custom_fields->fields[0])
 
 218   $form->addInput(array('type'=>'checkbox','name'=>'chcf_1'));
 
 219 if ($user->isPluginEnabled('wu'))
 
 220   $form->addInput(array('type'=>'checkbox','name'=>'chunits'));
 
 222 // Add group by control.
 
 223 $group_by_options['no_grouping'] = $i18n->get('form.reports.group_by_no');
 
 224 $group_by_options['date'] = $i18n->get('form.reports.group_by_date');
 
 225 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
 
 226   $group_by_options['user'] = $i18n->get('form.reports.group_by_user');
 
 227 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id))
 
 228   $group_by_options['client'] = $i18n->get('form.reports.group_by_client');
 
 229 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
 
 230   $group_by_options['project'] = $i18n->get('form.reports.group_by_project');
 
 231 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
 
 232   $group_by_options['task'] = $i18n->get('form.reports.group_by_task');
 
 233 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 234   $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
 
 236 $group_by_options_size = sizeof($group_by_options);
 
 237 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by1','data'=>$group_by_options));
 
 238 if ($group_by_options_size > 2) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by2','data'=>$group_by_options));
 
 239 if ($group_by_options_size > 3) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by3','data'=>$group_by_options));
 
 240 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly'));
 
 242 // Add text field for a new favorite report name.
 
 243 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
 
 245 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
 
 247 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
 
 249 // Create a bean (which is a mechanism to remember form values in session).
 
 250 $bean = new ActionForm('reportBean', $form, $request);
 
 251 // At this point form values are obtained from session if they are there.
 
 253 if ($request->isGet() && !$bean->isSaved()) {
 
 254   // No previous form data were found in session. Use the following default values.
 
 255   $form->setValueByElement('users', array_keys($user_list));
 
 256   $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->date_format));
 
 257   $form->setValueByElement('start_date', $period->getStartDate());
 
 258   $form->setValueByElement('end_date', $period->getEndDate());
 
 259   $form->setValueByElement('chclient', '1');
 
 260   $form->setValueByElement('chinvoice', '0');
 
 261   $form->setValueByElement('chpaid', '0');
 
 262   $form->setValueByElement('chip', '0');
 
 263   $form->setValueByElement('chproject', '1');
 
 264   $form->setValueByElement('chstart', '1');
 
 265   $form->setValueByElement('chduration', '1');
 
 266   $form->setValueByElement('chcost', '0');
 
 267   $form->setValueByElement('chtask', '1');
 
 268   $form->setValueByElement('chfinish', '1');
 
 269   $form->setValueByElement('chnote', '1');
 
 270   $form->setValueByElement('chcf_1', '0');
 
 271   $form->setValueByElement('chunits', '0');
 
 272   $form->setValueByElement('chtotalsonly', '0');
 
 275 $form->setValueByElement('fav_report_changed','');
 
 277 // Disable the Delete button when no favorite report is selected.
 
 278 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
 
 279   $form->getElement('btn_delete')->setEnabled(false);
 
 281 if ($request->isPost()) {
 
 282   if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
 
 283     // User changed favorite report. We need to load new values into the form.
 
 284     if ($bean->getAttribute('favorite_report')) {
 
 285       // This loads new favorite report options into the bean (into our form).
 
 286       ttFavReportHelper::loadReport($user->id, $bean);
 
 288       // If user selected no favorite report - mark all user checkboxes (most probable scenario).
 
 289       if ($bean->getAttribute('favorite_report') == -1)
 
 290         $form->setValueByElement('users', array_keys($user_list));
 
 292       // Save form data in session for future use.
 
 294       header('Location: reports.php');
 
 297   } elseif ($bean->getAttribute('btn_save')) {
 
 298     // User clicked the Save button. We need to save form options as new favorite report.
 
 299     if (!ttValidString($bean->getAttribute('new_fav_report'))) $err->add($i18n->get('error.field'), $i18n->get('form.reports.save_as_favorite'));
 
 302       $id = ttFavReportHelper::saveReport($user->id, $bean); // TODO: review "on behalf" situations (both user and group), redesign if needed.
 
 304         $err->add($i18n->get('error.db'));
 
 306         $bean->setAttribute('favorite_report', $id);
 
 308         header('Location: reports.php');
 
 312   } elseif($bean->getAttribute('btn_delete')) {
 
 313     // Delete button pressed. User wants to delete a favorite report.
 
 314     if ($bean->getAttribute('favorite_report')) {
 
 315       ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
 
 316       // Load default report.
 
 317       $bean->setAttribute('favorite_report','');
 
 318       $bean->setAttribute('new_fav_report', $report_list[0]['name']);
 
 319       ttFavReportHelper::loadReport($user->id, $bean);
 
 320       $form->setValueByElement('users', array_keys($user_list));
 
 322       header('Location: reports.php');
 
 326     // Generate button pressed. Check some values.
 
 327     if (!$bean->getAttribute('period')) {
 
 328       $start_date = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
 
 330       if ($start_date->isError() || !$bean->getAttribute('start_date'))
 
 331         $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
 
 333       $end_date = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
 
 334       if ($end_date->isError() || !$bean->getAttribute('end_date'))
 
 335         $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
 
 337       if ($start_date->compare($end_date) > 0)
 
 338         $err->add($i18n->get('error.interval'), $i18n->get('label.end_date'), $i18n->get('label.start_date'));
 
 340     $group_by1 = $bean->getAttribute('group_by1');
 
 341     $group_by2 = $bean->getAttribute('group_by2');
 
 342     $group_by3 = $bean->getAttribute('group_by3');
 
 343     if (($group_by3 != null && $group_by3 != 'no_grouping') && ($group_by3 == $group_by1 || $group_by3 == $group_by2))
 
 344       $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
 
 345     if (($group_by2 != null && $group_by2 != 'no_grouping') && ($group_by2 == $group_by1 || $group_by3 == $group_by2))
 
 346       $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
 
 347     // Check remaining values.
 
 348     if (!ttReportHelper::verifyBean($bean)) $err->add($i18n->get('error.sys'));
 
 352       // Now we can go ahead and create a report.
 
 353       header('Location: report.php');
 
 359 $smarty->assign('project_list', $project_list);
 
 360 $smarty->assign('task_list', $task_list);
 
 361 $smarty->assign('assigned_projects', $assigned_projects);
 
 362 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 363 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
 
 364 $smarty->assign('title', $i18n->get('title.reports'));
 
 365 $smarty->assign('content_page_name', 'reports.tpl');
 
 366 $smarty->display('index.tpl');