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