Changed the meaning of override_punch_mode to apply only to lower roles.
[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 (!ttAccessAllowed('track_own_time')) {
39   header('Location: access_denied.php');
40   exit();
41 }
42
43 // Use custom fields plugin if it is enabled.
44 if ($user->isPluginEnabled('cf')) {
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->isPost()) {
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 ($user->isPluginEnabled('iv'))
74     $cl_billable = $request->getParameter('billable');
75   if ($user->isPluginEnabled('ps'))
76     $cl_paid = $request->getParameter('paid');
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   $cl_paid = $time_rec['paid'];
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->getKey('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 = ttTeamHelper::getActiveClients($user->team_id, 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->getKey('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->getKey('dropdown.select'))));
136
137   // Dropdown for clients if the clients plugin is enabled.
138   if ($user->isPluginEnabled('cl')) {
139     $active_clients = ttTeamHelper::getActiveClients($user->team_id, 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->getKey('dropdown.select'))));
163   }
164 }
165
166 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
167   $task_list = ttTeamHelper::getActiveTasks($user->team_id);
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->getKey('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 && // Punch mode enabled for team.
182         !($user->behalf_id && $user->can('override_punch_mode'))) { // Cannot override for lower roles.
183     // Make the start and finish fields read-only.
184     $form->getElement('start')->setEnabled(false);
185     $form->getElement('finish')->setEnabled(false);
186   }
187 }
188 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
189   $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
190 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
191 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 200px;','value'=>$cl_note));
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'=>$custom_fields->options,
203       'empty' => array('' => $i18n->getKey('dropdown.select'))));
204   }
205 }
206 // Hidden control for record id.
207 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
208 if ($user->isPluginEnabled('iv'))
209   $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
210 if ($user->canManageTeam() && $user->isPluginEnabled('ps'))
211   $form->addInput(array('type'=>'checkbox','name'=>'paid','value'=>$cl_paid));
212 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
213 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.save')));
214 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.copy')));
215 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete')));
216
217 if ($request->isPost()) {
218
219   // Validate user input.
220   if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
221     $err->add($i18n->getKey('error.client'));
222   if ($custom_fields) {
223     if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
224   }
225   if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
226     if (!$cl_project) $err->add($i18n->getKey('error.project'));
227   }
228   if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
229     if (!$cl_task) $err->add($i18n->getKey('error.task'));
230   }
231   if (!$cl_duration) {
232     if ('0' == $cl_duration)
233       $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
234     elseif ($cl_start || $cl_finish) {
235       if (!ttTimeHelper::isValidTime($cl_start))
236         $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
237       if ($cl_finish) {
238         if (!ttTimeHelper::isValidTime($cl_finish))
239           $err->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
240         if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
241           $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
242       }
243     } else {
244       if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
245         $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
246         $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
247       }
248       if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
249         $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
250     }
251   } else {
252     if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
253       $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
254   }
255   if (!ttValidDate($cl_date)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.date'));
256   if (!ttValidString($cl_note, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
257   // Finished validating user input.
258
259   // This is a new date for the time record.
260   $new_date = new DateAndTime($user->date_format, $cl_date);
261
262   // Prohibit creating entries in future.
263   if (!$user->future_entries) {
264     $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
265     if ($new_date->after($browser_today))
266       $err->add($i18n->getKey('error.future_date'));
267   }
268
269   // Save record.
270   if ($request->getParameter('btn_save')) {
271     // We need to:
272     // 1) Prohibit saving locked time entries in any form.
273     // 2) Prohibit saving completed unlocked entries into locked interval.
274     // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
275
276     // Now, step by step.
277     if ($err->no()) {
278       // 1) Prohibit saving locked entries in any form.
279       if ($user->isDateLocked($item_date))
280         $err->add($i18n->getKey('error.range_locked'));
281
282       // 2) Prohibit saving completed unlocked entries into locked range.
283       if ($err->no() && $user->isDateLocked($new_date))
284         $err->add($i18n->getKey('error.range_locked'));
285
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           $err->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 ($err->no()) {
299       if (ttTimeHelper::overlaps($user->getActiveUser(), $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish, $cl_id))
300         $err->add($i18n->getKey('error.overlap'));
301     }
302
303     // Now, an update.
304     if ($err->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         'paid'=>$cl_paid));
318
319       // If we have custom fields - update values.
320       if ($res && $custom_fields) {
321         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
322           $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
323         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
324           $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
325       }
326       if ($res)
327       {
328         header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
329         exit();
330       }
331     }
332   }
333
334   // Save as new record.
335   if ($request->getParameter('btn_copy')) {
336     // We need to:
337     // 1) Prohibit saving into locked range.
338     // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
339
340     // Now, step by step.
341     if ($err->no()) {
342       // 1) Prohibit saving into locked range.
343       if ($user->isDateLocked($new_date))
344         $err->add($i18n->getKey('error.range_locked'));
345
346       // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
347       $uncompleted = ($cl_finish == '' && $cl_duration == '');
348       if ($uncompleted) {
349         $not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser());
350         if ($not_completed_rec) {
351           // We have another not completed record.
352           $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
353         }
354       }
355     }
356
357     // Prohibit creating an overlapping record.
358     if ($err->no()) {
359       if (ttTimeHelper::overlaps($user->getActiveUser(), $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish))
360         $err->add($i18n->getKey('error.overlap'));
361     }
362
363     // Now, a new insert.
364     if ($err->no()) {
365
366       $id = ttTimeHelper::insert(array(
367         'date'=>$new_date->toString(DB_DATEFORMAT),
368         'user_id'=>$user->getActiveUser(),
369         'client'=>$cl_client,
370         'project'=>$cl_project,
371         'task'=>$cl_task,
372         'start'=>$cl_start,
373         'finish'=>$cl_finish,
374         'duration'=>$cl_duration,
375         'note'=>$cl_note,
376         'billable'=>$cl_billable,
377         'paid'=>$cl_paid));
378
379       // Insert a custom field if we have it.
380       $res = true;
381       if ($id && $custom_fields && $cl_cf_1) {
382         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
383           $res = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
384         elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
385           $res = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
386       }
387       if ($id && $res) {
388         header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
389         exit();
390       }
391       $err->add($i18n->getKey('error.db'));
392     }
393   }
394
395   if ($request->getParameter('btn_delete')) {
396     header("Location: time_delete.php?id=$cl_id");
397     exit();
398   }
399 } // isPost
400
401 $smarty->assign('client_list', $client_list);
402 $smarty->assign('project_list', $project_list);
403 $smarty->assign('task_list', $task_list);
404 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
405 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
406 $smarty->assign('title', $i18n->getKey('title.edit_time_record'));
407 $smarty->assign('content_page_name', 'time_edit.tpl');
408 $smarty->display('index.tpl');