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']);
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');
77 if ($user->isPluginEnabled('ps'))
78 $cl_paid = $request->getParameter('paid');
80 $cl_client = $time_rec['client_id'];
81 $cl_project = $time_rec['project_id'];
82 $cl_task = $time_rec['task_id'];
83 $cl_start = $time_rec['start'];
84 $cl_finish = $time_rec['finish'];
85 $cl_duration = $time_rec['duration'];
86 $cl_date = $item_date->toString($user->date_format);
87 $cl_note = $time_rec['comment'];
89 // If we have custom fields - obtain values for them.
91 // Get custom field value for time record.
92 $fields = $custom_fields->get($time_rec['id']);
93 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
94 $cl_cf_1 = $fields[0]['value'];
95 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
96 $cl_cf_1 = $fields[0]['option_id'];
99 $cl_billable = $time_rec['billable'];
100 $cl_paid = $time_rec['paid'];
102 // Add an info message to the form if we are editing an uncompleted record.
103 if (strlen($cl_start) > 0 && $cl_start == $cl_finish && $cl_duration == '0:00') {
106 $msg->add($i18n->get('form.time_edit.uncompleted'));
110 // Initialize elements of 'timeRecordForm'.
111 $form = new Form('timeRecordForm');
113 // Dropdown for clients in MODE_TIME. Use all active clients.
114 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
115 $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
116 $form->addInput(array('type'=>'combobox',
117 'onchange'=>'fillProjectDropdown(this.value);',
119 'style'=>'width: 250px;',
121 'data'=>$active_clients,
122 'datakeys'=>array('id', 'name'),
123 'empty'=>array(''=>$i18n->get('dropdown.select'))));
124 // Note: in other modes the client list is filtered to relevant clients only. See below.
127 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
128 // Dropdown for projects assigned to user.
129 $project_list = $user->getAssignedProjects();
130 $form->addInput(array('type'=>'combobox',
131 'onchange'=>'fillTaskDropdown(this.value);',
133 'style'=>'width: 250px;',
134 'value'=>$cl_project,
135 'data'=>$project_list,
136 'datakeys'=>array('id','name'),
137 'empty'=>array(''=>$i18n->get('dropdown.select'))));
139 // Dropdown for clients if the clients plugin is enabled.
140 if ($user->isPluginEnabled('cl')) {
141 $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
142 // We need an array of assigned project ids to do some trimming.
143 foreach($project_list as $project)
144 $projects_assigned_to_user[] = $project['id'];
146 // Build a client list out of active clients. Use only clients that are relevant to user.
147 // Also trim their associated project list to only assigned projects (to user).
148 foreach($active_clients as $client) {
149 $projects_assigned_to_client = explode(',', $client['projects']);
150 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
151 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
153 $client['projects'] = implode(',', $intersection);
154 $client_list[] = $client;
157 $form->addInput(array('type'=>'combobox',
158 'onchange'=>'fillProjectDropdown(this.value);',
160 'style'=>'width: 250px;',
162 'data'=>$client_list,
163 'datakeys'=>array('id', 'name'),
164 'empty'=>array(''=>$i18n->get('dropdown.select'))));
168 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
169 $task_list = ttTeamHelper::getActiveTasks($user->group_id);
170 $form->addInput(array('type'=>'combobox',
172 'style'=>'width: 250px;',
175 'datakeys'=>array('id','name'),
176 'empty'=>array(''=>$i18n->get('dropdown.select'))));
179 // Add other controls.
180 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
181 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
182 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
183 if ($user->punch_mode && !$user->canOverridePunchMode()) {
184 // Make the start and finish fields read-only.
185 $form->getElement('start')->setEnabled(false);
186 $form->getElement('finish')->setEnabled(false);
189 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
190 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
191 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
192 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 200px;','value'=>$cl_note));
193 // If we have custom fields - add controls for them.
194 if ($custom_fields && $custom_fields->fields[0]) {
195 // Only one custom field is supported at this time.
196 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
197 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
198 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
199 $form->addInput(array('type'=>'combobox',
201 'style'=>'width: 250px;',
203 'data'=>$custom_fields->options,
204 'empty' => array('' => $i18n->get('dropdown.select'))));
207 // Hidden control for record id.
208 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
209 if ($user->isPluginEnabled('iv'))
210 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
211 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))
212 $form->addInput(array('type'=>'checkbox','name'=>'paid','value'=>$cl_paid));
213 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
214 $on_click_action = 'browser_today.value=get_date();';
215 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>$on_click_action,'value'=>$i18n->get('button.copy')));
216 if ($confirm_save) $on_click_action .= 'return(confirmSave());';
217 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>$on_click_action,'value'=>$i18n->get('button.save')));
218 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
220 if ($request->isPost()) {
222 // Validate user input.
223 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
224 $err->add($i18n->get('error.client'));
225 if ($custom_fields) {
226 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
228 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
229 if (!$cl_project) $err->add($i18n->get('error.project'));
231 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
232 if (!$cl_task) $err->add($i18n->get('error.task'));
235 if ('0' == $cl_duration)
236 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
237 elseif ($cl_start || $cl_finish) {
238 if (!ttTimeHelper::isValidTime($cl_start))
239 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
241 if (!ttTimeHelper::isValidTime($cl_finish))
242 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
243 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
244 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
247 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
248 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
249 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
251 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
252 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
255 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
256 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
258 if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
259 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
260 // Finished validating user input.
262 // This is a new date for the time record.
263 $new_date = new DateAndTime($user->date_format, $cl_date);
265 // Prohibit creating entries in future.
266 if (!$user->future_entries) {
267 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
268 if ($new_date->after($browser_today))
269 $err->add($i18n->get('error.future_date'));
273 if ($request->getParameter('btn_save')) {
275 // 1) Prohibit saving locked time entries in any form.
276 // 2) Prohibit saving completed unlocked entries into locked interval.
277 // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
279 // Now, step by step.
281 // 1) Prohibit saving locked entries in any form.
282 if ($user->isDateLocked($item_date))
283 $err->add($i18n->get('error.range_locked'));
285 // 2) Prohibit saving completed unlocked entries into locked range.
286 if ($err->no() && $user->isDateLocked($new_date))
287 $err->add($i18n->get('error.range_locked'));
289 // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
290 $uncompleted = ($cl_finish == '' && $cl_duration == '');
292 $not_completed_rec = ttTimeHelper::getUncompleted($user_id);
293 if ($not_completed_rec && ($time_rec['id'] <> $not_completed_rec['id'])) {
294 // We have another not completed record.
295 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
300 // Prohibit creating an overlapping record.
302 if (ttTimeHelper::overlaps($user_id, $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish, $cl_id))
303 $err->add($i18n->get('error.overlap'));
308 $res = ttTimeHelper::update(array(
310 'date'=>$new_date->toString(DB_DATEFORMAT),
312 'client'=>$cl_client,
313 'project'=>$cl_project,
316 'finish'=>$cl_finish,
317 'duration'=>$cl_duration,
319 'billable'=>$cl_billable,
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 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
327 $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
331 header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
337 // Save as new record.
338 if ($request->getParameter('btn_copy')) {
340 // 1) Prohibit saving into locked range.
341 // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
343 // Now, step by step.
345 // 1) Prohibit saving into locked range.
346 if ($user->isDateLocked($new_date))
347 $err->add($i18n->get('error.range_locked'));
349 // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
350 $uncompleted = ($cl_finish == '' && $cl_duration == '');
352 $not_completed_rec = ttTimeHelper::getUncompleted($user_id);
353 if ($not_completed_rec) {
354 // We have another not completed record.
355 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
360 // Prohibit creating an overlapping record.
362 if (ttTimeHelper::overlaps($user_id, $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish))
363 $err->add($i18n->get('error.overlap'));
366 // Now, a new insert.
369 $id = ttTimeHelper::insert(array(
370 'date'=>$new_date->toString(DB_DATEFORMAT),
372 'group_id'=>$user->getGroup(),
373 'org_id' => $user->org_id,
374 'client'=>$cl_client,
375 'project'=>$cl_project,
378 'finish'=>$cl_finish,
379 'duration'=>$cl_duration,
381 'billable'=>$cl_billable,
384 // Insert a custom field if we have it.
386 if ($id && $custom_fields && $cl_cf_1) {
387 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
388 $res = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
389 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
390 $res = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
393 header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
396 $err->add($i18n->get('error.db'));
400 if ($request->getParameter('btn_delete')) {
401 header("Location: time_delete.php?id=$cl_id");
407 $smarty->assign('confirm_save', true);
408 $smarty->assign('entry_date', $cl_date);
410 $smarty->assign('client_list', $client_list);
411 $smarty->assign('project_list', $project_list);
412 $smarty->assign('task_list', $task_list);
413 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
414 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
415 $smarty->assign('title', $i18n->get('title.edit_time_record'));
416 $smarty->assign('content_page_name', 'time_edit.tpl');
417 $smarty->display('index.tpl');