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('ttConfigHelper');
32 import('ttUserHelper');
33 import('ttGroupHelper');
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 if ($request->isPost()) {
52 $userChanged = $request->getParameter('user_changed'); // Reused in multiple places below.
53 if ($userChanged && !($user->can('track_time') && $user->isUserValid($request->getParameter('user')))) {
54 header('Location: access_denied.php'); // Group changed, but no rght or wrong user id.
58 // End of access checks.
60 // Determine user for whom we display this page.
61 if ($request->isPost() && $userChanged) {
62 $user_id = $request->getParameter('user');
63 $user->setOnBehalfUser($user_id);
65 $user_id = $user->getUser();
68 $group_id = $user->getGroup();
70 $showClient = $user->isPluginEnabled('cl');
71 $trackingMode = $user->getTrackingMode();
72 $showProject = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
73 $showTask = MODE_PROJECTS_AND_TASKS == $trackingMode;
74 $recordType = $user->getRecordType();
75 $showStart = TYPE_START_FINISH == $recordType || TYPE_ALL == $recordType;
76 $showFinish = $showStart;
77 $showDuration = TYPE_DURATION == $recordType || TYPE_ALL == $recordType;
78 $showFiles = $user->isPluginEnabled('at');
80 // Initialize and store date in session.
81 $cl_date = $request->getParameter('date', @$_SESSION['date']);
82 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
83 if($selected_date->isError())
84 $selected_date = new DateAndTime(DB_DATEFORMAT);
86 $cl_date = $selected_date->toString(DB_DATEFORMAT);
87 $_SESSION['date'] = $cl_date;
89 // Use custom fields plugin if it is enabled.
90 if ($user->isPluginEnabled('cf')) {
91 require_once('plugins/CustomFields.class.php');
92 $custom_fields = new CustomFields();
93 $smarty->assign('custom_fields', $custom_fields);
96 $config = new ttConfigHelper($user->getConfig());
97 $showNoteColumn = !$config->getDefinedValue('time_note_on_separate_row');
98 $showNoteRow = $config->getDefinedValue('time_note_on_separate_row');
100 // Determine column span for note field.
102 if ($showClient) $colspan++;
103 if ($user->isPluginEnabled('cf')) $colspan++;
104 if ($showProject) $colspan++;
105 if ($showTask) $colspan++;
106 if ($showStart) $colspan++;
107 if ($showFinish) $colspan++;
108 $colspan++; // There is always a duration.
109 if ($showFiles) $colspan++;
110 $colspan++; // There is always an edit column.
111 $colspan++; // There is always a delete column.
112 $colspan--; // Remove one column for label.
113 $smarty->assign('colspan', $colspan);
116 if ($user->isPluginEnabled('mq')){
117 require_once('plugins/MonthlyQuota.class.php');
118 $quota = new MonthlyQuota();
119 $month_quota_minutes = $quota->getUserQuota($selected_date->mYear, $selected_date->mMonth);
120 $quota_minutes_from_1st = $quota->getUserQuotaFrom1st($selected_date);
121 $month_total = ttTimeHelper::getTimeForMonth($selected_date);
122 $month_total_minutes = ttTimeHelper::toMinutes($month_total);
123 $balance_left = $quota_minutes_from_1st - $month_total_minutes;
124 $minutes_left = $month_quota_minutes - $month_total_minutes;
126 $smarty->assign('month_total', $month_total);
127 $smarty->assign('month_quota', ttTimeHelper::toAbsDuration($month_quota_minutes));
128 $smarty->assign('over_balance', $balance_left < 0);
129 $smarty->assign('balance_remaining', ttTimeHelper::toAbsDuration($balance_left));
130 $smarty->assign('over_quota', $minutes_left < 0);
131 $smarty->assign('quota_remaining', ttTimeHelper::toAbsDuration($minutes_left));
134 // Initialize variables.
135 $cl_start = trim($request->getParameter('start'));
136 $cl_finish = trim($request->getParameter('finish'));
137 $cl_duration = trim($request->getParameter('duration'));
138 $cl_note = trim($request->getParameter('note'));
140 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
141 $_SESSION['cf_1'] = $cl_cf_1;
143 if ($user->isPluginEnabled('iv')) {
144 if ($request->isPost()) {
145 $cl_billable = $request->getParameter('billable');
146 $_SESSION['billable'] = (int) $cl_billable;
148 if (isset($_SESSION['billable']))
149 $cl_billable = $_SESSION['billable'];
151 //$on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
152 //$on_behalf_group_id = $request->getParameter('onBehalfGroup', (isset($_SESSION['behalf_group_id'])? $_SESSION['behalf_group_id'] : $user->group_id));
153 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
154 $_SESSION['client'] = $cl_client;
155 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
156 $_SESSION['project'] = $cl_project;
157 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
158 $_SESSION['task'] = $cl_task;
160 // Elements of timeRecordForm.
161 $form = new Form('timeRecordForm');
162 if ($user->can('track_time')) {
163 $rank = $user->getMaxRankForGroup($group_id);
164 if ($user->can('track_own_time'))
165 $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
167 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
168 $user_list = $user->getUsers($options);
169 if (count($user_list) >= 1) {
170 $form->addInput(array('type'=>'combobox',
171 'onchange'=>'document.timeRecordForm.user_changed.value=1;document.timeRecordForm.submit();',
173 'style'=>'width: 250px;',
176 'datakeys'=>array('id','name')));
177 $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
178 $smarty->assign('user_dropdown', 1);
182 // Dropdown for clients in MODE_TIME. Use all active clients.
183 if (MODE_TIME == $user->getTrackingMode() && $showClient) {
184 $active_clients = ttGroupHelper::getActiveClients(true);
185 $form->addInput(array('type'=>'combobox',
186 'onchange'=>'fillProjectDropdown(this.value);',
188 'style'=>'width: 250px;',
190 'data'=>$active_clients,
191 'datakeys'=>array('id', 'name'),
192 'empty'=>array(''=>$i18n->get('dropdown.select'))));
193 // Note: in other modes the client list is filtered to relevant clients only. See below.
196 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
197 // Dropdown for projects assigned to user.
198 $project_list = $user->getAssignedProjects();
199 $form->addInput(array('type'=>'combobox',
200 'onchange'=>'fillTaskDropdown(this.value);',
202 'style'=>'width: 250px;',
203 'value'=>$cl_project,
204 'data'=>$project_list,
205 'datakeys'=>array('id','name'),
206 'empty'=>array(''=>$i18n->get('dropdown.select'))));
208 // Dropdown for clients if the clients plugin is enabled.
210 $active_clients = ttGroupHelper::getActiveClients(true);
211 // We need an array of assigned project ids to do some trimming.
212 foreach($project_list as $project)
213 $projects_assigned_to_user[] = $project['id'];
215 // Build a client list out of active clients. Use only clients that are relevant to user.
216 // Also trim their associated project list to only assigned projects (to user).
217 foreach($active_clients as $client) {
218 $projects_assigned_to_client = explode(',', $client['projects']);
219 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
220 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
222 $client['projects'] = implode(',', $intersection);
223 $client_list[] = $client;
226 $form->addInput(array('type'=>'combobox',
227 'onchange'=>'fillProjectDropdown(this.value);',
229 'style'=>'width: 250px;',
231 'data'=>$client_list,
232 'datakeys'=>array('id', 'name'),
233 'empty'=>array(''=>$i18n->get('dropdown.select'))));
237 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
238 $task_list = ttGroupHelper::getActiveTasks();
239 $form->addInput(array('type'=>'combobox',
241 'style'=>'width: 250px;',
244 'datakeys'=>array('id','name'),
245 'empty'=>array(''=>$i18n->get('dropdown.select'))));
248 // Add other controls.
249 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
250 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
251 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
252 if ($user->punch_mode && !$user->canOverridePunchMode()) {
253 // Make the start and finish fields read-only.
254 $form->getElement('start')->setEnabled(false);
255 $form->getElement('finish')->setEnabled(false);
258 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
259 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
260 if (!defined('NOTE_INPUT_HEIGHT'))
261 define('NOTE_INPUT_HEIGHT', 40);
262 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
263 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
264 if ($user->isPluginEnabled('iv'))
265 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
266 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
267 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
269 // If we have custom fields - add controls for them.
270 if ($custom_fields && $custom_fields->fields[0]) {
271 // Only one custom field is supported at this time.
272 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
273 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
274 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
275 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
276 'style'=>'width: 250px;',
278 'data'=>$custom_fields->options,
279 'empty'=>array(''=>$i18n->get('dropdown.select'))));
283 // If we have templates, add a dropdown to select one.
284 if ($user->isPluginEnabled('tp')){
285 $templates = ttGroupHelper::getActiveTemplates();
286 if (count($templates) >= 1) {
287 $form->addInput(array('type'=>'combobox',
288 'onchange'=>'fillNote(this.value);',
290 'style'=>'width: 250px;',
292 'datakeys'=>array('id','name'),
293 'empty'=>array(''=>$i18n->get('dropdown.select'))));
294 $smarty->assign('template_dropdown', 1);
295 $smarty->assign('templates', $templates);
300 if ($request->isPost()) {
301 if ($request->getParameter('btn_submit')) {
303 // Validate user input.
304 if ($showClient && $user->isPluginEnabled('cm') && !$cl_client)
305 $err->add($i18n->get('error.client'));
306 if ($custom_fields) {
307 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
309 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
310 if (!$cl_project) $err->add($i18n->get('error.project'));
312 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode() && $user->task_required) {
313 if (!$cl_task) $err->add($i18n->get('error.task'));
315 if (strlen($cl_duration) == 0) {
316 if ($cl_start || $cl_finish) {
317 if (!ttTimeHelper::isValidTime($cl_start))
318 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
320 if (!ttTimeHelper::isValidTime($cl_finish))
321 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
322 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
323 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
326 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
327 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
328 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
330 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
331 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
334 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
335 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
337 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
338 if ($user->isPluginEnabled('tp') && !ttValidTemplateText($cl_note)) {
339 $err->add($i18n->get('error.field'), $i18n->get('label.note'));
341 if (!ttTimeHelper::canAdd()) $err->add($i18n->get('error.expired'));
342 // Finished validating user input.
344 // Prohibit creating entries in future.
345 if (!$user->future_entries) {
346 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
347 if ($selected_date->after($browser_today))
348 $err->add($i18n->get('error.future_date'));
351 // Prohibit creating entries in locked range.
352 if ($user->isDateLocked($selected_date))
353 $err->add($i18n->get('error.range_locked'));
355 // Prohibit creating another uncompleted record.
357 if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
358 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
361 // Prohibit creating an overlapping record.
363 if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
364 $err->add($i18n->get('error.overlap'));
369 $id = ttTimeHelper::insert(array(
371 'user_id' => $user_id,
372 'group_id' => $group_id,
373 'org_id' => $user->org_id,
374 'client' => $cl_client,
375 'project' => $cl_project,
377 'start' => $cl_start,
378 'finish' => $cl_finish,
379 'duration' => $cl_duration,
381 'billable' => $cl_billable));
383 // Insert a custom field if we have it.
385 if ($id && $custom_fields && $cl_cf_1) {
386 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
387 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
388 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
389 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
391 if ($id && $result) {
392 header('Location: time.php');
395 $err->add($i18n->get('error.db'));
397 } elseif ($request->getParameter('btn_stop')) {
398 // Stop button pressed to finish an uncompleted record.
399 $record_id = $request->getParameter('record_id');
400 $record = ttTimeHelper::getRecord($record_id);
401 $browser_date = $request->getParameter('browser_date');
402 $browser_time = $request->getParameter('browser_time');
404 // Can we complete this record?
405 if ($record['date'] == $browser_date // closing today's record
406 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
407 && !ttTimeHelper::overlaps($user_id, $browser_date, $record['start'], $browser_time)) { // no overlap
408 $res = ttTimeHelper::update(array(
410 'date'=>$record['date'],
412 'client'=>$record['client_id'],
413 'project'=>$record['project_id'],
414 'task'=>$record['task_id'],
415 'start'=>$record['start'],
416 'finish'=>$browser_time,
417 'note'=>$record['comment'],
418 'billable'=>$record['billable']));
420 $err->add($i18n->get('error.db'));
422 // Cannot complete, redirect for manual edit.
423 header('Location: time_edit.php?id='.$record_id);
429 $week_total = ttTimeHelper::getTimeForWeek($selected_date);
430 $timeRecords = $showFiles? ttTimeHelper::getRecordsWithFiles($user_id, $cl_date) : ttTimeHelper::getRecords($user_id, $cl_date);
432 $smarty->assign('selected_date', $selected_date);
433 $smarty->assign('week_total', $week_total);
434 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($cl_date));
435 $smarty->assign('time_records', $timeRecords);
436 $smarty->assign('show_client', $showClient);
437 $smarty->assign('show_cf_1', $user->isPluginEnabled('cf'));
438 $smarty->assign('show_project', $showProject);
439 $smarty->assign('show_task', $showTask);
440 $smarty->assign('show_start', $showStart);
441 $smarty->assign('show_finish', $showFinish);
442 $smarty->assign('show_duration', $showDuration);
445 $smarty->assign('show_note_column', $showNoteColumn);
446 $smarty->assign('show_note_row', $showNoteRow);
447 $smarty->assign('show_files', $showFiles);
448 $smarty->assign('client_list', $client_list);
449 $smarty->assign('project_list', $project_list);
450 $smarty->assign('task_list', $task_list);
451 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
452 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
453 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
454 $smarty->assign('title', $i18n->get('title.time'));
455 $smarty->assign('content_page_name', 'time.tpl');
456 $smarty->display('index.tpl');