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('ttTeamHelper');
33 import('ttClientHelper');
34 import('ttTimeHelper');
35 import('DateAndTime');
38 if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_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($user->group_id);
56 $smarty->assign('custom_fields', $custom_fields);
59 $item_date = new DateAndTime(DB_DATEFORMAT, $time_rec['date']);
61 // Initialize variables.
62 $cl_start = $cl_finish = $cl_duration = $cl_date = $cl_note = $cl_project = $cl_task = $cl_billable = null;
63 if ($request->isPost()) {
64 $cl_start = trim($request->getParameter('start'));
65 $cl_finish = trim($request->getParameter('finish'));
66 $cl_duration = trim($request->getParameter('duration'));
67 $cl_date = $request->getParameter('date');
68 $cl_note = trim($request->getParameter('note'));
69 $cl_cf_1 = trim($request->getParameter('cf_1'));
70 $cl_client = $request->getParameter('client');
71 $cl_project = $request->getParameter('project');
72 $cl_task = $request->getParameter('task');
74 if ($user->isPluginEnabled('iv'))
75 $cl_billable = $request->getParameter('billable');
76 if ($user->isPluginEnabled('ps'))
77 $cl_paid = $request->getParameter('paid');
79 $cl_client = $time_rec['client_id'];
80 $cl_project = $time_rec['project_id'];
81 $cl_task = $time_rec['task_id'];
82 $cl_start = $time_rec['start'];
83 $cl_finish = $time_rec['finish'];
84 $cl_duration = $time_rec['duration'];
85 $cl_date = $item_date->toString($user->date_format);
86 $cl_note = $time_rec['comment'];
88 // If we have custom fields - obtain values for them.
90 // Get custom field value for time record.
91 $fields = $custom_fields->get($time_rec['id']);
92 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
93 $cl_cf_1 = $fields[0]['value'];
94 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
95 $cl_cf_1 = $fields[0]['option_id'];
98 $cl_billable = $time_rec['billable'];
99 $cl_paid = $time_rec['paid'];
101 // Add an info message to the form if we are editing an uncompleted record.
102 if (strlen($cl_start) > 0 && $cl_start == $cl_finish && $cl_duration == '0:00') {
105 $msg->add($i18n->get('form.time_edit.uncompleted'));
109 // Initialize elements of 'timeRecordForm'.
110 $form = new Form('timeRecordForm');
112 // Dropdown for clients in MODE_TIME. Use all active clients.
113 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
114 $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
115 $form->addInput(array('type'=>'combobox',
116 'onchange'=>'fillProjectDropdown(this.value);',
118 'style'=>'width: 250px;',
120 'data'=>$active_clients,
121 'datakeys'=>array('id', 'name'),
122 'empty'=>array(''=>$i18n->get('dropdown.select'))));
123 // Note: in other modes the client list is filtered to relevant clients only. See below.
126 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
127 // Dropdown for projects assigned to user.
128 $project_list = $user->getAssignedProjects();
129 $form->addInput(array('type'=>'combobox',
130 'onchange'=>'fillTaskDropdown(this.value);',
132 'style'=>'width: 250px;',
133 'value'=>$cl_project,
134 'data'=>$project_list,
135 'datakeys'=>array('id','name'),
136 'empty'=>array(''=>$i18n->get('dropdown.select'))));
138 // Dropdown for clients if the clients plugin is enabled.
139 if ($user->isPluginEnabled('cl')) {
140 $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
141 // We need an array of assigned project ids to do some trimming.
142 foreach($project_list as $project)
143 $projects_assigned_to_user[] = $project['id'];
145 // Build a client list out of active clients. Use only clients that are relevant to user.
146 // Also trim their associated project list to only assigned projects (to user).
147 foreach($active_clients as $client) {
148 $projects_assigned_to_client = explode(',', $client['projects']);
149 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
150 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
152 $client['projects'] = implode(',', $intersection);
153 $client_list[] = $client;
156 $form->addInput(array('type'=>'combobox',
157 'onchange'=>'fillProjectDropdown(this.value);',
159 'style'=>'width: 250px;',
161 'data'=>$client_list,
162 'datakeys'=>array('id', 'name'),
163 'empty'=>array(''=>$i18n->get('dropdown.select'))));
167 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
168 $task_list = ttTeamHelper::getActiveTasks($user->group_id);
169 $form->addInput(array('type'=>'combobox',
171 'style'=>'width: 250px;',
174 'datakeys'=>array('id','name'),
175 'empty'=>array(''=>$i18n->get('dropdown.select'))));
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 if ($user->punch_mode && !$user->canOverridePunchMode()) {
183 // Make the start and finish fields read-only.
184 $form->getElement('start')->setEnabled(false);
185 $form->getElement('finish')->setEnabled(false);
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',
200 'style'=>'width: 250px;',
202 'data'=>$custom_fields->options,
203 'empty' => array('' => $i18n->get('dropdown.select'))));
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->can('manage_invoices') && $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 // TODO: improve on conditional confirmSave.
214 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date();return(confirmSave())','value'=>$i18n->get('button.save')));
215 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.copy')));
216 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
218 if ($request->isPost()) {
220 // Validate user input.
221 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
222 $err->add($i18n->get('error.client'));
223 if ($custom_fields) {
224 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
226 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
227 if (!$cl_project) $err->add($i18n->get('error.project'));
229 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
230 if (!$cl_task) $err->add($i18n->get('error.task'));
233 if ('0' == $cl_duration)
234 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
235 elseif ($cl_start || $cl_finish) {
236 if (!ttTimeHelper::isValidTime($cl_start))
237 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
239 if (!ttTimeHelper::isValidTime($cl_finish))
240 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
241 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
242 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
245 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
246 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
247 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
249 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
250 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
253 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
254 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
256 if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
257 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
258 // Finished validating user input.
260 // This is a new date for the time record.
261 $new_date = new DateAndTime($user->date_format, $cl_date);
263 // Prohibit creating entries in future.
264 if (!$user->future_entries) {
265 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
266 if ($new_date->after($browser_today))
267 $err->add($i18n->get('error.future_date'));
271 if ($request->getParameter('btn_save')) {
273 // 1) Prohibit saving locked time entries in any form.
274 // 2) Prohibit saving completed unlocked entries into locked interval.
275 // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
277 // Now, step by step.
279 // 1) Prohibit saving locked entries in any form.
280 if ($user->isDateLocked($item_date))
281 $err->add($i18n->get('error.range_locked'));
283 // 2) Prohibit saving completed unlocked entries into locked range.
284 if ($err->no() && $user->isDateLocked($new_date))
285 $err->add($i18n->get('error.range_locked'));
287 // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
288 $uncompleted = ($cl_finish == '' && $cl_duration == '');
290 $not_completed_rec = ttTimeHelper::getUncompleted($user_id);
291 if ($not_completed_rec && ($time_rec['id'] <> $not_completed_rec['id'])) {
292 // We have another not completed record.
293 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
298 // Prohibit creating an overlapping record.
300 if (ttTimeHelper::overlaps($user_id, $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish, $cl_id))
301 $err->add($i18n->get('error.overlap'));
306 $res = ttTimeHelper::update(array(
308 'date'=>$new_date->toString(DB_DATEFORMAT),
310 'client'=>$cl_client,
311 'project'=>$cl_project,
314 'finish'=>$cl_finish,
315 'duration'=>$cl_duration,
317 'billable'=>$cl_billable,
320 // If we have custom fields - update values.
321 if ($res && $custom_fields) {
322 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
323 $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
324 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
325 $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
329 header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
335 // Save as new record.
336 if ($request->getParameter('btn_copy')) {
338 // 1) Prohibit saving into locked range.
339 // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
341 // Now, step by step.
343 // 1) Prohibit saving into locked range.
344 if ($user->isDateLocked($new_date))
345 $err->add($i18n->get('error.range_locked'));
347 // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
348 $uncompleted = ($cl_finish == '' && $cl_duration == '');
350 $not_completed_rec = ttTimeHelper::getUncompleted($user_id);
351 if ($not_completed_rec) {
352 // We have another not completed record.
353 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
358 // Prohibit creating an overlapping record.
360 if (ttTimeHelper::overlaps($user_id, $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish))
361 $err->add($i18n->get('error.overlap'));
364 // Now, a new insert.
367 $id = ttTimeHelper::insert(array(
368 'date'=>$new_date->toString(DB_DATEFORMAT),
370 'group_id'=>$user->getGroup(),
371 'org_id' => $user->org_id,
372 'client'=>$cl_client,
373 'project'=>$cl_project,
376 'finish'=>$cl_finish,
377 'duration'=>$cl_duration,
379 'billable'=>$cl_billable,
382 // Insert a custom field if we have it.
384 if ($id && $custom_fields && $cl_cf_1) {
385 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
386 $res = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
387 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
388 $res = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
391 header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
394 $err->add($i18n->get('error.db'));
398 if ($request->getParameter('btn_delete')) {
399 header("Location: time_delete.php?id=$cl_id");
404 // TODO: improve on conditional confirmSave.
405 $smarty->assign('entry_date', $cl_date);
407 $smarty->assign('client_list', $client_list);
408 $smarty->assign('project_list', $project_list);
409 $smarty->assign('task_list', $task_list);
410 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
411 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
412 $smarty->assign('title', $i18n->get('title.edit_time_record'));
413 $smarty->assign('content_page_name', 'time_edit.tpl');
414 $smarty->display('index.tpl');