Got rid of PHP warnning when argument is not an array
[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 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     ));
120   // Note: in other modes the client list is filtered to relevant clients only. See below.
121 }
122
123 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
124   // Dropdown for projects assigned to user.
125   $project_list = $user->getAssignedProjects();
126   $form->addInput(array('type'=>'combobox',
127     'onchange'=>'fillTaskDropdown(this.value);',
128     'name'=>'project',
129     'style'=>'width: 250px;',
130     'value'=>$cl_project,
131     'data'=>$project_list,
132     'datakeys'=>array('id','name'),
133     'empty'=>array(''=>$i18n->getKey('dropdown.select'))
134   ));
135
136   // Dropdown for clients if the clients plugin is enabled.
137   if (in_array('cl', explode(',', $user->plugins))) {
138     $active_clients = ttTeamHelper::getActiveClients($user->team_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->getKey('dropdown.select'))
162     ));
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   
178 // Add other controls.
179 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
180   $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
181   $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
182 }
183 if (!$user->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
184   // Make the start and finish fields read-only.
185   $form->getElement('start')->setEnable(false);
186   $form->getElement('finish')->setEnable(false);        
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: 60px;','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   } else if ($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 }
207 // Hidden control for record id.
208 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
209 if (in_array('iv', explode(',', $user->plugins)))
210   $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable));
211 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save click.
212 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.save')));
213 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete')));
214
215 if ($request->getMethod() == 'POST') {
216
217   // Validate user input.
218   if (in_array('cl', explode(',', $user->plugins)) && in_array('cm', explode(',', $user->plugins)) && !$cl_client)
219     $errors->add($i18n->getKey('error.client'));
220   if ($custom_fields) {
221     if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $errors->add($i18n->getKey('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) $errors->add($i18n->getKey('error.project'));     
225   }
226   if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
227     if (!$cl_task) $errors->add($i18n->getKey('error.task'));
228   }
229   if (!$cl_duration) {
230     if ('0' == $cl_duration)
231       $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
232     else if ($cl_start || $cl_finish) {
233       if (!ttTimeHelper::isValidTime($cl_start))
234         $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
235       if ($cl_finish) {
236         if (!ttTimeHelper::isValidTime($cl_finish))
237           $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
238         if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
239           $errors->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
240       }
241     } else {
242       if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
243         $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
244         $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
245       }
246       if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
247         $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
248     }
249   } else {
250     if (!ttTimeHelper::isValidDuration($cl_duration))
251       $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
252   }
253   if (!ttValidDate($cl_date)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.date'));
254   if (!ttValidString($cl_note, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
255   // Finished validating user input.
256     
257   // Determine lock date.
258   $lock_interval = $user->lock_interval;
259   $lockdate = 0;
260   if ($lock_interval > 0) {
261     $lockdate = new DateAndTime();
262     $lockdate->decDay($lock_interval);
263   }
264
265   // This is a new date for the time record.
266   $new_date = new DateAndTime($user->date_format, $cl_date);
267   
268   // Prohibit creating entries in future.
269   if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
270     $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
271     if ($new_date->after($browser_today))
272       $errors->add($i18n->getKey('error.future_date'));
273   }
274
275   // Save record.
276   if ($request->getParameter('btn_save')) {
277     // We need to:
278     // 1) Prohibit saving locked time entries in any form.
279     // 2) Prohibit saving completed unlocked entries into locked interval.
280     // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
281       
282     // Now, step by step.
283     if ($errors->isEmpty()) {
284       // 1) Prohibit saving locked time entries in any form.
285       if($lockdate && $item_date->before($lockdate))
286         $errors->add($i18n->getKey('error.period_locked'));        
287       // 2) Prohibit saving completed unlocked entries into locked interval.
288       if($errors->isEmpty() && $lockdate && $new_date->before($lockdate))
289         $errors->add($i18n->getKey('error.period_locked'));        
290       // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
291       $uncompleted = ($cl_finish == '' && $cl_duration == '');
292       if ($uncompleted) {
293         $not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser());
294         if ($not_completed_rec && ($time_rec['id'] <> $not_completed_rec['id'])) {
295           // We have another not completed record.
296           $errors->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
297         }
298       }
299     }
300     
301     // Prohibit creating an overlapping record.
302     if ($errors->isEmpty()) {
303       if (ttTimeHelper::overlaps($user->getActiveUser(), $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish, $cl_id))
304         $errors->add($i18n->getKey('error.overlap'));
305     }
306
307     // Now, an update.
308     if ($errors->isEmpty()) {
309       $res = ttTimeHelper::update(array(
310           'id'=>$cl_id,  
311           'date'=>$new_date->toString(DB_DATEFORMAT),
312           'user_id'=>$user->getActiveUser(),
313           'client'=>$cl_client,
314           'project'=>$cl_project,
315           'task'=>$cl_task,
316           'start'=>$cl_start,
317           'finish'=>$cl_finish,
318           'duration'=>$cl_duration,
319           'note'=>$cl_note,
320           'billable'=>$cl_billable));
321         
322       // If we have custom fields - update values.
323       if ($res && $custom_fields) {
324         if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
325           $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
326         else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
327           $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
328       }
329       if ($res)
330       {
331         header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
332         exit();
333       }
334     }
335   }
336       
337   if ($request->getParameter('btn_delete')) {
338     header("Location: time_delete.php?id=$cl_id");
339     exit();
340   }
341 } // End of if ($request->getMethod() == 'POST')
342
343 $smarty->assign('client_list', $client_list);
344 $smarty->assign('project_list', $project_list);
345 $smarty->assign('task_list', $task_list);
346 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
347 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
348 $smarty->assign('title', $i18n->getKey('title.edit_time_record'));
349 $smarty->assign('content_page_name', 'mobile/time_edit.tpl');
350 $smarty->display('mobile/index.tpl');
351 ?>