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 (defined('SUBGROUP_DEBUG') && isTrue(SUBGROUP_DEBUG)) {
111 if ($user->can('manage_subgroups')) {
112 $groups = $user->getGroups();
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 if ($user->can('track_own_time'))
128 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1,'include_self'=>true,'self_first'=>true);
130 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1);
131 $user_list = $user->getUsers($options);
132 if (count($user_list) >= 1) {
133 $form->addInput(array('type'=>'combobox',
134 'onchange'=>'this.form.submit();',
135 'name'=>'onBehalfUser',
136 'style'=>'width: 250px;',
137 'value'=>$on_behalf_id,
139 'datakeys'=>array('id','name')));
140 $smarty->assign('on_behalf_control', 1);
144 // Dropdown for clients in MODE_TIME. Use all active clients.
145 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
146 $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
147 $form->addInput(array('type'=>'combobox',
148 'onchange'=>'fillProjectDropdown(this.value);',
150 'style'=>'width: 250px;',
152 'data'=>$active_clients,
153 'datakeys'=>array('id', 'name'),
154 'empty'=>array(''=>$i18n->get('dropdown.select'))));
155 // Note: in other modes the client list is filtered to relevant clients only. See below.
158 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
159 // Dropdown for projects assigned to user.
160 $project_list = $user->getAssignedProjects();
161 $form->addInput(array('type'=>'combobox',
162 'onchange'=>'fillTaskDropdown(this.value);',
164 'style'=>'width: 250px;',
165 'value'=>$cl_project,
166 'data'=>$project_list,
167 'datakeys'=>array('id','name'),
168 'empty'=>array(''=>$i18n->get('dropdown.select'))));
170 // Dropdown for clients if the clients plugin is enabled.
171 if ($user->isPluginEnabled('cl')) {
172 $active_clients = ttTeamHelper::getActiveClients($user->group_id, true);
173 // We need an array of assigned project ids to do some trimming.
174 foreach($project_list as $project)
175 $projects_assigned_to_user[] = $project['id'];
177 // Build a client list out of active clients. Use only clients that are relevant to user.
178 // Also trim their associated project list to only assigned projects (to user).
179 foreach($active_clients as $client) {
180 $projects_assigned_to_client = explode(',', $client['projects']);
181 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
182 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
184 $client['projects'] = implode(',', $intersection);
185 $client_list[] = $client;
188 $form->addInput(array('type'=>'combobox',
189 'onchange'=>'fillProjectDropdown(this.value);',
191 'style'=>'width: 250px;',
193 'data'=>$client_list,
194 'datakeys'=>array('id', 'name'),
195 'empty'=>array(''=>$i18n->get('dropdown.select'))));
199 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
200 $task_list = ttTeamHelper::getActiveTasks($user->group_id);
201 $form->addInput(array('type'=>'combobox',
203 'style'=>'width: 250px;',
206 'datakeys'=>array('id','name'),
207 'empty'=>array(''=>$i18n->get('dropdown.select'))));
210 // Add other controls.
211 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
212 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
213 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
214 if ($user->punch_mode && !$user->canOverridePunchMode()) {
215 // Make the start and finish fields read-only.
216 $form->getElement('start')->setEnabled(false);
217 $form->getElement('finish')->setEnabled(false);
220 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
221 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
222 if (!defined('NOTE_INPUT_HEIGHT'))
223 define('NOTE_INPUT_HEIGHT', 40);
224 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
225 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
226 if ($user->isPluginEnabled('iv'))
227 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
228 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
229 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
231 // If we have custom fields - add controls for them.
232 if ($custom_fields && $custom_fields->fields[0]) {
233 // Only one custom field is supported at this time.
234 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
235 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
236 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
237 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
238 'style'=>'width: 250px;',
240 'data'=>$custom_fields->options,
241 'empty'=>array(''=>$i18n->get('dropdown.select'))));
246 if ($request->isPost()) {
247 if ($request->getParameter('btn_submit')) {
249 // Validate user input.
250 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
251 $err->add($i18n->get('error.client'));
252 if ($custom_fields) {
253 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
255 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
256 if (!$cl_project) $err->add($i18n->get('error.project'));
258 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
259 if (!$cl_task) $err->add($i18n->get('error.task'));
261 if (strlen($cl_duration) == 0) {
262 if ($cl_start || $cl_finish) {
263 if (!ttTimeHelper::isValidTime($cl_start))
264 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
266 if (!ttTimeHelper::isValidTime($cl_finish))
267 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
268 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
269 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
272 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
273 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
274 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
276 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
277 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
280 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
281 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
283 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
284 // Finished validating user input.
286 // Prohibit creating entries in future.
287 if (!$user->future_entries) {
288 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
289 if ($selected_date->after($browser_today))
290 $err->add($i18n->get('error.future_date'));
293 // Prohibit creating entries in locked range.
294 if ($user->isDateLocked($selected_date))
295 $err->add($i18n->get('error.range_locked'));
297 // Prohibit creating another uncompleted record.
299 if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
300 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
303 // Prohibit creating an overlapping record.
305 if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
306 $err->add($i18n->get('error.overlap'));
311 $id = ttTimeHelper::insert(array(
313 'user_id' => $user->getActiveUser(),
314 'group_id' => $user->getActiveGroup(),
315 'client' => $cl_client,
316 'project' => $cl_project,
318 'start' => $cl_start,
319 'finish' => $cl_finish,
320 'duration' => $cl_duration,
322 'billable' => $cl_billable));
324 // Insert a custom field if we have it.
326 if ($id && $custom_fields && $cl_cf_1) {
327 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
328 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
329 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
330 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
332 if ($id && $result) {
333 header('Location: time.php');
336 $err->add($i18n->get('error.db'));
338 } elseif ($request->getParameter('btn_stop')) {
339 // Stop button pressed to finish an uncompleted record.
340 $record_id = $request->getParameter('record_id');
341 $record = ttTimeHelper::getRecord($record_id, $user->getActiveUser());
342 $browser_date = $request->getParameter('browser_date');
343 $browser_time = $request->getParameter('browser_time');
345 // Can we complete this record?
346 if ($record['date'] == $browser_date // closing today's record
347 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
348 && !ttTimeHelper::overlaps($user->getActiveUser(), $browser_date, $record['start'], $browser_time)) { // no overlap
349 $res = ttTimeHelper::update(array(
351 'date'=>$record['date'],
352 'user_id'=>$user->getActiveUser(),
353 'client'=>$record['client_id'],
354 'project'=>$record['project_id'],
355 'task'=>$record['task_id'],
356 'start'=>$record['start'],
357 'finish'=>$browser_time,
358 'note'=>$record['comment'],
359 'billable'=>$record['billable']));
361 $err->add($i18n->get('error.db'));
363 // Cannot complete, redirect for manual edit.
364 header('Location: time_edit.php?id='.$record_id);
368 elseif ($request->getParameter('onBehalfGroup')) {
369 if($user->can('manage_subgroups')) {
370 unset($_SESSION['behalf_group_id']);
371 unset($_SESSION['behalf_group_name']);
373 if($on_behalf_group_id != $user->group_id) {
374 $_SESSION['behalf_group_id'] = $on_behalf_group_id;
375 $_SESSION['behalf_group_name'] = ttGroupHelper::getGroupName($on_behalf_group_id);
377 header('Location: time.php');
381 elseif ($request->getParameter('onBehalfUser')) {
382 if($user->can('track_time')) {
383 unset($_SESSION['behalf_id']);
384 unset($_SESSION['behalf_name']);
386 if($on_behalf_id != $user->id) {
387 $_SESSION['behalf_id'] = $on_behalf_id;
388 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
390 header('Location: time.php');
396 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
398 $smarty->assign('selected_date', $selected_date);
399 $smarty->assign('week_total', $week_total);
400 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
401 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
402 $smarty->assign('client_list', $client_list);
403 $smarty->assign('project_list', $project_list);
404 $smarty->assign('task_list', $task_list);
405 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
406 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
407 $smarty->assign('timestring', $selected_date->toString($user->date_format));
408 $smarty->assign('title', $i18n->get('title.time'));
409 $smarty->assign('content_page_name', 'time.tpl');
410 $smarty->display('index.tpl');