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 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
152 $_SESSION['client'] = $cl_client;
153 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
154 $_SESSION['project'] = $cl_project;
155 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
156 $_SESSION['task'] = $cl_task;
158 // Elements of timeRecordForm.
159 $form = new Form('timeRecordForm');
160 if ($user->can('track_time')) {
161 $rank = $user->getMaxRankForGroup($group_id);
162 if ($user->can('track_own_time'))
163 $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
165 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
166 $user_list = $user->getUsers($options);
167 if (count($user_list) >= 1) {
168 $form->addInput(array('type'=>'combobox',
169 'onchange'=>'document.timeRecordForm.user_changed.value=1;document.timeRecordForm.submit();',
171 'style'=>'width: 250px;',
174 'datakeys'=>array('id','name')));
175 $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
176 $smarty->assign('user_dropdown', 1);
180 // Dropdown for clients in MODE_TIME. Use all active clients.
181 if (MODE_TIME == $trackingMode && $showClient) {
182 $active_clients = ttGroupHelper::getActiveClients(true);
183 $form->addInput(array('type'=>'combobox',
184 'onchange'=>'fillProjectDropdown(this.value);',
186 'style'=>'width: 250px;',
188 'data'=>$active_clients,
189 'datakeys'=>array('id', 'name'),
190 'empty'=>array(''=>$i18n->get('dropdown.select'))));
191 // Note: in other modes the client list is filtered to relevant clients only. See below.
195 // Dropdown for projects assigned to user.
196 $project_list = $user->getAssignedProjects();
197 $form->addInput(array('type'=>'combobox',
198 'onchange'=>'fillTaskDropdown(this.value);',
200 'style'=>'width: 250px;',
201 'value'=>$cl_project,
202 'data'=>$project_list,
203 'datakeys'=>array('id','name'),
204 'empty'=>array(''=>$i18n->get('dropdown.select'))));
206 // Dropdown for clients if the clients plugin is enabled.
208 $active_clients = ttGroupHelper::getActiveClients(true);
209 // We need an array of assigned project ids to do some trimming.
210 foreach($project_list as $project)
211 $projects_assigned_to_user[] = $project['id'];
213 // Build a client list out of active clients. Use only clients that are relevant to user.
214 // Also trim their associated project list to only assigned projects (to user).
215 foreach($active_clients as $client) {
216 $projects_assigned_to_client = explode(',', $client['projects']);
217 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
218 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
220 $client['projects'] = implode(',', $intersection);
221 $client_list[] = $client;
224 $form->addInput(array('type'=>'combobox',
225 'onchange'=>'fillProjectDropdown(this.value);',
227 'style'=>'width: 250px;',
229 'data'=>$client_list,
230 'datakeys'=>array('id', 'name'),
231 'empty'=>array(''=>$i18n->get('dropdown.select'))));
236 $task_list = ttGroupHelper::getActiveTasks();
237 $form->addInput(array('type'=>'combobox',
239 'style'=>'width: 250px;',
242 'datakeys'=>array('id','name'),
243 'empty'=>array(''=>$i18n->get('dropdown.select'))));
246 // Add other controls.
248 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
249 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
250 if ($user->punch_mode && !$user->canOverridePunchMode()) {
251 // Make the start and finish fields read-only.
252 $form->getElement('start')->setEnabled(false);
253 $form->getElement('finish')->setEnabled(false);
257 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
258 if (!defined('NOTE_INPUT_HEIGHT'))
259 define('NOTE_INPUT_HEIGHT', 40);
260 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
261 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
265 // TODO: refactoring ongoing down from here. Use $showBillable, perhaps?
266 if ($user->isPluginEnabled('iv'))
267 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
268 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
269 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
271 // If we have custom fields - add controls for them.
272 if ($custom_fields && $custom_fields->fields[0]) {
273 // Only one custom field is supported at this time.
274 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
275 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
276 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
277 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
278 'style'=>'width: 250px;',
280 'data'=>$custom_fields->options,
281 'empty'=>array(''=>$i18n->get('dropdown.select'))));
285 // If we have templates, add a dropdown to select one.
286 if ($user->isPluginEnabled('tp')){
287 $templates = ttGroupHelper::getActiveTemplates();
288 if (count($templates) >= 1) {
289 $form->addInput(array('type'=>'combobox',
290 'onchange'=>'fillNote(this.value);',
292 'style'=>'width: 250px;',
294 'datakeys'=>array('id','name'),
295 'empty'=>array(''=>$i18n->get('dropdown.select'))));
296 $smarty->assign('template_dropdown', 1);
297 $smarty->assign('templates', $templates);
302 if ($request->isPost()) {
303 if ($request->getParameter('btn_submit')) {
305 // Validate user input.
306 if ($showClient && $user->isPluginEnabled('cm') && !$cl_client)
307 $err->add($i18n->get('error.client'));
308 if ($custom_fields) {
309 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
311 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
312 if (!$cl_project) $err->add($i18n->get('error.project'));
314 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode() && $user->task_required) {
315 if (!$cl_task) $err->add($i18n->get('error.task'));
317 if (strlen($cl_duration) == 0) {
318 if ($cl_start || $cl_finish) {
319 if (!ttTimeHelper::isValidTime($cl_start))
320 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
322 if (!ttTimeHelper::isValidTime($cl_finish))
323 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
324 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
325 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
328 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
329 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
330 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
332 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
333 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
336 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
337 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
339 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
340 if ($user->isPluginEnabled('tp') && !ttValidTemplateText($cl_note)) {
341 $err->add($i18n->get('error.field'), $i18n->get('label.note'));
343 if (!ttTimeHelper::canAdd()) $err->add($i18n->get('error.expired'));
344 // Finished validating user input.
346 // Prohibit creating entries in future.
347 if (!$user->future_entries) {
348 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
349 if ($selected_date->after($browser_today))
350 $err->add($i18n->get('error.future_date'));
353 // Prohibit creating entries in locked range.
354 if ($user->isDateLocked($selected_date))
355 $err->add($i18n->get('error.range_locked'));
357 // Prohibit creating another uncompleted record.
359 if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
360 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
363 // Prohibit creating an overlapping record.
365 if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
366 $err->add($i18n->get('error.overlap'));
371 $id = ttTimeHelper::insert(array(
373 'user_id' => $user_id,
374 'group_id' => $group_id,
375 'org_id' => $user->org_id,
376 'client' => $cl_client,
377 'project' => $cl_project,
379 'start' => $cl_start,
380 'finish' => $cl_finish,
381 'duration' => $cl_duration,
383 'billable' => $cl_billable));
385 // Insert a custom field if we have it.
387 if ($id && $custom_fields && $cl_cf_1) {
388 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
389 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
390 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
391 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
393 if ($id && $result) {
394 header('Location: time.php');
397 $err->add($i18n->get('error.db'));
399 } elseif ($request->getParameter('btn_stop')) {
400 // Stop button pressed to finish an uncompleted record.
401 $record_id = $request->getParameter('record_id');
402 $record = ttTimeHelper::getRecord($record_id);
403 $browser_date = $request->getParameter('browser_date');
404 $browser_time = $request->getParameter('browser_time');
406 // Can we complete this record?
407 if ($record['date'] == $browser_date // closing today's record
408 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
409 && !ttTimeHelper::overlaps($user_id, $browser_date, $record['start'], $browser_time)) { // no overlap
410 $res = ttTimeHelper::update(array(
412 'date'=>$record['date'],
414 'client'=>$record['client_id'],
415 'project'=>$record['project_id'],
416 'task'=>$record['task_id'],
417 'start'=>$record['start'],
418 'finish'=>$browser_time,
419 'note'=>$record['comment'],
420 'billable'=>$record['billable']));
422 $err->add($i18n->get('error.db'));
424 // Cannot complete, redirect for manual edit.
425 header('Location: time_edit.php?id='.$record_id);
431 $week_total = ttTimeHelper::getTimeForWeek($selected_date);
432 $timeRecords = $showFiles? ttTimeHelper::getRecordsWithFiles($user_id, $cl_date) : ttTimeHelper::getRecords($user_id, $cl_date);
434 $smarty->assign('selected_date', $selected_date);
435 $smarty->assign('week_total', $week_total);
436 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($cl_date));
437 $smarty->assign('time_records', $timeRecords);
438 $smarty->assign('show_client', $showClient);
439 $smarty->assign('show_cf_1', $user->isPluginEnabled('cf'));
440 $smarty->assign('show_project', $showProject);
441 $smarty->assign('show_task', $showTask);
442 $smarty->assign('show_start', $showStart);
443 $smarty->assign('show_finish', $showFinish);
444 $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');