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