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