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('ttTeamHelper');
34 import('ttClientHelper');
35 import('ttTimeHelper');
36 import('DateAndTime');
39 if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
40 header('Location: access_denied.php');
43 if ($user->behalf_id && (!$user->can('track_time') || !$user->checkBehalfId())) {
44 header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
47 if (!$user->behalf_id && !$user->can('track_own_time') && !$user->adjustBehalfId()) {
48 header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
51 // End of access checks.
53 // Initialize and store date in session.
54 $cl_date = $request->getParameter('date', @$_SESSION['date']);
55 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
56 if($selected_date->isError())
57 $selected_date = new DateAndTime(DB_DATEFORMAT);
59 $cl_date = $selected_date->toString(DB_DATEFORMAT);
60 $_SESSION['date'] = $cl_date;
62 // Use custom fields plugin if it is enabled.
63 if ($user->isPluginEnabled('cf')) {
64 require_once('plugins/CustomFields.class.php');
65 $custom_fields = new CustomFields($user->group_id);
66 $smarty->assign('custom_fields', $custom_fields);
69 if ($user->isPluginEnabled('mq')){
70 require_once('plugins/MonthlyQuota.class.php');
71 $quota = new MonthlyQuota();
72 $month_quota_minutes = $quota->get($selected_date->mYear, $selected_date->mMonth);
73 $month_total = ttTimeHelper::getTimeForMonth($user->getActiveUser(), $selected_date);
74 $minutes_left = $month_quota_minutes - ttTimeHelper::toMinutes($month_total);
76 $smarty->assign('month_total', $month_total);
77 $smarty->assign('over_quota', $minutes_left < 0);
78 $smarty->assign('quota_remaining', ttTimeHelper::toAbsDuration($minutes_left));
81 // Initialize variables.
82 $cl_start = trim($request->getParameter('start'));
83 $cl_finish = trim($request->getParameter('finish'));
84 $cl_duration = trim($request->getParameter('duration'));
85 $cl_note = trim($request->getParameter('note'));
87 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
88 $_SESSION['cf_1'] = $cl_cf_1;
90 if ($user->isPluginEnabled('iv')) {
91 if ($request->isPost()) {
92 $cl_billable = $request->getParameter('billable');
93 $_SESSION['billable'] = (int) $cl_billable;
95 if (isset($_SESSION['billable']))
96 $cl_billable = $_SESSION['billable'];
98 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
99 $on_behalf_group_id = $request->getParameter('onBehalfGroup', (isset($_SESSION['behalf_group_id'])? $_SESSION['behalf_group_id'] : $user->group_id));
100 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
101 $_SESSION['client'] = $cl_client;
102 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
103 $_SESSION['project'] = $cl_project;
104 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
105 $_SESSION['task'] = $cl_task;
107 // Elements of timeRecordForm.
108 $form = new Form('timeRecordForm');
110 if (isTrue(SUBGROUP_DEBUG)) {
111 if ($user->can('manage_subgroups')) {
112 $groups = $user->getGroupsForDropdown();
113 if (count($groups) > 1) {
114 $form->addInput(array('type'=>'combobox',
115 'onchange'=>'this.form.submit();',
116 'name'=>'onBehalfGroup',
117 'style'=>'width: 250px;',
118 'value'=>$on_behalf_group_id,
120 'datakeys'=>array('id','name')));
121 $smarty->assign('on_behalf_group_control', 1);
126 if ($user->can('track_time')) {
127 $rank = $user->getMaxRankForGroup($on_behalf_group_id);
128 if ($user->can('track_own_time'))
129 $options = array('group_id'=>$on_behalf_group_id,'status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
131 $options = array('group_id'=>$on_behalf_group_id,'status'=>ACTIVE,'max_rank'=>$rank);
132 $user_list = $user->getUsers($options);
133 if (count($user_list) >= 1) {
134 $form->addInput(array('type'=>'combobox',
135 'onchange'=>'this.form.submit();',
136 'name'=>'onBehalfUser',
137 'style'=>'width: 250px;',
138 'value'=>$on_behalf_id,
140 'datakeys'=>array('id','name')));
141 $smarty->assign('on_behalf_control', 1);
145 // Dropdown for clients in MODE_TIME. Use all active clients.
146 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
147 $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
148 $form->addInput(array('type'=>'combobox',
149 'onchange'=>'fillProjectDropdown(this.value);',
151 'style'=>'width: 250px;',
153 'data'=>$active_clients,
154 'datakeys'=>array('id', 'name'),
155 'empty'=>array(''=>$i18n->get('dropdown.select'))));
156 // Note: in other modes the client list is filtered to relevant clients only. See below.
159 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
160 // Dropdown for projects assigned to user.
161 $project_list = $user->getAssignedProjects();
162 $form->addInput(array('type'=>'combobox',
163 'onchange'=>'fillTaskDropdown(this.value);',
165 'style'=>'width: 250px;',
166 'value'=>$cl_project,
167 'data'=>$project_list,
168 'datakeys'=>array('id','name'),
169 'empty'=>array(''=>$i18n->get('dropdown.select'))));
171 // Dropdown for clients if the clients plugin is enabled.
172 if ($user->isPluginEnabled('cl')) {
173 $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
174 // We need an array of assigned project ids to do some trimming.
175 foreach($project_list as $project)
176 $projects_assigned_to_user[] = $project['id'];
178 // Build a client list out of active clients. Use only clients that are relevant to user.
179 // Also trim their associated project list to only assigned projects (to user).
180 foreach($active_clients as $client) {
181 $projects_assigned_to_client = explode(',', $client['projects']);
182 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
183 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
185 $client['projects'] = implode(',', $intersection);
186 $client_list[] = $client;
189 $form->addInput(array('type'=>'combobox',
190 'onchange'=>'fillProjectDropdown(this.value);',
192 'style'=>'width: 250px;',
194 'data'=>$client_list,
195 'datakeys'=>array('id', 'name'),
196 'empty'=>array(''=>$i18n->get('dropdown.select'))));
200 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
201 $task_list = ttTeamHelper::getActiveTasks($user->group_id);
202 $form->addInput(array('type'=>'combobox',
204 'style'=>'width: 250px;',
207 'datakeys'=>array('id','name'),
208 'empty'=>array(''=>$i18n->get('dropdown.select'))));
211 // Add other controls.
212 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
213 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
214 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
215 if ($user->punch_mode && !$user->canOverridePunchMode()) {
216 // Make the start and finish fields read-only.
217 $form->getElement('start')->setEnabled(false);
218 $form->getElement('finish')->setEnabled(false);
221 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
222 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
223 if (!defined('NOTE_INPUT_HEIGHT'))
224 define('NOTE_INPUT_HEIGHT', 40);
225 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
226 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
227 if ($user->isPluginEnabled('iv'))
228 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
229 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
230 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
232 // If we have custom fields - add controls for them.
233 if ($custom_fields && $custom_fields->fields[0]) {
234 // Only one custom field is supported at this time.
235 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
236 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
237 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
238 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
239 'style'=>'width: 250px;',
241 'data'=>$custom_fields->options,
242 'empty'=>array(''=>$i18n->get('dropdown.select'))));
247 if ($request->isPost()) {
248 if ($request->getParameter('btn_submit')) {
250 // Validate user input.
251 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
252 $err->add($i18n->get('error.client'));
253 if ($custom_fields) {
254 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
256 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
257 if (!$cl_project) $err->add($i18n->get('error.project'));
259 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
260 if (!$cl_task) $err->add($i18n->get('error.task'));
262 if (strlen($cl_duration) == 0) {
263 if ($cl_start || $cl_finish) {
264 if (!ttTimeHelper::isValidTime($cl_start))
265 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
267 if (!ttTimeHelper::isValidTime($cl_finish))
268 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
269 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
270 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
273 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
274 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
275 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
277 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
278 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
281 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
282 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
284 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
285 // Finished validating user input.
287 // Prohibit creating entries in future.
288 if (!$user->future_entries) {
289 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
290 if ($selected_date->after($browser_today))
291 $err->add($i18n->get('error.future_date'));
294 // Prohibit creating entries in locked range.
295 if ($user->isDateLocked($selected_date))
296 $err->add($i18n->get('error.range_locked'));
298 // Prohibit creating another uncompleted record.
300 if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
301 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
304 // Prohibit creating an overlapping record.
306 if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
307 $err->add($i18n->get('error.overlap'));
312 $id = ttTimeHelper::insert(array(
314 'user_id' => $user->getActiveUser(),
315 'group_id' => $user->getActiveGroup(),
316 'org_id' => $user->org_id,
317 'client' => $cl_client,
318 'project' => $cl_project,
320 'start' => $cl_start,
321 'finish' => $cl_finish,
322 'duration' => $cl_duration,
324 'billable' => $cl_billable));
326 // Insert a custom field if we have it.
328 if ($id && $custom_fields && $cl_cf_1) {
329 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
330 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
331 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
332 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
334 if ($id && $result) {
335 header('Location: time.php');
338 $err->add($i18n->get('error.db'));
340 } elseif ($request->getParameter('btn_stop')) {
341 // Stop button pressed to finish an uncompleted record.
342 $record_id = $request->getParameter('record_id');
343 $record = ttTimeHelper::getRecord($record_id, $user->getActiveUser());
344 $browser_date = $request->getParameter('browser_date');
345 $browser_time = $request->getParameter('browser_time');
347 // Can we complete this record?
348 if ($record['date'] == $browser_date // closing today's record
349 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
350 && !ttTimeHelper::overlaps($user->getActiveUser(), $browser_date, $record['start'], $browser_time)) { // no overlap
351 $res = ttTimeHelper::update(array(
353 'date'=>$record['date'],
354 'user_id'=>$user->getActiveUser(),
355 'client'=>$record['client_id'],
356 'project'=>$record['project_id'],
357 'task'=>$record['task_id'],
358 'start'=>$record['start'],
359 'finish'=>$browser_time,
360 'note'=>$record['comment'],
361 'billable'=>$record['billable']));
363 $err->add($i18n->get('error.db'));
365 // Cannot complete, redirect for manual edit.
366 header('Location: time_edit.php?id='.$record_id);
370 elseif ($request->getParameter('onBehalfUser') || $request->getParameter('onBehalfGroup')) {
371 // User changed either on behalf user or group.
372 // TODO: Organize this code into a separate function.
374 // We get here if one of the dropdowns changed. Handle these 2 situations differently.
375 // 1) User changed. Determine if user changed. Then do exactly as before.
377 // Group changed. Determine if group changed.
378 // Adjust group info.
379 // Adjust user info to first user in group (or self if we are in home group now).
381 // Determine if user was changed.
382 if ($request->getParameter('onBehalfUser')) {
383 $request_user_id = $request->getParameter('onBehalfUser');
384 $session_user_id = $_SESSION['behalf_id'];
385 $user_changed = !(($session_user_id == null && ($user->id == $request_user_id))
386 || ($session_user_id != null && ($request_user_id == $session_user_id)));
387 if ($user_changed && $user->can('track_time')) {
388 unset($_SESSION['behalf_id']);
389 unset($_SESSION['behalf_name']);
391 if($request_user_id != $user->id) {
392 $_SESSION['behalf_id'] = $request_user_id;
393 $_SESSION['behalf_name'] = ttUserHelper::getUserName($request_user_id);
398 if ($request->getParameter('onBehalfGroup')) {
399 // Determine if group was changed.
400 $request_group_id = $request->getParameter('onBehalfGroup');
401 $session_group_id = $_SESSION['behalf_group_id'];
402 $group_changed = !(($session_group_id == null && ($user->group_id == $request_group_id))
403 || ($session_group_id != null && ($request_group_id == $session_group_id)));
405 if ($group_changed && $user->can('manage_subgroups')) {
406 unset($_SESSION['behalf_group_id']);
407 unset($_SESSION['behalf_group_name']);
408 if ($request_group_id == $user->group_id)
409 $user->behalf_group_id = null;
411 if (($request_group_id != $user->group_id) && $user->isSubgroupValid($request_group_id)) {
412 $_SESSION['behalf_group_id'] = $request_group_id;
413 $_SESSION['behalf_group_name'] = ttGroupHelper::getGroupName($request_group_id);
414 $user->behalf_group_id = $request_group_id;
417 unset($_SESSION['behalf_id']);
418 unset($_SESSION['behalf_name']);
419 if ($request_group_id != $user->group_id)
420 $user->adjustBehalfId();
423 header('Location: time.php');
428 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
430 $smarty->assign('selected_date', $selected_date);
431 $smarty->assign('week_total', $week_total);
432 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
433 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
434 $smarty->assign('client_list', $client_list);
435 $smarty->assign('project_list', $project_list);
436 $smarty->assign('task_list', $task_list);
437 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
438 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
439 $smarty->assign('timestring', $selected_date->toString($user->date_format));
440 $smarty->assign('title', $i18n->get('title.time'));
441 $smarty->assign('content_page_name', 'time.tpl');
442 $smarty->display('index.tpl');