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