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');
35 import('ttProjectHelper');
36 import('ttFavReportHelper');
37 import('ttClientHelper');
40 if (!ttAccessCheck(right_view_reports)) {
41 header('Location: access_denied.php');
45 // Use custom fields plugin if it is enabled.
46 if (in_array('cf', explode(',', $user->plugins))) {
47 require_once('plugins/CustomFields.class.php');
48 $custom_fields = new CustomFields($user->team_id);
49 $smarty->assign('custom_fields', $custom_fields);
52 $form = new Form('reportForm');
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;',
61 'datakeys'=>array('id','name'),
62 'empty'=>array('-1'=>$i18n->getKey('dropdown.no'))
64 $form->addInput(array('type'=>'hidden','name'=>'fav_report_changed'));
65 // Generate and Delete buttons.
66 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->getKey('button.generate')));
67 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete'),'onclick'=>"return confirm('".$i18n->getKey('form.reports.confirm_delete')."')"));
69 // Dropdown for clients if the clients plugin is enabled.
70 if (in_array('cl', explode(',', $user->plugins)) && !($user->isClient() && $user->client_id)) {
71 if ($user->canManageTeam() || ($user->isClient() && !$user->client_id))
72 $client_list = ttClientHelper::getClients($user->team_id);
74 $client_list = ttClientHelper::getClientsForUser();
75 $form->addInput(array('type'=>'combobox',
77 'style'=>'width: 250px;',
79 'datakeys'=>array('id', 'name'),
80 'empty'=>array(''=>$i18n->getKey('dropdown.all'))
84 // If we have a TYPE_DROPDOWN custom field - add control to select an option.
85 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
86 $form->addInput(array('type'=>'combobox','name'=>'option',
87 'style'=>'width: 250px;',
89 'data'=>$custom_fields->options,
90 'empty'=>array(''=>$i18n->getKey('dropdown.all'))
94 // Add controls for projects and tasks.
95 if ($user->canManageTeam()) {
96 $project_list = ttProjectHelper::getProjects(); // Manager and co-managers can run reports on all active and inactive projects.
97 } else if ($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->getKey('dropdown.all'))
110 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
111 $task_list = ttTeamHelper::getActiveTasks($user->team_id);
112 $form->addInput(array('type'=>'combobox',
114 'style'=>'width: 250px;',
116 'datakeys'=>array('id','name'),
117 'empty'=>array(''=>$i18n->getKey('dropdown.all'))
121 // Add include records control.
122 $include_options = array('1'=>$i18n->getKey('form.reports.include_billable'),
123 '2'=>$i18n->getKey('form.reports.include_not_billable'));
124 $form->addInput(array('type'=>'combobox',
125 'name'=>'include_records',
126 'style'=>'width: 250px;',
127 'data'=>$include_options,
128 'empty'=>array(''=>$i18n->getKey('dropdown.all'))
131 // Add invoiced / not invoiced selector.
132 $invoice_options = array('1'=>$i18n->getKey('form.reports.include_invoiced'),
133 '2'=>$i18n->getKey('form.reports.include_not_invoiced'));
134 $form->addInput(array('type'=>'combobox',
136 'style'=>'width: 250px;',
137 'data'=>$invoice_options,
138 'empty'=>array(''=>$i18n->getKey('dropdown.all'))
141 $user_list = array();
142 if ($user->canManageTeam() || $user->isClient()) {
143 // Prepare user and assigned projects arrays.
144 if ($user->canManageTeam())
145 $users = ttTeamHelper::getUsers(); // Active and inactive users for managers.
146 else if ($user->isClient())
147 $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
149 foreach ($users as $single_user) {
150 $user_list[$single_user['id']] = $single_user['name'];
151 $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
153 foreach ($projects as $single_project) {
154 $assigned_projects[$single_user['id']][] = $single_project['id'];
158 $row_count = ceil(count($user_list)/3);
159 $form->addInput(array('type'=>'checkboxgroup',
163 'groupin'=>$row_count,
164 'style'=>'width: 100%;'
168 // Add control for time period.
169 $form->addInput(array('type'=>'combobox',
171 'style'=>'width: 250px;',
172 'data'=>array(INTERVAL_THIS_MONTH=>$i18n->getKey('dropdown.this_month'),
173 INTERVAL_LAST_MONTH=>$i18n->getKey('dropdown.last_month'),
174 INTERVAL_THIS_WEEK=>$i18n->getKey('dropdown.this_week'),
175 INTERVAL_LAST_WEEK=>$i18n->getKey('dropdown.last_week')),
176 'empty'=>array(''=>$i18n->getKey('dropdown.select'))
178 // Add controls for start and end dates.
179 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
180 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
182 // Add checkboxes for fields.
183 if (in_array('cl', explode(',', $user->plugins)))
184 $form->addInput(array('type'=>'checkbox','name'=>'chclient','data'=>1));
185 if (($user->canManageTeam() || $user->isClient()) && in_array('iv', explode(',', $user->plugins)))
186 $form->addInput(array('type'=>'checkbox','name'=>'chinvoice','data'=>1));
187 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
188 $form->addInput(array('type'=>'checkbox','name'=>'chproject','data'=>1));
189 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
190 $form->addInput(array('type'=>'checkbox','name'=>'chtask','data'=>1));
191 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
192 $form->addInput(array('type'=>'checkbox','name'=>'chstart','data'=>1));
193 $form->addInput(array('type'=>'checkbox','name'=>'chfinish','data'=>1));
195 $form->addInput(array('type'=>'checkbox','name'=>'chduration','data'=>1));
196 $form->addInput(array('type'=>'checkbox','name'=>'chnote','data'=>1));
197 if (defined('COST_ON_REPORTS') && isTrue(COST_ON_REPORTS))
198 $form->addInput(array('type'=>'checkbox','name'=>'chcost','data'=>1));
199 // If we have a custom field - add a checkbox for it.
200 if ($custom_fields && $custom_fields->fields[0])
201 $form->addInput(array('type'=>'checkbox','name'=>'chcf_1','data'=>1));
203 // Add group by control.
204 $group_by_options['no_grouping'] = $i18n->getKey('form.reports.group_by_no');
205 $group_by_options['date'] = $i18n->getKey('form.reports.group_by_date');
206 if ($user->canManageTeam() || $user->isClient())
207 $group_by_options['user'] = $i18n->getKey('form.reports.group_by_user');
208 if (in_array('cl', explode(',', $user->plugins)) && !($user->isClient() && $user->client_id))
209 $group_by_options['client'] = $i18n->getKey('form.reports.group_by_client');
210 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
211 $group_by_options['project'] = $i18n->getKey('form.reports.group_by_project');
212 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
213 $group_by_options['task'] = $i18n->getKey('form.reports.group_by_task');
214 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
215 $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
217 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by','data'=>$group_by_options));
218 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly','data'=>1));
220 // Add text field for a new favorite report name.
221 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
223 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('button.save')));
225 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->getKey('button.generate')));
227 // Create a bean (which is a mechanism to remember form values in session).
228 $bean = new ActionForm('reportBean', $form, $request);
229 // At this point form values are obtained from session if they are there...
231 if (($request->getMethod() == 'GET') && !$bean->isSaved()) {
232 // No previous form data were found in session. Use the following default values.
233 $form->setValueByElement('users', array_keys($user_list));
234 $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->date_format));
235 $form->setValueByElement('start_date', $period->getBeginDate());
236 $form->setValueByElement('end_date', $period->getEndDate());
237 $form->setValueByElement('chclient', 1);
238 $form->setValueByElement('chinvoice', 0);
239 $form->setValueByElement('chproject', 1);
240 $form->setValueByElement('chstart', 1);
241 $form->setValueByElement('chduration', 1);
242 $form->setValueByElement('chcost', 0);
243 $form->setValueByElement('chtask', 1);
244 $form->setValueByElement('chfinish', 1);
245 $form->setValueByElement('chnote', 1);
246 $form->setValueByElement('chcf_1', 0);
247 $form->setValueByElement('chtotalsonly', 0);
250 $form->setValueByElement('fav_report_changed','');
252 // Disable the Delete button when no favorite report is selected.
253 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
254 $form->getElement('btn_delete')->setEnable(false);
256 if ($request->getMethod() == 'POST') {
257 if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
258 // User changed favorite report. We need to load new values into the form.
259 if ($bean->getAttribute('favorite_report')) {
260 // This loads new favorite report options into the bean (into our form).
261 ttFavReportHelper::loadReport($user->id, $bean);
263 // If user selected no favorite report - mark all user checkboxes (most probable scenario).
264 if ($bean->getAttribute('favorite_report') == -1)
265 $form->setValueByElement('users', array_keys($user_list));
267 // Save form data in session for future use.
269 header('Location: reports.php');
272 } elseif ($bean->getAttribute('btn_save')) {
273 // User clicked the Save button. We need to save form options as new favorite report.
274 if (!ttValidString($bean->getAttribute('new_fav_report'))) $errors->add($i18n->getKey('error.field'), $i18n->getKey('form.reports.save_as_favorite'));
276 if ($errors->isEmpty()) {
277 $id = ttFavReportHelper::saveReport($user->id, $bean);
279 $errors->add($i18n->getKey('error.db'));
280 if ($errors->isEmpty()) {
281 $bean->setAttribute('favorite_report', $id);
283 header('Location: reports.php');
287 } elseif($bean->getAttribute('btn_delete')) {
288 // Delete button pressed. User wants to delete a favorite report.
289 if ($bean->getAttribute('favorite_report')) {
290 ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
291 // Load default report.
292 $bean->setAttribute('favorite_report','');
293 $bean->setAttribute('new_fav_report', $report_list[0]['name']);
294 ttFavReportHelper::loadReport($user->id, $bean);
295 $form->setValueByElement('users', array_keys($user_list));
297 header('Location: reports.php');
301 // Generate button pressed. Check some values.
302 if (!$bean->getAttribute('period')) {
303 $start_date = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
305 if ($start_date->isError() || !$bean->getAttribute('start_date'))
306 $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.start_date'));
308 $end_date = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
309 if ($end_date->isError() || !$bean->getAttribute('end_date'))
310 $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.end_date'));
312 if ($start_date->compare($end_date) > 0)
313 $errors->add($i18n->getKey('error.interval'), $i18n->getKey('label.end_date'), $i18n->getKey('label.start_date'));
318 if ($errors->isEmpty()) {
319 // Now we can go ahead and create a report.
320 header('Location: report.php');
326 $smarty->assign('project_list', $project_list);
327 $smarty->assign('task_list', $task_list);
328 $smarty->assign('assigned_projects', $assigned_projects);
329 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
330 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
331 $smarty->assign('title', $i18n->getKey('title.reports'));
332 $smarty->assign('content_page_name', 'reports.tpl');
333 $smarty->display('index.tpl');