Simplified Checkbox class.
[timetracker.git] / reports.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('form.ActionForm');
32 import('DateAndTime');
33 import('ttTeamHelper');
34 import('Period');
35 import('ttProjectHelper');
36 import('ttFavReportHelper');
37 import('ttClientHelper');
38
39 // Access check.
40 if (!ttAccessCheck(right_view_reports)) {
41   header('Location: access_denied.php');
42   exit();
43 }
44
45 // Use custom fields plugin if it is enabled.
46 if ($user->isPluginEnabled('cf')) {
47   require_once('plugins/CustomFields.class.php');
48   $custom_fields = new CustomFields($user->team_id);
49   $smarty->assign('custom_fields', $custom_fields);
50 }
51
52 $form = new Form('reportForm');
53
54 // Get saved favorite reports for user.
55 $report_list = ttFavReportHelper::getReports($user->id);
56 $form->addInput(array('type'=>'combobox',
57   'name'=>'favorite_report',
58   'onchange'=>'document.reportForm.fav_report_changed.value=1;document.reportForm.submit();',
59   'style'=>'width: 250px;',
60   'data'=>$report_list,
61   'datakeys'=>array('id','name'),
62   'empty'=>array('-1'=>$i18n->getKey('dropdown.no'))));
63 $form->addInput(array('type'=>'hidden','name'=>'fav_report_changed'));
64 // Generate and Delete buttons.
65 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->getKey('button.generate')));
66 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete'),'onclick'=>"return confirm('".$i18n->getKey('form.reports.confirm_delete')."')"));
67
68 // Dropdown for clients if the clients plugin is enabled.
69 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id)) {
70   if ($user->canManageTeam() || ($user->isClient() && !$user->client_id))
71     $client_list = ttClientHelper::getClients($user->team_id);
72   else
73     $client_list = ttClientHelper::getClientsForUser();
74   $form->addInput(array('type'=>'combobox',
75     'name'=>'client',
76     'style'=>'width: 250px;',
77     'data'=>$client_list,
78     'datakeys'=>array('id', 'name'),
79     'empty'=>array(''=>$i18n->getKey('dropdown.all'))));
80 }
81
82 // If we have a TYPE_DROPDOWN custom field - add control to select an option.
83 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
84     $form->addInput(array('type'=>'combobox','name'=>'option',
85       'style'=>'width: 250px;',
86       'value'=>$cl_cf_1,
87       'data'=>$custom_fields->options,
88       'empty'=>array(''=>$i18n->getKey('dropdown.all'))));
89 }
90
91 // Add controls for projects and tasks.
92 if ($user->canManageTeam()) {
93   $project_list = ttProjectHelper::getProjects(); // Manager and co-managers can run reports on all active and inactive projects.
94 } elseif ($user->isClient()) {
95   $project_list = ttProjectHelper::getProjectsForClient();
96 } else {
97   $project_list = ttProjectHelper::getAssignedProjects($user->id);      
98 }
99 $form->addInput(array('type'=>'combobox',
100   'onchange'=>'fillTaskDropdown(this.value);selectAssignedUsers(this.value);',
101   'name'=>'project',
102   'style'=>'width: 250px;',
103   'data'=>$project_list,
104   'datakeys'=>array('id','name'),
105   'empty'=>array(''=>$i18n->getKey('dropdown.all'))));
106 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
107   $task_list = ttTeamHelper::getActiveTasks($user->team_id);
108   $form->addInput(array('type'=>'combobox',
109     'name'=>'task',
110     'style'=>'width: 250px;',
111     'data'=>$task_list,
112     'datakeys'=>array('id','name'),
113     'empty'=>array(''=>$i18n->getKey('dropdown.all'))));
114 }
115
116 // Add include records control.
117 $include_options = array('1'=>$i18n->getKey('form.reports.include_billable'),
118   '2'=>$i18n->getKey('form.reports.include_not_billable'));
119 $form->addInput(array('type'=>'combobox',
120   'name'=>'include_records',
121   'style'=>'width: 250px;',
122   'data'=>$include_options,
123   'empty'=>array(''=>$i18n->getKey('dropdown.all'))));
124
125 // Add invoiced / not invoiced selector.
126 $invoice_options = array('1'=>$i18n->getKey('form.reports.include_invoiced'),
127   '2'=>$i18n->getKey('form.reports.include_not_invoiced'));
128 $form->addInput(array('type'=>'combobox',
129   'name'=>'invoice',
130   'style'=>'width: 250px;',
131   'data'=>$invoice_options,
132   'empty'=>array(''=>$i18n->getKey('dropdown.all'))));
133
134 $user_list = array();
135 if ($user->canManageTeam() || $user->isClient()) {
136   // Prepare user and assigned projects arrays.
137   if ($user->canManageTeam())
138     $users = ttTeamHelper::getUsers(); // Active and inactive users for managers.
139   elseif ($user->isClient())
140     $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
141
142   foreach ($users as $single_user) {
143     $user_list[$single_user['id']] = $single_user['name'];
144     $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
145     if ($projects) {
146       foreach ($projects as $single_project) {
147         $assigned_projects[$single_user['id']][] = $single_project['id'];
148       }
149     }
150   }
151   $row_count = ceil(count($user_list)/3);
152   $form->addInput(array('type'=>'checkboxgroup',
153     'name'=>'users',
154     'data'=>$user_list,
155     'layout'=>'V',
156     'groupin'=>$row_count,
157     'style'=>'width: 100%;'));
158 }
159
160 // Add control for time period.
161 $form->addInput(array('type'=>'combobox',
162   'name'=>'period',
163   'style'=>'width: 250px;',
164   'data'=>array(INTERVAL_THIS_MONTH=>$i18n->getKey('dropdown.this_month'),
165     INTERVAL_LAST_MONTH=>$i18n->getKey('dropdown.last_month'),
166     INTERVAL_THIS_WEEK=>$i18n->getKey('dropdown.this_week'),
167     INTERVAL_LAST_WEEK=>$i18n->getKey('dropdown.last_week')),
168   'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
169 // Add controls for start and end dates.
170 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
171 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
172
173 // Add checkboxes for fields.
174 if ($user->isPluginEnabled('cl'))
175   $form->addInput(array('type'=>'checkbox','name'=>'chclient'));
176 if (($user->canManageTeam() || $user->isClient()) && $user->isPluginEnabled('iv'))
177   $form->addInput(array('type'=>'checkbox','name'=>'chinvoice'));
178 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
179   $form->addInput(array('type'=>'checkbox','name'=>'chproject'));
180 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
181   $form->addInput(array('type'=>'checkbox','name'=>'chtask'));
182 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
183   $form->addInput(array('type'=>'checkbox','name'=>'chstart'));
184   $form->addInput(array('type'=>'checkbox','name'=>'chfinish'));
185 }
186 $form->addInput(array('type'=>'checkbox','name'=>'chduration'));
187 $form->addInput(array('type'=>'checkbox','name'=>'chnote'));
188 if (defined('COST_ON_REPORTS') && isTrue(COST_ON_REPORTS))
189   $form->addInput(array('type'=>'checkbox','name'=>'chcost'));
190 // If we have a custom field - add a checkbox for it.
191 if ($custom_fields && $custom_fields->fields[0])
192   $form->addInput(array('type'=>'checkbox','name'=>'chcf_1'));
193
194 // Add group by control.
195 $group_by_options['no_grouping'] = $i18n->getKey('form.reports.group_by_no');
196 $group_by_options['date'] = $i18n->getKey('form.reports.group_by_date');
197 if ($user->canManageTeam() || $user->isClient())
198   $group_by_options['user'] = $i18n->getKey('form.reports.group_by_user');
199 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id))
200   $group_by_options['client'] = $i18n->getKey('form.reports.group_by_client');
201 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
202   $group_by_options['project'] = $i18n->getKey('form.reports.group_by_project');
203 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
204   $group_by_options['task'] = $i18n->getKey('form.reports.group_by_task');
205 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
206   $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
207 }
208 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by','data'=>$group_by_options));
209 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly'));
210
211 // Add text field for a new favorite report name.
212 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
213 // Save button.
214 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('button.save')));
215
216 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->getKey('button.generate')));
217
218 // Create a bean (which is a mechanism to remember form values in session).
219 $bean = new ActionForm('reportBean', $form, $request);
220 // At this point form values are obtained from session if they are there.
221
222 if (($request->getMethod() == 'GET') && !$bean->isSaved()) {
223   // No previous form data were found in session. Use the following default values.
224   $form->setValueByElement('users', array_keys($user_list));
225   $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->date_format));
226   $form->setValueByElement('start_date', $period->getBeginDate());
227   $form->setValueByElement('end_date', $period->getEndDate());
228   $form->setValueByElement('chclient', '1');
229   $form->setValueByElement('chinvoice', '0');
230   $form->setValueByElement('chproject', '1');
231   $form->setValueByElement('chstart', '1');
232   $form->setValueByElement('chduration', '1');
233   $form->setValueByElement('chcost', '0');
234   $form->setValueByElement('chtask', '1');
235   $form->setValueByElement('chfinish', '1');
236   $form->setValueByElement('chnote', '1');
237   $form->setValueByElement('chcf_1', '0');
238   $form->setValueByElement('chtotalsonly', '0');
239 }
240
241 $form->setValueByElement('fav_report_changed','');
242
243 // Disable the Delete button when no favorite report is selected.
244 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
245   $form->getElement('btn_delete')->setEnabled(false);
246
247 if ($request->isPost()) {
248   if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
249     // User changed favorite report. We need to load new values into the form.
250     if ($bean->getAttribute('favorite_report')) {
251       // This loads new favorite report options into the bean (into our form).
252       ttFavReportHelper::loadReport($user->id, $bean);
253
254       // If user selected no favorite report - mark all user checkboxes (most probable scenario).
255       if ($bean->getAttribute('favorite_report') == -1)
256         $form->setValueByElement('users', array_keys($user_list));
257
258       // Save form data in session for future use.
259       $bean->saveBean();
260       header('Location: reports.php');
261       exit();
262     }
263   } elseif ($bean->getAttribute('btn_save')) {
264     // User clicked the Save button. We need to save form options as new favorite report.
265     if (!ttValidString($bean->getAttribute('new_fav_report'))) $err->add($i18n->getKey('error.field'), $i18n->getKey('form.reports.save_as_favorite'));
266
267     if ($err->no()) {
268       $id = ttFavReportHelper::saveReport($user->id, $bean);
269       if (!$id)
270         $err->add($i18n->getKey('error.db'));
271       if ($err->no()) {
272         $bean->setAttribute('favorite_report', $id);
273         $bean->saveBean();
274         header('Location: reports.php');
275         exit();
276       }
277     }
278   } elseif($bean->getAttribute('btn_delete')) {
279     // Delete button pressed. User wants to delete a favorite report.
280     if ($bean->getAttribute('favorite_report')) {
281       ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
282       // Load default report.
283       $bean->setAttribute('favorite_report','');
284       $bean->setAttribute('new_fav_report', $report_list[0]['name']);
285       ttFavReportHelper::loadReport($user->id, $bean);
286       $form->setValueByElement('users', array_keys($user_list));
287       $bean->saveBean();
288       header('Location: reports.php');
289       exit();
290     }
291   } else {
292     // Generate button pressed. Check some values.
293     if (!$bean->getAttribute('period')) {
294       $start_date = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
295
296       if ($start_date->isError() || !$bean->getAttribute('start_date'))
297         $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start_date'));
298
299       $end_date = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
300       if ($end_date->isError() || !$bean->getAttribute('end_date'))
301         $err->add($i18n->getKey('error.field'), $i18n->getKey('label.end_date'));
302
303       if ($start_date->compare($end_date) > 0)
304         $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.end_date'), $i18n->getKey('label.start_date'));
305     }
306
307     $bean->saveBean();
308
309     if ($err->no()) {
310       // Now we can go ahead and create a report.
311       header('Location: report.php');
312       exit();
313     }
314   }
315 } // isPost
316
317 $smarty->assign('project_list', $project_list);
318 $smarty->assign('task_list', $task_list);
319 $smarty->assign('assigned_projects', $assigned_projects);
320 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
321 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
322 $smarty->assign('title', $i18n->getKey('title.reports'));
323 $smarty->assign('content_page_name', 'reports.tpl');
324 $smarty->display('index.tpl');