More refactoring for subgroups.
[timetracker.git] / mobile / time.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('ttGroupHelper');
33 import('ttClientHelper');
34 import('ttTimeHelper');
35 import('DateAndTime');
36
37 // Access checks.
38 if (!ttAccessAllowed('track_own_time')) {
39   header('Location: access_denied.php');
40   exit();
41 }
42 // End of access checks.
43
44 $user_id = $user->getUser();
45
46 // Initialize and store date in session.
47 $cl_date = $request->getParameter('date', @$_SESSION['date']);
48 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
49 if($selected_date->isError())
50   $selected_date = new DateAndTime(DB_DATEFORMAT);
51 if(!$cl_date)
52   $cl_date = $selected_date->toString(DB_DATEFORMAT);
53 $_SESSION['date'] = $cl_date;
54
55 // Determine previous and next dates for simple navigation.
56 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
57 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
58
59 // Use custom fields plugin if it is enabled.
60 if ($user->isPluginEnabled('cf')) {
61   require_once('../plugins/CustomFields.class.php');
62   $custom_fields = new CustomFields();
63   $smarty->assign('custom_fields', $custom_fields);
64 }
65
66 // Initialize variables.
67 $cl_start = trim($request->getParameter('start'));
68 $cl_finish = trim($request->getParameter('finish'));
69 $cl_duration = trim($request->getParameter('duration'));
70 $cl_note = trim($request->getParameter('note'));
71 // Custom field.
72 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
73 $_SESSION['cf_1'] = $cl_cf_1;
74 $cl_billable = 1;
75 if ($user->isPluginEnabled('iv')) {
76   if ($request->isPost()) {
77     $cl_billable = $request->getParameter('billable');
78     $_SESSION['billable'] = (int) $cl_billable;
79   } else 
80     if (isset($_SESSION['billable']))
81       $cl_billable = $_SESSION['billable'];
82 }
83 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
84 $_SESSION['client'] = $cl_client;
85 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
86 $_SESSION['project'] = $cl_project;
87 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
88 $_SESSION['task'] = $cl_task;
89
90 // Elements of timeRecordForm.
91 $form = new Form('timeRecordForm');
92
93 // Dropdown for clients in MODE_TIME. Use all active clients.
94 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
95     $active_clients = ttGroupHelper::getActiveClients(true);
96     $form->addInput(array('type'=>'combobox',
97       'onchange'=>'fillProjectDropdown(this.value);',
98       'name'=>'client',
99       'style'=>'width: 250px;',
100       'value'=>$cl_client,
101       'data'=>$active_clients,
102       'datakeys'=>array('id', 'name'),
103       'empty'=>array(''=>$i18n->get('dropdown.select'))));
104   // Note: in other modes the client list is filtered to relevant clients only. See below.
105 }
106
107 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
108   // Dropdown for projects assigned to user.
109   $project_list = $user->getAssignedProjects();
110   $form->addInput(array('type'=>'combobox',
111     'onchange'=>'fillTaskDropdown(this.value);',
112     'name'=>'project',
113     'style'=>'width: 250px;',
114     'value'=>$cl_project,
115     'data'=>$project_list,
116     'datakeys'=>array('id','name'),
117     'empty'=>array(''=>$i18n->get('dropdown.select'))));
118
119   // Dropdown for clients if the clients plugin is enabled.
120   if ($user->isPluginEnabled('cl')) {
121     $active_clients = ttGroupHelper::getActiveClients(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       if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
131         $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
132       if ($intersection) {
133         $client['projects'] = implode(',', $intersection);
134         $client_list[] = $client;
135       }
136     }
137     $form->addInput(array('type'=>'combobox',
138       'onchange'=>'fillProjectDropdown(this.value);',
139       'name'=>'client',
140       'style'=>'width: 250px;',
141       'value'=>$cl_client,
142       'data'=>$client_list,
143       'datakeys'=>array('id', 'name'),
144       'empty'=>array(''=>$i18n->get('dropdown.select'))));
145   }
146 }
147
148 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
149   $task_list = ttGroupHelper::getActiveTasks();
150   $form->addInput(array('type'=>'combobox',
151     'name'=>'task',
152     'style'=>'width: 250px;',
153     'value'=>$cl_task,
154     'data'=>$task_list,
155     'datakeys'=>array('id','name'),
156     'empty'=>array(''=>$i18n->get('dropdown.select'))));
157 }
158 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
159   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
160   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
161   if ($user->punch_mode && !$user->canOverridePunchMode()) {
162     // Make the start and finish fields read-only.
163     $form->getElement('start')->setEnabled(false);
164     $form->getElement('finish')->setEnabled(false);
165   }
166 }
167 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
168   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
169 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 60px;','value'=>$cl_note));
170 if ($user->isPluginEnabled('iv'))
171   $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
172 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
173 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
174
175 // If we have custom fields - add controls for them.
176 if ($custom_fields && $custom_fields->fields[0]) {
177   // Only one custom field is supported at this time.
178   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
179     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
180   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
181     $form->addInput(array('type'=>'combobox','name'=>'cf_1',
182       'style'=>'width: 250px;',
183       'value'=>$cl_cf_1,
184       'data'=>$custom_fields->options,
185       'empty'=>array(''=>$i18n->get('dropdown.select'))));
186   }
187 }
188
189 // Submit.
190 if ($request->isPost()) {
191   if ($request->getParameter('btn_submit')) {
192
193     // Validate user input.
194     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
195       $err->add($i18n->get('error.client'));
196     if ($custom_fields) {
197       if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
198     }
199     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
200       if (!$cl_project) $err->add($i18n->get('error.project'));
201     }
202     if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
203       if (!$cl_task) $err->add($i18n->get('error.task'));
204     }
205     if (strlen($cl_duration) == 0) {
206       if ($cl_start || $cl_finish) {
207         if (!ttTimeHelper::isValidTime($cl_start))
208           $err->add($i18n->get('error.field'), $i18n->get('label.start'));
209         if ($cl_finish) {
210           if (!ttTimeHelper::isValidTime($cl_finish))
211             $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
212           if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
213             $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
214         }
215       } else {
216         if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
217           $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
218           $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
219         }
220         if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
221           $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
222       }
223     } else {
224       if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
225         $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
226     }
227     if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
228     // Finished validating user input.
229
230     // Prohibit creating entries in future.
231     if (!$user->future_entries) {
232       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
233       if ($selected_date->after($browser_today))
234         $err->add($i18n->get('error.future_date'));
235     }
236
237     // Prohibit creating entries in locked range.
238     if ($user->isDateLocked($selected_date))
239       $err->add($i18n->get('error.range_locked'));
240
241     // Prohibit creating another uncompleted record.
242     if ($err->no()) {
243       if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
244         $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
245     }
246
247     // Prohibit creating an overlapping record.
248     if ($err->no()) {
249       if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
250         $err->add($i18n->get('error.overlap'));
251     }
252
253     if ($err->no()) {
254       $id = ttTimeHelper::insert(array(
255         'date' => $cl_date,
256         'user_id' => $user_id,
257         'group_id' => $user->getGroup(),
258         'org_id' => $user->org_id,
259         'client' => $cl_client,
260         'project' => $cl_project,
261         'task' => $cl_task,
262         'start' => $cl_start,
263         'finish' => $cl_finish,
264         'duration' => $cl_duration,
265         'note' => $cl_note,
266         'billable' => $cl_billable));
267
268       // Insert a custom field if we have it.
269       $result = true;
270       if ($id && $custom_fields && $cl_cf_1) {
271         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
272           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
273         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
274           $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
275       }
276
277       if ($id && $result) {
278         header('Location: time.php');
279         exit();
280       }
281       $err->add($i18n->get('error.db'));
282     }
283   }
284 } // isPost
285
286 $smarty->assign('next_date', $next_date);
287 $smarty->assign('prev_date', $prev_date);
288 $smarty->assign('time_records', ttTimeHelper::getRecords($user_id, $cl_date));
289 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user_id, $cl_date));
290 $smarty->assign('client_list', $client_list);
291 $smarty->assign('project_list', $project_list);
292 $smarty->assign('task_list', $task_list);
293 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
294 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
295 $smarty->assign('timestring', $selected_date->toString($user->date_format));
296 $smarty->assign('title', $i18n->get('title.time'));
297 $smarty->assign('content_page_name', 'mobile/time.tpl');
298 $smarty->display('mobile/index.tpl');