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 (!ttAccessCheck(right_data_entry)) {
39 header('Location: access_denied.php');
43 // Use custom fields plugin if it is enabled.
44 if ($user->isPluginEnabled('cf')) {
45 require_once('plugins/CustomFields.class.php');
46 $custom_fields = new CustomFields($user->team_id);
47 $smarty->assign('custom_fields', $custom_fields);
50 $cl_id = $request->getParameter('id');
52 // Get the time record we are editing.
53 $time_rec = ttTimeHelper::getRecord($cl_id, $user->getActiveUser());
55 // Prohibit editing invoiced records.
56 if ($time_rec['invoice_id']) die($i18n->getKey('error.sys'));
58 $item_date = new DateAndTime(DB_DATEFORMAT, $time_rec['date']);
60 // Initialize variables.
61 $cl_start = $cl_finish = $cl_duration = $cl_date = $cl_note = $cl_project = $cl_task = $cl_billable = null;
62 if ($request->isPost()) {
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');
73 if ($user->isPluginEnabled('iv'))
74 $cl_billable = $request->getParameter('billable');
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'];
85 // If we have custom fields - obtain values for them.
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 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
92 $cl_cf_1 = $fields[0]['option_id'];
95 $cl_billable = $time_rec['billable'];
97 // Add an info message to the form if we are editing an uncompleted record.
98 if (strlen($cl_start) > 0 && $cl_start == $cl_finish && $cl_duration == '0:00') {
101 $msg->add($i18n->getKey('form.time_edit.uncompleted'));
105 // Initialize elements of 'timeRecordForm'.
106 $form = new Form('timeRecordForm');
108 // Dropdown for clients in MODE_TIME. Use all active clients.
109 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
110 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
111 $form->addInput(array('type'=>'combobox',
112 'onchange'=>'fillProjectDropdown(this.value);',
114 'style'=>'width: 250px;',
116 'data'=>$active_clients,
117 'datakeys'=>array('id', 'name'),
118 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
119 // Note: in other modes the client list is filtered to relevant clients only. See below.
122 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
123 // Dropdown for projects assigned to user.
124 $project_list = $user->getAssignedProjects();
125 $form->addInput(array('type'=>'combobox',
126 'onchange'=>'fillTaskDropdown(this.value);',
128 'style'=>'width: 250px;',
129 'value'=>$cl_project,
130 'data'=>$project_list,
131 'datakeys'=>array('id','name'),
132 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
134 // Dropdown for clients if the clients plugin is enabled.
135 if ($user->isPluginEnabled('cl')) {
136 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
137 // We need an array of assigned project ids to do some trimming.
138 foreach($project_list as $project)
139 $projects_assigned_to_user[] = $project['id'];
141 // Build a client list out of active clients. Use only clients that are relevant to user.
142 // Also trim their associated project list to only assigned projects (to user).
143 foreach($active_clients as $client) {
144 $projects_assigned_to_client = explode(',', $client['projects']);
145 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
146 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
148 $client['projects'] = implode(',', $intersection);
149 $client_list[] = $client;
152 $form->addInput(array('type'=>'combobox',
153 'onchange'=>'fillProjectDropdown(this.value);',
155 'style'=>'width: 250px;',
157 'data'=>$client_list,
158 'datakeys'=>array('id', 'name'),
159 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
163 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
164 $task_list = ttTeamHelper::getActiveTasks($user->team_id);
165 $form->addInput(array('type'=>'combobox',
167 'style'=>'width: 250px;',
170 'datakeys'=>array('id','name'),
171 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
174 // Add other controls.
175 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
176 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
177 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
179 if (!$user->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
180 // Make the start and finish fields read-only.
181 $form->getElement('start')->setEnabled(false);
182 $form->getElement('finish')->setEnabled(false);
184 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
185 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
186 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
187 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 200px;','value'=>$cl_note));
188 // If we have custom fields - add controls for them.
189 if ($custom_fields && $custom_fields->fields[0]) {
190 // Only one custom field is supported at this time.
191 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
192 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
193 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
194 $form->addInput(array('type'=>'combobox',
196 'style'=>'width: 250px;',
198 'data'=>$custom_fields->options,
199 'empty' => array('' => $i18n->getKey('dropdown.select'))));
202 // Hidden control for record id.
203 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
204 if ($user->isPluginEnabled('iv'))
205 $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable));
206 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
207 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.save')));
208 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.copy')));
209 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete')));
211 if ($request->isPost()) {
213 // Validate user input.
214 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
215 $err->add($i18n->getKey('error.client'));
216 if ($custom_fields) {
217 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
219 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
220 if (!$cl_project) $err->add($i18n->getKey('error.project'));
223 if ('0' == $cl_duration)
224 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
225 elseif ($cl_start || $cl_finish) {
226 if (!ttTimeHelper::isValidTime($cl_start))
227 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
229 if (!ttTimeHelper::isValidTime($cl_finish))
230 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
231 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
232 $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
235 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
236 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
237 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
239 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
240 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
243 if (!ttTimeHelper::isValidDuration($cl_duration))
244 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
246 if (!ttValidDate($cl_date)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.date'));
247 if (!ttValidString($cl_note, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
248 // Finished validating user input.
250 // This is a new date for the time record.
251 $new_date = new DateAndTime($user->date_format, $cl_date);
253 // Prohibit creating entries in future.
254 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
255 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
256 if ($new_date->after($browser_today))
257 $err->add($i18n->getKey('error.future_date'));
261 if ($request->getParameter('btn_save')) {
263 // 1) Prohibit saving locked time entries in any form.
264 // 2) Prohibit saving completed unlocked entries into locked interval.
265 // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
267 // Now, step by step.
269 // 1) Prohibit saving locked entries in any form.
270 if ($user->isDateLocked($item_date))
271 $err->add($i18n->getKey('error.range_locked'));
273 // 2) Prohibit saving completed unlocked entries into locked range.
274 if ($err->no() && $user->isDateLocked($new_date))
275 $err->add($i18n->getKey('error.range_locked'));
277 // 3) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
278 $uncompleted = ($cl_finish == '' && $cl_duration == '');
280 $not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser());
281 if ($not_completed_rec && ($time_rec['id'] <> $not_completed_rec['id'])) {
282 // We have another not completed record.
283 $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
288 // Prohibit creating an overlapping record.
290 if (ttTimeHelper::overlaps($user->getActiveUser(), $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish, $cl_id))
291 $err->add($i18n->getKey('error.overlap'));
296 $res = ttTimeHelper::update(array(
298 'date'=>$new_date->toString(DB_DATEFORMAT),
299 'user_id'=>$user->getActiveUser(),
300 'client'=>$cl_client,
301 'project'=>$cl_project,
304 'finish'=>$cl_finish,
305 'duration'=>$cl_duration,
307 'billable'=>$cl_billable));
309 // If we have custom fields - update values.
310 if ($res && $custom_fields) {
311 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
312 $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
313 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
314 $res = $custom_fields->update($cl_id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
318 header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
324 // Save as new record.
325 if ($request->getParameter('btn_copy')) {
327 // 1) Prohibit saving into locked range.
328 // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
330 // Now, step by step.
332 // 1) Prohibit saving into locked range.
333 if ($user->isDateLocked($new_date))
334 $err->add($i18n->getKey('error.range_locked'));
336 // 2) Prohibit saving uncompleted unlocked entries when another uncompleted entry exists.
337 $uncompleted = ($cl_finish == '' && $cl_duration == '');
339 $not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser());
340 if ($not_completed_rec) {
341 // We have another not completed record.
342 $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
347 // Prohibit creating an overlapping record.
349 if (ttTimeHelper::overlaps($user->getActiveUser(), $new_date->toString(DB_DATEFORMAT), $cl_start, $cl_finish))
350 $err->add($i18n->getKey('error.overlap'));
353 // Now, a new insert.
356 $id = ttTimeHelper::insert(array(
357 'date'=>$new_date->toString(DB_DATEFORMAT),
358 'user_id'=>$user->getActiveUser(),
359 'client'=>$cl_client,
360 'project'=>$cl_project,
363 'finish'=>$cl_finish,
364 'duration'=>$cl_duration,
366 'billable'=>$cl_billable));
368 // Insert a custom field if we have it.
370 if ($id && $custom_fields && $cl_cf_1) {
371 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
372 $res = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
373 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
374 $res = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
377 header('Location: time.php?date='.$new_date->toString(DB_DATEFORMAT));
380 $err->add($i18n->getKey('error.db'));
384 if ($request->getParameter('btn_delete')) {
385 header("Location: time_delete.php?id=$cl_id");
390 $smarty->assign('client_list', $client_list);
391 $smarty->assign('project_list', $project_list);
392 $smarty->assign('task_list', $task_list);
393 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
394 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
395 $smarty->assign('title', $i18n->getKey('title.edit_time_record'));
396 $smarty->assign('content_page_name', 'time_edit.tpl');
397 $smarty->display('index.tpl');