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