A bit more refactoring on time entry pages.
[timetracker.git] / mobile / time_edit.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 checks.
38 if (!ttAccessAllowed('track_own_time')) {
39   header('Location: access_denied.php');
40   exit();
41 }
42 $cl_id = (int)$request->getParameter('id');
43 $user_id = $user->getUser();
44 $time_rec = ttTimeHelper::getRecord($cl_id, $user_id);
45 if (!$time_rec || $time_rec['invoice_id']) {
46   // Prohibit editing not ours or invoiced records.
47   header('Location: access_denied.php');
48   exit();
49 }
50 // End of access checks.
51
52 // Use custom fields plugin if it is enabled.
53 if ($user->isPluginEnabled('cf')) {
54   require_once('../plugins/CustomFields.class.php');
55   $custom_fields = new CustomFields($user->group_id);
56   $smarty->assign('custom_fields', $custom_fields);
57 }
58
59 $item_date = new DateAndTime(DB_DATEFORMAT, $time_rec['date']);
60
61 // Initialize variables.
62 $cl_start = $cl_finish = $cl_duration = $cl_date = $cl_note = $cl_project = $cl_task = $cl_billable = null;
63 if ($request->isPost()) {
64   $cl_start = trim($request->getParameter('start'));
65   $cl_finish = trim($request->getParameter('finish'));
66   $cl_duration = trim($request->getParameter('duration'));
67   $cl_date = $request->getParameter('date');
68   $cl_note = trim($request->getParameter('note'));
69   $cl_cf_1 = trim($request->getParameter('cf_1'));
70   $cl_client = $request->getParameter('client');
71   $cl_project = $request->getParameter('project');
72   $cl_task = $request->getParameter('task');
73   $cl_billable = 1;
74   if ($user->isPluginEnabled('iv'))
75     $cl_billable = $request->getParameter('billable');
76 } else {
77   $cl_client = $time_rec['client_id'];
78   $cl_project = $time_rec['project_id'];
79   $cl_task = $time_rec['task_id'];
80   $cl_start = $time_rec['start'];
81   $cl_finish = $time_rec['finish'];
82   $cl_duration = $time_rec['duration'];
83   $cl_date = $item_date->toString($user->date_format);
84   $cl_note = $time_rec['comment'];
85
86   // If we have custom fields - obtain values for them.
87   if ($custom_fields) {
88     // Get custom field value for time record.
89     $fields = $custom_fields->get($time_rec['id']);
90     if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
91       $cl_cf_1 = $fields[0]['value'];
92     elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
93       $cl_cf_1 = $fields[0]['option_id'];
94   }
95
96   $cl_billable = $time_rec['billable'];
97
98   // Add an info message to the form if we are editing an uncompleted record.
99   if (strlen($cl_start) > 0 && $cl_start == $cl_finish && $cl_duration == '0:00') {
100     $cl_finish = '';
101     $cl_duration = '';
102     $msg->add($i18n->get('form.time_edit.uncompleted'));
103   }
104 }
105
106 // Initialize elements of 'timeRecordForm'.
107 $form = new Form('timeRecordForm');
108
109 // Dropdown for clients in MODE_TIME. Use all active clients.
110 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
111     $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
112     $form->addInput(array('type'=>'combobox',
113       'onchange'=>'fillProjectDropdown(this.value);',
114       'name'=>'client',
115       'style'=>'width: 250px;',
116       'value'=>$cl_client,
117       'data'=>$active_clients,
118       'datakeys'=>array('id', 'name'),
119       'empty'=>array(''=>$i18n->get('dropdown.select'))));
120   // Note: in other modes the client list is filtered to relevant clients only. See below.
121 }
122
123 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
124   // Dropdown for projects assigned to user.
125   $project_list = $user->getAssignedProjects();
126   $form->addInput(array('type'=>'combobox',
127     'onchange'=>'fillTaskDropdown(this.value);',
128     'name'=>'project',
129     'style'=>'width: 250px;',
130     'value'=>$cl_project,
131     'data'=>$project_list,
132     'datakeys'=>array('id','name'),
133     'empty'=>array(''=>$i18n->get('dropdown.select'))));
134
135   // Dropdown for clients if the clients plugin is enabled.
136   if ($user->isPluginEnabled('cl')) {
137     $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
138     // We need an array of assigned project ids to do some trimming.
139     foreach($project_list as $project)
140       $projects_assigned_to_user[] = $project['id'];
141
142     // Build a client list out of active clients. Use only clients that are relevant to user.
143     // Also trim their associated project list to only assigned projects (to user).
144     foreach($active_clients as $client) {
145       $projects_assigned_to_client = explode(',', $client['projects']);
146       if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
147         $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
148       if ($intersection) {
149         $client['projects'] = implode(',', $intersection);
150         $client_list[] = $client;
151       }
152     }
153     $form->addInput(array('type'=>'combobox',
154       'onchange'=>'fillProjectDropdown(this.value);',
155       'name'=>'client',
156       'style'=>'width: 250px;',
157       'value'=>$cl_client,
158       'data'=>$client_list,
159       'datakeys'=>array('id', 'name'),
160       'empty'=>array(''=>$i18n->get('dropdown.select'))));
161   }
162 }
163
164 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
165   $task_list = ttTeamHelper::getActiveTasks($user->group_id);
166   $form->addInput(array('type'=>'combobox',
167     'name'=>'task',
168     'style'=>'width: 250px;',
169     'value'=>$cl_task,
170     'data'=>$task_list,
171     'datakeys'=>array('id','name'),
172     'empty'=>array(''=>$i18n->get('dropdown.select'))));
173 }
174
175 // Add other controls.
176 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
177   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
178   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
179   if ($user->punch_mode && !$user->canOverridePunchMode()) {
180     // Make the start and finish fields read-only.
181     $form->getElement('start')->setEnabled(false);
182     $form->getElement('finish')->setEnabled(false);
183   }
184 }
185 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
186   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
187 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
188 $form->addInput(array('type'=>'textarea','name'=>'note','class'=>'mobile-textarea','value'=>$cl_note));
189 // If we have custom fields - add controls for them.
190 if ($custom_fields && $custom_fields->fields[0]) {
191   // Only one custom field is supported at this time.
192   if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
193     $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
194   } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
195     $form->addInput(array('type'=>'combobox',
196       'name'=>'cf_1',
197       'style'=>'width: 250px;',
198       'value'=>$cl_cf_1,
199       'data'=>$custom_fields->options,
200       'empty' => array('' => $i18n->get('dropdown.select'))));
201   }
202 }
203 // Hidden control for record id.
204 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
205 if ($user->isPluginEnabled('iv'))
206   $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
207 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save click.
208 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.save')));
209 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
210
211 if ($request->isPost()) {
212
213   // Validate user input.
214   if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
215     $err->add($i18n->get('error.client'));
216   if ($custom_fields) {
217     if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
218   }
219   if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
220     if (!$cl_project) $err->add($i18n->get('error.project'));
221   }
222   if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
223     if (!$cl_task) $err->add($i18n->get('error.task'));
224   }
225   if (!$cl_duration) {
226     if ('0' == $cl_duration)
227       $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
228     elseif ($cl_start || $cl_finish) {
229       if (!ttTimeHelper::isValidTime($cl_start))
230         $err->add($i18n->get('error.field'), $i18n->get('label.start'));
231       if ($cl_finish) {
232         if (!ttTimeHelper::isValidTime($cl_finish))
233           $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
234         if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
235           $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
236       }
237     } else {
238       if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
239         $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
240         $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
241       }
242       if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
243         $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
244     }
245   } else {
246     if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
247       $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
248   }
249   if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
250   if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
251   // Finished validating user input.
252
253   // This is a new date for the time record.
254   $new_date = new DateAndTime($user->date_format, $cl_date);
255
256   // Prohibit creating entries in future.
257   if (!$user->future_entries) {
258     $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
259     if ($new_date->after($browser_today))
260       $err->add($i18n->get('error.future_date'));
261   }
262
263   // Save record.
264   if ($request->getParameter('btn_save')) {
265     // We need to:
266     // 1) Prohibit saving locked entries in any form.
267     // 2) Prohibit saving completed unlocked entries into locked range.
268     // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
269
270     // Now, step by step.
271     if ($err->no()) {
272       // 1) Prohibit saving locked entries in any form.
273       if ($user->isDateLocked($item_date))
274         $err->add($i18n->get('error.range_locked'));
275
276       // 2) Prohibit saving completed unlocked entries into locked range.
277       if ($err->no() && $user->isDateLocked($new_date))
278         $err->add($i18n->get('error.range_locked'));
279
280       // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
281       $uncompleted = ($cl_finish == '' && $cl_duration == '');
282       if ($uncompleted) {
283         $not_completed_rec = ttTimeHelper::getUncompleted($user_id);
284         if ($not_completed_rec && ($time_rec['id'] <> $not_completed_rec['id'])) {
285           // We have another not completed record.
286           $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
287         }
288       }
289     }
290
291     // Prohibit creating an overlapping record.
292     if ($err->no()) {
293       if (ttTimeHelper::overlaps($user_id, $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish, $cl_id))
294         $err->add($i18n->get('error.overlap'));
295     }
296
297     // Now, an update.
298     if ($err->no()) {
299       $res = ttTimeHelper::update(array(
300           'id'=>$cl_id,  
301           'date'=>$new_date->toString(DB_DATEFORMAT),
302           'user_id'=>$user_id,
303           'client'=>$cl_client,
304           'project'=>$cl_project,
305           'task'=>$cl_task,
306           'start'=>$cl_start,
307           'finish'=>$cl_finish,
308           'duration'=>$cl_duration,
309           'note'=>$cl_note,
310           'billable'=>$cl_billable));
311
312       // If we have custom fields - update values.
313       if ($res && $custom_fields) {
314         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
315           $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
316         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
317           $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
318       }
319       if ($res)
320       {
321         header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
322         exit();
323       }
324     }
325   }
326
327   if ($request->getParameter('btn_delete')) {
328     header("Location: time_delete.php?id=$cl_id");
329     exit();
330   }
331 } // isPost
332
333 $smarty->assign('client_list', $client_list);
334 $smarty->assign('project_list', $project_list);
335 $smarty->assign('task_list', $task_list);
336 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
337 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
338 $smarty->assign('title', $i18n->get('title.edit_time_record'));
339 $smarty->assign('content_page_name', 'mobile/time_edit.tpl');
340 $smarty->display('mobile/index.tpl');