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');
30 import('ttDebugTracer');
32 import('ttConfigHelper');
33 import('ttUserHelper');
34 import('ttGroupHelper');
35 import('ttClientHelper');
36 import('ttTimeHelper');
37 import('DateAndTime');
39 // Initialize ttDebugTracer to print diagnostic messages.
40 $debug = new ttDebugTracer(__FILE__);
41 $debug->println('page entry after imports');
44 if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
45 header('Location: access_denied.php');
48 if ($user->behalf_id && (!$user->can('track_time') || !$user->checkBehalfId())) {
49 header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
52 if (!$user->behalf_id && !$user->can('track_own_time') && !$user->adjustBehalfId()) {
53 header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
56 if ($request->isPost()) {
57 $userChanged = $request->getParameter('user_changed'); // Reused in multiple places below.
58 if ($userChanged && !($user->can('track_time') && $user->isUserValid($request->getParameter('user')))) {
59 header('Location: access_denied.php'); // Group changed, but no rght or wrong user id.
63 $debug->println('after all access checks');
64 // End of access checks.
66 // Determine user for whom we display this page.
67 if ($request->isPost() && $userChanged) {
68 $user_id = $request->getParameter('user');
69 $user->setOnBehalfUser($user_id);
71 $user_id = $user->getUser();
74 $group_id = $user->getGroup();
75 $debug->println("user_id: $user_id group_id: $group_id");
77 $showClient = $user->isPluginEnabled('cl');
78 $trackingMode = $user->getTrackingMode();
79 $showProject = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
80 $showTask = MODE_PROJECTS_AND_TASKS == $trackingMode;
81 $recordType = $user->getRecordType();
82 $showStart = TYPE_START_FINISH == $recordType || TYPE_ALL == $recordType;
83 $showFinish = $showStart;
84 $showDuration = TYPE_DURATION == $recordType || TYPE_ALL == $recordType;
85 $showFiles = $user->isPluginEnabled('at');
87 // Initialize and store date in session.
88 $cl_date = $request->getParameter('date', @$_SESSION['date']);
89 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
90 if($selected_date->isError())
91 $selected_date = new DateAndTime(DB_DATEFORMAT);
93 $cl_date = $selected_date->toString(DB_DATEFORMAT);
94 $_SESSION['date'] = $cl_date;
96 // Use custom fields plugin if it is enabled.
97 if ($user->isPluginEnabled('cf')) {
98 require_once('plugins/CustomFields.class.php');
99 $custom_fields = new CustomFields();
100 $smarty->assign('custom_fields', $custom_fields);
103 $config = new ttConfigHelper($user->getConfig());
104 $showNoteColumn = !$config->getDefinedValue('time_note_on_separate_row');
105 $showNoteRow = $config->getDefinedValue('time_note_on_separate_row');
107 // Determine column span for note field.
109 if ($showClient) $colspan++;
110 if ($user->isPluginEnabled('cf')) $colspan++;
111 if ($showProject) $colspan++;
112 if ($showTask) $colspan++;
113 if ($showStart) $colspan++;
114 if ($showFinish) $colspan++;
115 $colspan++; // There is always a duration.
116 if ($showFiles) $colspan++;
117 $colspan++; // There is always an edit column.
118 // $colspan++; // There is always a delete column.
119 // $colspan--; // Remove one column for label.
120 $smarty->assign('colspan', $colspan);
123 if ($user->isPluginEnabled('mq')){
124 require_once('plugins/MonthlyQuota.class.php');
125 $quota = new MonthlyQuota();
126 $month_quota_minutes = $quota->getUserQuota($selected_date->mYear, $selected_date->mMonth);
127 $quota_minutes_from_1st = $quota->getUserQuotaFrom1st($selected_date);
128 $month_total = ttTimeHelper::getTimeForMonth($selected_date);
129 $month_total_minutes = ttTimeHelper::toMinutes($month_total);
130 $balance_left = $quota_minutes_from_1st - $month_total_minutes;
131 $minutes_left = $month_quota_minutes - $month_total_minutes;
133 $smarty->assign('month_total', $month_total);
134 $smarty->assign('month_quota', ttTimeHelper::toAbsDuration($month_quota_minutes));
135 $smarty->assign('over_balance', $balance_left < 0);
136 $smarty->assign('balance_remaining', ttTimeHelper::toAbsDuration($balance_left));
137 $smarty->assign('over_quota', $minutes_left < 0);
138 $smarty->assign('quota_remaining', ttTimeHelper::toAbsDuration($minutes_left));
141 // Initialize variables.
142 $cl_start = trim($request->getParameter('start'));
143 $cl_finish = trim($request->getParameter('finish'));
144 $cl_duration = trim($request->getParameter('duration'));
145 $cl_note = trim($request->getParameter('note'));
147 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
148 $_SESSION['cf_1'] = $cl_cf_1;
150 if ($user->isPluginEnabled('iv')) {
151 if ($request->isPost()) {
152 $cl_billable = $request->getParameter('billable');
153 $_SESSION['billable'] = (int) $cl_billable;
155 if (isset($_SESSION['billable']))
156 $cl_billable = $_SESSION['billable'];
158 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
159 $_SESSION['client'] = $cl_client;
160 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
161 $_SESSION['project'] = $cl_project;
162 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
163 $_SESSION['task'] = $cl_task;
165 // Elements of timeRecordForm.
166 $form = new Form('timeRecordForm');
167 if ($user->can('track_time')) {
168 $rank = $user->getMaxRankForGroup($group_id);
169 if ($user->can('track_own_time'))
170 $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
172 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
173 $user_list = $user->getUsers($options);
174 if (count($user_list) >= 1) {
175 $form->addInput(array('type'=>'combobox',
176 'onchange'=>'document.timeRecordForm.user_changed.value=1;document.timeRecordForm.submit();',
178 'style'=>'width: 250px;',
181 'datakeys'=>array('id','name')));
182 $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
183 $smarty->assign('user_dropdown', 1);
187 // Dropdown for clients in MODE_TIME. Use all active clients.
188 if (MODE_TIME == $trackingMode && $showClient) {
189 $active_clients = ttGroupHelper::getActiveClients(true);
190 $form->addInput(array('type'=>'combobox',
191 'onchange'=>'fillProjectDropdown(this.value);',
193 'style'=>'width: 250px;',
195 'data'=>$active_clients,
196 'datakeys'=>array('id', 'name'),
197 'empty'=>array(''=>$i18n->get('dropdown.select'))));
198 // Note: in other modes the client list is filtered to relevant clients only. See below.
202 // Dropdown for projects assigned to user.
203 $project_list = $user->getAssignedProjects();
204 $form->addInput(array('type'=>'combobox',
205 'onchange'=>'fillTaskDropdown(this.value);',
207 'style'=>'width: 250px;',
208 'value'=>$cl_project,
209 'data'=>$project_list,
210 'datakeys'=>array('id','name'),
211 'empty'=>array(''=>$i18n->get('dropdown.select'))));
213 // Dropdown for clients if the clients plugin is enabled.
215 $active_clients = ttGroupHelper::getActiveClients(true);
216 // We need an array of assigned project ids to do some trimming.
217 foreach($project_list as $project)
218 $projects_assigned_to_user[] = $project['id'];
220 // Build a client list out of active clients. Use only clients that are relevant to user.
221 // Also trim their associated project list to only assigned projects (to user).
222 foreach($active_clients as $client) {
223 $projects_assigned_to_client = explode(',', $client['projects']);
224 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
225 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
227 $client['projects'] = implode(',', $intersection);
228 $client_list[] = $client;
231 $form->addInput(array('type'=>'combobox',
232 'onchange'=>'fillProjectDropdown(this.value);',
234 'style'=>'width: 250px;',
236 'data'=>$client_list,
237 'datakeys'=>array('id', 'name'),
238 'empty'=>array(''=>$i18n->get('dropdown.select'))));
243 $task_list = ttGroupHelper::getActiveTasks();
244 $form->addInput(array('type'=>'combobox',
246 'style'=>'width: 250px;',
249 'datakeys'=>array('id','name'),
250 'empty'=>array(''=>$i18n->get('dropdown.select'))));
253 // Add other controls.
255 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
256 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
257 if ($user->punch_mode && !$user->canOverridePunchMode()) {
258 // Make the start and finish fields read-only.
259 $form->getElement('start')->setEnabled(false);
260 $form->getElement('finish')->setEnabled(false);
264 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
265 if (!defined('NOTE_INPUT_HEIGHT'))
266 define('NOTE_INPUT_HEIGHT', 40);
267 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
268 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
272 // TODO: refactoring ongoing down from here. Use $showBillable, perhaps?
273 if ($user->isPluginEnabled('iv'))
274 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
275 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
276 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
278 // If we have custom fields - add controls for them.
279 if ($custom_fields && $custom_fields->fields[0]) {
280 // Only one custom field is supported at this time.
281 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
282 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
283 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
284 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
285 'style'=>'width: 250px;',
287 'data'=>$custom_fields->options,
288 'empty'=>array(''=>$i18n->get('dropdown.select'))));
292 // If we have templates, add a dropdown to select one.
293 if ($user->isPluginEnabled('tp')){
294 $templates = ttGroupHelper::getActiveTemplates();
295 if (count($templates) >= 1) {
296 $form->addInput(array('type'=>'combobox',
297 'onchange'=>'fillNote(this.value);',
299 'style'=>'width: 250px;',
301 'datakeys'=>array('id','name'),
302 'empty'=>array(''=>$i18n->get('dropdown.select'))));
303 $smarty->assign('template_dropdown', 1);
304 $smarty->assign('templates', $templates);
309 if ($request->isPost()) {
310 if ($request->getParameter('btn_submit')) {
312 // Validate user input.
313 if ($showClient && $user->isPluginEnabled('cm') && !$cl_client)
314 $err->add($i18n->get('error.client'));
315 if ($custom_fields) {
316 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
318 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
319 if (!$cl_project) $err->add($i18n->get('error.project'));
321 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode() && $user->task_required) {
322 if (!$cl_task) $err->add($i18n->get('error.task'));
324 if (strlen($cl_duration) == 0) {
325 if ($cl_start || $cl_finish) {
326 if (!ttTimeHelper::isValidTime($cl_start))
327 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
329 if (!ttTimeHelper::isValidTime($cl_finish))
330 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
331 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
332 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
335 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
336 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
337 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
339 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
340 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
343 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
344 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
346 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
347 if ($user->isPluginEnabled('tp') && !ttValidTemplateText($cl_note)) {
348 $err->add($i18n->get('error.field'), $i18n->get('label.note'));
350 if (!ttTimeHelper::canAdd()) $err->add($i18n->get('error.expired'));
351 // Finished validating user input.
353 // Prohibit creating entries in future.
354 if (!$user->future_entries) {
355 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
356 if ($selected_date->after($browser_today))
357 $err->add($i18n->get('error.future_date'));
360 // Prohibit creating entries in locked range.
361 if ($user->isDateLocked($selected_date))
362 $err->add($i18n->get('error.range_locked'));
364 // Prohibit creating another uncompleted record.
366 if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
367 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
370 // Prohibit creating an overlapping record.
372 if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
373 $err->add($i18n->get('error.overlap'));
378 $id = ttTimeHelper::insert(array(
380 'user_id' => $user_id,
381 'group_id' => $group_id,
382 'org_id' => $user->org_id,
383 'client' => $cl_client,
384 'project' => $cl_project,
386 'start' => $cl_start,
387 'finish' => $cl_finish,
388 'duration' => $cl_duration,
390 'billable' => $cl_billable));
392 // Insert a custom field if we have it.
394 if ($id && $custom_fields && $cl_cf_1) {
395 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
396 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
397 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
398 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
400 if ($id && $result) {
401 header('Location: time.php');
404 $err->add($i18n->get('error.db'));
406 } elseif ($request->getParameter('btn_stop')) {
407 // Stop button pressed to finish an uncompleted record.
408 $record_id = $request->getParameter('record_id');
409 $record = ttTimeHelper::getRecord($record_id);
410 $browser_date = $request->getParameter('browser_date');
411 $browser_time = $request->getParameter('browser_time');
413 // Can we complete this record?
414 if ($record['date'] == $browser_date // closing today's record
415 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
416 && !ttTimeHelper::overlaps($user_id, $browser_date, $record['start'], $browser_time)) { // no overlap
417 $res = ttTimeHelper::update(array(
419 'date'=>$record['date'],
421 'client'=>$record['client_id'],
422 'project'=>$record['project_id'],
423 'task'=>$record['task_id'],
424 'start'=>$record['start'],
425 'finish'=>$browser_time,
426 'note'=>$record['comment'],
427 'billable'=>$record['billable']));
429 $err->add($i18n->get('error.db'));
431 // Cannot complete, redirect for manual edit.
432 header('Location: time_edit.php?id='.$record_id);
438 $week_total = ttTimeHelper::getTimeForWeek($selected_date);
439 $timeRecords = $showFiles? ttTimeHelper::getRecordsWithFiles($user_id, $cl_date) : ttTimeHelper::getRecords($user_id, $cl_date);
441 $debug->println('smarty assignments');
443 $smarty->assign('selected_date', $selected_date);
444 $smarty->assign('week_total', $week_total);
445 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($cl_date));
446 $smarty->assign('time_records', $timeRecords);
447 $smarty->assign('show_client', $showClient);
448 $smarty->assign('show_cf_1', $user->isPluginEnabled('cf'));
449 $smarty->assign('show_project', $showProject);
450 $smarty->assign('show_task', $showTask);
451 $smarty->assign('show_start', $showStart);
452 $smarty->assign('show_finish', $showFinish);
453 $smarty->assign('show_duration', $showDuration);
454 $smarty->assign('show_note_column', $showNoteColumn);
455 $smarty->assign('show_note_row', $showNoteRow);
456 $smarty->assign('show_files', $showFiles);
457 $smarty->assign('client_list', $client_list);
458 $smarty->assign('project_list', $project_list);
459 $smarty->assign('task_list', $task_list);
460 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
461 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
462 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
463 $smarty->assign('title', $i18n->get('title.time'));
464 $smarty->assign('content_page_name', 'time.tpl');
465 $smarty->display('index.tpl');