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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('../initialize.php');
31 import('ttUserHelper');
32 import('ttGroupHelper');
33 import('ttClientHelper');
34 import('ttTimeHelper');
35 import('DateAndTime');
38 if (!ttAccessAllowed('track_own_time')) {
39 header('Location: access_denied.php');
42 $cl_id = (int)$request->getParameter('id');
43 $user_id = $user->getUser();
44 $time_rec = ttTimeHelper::getRecord($cl_id, $user_id);
45 if (!$time_rec || $time_rec['invoice_id']) {
46 // Prohibit editing not ours or invoiced records.
47 header('Location: access_denied.php');
50 // End of access checks.
52 // Use custom fields plugin if it is enabled.
53 if ($user->isPluginEnabled('cf')) {
54 require_once('../plugins/CustomFields.class.php');
55 $custom_fields = new CustomFields();
56 $smarty->assign('custom_fields', $custom_fields);
59 $item_date = new DateAndTime(DB_DATEFORMAT, $time_rec['date']);
60 $confirm_save = $user->getConfigOption('confirm_save');
62 // Initialize variables.
63 $cl_start = $cl_finish = $cl_duration = $cl_date = $cl_note = $cl_project = $cl_task = $cl_billable = null;
64 if ($request->isPost()) {
65 $cl_start = trim($request->getParameter('start'));
66 $cl_finish = trim($request->getParameter('finish'));
67 $cl_duration = trim($request->getParameter('duration'));
68 $cl_date = $request->getParameter('date');
69 $cl_note = trim($request->getParameter('note'));
70 $cl_cf_1 = trim($request->getParameter('cf_1'));
71 $cl_client = $request->getParameter('client');
72 $cl_project = $request->getParameter('project');
73 $cl_task = $request->getParameter('task');
75 if ($user->isPluginEnabled('iv'))
76 $cl_billable = $request->getParameter('billable');
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'];
87 // If we have custom fields - obtain values for them.
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'];
97 $cl_billable = $time_rec['billable'];
99 // Add an info message to the form if we are editing an uncompleted record.
100 if (strlen($cl_start) > 0 && $cl_start == $cl_finish && $cl_duration == '0:00') {
103 $msg->add($i18n->get('form.time_edit.uncompleted'));
107 // Initialize elements of 'timeRecordForm'.
108 $form = new Form('timeRecordForm');
110 // Dropdown for clients in MODE_TIME. Use all active clients.
111 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
112 $active_clients = ttGroupHelper::getActiveClients(true);
113 $form->addInput(array('type'=>'combobox',
114 'onchange'=>'fillProjectDropdown(this.value);',
116 'style'=>'width: 250px;',
118 'data'=>$active_clients,
119 'datakeys'=>array('id', 'name'),
120 'empty'=>array(''=>$i18n->get('dropdown.select'))));
121 // Note: in other modes the client list is filtered to relevant clients only. See below.
124 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
125 // Dropdown for projects assigned to user.
126 $project_list = $user->getAssignedProjects();
127 $form->addInput(array('type'=>'combobox',
128 'onchange'=>'fillTaskDropdown(this.value);',
130 'style'=>'width: 250px;',
131 'value'=>$cl_project,
132 'data'=>$project_list,
133 'datakeys'=>array('id','name'),
134 'empty'=>array(''=>$i18n->get('dropdown.select'))));
136 // Dropdown for clients if the clients plugin is enabled.
137 if ($user->isPluginEnabled('cl')) {
138 $active_clients = ttGroupHelper::getActiveClients(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'];
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);
150 $client['projects'] = implode(',', $intersection);
151 $client_list[] = $client;
154 $form->addInput(array('type'=>'combobox',
155 'onchange'=>'fillProjectDropdown(this.value);',
157 'style'=>'width: 250px;',
159 'data'=>$client_list,
160 'datakeys'=>array('id', 'name'),
161 'empty'=>array(''=>$i18n->get('dropdown.select'))));
165 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
166 $task_list = ttGroupHelper::getActiveTasks();
167 $form->addInput(array('type'=>'combobox',
169 'style'=>'width: 250px;',
172 'datakeys'=>array('id','name'),
173 'empty'=>array(''=>$i18n->get('dropdown.select'))));
176 // Add other controls.
177 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
178 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
179 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
180 if ($user->punch_mode && !$user->canOverridePunchMode()) {
181 // Make the start and finish fields read-only.
182 $form->getElement('start')->setEnabled(false);
183 $form->getElement('finish')->setEnabled(false);
186 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
187 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
188 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
189 $form->addInput(array('type'=>'textarea','name'=>'note','class'=>'mobile-textarea','value'=>$cl_note));
190 // If we have custom fields - add controls for them.
191 if ($custom_fields && $custom_fields->fields[0]) {
192 // Only one custom field is supported at this time.
193 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
194 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
195 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
196 $form->addInput(array('type'=>'combobox',
198 'style'=>'width: 250px;',
200 'data'=>$custom_fields->options,
201 'empty' => array('' => $i18n->get('dropdown.select'))));
204 // Hidden control for record id.
205 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
206 if ($user->isPluginEnabled('iv'))
207 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
208 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save click.
209 $on_click_action = 'browser_today.value=get_date();';
210 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>$on_click_action,'value'=>$i18n->get('button.copy')));
211 if ($confirm_save) $on_click_action .= 'return(confirmSave());';
212 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>$on_click_action,'value'=>$i18n->get('button.save')));
213 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
215 if ($request->isPost()) {
217 // Validate user input.
218 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
219 $err->add($i18n->get('error.client'));
220 if ($custom_fields) {
221 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
223 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
224 if (!$cl_project) $err->add($i18n->get('error.project'));
226 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
227 if (!$cl_task) $err->add($i18n->get('error.task'));
230 if ('0' == $cl_duration)
231 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
232 elseif ($cl_start || $cl_finish) {
233 if (!ttTimeHelper::isValidTime($cl_start))
234 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
236 if (!ttTimeHelper::isValidTime($cl_finish))
237 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
238 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
239 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
242 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
243 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
244 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
246 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
247 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
250 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
251 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
253 if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
254 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
255 if (!ttTimeHelper::canAdd()) $err->add($i18n->get('error.expired'));
256 // Finished validating user input.
258 // This is a new date for the time record.
259 $new_date = new DateAndTime($user->date_format, $cl_date);
261 // Prohibit creating entries in future.
262 if (!$user->future_entries) {
263 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
264 if ($new_date->after($browser_today))
265 $err->add($i18n->get('error.future_date'));
269 if ($request->getParameter('btn_save')) {
271 // 1) Prohibit saving locked entries in any form.
272 // 2) Prohibit saving completed unlocked entries into locked range.
273 // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
275 // Now, step by step.
277 // 1) Prohibit saving locked entries in any form.
278 if ($user->isDateLocked($item_date))
279 $err->add($i18n->get('error.range_locked'));
281 // 2) Prohibit saving completed unlocked entries into locked range.
282 if ($err->no() && $user->isDateLocked($new_date))
283 $err->add($i18n->get('error.range_locked'));
285 // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
286 $uncompleted = ($cl_finish == '' && $cl_duration == '');
288 $not_completed_rec = ttTimeHelper::getUncompleted($user_id);
289 if ($not_completed_rec && ($time_rec['id'] <> $not_completed_rec['id'])) {
290 // We have another not completed record.
291 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
296 // Prohibit creating an overlapping record.
298 if (ttTimeHelper::overlaps($user_id, $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish, $cl_id))
299 $err->add($i18n->get('error.overlap'));
304 $res = ttTimeHelper::update(array(
306 'date'=>$new_date->toString(DB_DATEFORMAT),
308 'client'=>$cl_client,
309 'project'=>$cl_project,
312 'finish'=>$cl_finish,
313 'duration'=>$cl_duration,
315 'billable'=>$cl_billable));
317 // If we have custom fields - update values.
318 if ($res && $custom_fields) {
319 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
320 $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
321 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
322 $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
326 header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
332 // Save as new record.
333 if ($request->getParameter('btn_copy')) {
335 // 1) Prohibit saving into locked range.
336 // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
338 // Now, step by step.
340 // 1) Prohibit saving into locked range.
341 if ($user->isDateLocked($new_date))
342 $err->add($i18n->get('error.range_locked'));
344 // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
345 $uncompleted = ($cl_finish == '' && $cl_duration == '');
347 $not_completed_rec = ttTimeHelper::getUncompleted($user_id);
348 if ($not_completed_rec) {
349 // We have another not completed record.
350 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
355 // Prohibit creating an overlapping record.
357 if (ttTimeHelper::overlaps($user_id, $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish))
358 $err->add($i18n->get('error.overlap'));
361 // Now, a new insert.
364 $id = ttTimeHelper::insert(array(
365 'date'=>$new_date->toString(DB_DATEFORMAT),
367 'group_id'=>$user->getGroup(),
368 'org_id' => $user->org_id,
369 'client'=>$cl_client,
370 'project'=>$cl_project,
373 'finish'=>$cl_finish,
374 'duration'=>$cl_duration,
376 'billable'=>$cl_billable,
379 // Insert a custom field if we have it.
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);
388 header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
391 $err->add($i18n->get('error.db'));
395 if ($request->getParameter('btn_delete')) {
396 header("Location: time_delete.php?id=$cl_id");
402 $smarty->assign('confirm_save', true);
403 $smarty->assign('entry_date', $cl_date);
405 $smarty->assign('client_list', $client_list);
406 $smarty->assign('project_list', $project_list);
407 $smarty->assign('task_list', $task_list);
408 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
409 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
410 $smarty->assign('title', $i18n->get('title.edit_time_record'));
411 $smarty->assign('content_page_name', 'mobile/time_edit.tpl');
412 $smarty->display('mobile/index.tpl');