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('ttGroupHelper');
 
  35 import('ttProjectHelper');
 
  36 import('ttFavReportHelper');
 
  37 import('ttClientHelper');
 
  38 import('ttReportHelper');
 
  41 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports'))) {
 
  42   header('Location: access_denied.php');
 
  45 if (!$user->exists()) {
 
  46   header('Location: access_denied.php'); // No users in subgroup.
 
  49 // End of access checks.
 
  51 $trackingMode = $user->getTrackingMode();
 
  53 // Use custom fields plugin if it is enabled.
 
  54 if ($user->isPluginEnabled('cf')) {
 
  55   require_once('plugins/CustomFields.class.php');
 
  56   $custom_fields = new CustomFields();
 
  57   $smarty->assign('custom_fields', $custom_fields);
 
  60 $form = new Form('reportForm');
 
  62 // Get saved favorite reports for user.
 
  63 $report_list = ttFavReportHelper::getReports();
 
  64 $form->addInput(array('type'=>'combobox',
 
  65   'name'=>'favorite_report',
 
  66   'onchange'=>'this.form.fav_report_changed.value=1;this.form.submit();',
 
  67   'style'=>'width: 250px;',
 
  69   'datakeys'=>array('id','name'),
 
  70   'empty'=>array('-1'=>$i18n->get('dropdown.no'))));
 
  71 $form->addInput(array('type'=>'hidden','name'=>'fav_report_changed'));
 
  72 // Generate and Delete buttons.
 
  73 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
 
  74 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete'),'onclick'=>"return confirm('".$i18n->get('form.reports.confirm_delete')."')"));
 
  76 // Dropdown for clients if the clients plugin is enabled.
 
  77 if ($user->isPluginEnabled('cl') && !$user->isClient()) {
 
  78   if ($user->can('view_reports') || $user->can('view_all_reports')) {
 
  79     $client_list = ttClientHelper::getClients(); // TODO: improve getClients for "view_reports"
 
  80                                                  // by filtering out not relevant clients.
 
  82     $client_list = ttClientHelper::getClientsForUser();
 
  83   $form->addInput(array('type'=>'combobox',
 
  85     'style'=>'width: 250px;',
 
  87     'datakeys'=>array('id', 'name'),
 
  88     'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
  91 // If we have a TYPE_DROPDOWN custom field - add control to select an option.
 
  92 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
  93     $form->addInput(array('type'=>'combobox','name'=>'option',
 
  94       'style'=>'width: 250px;',
 
  96       'data'=>$custom_fields->options,
 
  97       'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 100 // Add controls for projects and tasks.
 
 101 if ($user->can('view_reports') || $user->can('view_all_reports')) {
 
 102   $project_list = ttProjectHelper::getProjects(); // All active and inactive projects.
 
 103 } elseif ($user->isClient()) {
 
 104   $project_list = ttProjectHelper::getProjectsForClient();
 
 106   $project_list = ttProjectHelper::getAssignedProjects($user->getUser());
 
 108 $form->addInput(array('type'=>'combobox',
 
 109   'onchange'=>'fillTaskDropdown(this.value);selectAssignedUsers(this.value);',
 
 111   'style'=>'width: 250px;',
 
 112   'data'=>$project_list,
 
 113   'datakeys'=>array('id','name'),
 
 114   'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 115 if (MODE_PROJECTS_AND_TASKS == $trackingMode) {
 
 116   $task_list = ttGroupHelper::getActiveTasks();
 
 117   $form->addInput(array('type'=>'combobox',
 
 119     'style'=>'width: 250px;',
 
 121     'datakeys'=>array('id','name'),
 
 122     'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 125 // Add include records control.
 
 126 $include_options = array('1'=>$i18n->get('form.reports.include_billable'),
 
 127   '2'=>$i18n->get('form.reports.include_not_billable'));
 
 128 $form->addInput(array('type'=>'combobox',
 
 129   'name'=>'include_records',
 
 130   'style'=>'width: 250px;',
 
 131   'data'=>$include_options,
 
 132   'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 134 // Add invoiced / not invoiced selector.
 
 135 if ($user->can('manage_invoices')) {
 
 136   $invoice_options = array('1'=>$i18n->get('form.reports.include_invoiced'),
 
 137     '2'=>$i18n->get('form.reports.include_not_invoiced'));
 
 138   $form->addInput(array('type'=>'combobox',
 
 140     'style'=>'width: 250px;',
 
 141     'data'=>$invoice_options,
 
 142     'empty'=>array(''=>$i18n->get('dropdown.all'))));
 
 145 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps')) {
 
 146   $form->addInput(array('type'=>'combobox',
 
 147    'name'=>'paid_status',
 
 148    'style'=>'width: 250px;',
 
 149    'data'=>array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid')),
 
 150    'empty'=>array(''=>$i18n->get('dropdown.all'))
 
 154 // TODO: check rights.
 
 155 if ($user->isPluginEnabled('ts')) {
 
 156   $form->addInput(array('type'=>'combobox',
 
 158    'style'=>'width: 250px;',
 
 159    'data'=>array('1'=>$i18n->get('form.reports.include_assigned'),'2'=>$i18n->get('form.reports.include_not_assigned')),
 
 160    'empty'=>array(''=>$i18n->get('dropdown.all'))
 
 164 $user_list = array();
 
 165 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
 
 166   // Prepare user and assigned projects arrays.
 
 167   if ($user->can('view_reports') || $user->can('view_all_reports')) {
 
 168     $rank = $user->getMaxRankForGroup($user->getGroup());
 
 169     if ($user->can('view_all_reports')) $max_rank = MAX_RANK;
 
 170     if ($user->can('view_own_reports'))
 
 171       $options = array('max_rank'=>$max_rank,'include_self'=>true);
 
 173       $options = array('max_rank'=>$max_rank);
 
 174     $users = $user->getUsers($options); // Active and inactive users.
 
 176   elseif ($user->isClient())
 
 177     $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
 
 179   foreach ($users as $single_user) {
 
 180     $user_list[$single_user['id']] = $single_user['name'];
 
 181     $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
 
 183       foreach ($projects as $single_project) {
 
 184         $assigned_projects[$single_user['id']][] = $single_project['id'];
 
 188   $row_count = ceil(count($user_list)/3);
 
 189   $form->addInput(array('type'=>'checkboxgroup',
 
 193     'groupin'=>$row_count,
 
 194     'style'=>'width: 100%;'));
 
 197 // Add control for time period.
 
 198 $form->addInput(array('type'=>'combobox',
 
 200   'style'=>'width: 250px;',
 
 201   'data'=>array(INTERVAL_THIS_MONTH=>$i18n->get('dropdown.current_month'),
 
 202     INTERVAL_LAST_MONTH=>$i18n->get('dropdown.previous_month'),
 
 203     INTERVAL_THIS_WEEK=>$i18n->get('dropdown.current_week'),
 
 204     INTERVAL_LAST_WEEK=>$i18n->get('dropdown.previous_week'),
 
 205     INTERVAL_THIS_DAY=>$i18n->get('dropdown.current_day'),
 
 206     INTERVAL_LAST_DAY=>$i18n->get('dropdown.previous_day')),
 
 207   'empty'=>array(''=>$i18n->get('dropdown.select'))));
 
 208 // Add controls for start and end dates.
 
 209 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
 
 210 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
 
 212 // Add checkboxes for fields.
 
 213 if ($user->isPluginEnabled('cl'))
 
 214   $form->addInput(array('type'=>'checkbox','name'=>'chclient'));
 
 215 if (($user->can('manage_invoices') || $user->isClient()) && $user->isPluginEnabled('iv'))
 
 216   $form->addInput(array('type'=>'checkbox','name'=>'chinvoice'));
 
 217 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))
 
 218   $form->addInput(array('type'=>'checkbox','name'=>'chpaid'));
 
 219 if ($user->can('view_reports') || $user->can('view_all_reports'))
 
 220   $form->addInput(array('type'=>'checkbox','name'=>'chip'));
 
 221 if (MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode)
 
 222   $form->addInput(array('type'=>'checkbox','name'=>'chproject'));
 
 223 if (MODE_PROJECTS_AND_TASKS == $trackingMode)
 
 224   $form->addInput(array('type'=>'checkbox','name'=>'chtask'));
 
 225 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
 
 226   $form->addInput(array('type'=>'checkbox','name'=>'chstart'));
 
 227   $form->addInput(array('type'=>'checkbox','name'=>'chfinish'));
 
 229 $form->addInput(array('type'=>'checkbox','name'=>'chduration'));
 
 230 $form->addInput(array('type'=>'checkbox','name'=>'chnote'));
 
 231 $form->addInput(array('type'=>'checkbox','name'=>'chcost'));
 
 232 // If we have a custom field - add a checkbox for it.
 
 233 if ($custom_fields && $custom_fields->fields[0])
 
 234   $form->addInput(array('type'=>'checkbox','name'=>'chcf_1'));
 
 235 if ($user->isPluginEnabled('wu'))
 
 236   $form->addInput(array('type'=>'checkbox','name'=>'chunits'));
 
 238 // Add group by control.
 
 239 $group_by_options['no_grouping'] = $i18n->get('form.reports.group_by_no');
 
 240 $group_by_options['date'] = $i18n->get('form.reports.group_by_date');
 
 241 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
 
 242   $group_by_options['user'] = $i18n->get('form.reports.group_by_user');
 
 243 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id))
 
 244   $group_by_options['client'] = $i18n->get('form.reports.group_by_client');
 
 245 if (MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode)
 
 246   $group_by_options['project'] = $i18n->get('form.reports.group_by_project');
 
 247 if (MODE_PROJECTS_AND_TASKS == $trackingMode)
 
 248   $group_by_options['task'] = $i18n->get('form.reports.group_by_task');
 
 249 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
 
 250   $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
 
 252 $group_by_options_size = sizeof($group_by_options);
 
 253 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by1','data'=>$group_by_options));
 
 254 if ($group_by_options_size > 2) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by2','data'=>$group_by_options));
 
 255 if ($group_by_options_size > 3) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by3','data'=>$group_by_options));
 
 256 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly'));
 
 258 // Add text field for a new favorite report name.
 
 259 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
 
 261 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
 
 263 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
 
 265 // Create a bean (which is a mechanism to remember form values in session).
 
 266 $bean = new ActionForm('reportBean', $form, $request);
 
 267 // At this point form values are obtained from session if they are there.
 
 269 if ($request->isGet() && !$bean->isSaved()) {
 
 270   // No previous form data were found in session. Use the following default values.
 
 271   $form->setValueByElement('users', array_keys($user_list));
 
 272   $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->getDateFormat()));
 
 273   $form->setValueByElement('start_date', $period->getStartDate());
 
 274   $form->setValueByElement('end_date', $period->getEndDate());
 
 275   $form->setValueByElement('chclient', '1');
 
 276   $form->setValueByElement('chinvoice', '0');
 
 277   $form->setValueByElement('chpaid', '0');
 
 278   $form->setValueByElement('chip', '0');
 
 279   $form->setValueByElement('chproject', '1');
 
 280   $form->setValueByElement('chstart', '1');
 
 281   $form->setValueByElement('chduration', '1');
 
 282   $form->setValueByElement('chcost', '0');
 
 283   $form->setValueByElement('chtask', '1');
 
 284   $form->setValueByElement('chfinish', '1');
 
 285   $form->setValueByElement('chnote', '1');
 
 286   $form->setValueByElement('chcf_1', '0');
 
 287   $form->setValueByElement('chunits', '0');
 
 288   $form->setValueByElement('chtotalsonly', '0');
 
 291 $form->setValueByElement('fav_report_changed','');
 
 293 // Disable the Delete button when no favorite report is selected.
 
 294 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
 
 295   $form->getElement('btn_delete')->setEnabled(false);
 
 297 if ($request->isPost()) {
 
 298   if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
 
 299     // User changed favorite report. We need to load new values into the form.
 
 300     if ($bean->getAttribute('favorite_report')) {
 
 301       // This loads new favorite report options into the bean (into our form).
 
 302       ttFavReportHelper::loadReport($bean);
 
 304       // If user selected no favorite report - mark all user checkboxes (most probable scenario).
 
 305       if ($bean->getAttribute('favorite_report') == -1)
 
 306         $form->setValueByElement('users', array_keys($user_list));
 
 308       // Save form data in session for future use.
 
 310       header('Location: reports.php');
 
 313   } elseif ($bean->getAttribute('btn_save')) {
 
 314     // User clicked the Save button. We need to save form options as new favorite report.
 
 315     if (!ttValidString($bean->getAttribute('new_fav_report'))) $err->add($i18n->get('error.field'), $i18n->get('form.reports.save_as_favorite'));
 
 318       $id = ttFavReportHelper::saveReport($bean);
 
 320         $err->add($i18n->get('error.db'));
 
 322         $bean->setAttribute('favorite_report', $id);
 
 324         header('Location: reports.php');
 
 328   } elseif($bean->getAttribute('btn_delete')) {
 
 329     // Delete button pressed. User wants to delete a favorite report.
 
 330     if ($bean->getAttribute('favorite_report')) {
 
 331       ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
 
 332       // Load default report.
 
 333       $bean->setAttribute('favorite_report','');
 
 334       $bean->setAttribute('new_fav_report', $report_list[0]['name']);
 
 335       ttFavReportHelper::loadReport($bean);
 
 336       $form->setValueByElement('users', array_keys($user_list));
 
 338       header('Location: reports.php');
 
 342     // Generate button pressed. Check some values.
 
 343     if (!$bean->getAttribute('period')) {
 
 344       $start_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('start_date'));
 
 346       if ($start_date->isError() || !$bean->getAttribute('start_date'))
 
 347         $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
 
 349       $end_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('end_date'));
 
 350       if ($end_date->isError() || !$bean->getAttribute('end_date'))
 
 351         $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
 
 353       if ($start_date->compare($end_date) > 0)
 
 354         $err->add($i18n->get('error.interval'), $i18n->get('label.end_date'), $i18n->get('label.start_date'));
 
 356     $group_by1 = $bean->getAttribute('group_by1');
 
 357     $group_by2 = $bean->getAttribute('group_by2');
 
 358     $group_by3 = $bean->getAttribute('group_by3');
 
 359     if (($group_by3 != null && $group_by3 != 'no_grouping') && ($group_by3 == $group_by1 || $group_by3 == $group_by2))
 
 360       $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
 
 361     if (($group_by2 != null && $group_by2 != 'no_grouping') && ($group_by2 == $group_by1 || $group_by3 == $group_by2))
 
 362       $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
 
 363     // Check remaining values.
 
 364     if (!ttReportHelper::verifyBean($bean)) $err->add($i18n->get('error.sys'));
 
 368       // Now we can go ahead and create a report.
 
 369       header('Location: report.php');
 
 375 $smarty->assign('project_list', $project_list);
 
 376 $smarty->assign('task_list', $task_list);
 
 377 $smarty->assign('assigned_projects', $assigned_projects);
 
 378 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
 
 379 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
 
 380 $smarty->assign('title', $i18n->get('title.reports'));
 
 381 $smarty->assign('content_page_name', 'reports.tpl');
 
 382 $smarty->display('index.tpl');