Refactoring - more usage of ttUser::isPluginEnabled().
[timetracker.git] / mobile / timer.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('ttUserHelper');
32 import('ttTeamHelper');
33 import('ttClientHelper');
34 import('ttTimeHelper');
35 import('DateAndTime');
36
37 // Access check.
38 if (!ttAccessCheck(right_data_entry)) {
39   header('Location: access_denied.php');
40   exit();
41 }
42
43 // Initialize and store date in session.
44 $cl_date  = $request->getParameter('date', @$_SESSION['date']);
45 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
46 if($selected_date->isError())
47   $selected_date = new DateAndTime(DB_DATEFORMAT);
48 if(!$cl_date)
49   $cl_date = $selected_date->toString(DB_DATEFORMAT);
50 $_SESSION['date'] = $cl_date;
51 // TODO: for time page we may limit the day to today only.
52
53 // Use custom fields plugin if it is enabled.
54 if (in_array('cf', explode(',', $user->plugins))) {
55   require_once('../plugins/CustomFields.class.php');
56   $custom_fields = new CustomFields($user->team_id);
57   $smarty->assign('custom_fields', $custom_fields);
58 }
59
60 // Initialize variables.
61 $cl_start = trim($request->getParameter('browser_time'));
62 $cl_finish = trim($request->getParameter('browser_time'));
63 // Custom field.
64 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->getMethod()=='POST'? null : @$_SESSION['cf_1'])));
65 $_SESSION['cf_1'] = $cl_cf_1;
66 $cl_billable = 1;
67 if ($user->isPluginEnabled('iv')) {
68   if ($request->isPost()) {
69     $cl_billable = $request->getParameter('billable');
70     $_SESSION['billable'] = (int) $cl_billable;
71   } else 
72     if (isset($_SESSION['billable']))
73       $cl_billable = $_SESSION['billable'];
74 }
75 $cl_client = $request->getParameter('client', @$_SESSION['client']);
76 $_SESSION['client'] = $cl_client;
77 $cl_project = $request->getParameter('project', @$_SESSION['project']);
78 $_SESSION['project'] = $cl_project;
79 $cl_task = $request->getParameter('task', @$_SESSION['task']);
80 $_SESSION['task'] = $cl_task;
81
82 // Obtain uncompleted record. Assumtion is that only 1 uncompleted record is allowed.
83 $uncompleted = ttTimeHelper::getUncompleted($user->getActiveUser());
84 $enable_controls = ($uncompleted == null);
85
86 // Elements of timeRecordForm.
87 $form = new Form('timerRecordForm');
88
89 // Dropdown for clients in MODE_TIME. Use all active clients.
90 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
91     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
92     $form->addInput(array('type'=>'combobox',
93       'onchange'=>'fillProjectDropdown(this.value);',
94       'name'=>'client',
95       'style'=>'width: 250px;',
96       'enable'=>$enable_controls,
97       'value'=>$cl_client,
98       'data'=>$active_clients,
99       'datakeys'=>array('id', 'name'),
100       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
101   // Note: in other modes the client list is filtered to relevant clients only. See below.
102 }
103
104 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
105   // Dropdown for projects assigned to user.
106   $project_list = $user->getAssignedProjects();
107   $form->addInput(array('type'=>'combobox',
108     'onchange'=>'fillTaskDropdown(this.value);',
109     'name'=>'project',
110     'style'=>'width: 250px;',
111     'enable'=>$enable_controls,
112     'value'=>$cl_project,
113     'data'=>$project_list,
114     'datakeys'=>array('id','name'),
115     'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
116
117   // Dropdown for clients if the clients plugin is enabled.
118   if ($user->isPluginEnabled('cl')) {
119     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
120     // We need an array of assigned project ids to do some trimming. 
121     foreach($project_list as $project)
122       $projects_assigned_to_user[] = $project['id'];
123
124     // Build a client list out of active clients. Use only clients that are relevant to user.
125     // Also trim their associated project list to only assigned projects (to user).
126     foreach($active_clients as $client) {
127       $projects_assigned_to_client = explode(',', $client['projects']);
128       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
129       if ($intersection) {
130         $client['projects'] = implode(',', $intersection);
131         $client_list[] = $client;
132       }
133     }
134     $form->addInput(array('type'=>'combobox',
135       'onchange'=>'fillProjectDropdown(this.value);',
136       'name'=>'client',
137       'style'=>'width: 250px;',
138       'enable'=>$enable_controls,
139       'value'=>$cl_client,
140       'data'=>$client_list,
141       'datakeys'=>array('id', 'name'),
142       'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
143   }
144 }
145
146 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
147   $task_list = ttTeamHelper::getActiveTasks($user->team_id);
148   $form->addInput(array('type'=>'combobox',
149     'name'=>'task',
150     'style'=>'width: 250px;',
151     'enable'=>$enable_controls,
152     'value'=>$cl_task,
153     'data'=>$task_list,
154     'datakeys'=>array('id','name'),
155     'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
156 }
157 if ($user->isPluginEnabled('iv'))
158   $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable,'enable'=>$enable_controls));
159 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on button click.
160 $form->addInput(array('type'=>'hidden','name'=>'browser_time','value'=>''));  // User current time, which gets filled in on button click.
161 $enable_start = $uncompleted ? false : true;
162 if (!$uncompleted)
163   $form->addInput(array('type'=>'submit','name'=>'btn_start','onclick'=>'browser_time.value=get_time()','value'=>$i18n->getKey('label.start'),'enable'=>$enable_start));
164 else
165   $form->addInput(array('type'=>'submit','name'=>'btn_stop','onclick'=>'browser_time.value=get_time()','value'=>$i18n->getKey('label.finish'),'enable'=>!$enable_start));
166
167 // If we have custom fields - add controls for them.
168 if ($custom_fields && $custom_fields->fields[0]) {
169   // Only one custom field is supported at this time.
170   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
171     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
172   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
173     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
174       'style'=>'width: 250px;',
175       'value'=>$cl_cf_1,
176       'data'=>$custom_fields->options,
177       'empty'=>array(''=>$i18n->getKey('dropdown.select'))
178     ));
179   }
180 }
181
182 // Determine lock date. Time entries earlier than lock date cannot be created or modified.
183 $lock_interval = $user->lock_interval;
184 $lockdate = 0;
185 if ($lock_interval > 0) {
186   $lockdate = new DateAndTime();
187   $lockdate->decDay($lock_interval);
188 }
189
190 // Submit.
191 if ($request->isPost()) {
192   if ($request->getParameter('btn_start')) {
193     // Start button clicked. We need to create a new uncompleted record with only the start time.
194     $cl_finish = null;
195
196     // Validate user input.
197     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
198       $err->add($i18n->getKey('error.client'));
199     if ($custom_fields) {
200       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
201     }
202     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
203       if (!$cl_project) $err->add($i18n->getKey('error.project'));
204     }
205     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
206       if (!$cl_task) $err->add($i18n->getKey('error.task'));
207     }
208     // Finished validating user input.
209
210     // Prohibit creating entries in future.
211     if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
212       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
213       if ($selected_date->after($browser_today))
214         $err->add($i18n->getKey('error.future_date'));
215     }
216
217     // Prohibit creating time entries in locked interval.
218     if($lockdate && $selected_date->before($lockdate))
219       $err->add($i18n->getKey('error.period_locked'));
220
221     // Prohibit creating another uncompleted record.
222     if ($err->no() && $uncompleted) {
223       $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
224     }
225
226     // Prohibit creating an overlapping record.
227     if ($err->no()) {
228       if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
229         $err->add($i18n->getKey('error.overlap'));
230     }
231
232     if ($err->no()) {
233       $id = ttTimeHelper::insert(array(
234         'date' => $cl_date,
235         'user_id' => $user->getActiveUser(),
236         'client' => $cl_client,
237         'project' => $cl_project,
238         'task' => $cl_task,
239         'start' => $cl_start,
240         'finish' => $cl_finish,
241         'duration' => $cl_duration,
242         'note' => $cl_note,
243         'billable' => $cl_billable));
244
245       // Insert a custom field if we have it.
246       $result = true;
247       if ($id && $custom_fields && $cl_cf_1) {
248         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
249           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
250         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
251           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
252       }
253
254       if ($id && $result) {
255         header('Location: timer.php');
256         exit();
257       }
258       $err->add($i18n->getKey('error.db'));
259     }
260   }
261   if ($request->getParameter('btn_stop')) {
262     // Stop button clicked. We need to finish an uncompleted record in progress.
263     $record = ttTimeHelper::getRecord($uncompleted['id'], $user->getActiveUser());
264
265     // Can we complete this record?
266     if (ttTimeHelper::isValidInterval($record['start'], $cl_finish) // finish time is greater than start time
267       && !ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $record['start'], $cl_finish)) { // no overlap
268       $res = ttTimeHelper::update(array(
269         'id'=>$record['id'],
270         'date'=>$cl_date,
271         'user_id'=>$user->getActiveUser(),
272         'client'=>$record['client_id'],
273         'project'=>$record['project_id'],
274         'task'=>$record['task_id'],
275         'start'=>$record['start'],
276         'finish'=>$cl_finish,
277         'note'=>$record['comment'],
278         'billable'=>$record['billable']));
279       if ($res) {
280         header('Location: timer.php');
281         exit();
282       } else
283         $err->add($i18n->getKey('error.db'));
284     } else {
285       // Cannot complete, redirect for manual edit.
286       header('Location: time_edit.php?id='.$record['id']);
287       exit();
288     }
289   }
290 } // isPost
291
292 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $cl_date);
293 $smarty->assign('week_total', $week_total);
294
295 $smarty->assign('uncompleted', $uncompleted);
296
297
298
299 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
300 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
301 $smarty->assign('client_list', $client_list);
302 $smarty->assign('project_list', $project_list);
303 $smarty->assign('task_list', $task_list);
304 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
305 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
306 $smarty->assign('timestring', $selected_date->toString($user->date_format));
307 $smarty->assign('title', $i18n->getKey('title.time'));
308 $smarty->assign('content_page_name', 'mobile/timer.tpl');
309 $smarty->display('mobile/index.tpl');