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