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('ttClientHelper');
34 import('ttTimeHelper');
35 import('DateAndTime');
38 if (!(ttAccessAllowed('track_own_time') || ttAccessAllowed('track_time'))) {
39 header('Location: access_denied.php');
42 if ($user->behalf_id && (!$user->can('track_time') || !$user->checkBehalfId())) {
43 header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
46 if (!$user->behalf_id && !$user->can('track_own_time') && !$user->adjustBehalfId()) {
47 header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
50 if ($request->isPost()) {
51 $groupChanged = $request->getParameter('group_changed'); // Reused in multiple places below.
52 if ($groupChanged && !($user->can('manage_subgroups') && $user->isGroupValid($request->getParameter('group')))) {
53 header('Location: access_denied.php'); // Group changed, but no rght or wrong group id.
56 $userChanged = $request->getParameter('user_changed'); // Reused in multiple places below.
57 if ($userChanged && !($user->can('track_time') && $user->isUserValid($request->getParameter('user')))) {
58 header('Location: access_denied.php'); // Group changed, but no rght or wrong user id.
62 // End of access checks.
64 // Determine group for which we display this page.
65 if ($request->isPost() && $groupChanged) {
66 $group_id = $request->getParameter('group');
67 $user->setOnBehalfGroup($group_id);
69 $group_id = $user->getGroup();
71 // Determine user for which we display this page.
72 if ($request->isPost() && $userChanged) {
73 $user_id = $request->getParameter('user');
74 $user->setOnBehalfUser($user_id);
76 $user_id = $user->getUser();
79 // Initialize and store date in session.
80 $cl_date = $request->getParameter('date', @$_SESSION['date']);
81 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
82 if($selected_date->isError())
83 $selected_date = new DateAndTime(DB_DATEFORMAT);
85 $cl_date = $selected_date->toString(DB_DATEFORMAT);
86 $_SESSION['date'] = $cl_date;
88 // Use custom fields plugin if it is enabled.
89 if ($user->isPluginEnabled('cf')) {
90 require_once('plugins/CustomFields.class.php');
91 $custom_fields = new CustomFields();
92 $smarty->assign('custom_fields', $custom_fields);
95 if ($user->isPluginEnabled('mq')){
96 require_once('plugins/MonthlyQuota.class.php');
97 $quota = new MonthlyQuota();
98 $month_quota_minutes = $quota->get($selected_date->mYear, $selected_date->mMonth);
99 $month_total = ttTimeHelper::getTimeForMonth($user_id, $selected_date);
100 $minutes_left = $month_quota_minutes - ttTimeHelper::toMinutes($month_total);
102 $smarty->assign('month_total', $month_total);
103 $smarty->assign('over_quota', $minutes_left < 0);
104 $smarty->assign('quota_remaining', ttTimeHelper::toAbsDuration($minutes_left));
107 // Initialize variables.
108 $cl_start = trim($request->getParameter('start'));
109 $cl_finish = trim($request->getParameter('finish'));
110 $cl_duration = trim($request->getParameter('duration'));
111 $cl_note = trim($request->getParameter('note'));
113 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
114 $_SESSION['cf_1'] = $cl_cf_1;
116 if ($user->isPluginEnabled('iv')) {
117 if ($request->isPost()) {
118 $cl_billable = $request->getParameter('billable');
119 $_SESSION['billable'] = (int) $cl_billable;
121 if (isset($_SESSION['billable']))
122 $cl_billable = $_SESSION['billable'];
124 //$on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
125 //$on_behalf_group_id = $request->getParameter('onBehalfGroup', (isset($_SESSION['behalf_group_id'])? $_SESSION['behalf_group_id'] : $user->group_id));
126 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
127 $_SESSION['client'] = $cl_client;
128 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
129 $_SESSION['project'] = $cl_project;
130 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
131 $_SESSION['task'] = $cl_task;
133 // Elements of timeRecordForm.
134 $form = new Form('timeRecordForm');
136 if ($user->can('manage_subgroups')) {
137 $groups = $user->getGroupsForDropdown();
138 if (count($groups) > 1) {
139 $form->addInput(array('type'=>'combobox',
140 'onchange'=>'document.timeRecordForm.group_changed.value=1;document.timeRecordForm.submit();',
142 'style'=>'width: 250px;',
145 'datakeys'=>array('id','name')));
146 $form->addInput(array('type'=>'hidden','name'=>'group_changed'));
147 $smarty->assign('group_dropdown', 1);
150 if ($user->can('track_time')) {
151 $rank = $user->getMaxRankForGroup($group_id);
152 if ($user->can('track_own_time'))
153 $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
155 $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank);
156 $user_list = $user->getUsers($options);
157 if (count($user_list) >= 1) {
158 $form->addInput(array('type'=>'combobox',
159 'onchange'=>'document.timeRecordForm.user_changed.value=1;document.timeRecordForm.submit();',
161 'style'=>'width: 250px;',
164 'datakeys'=>array('id','name')));
165 $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
166 $smarty->assign('user_dropdown', 1);
170 // Dropdown for clients in MODE_TIME. Use all active clients.
171 if (MODE_TIME == $user->getTrackingMode() && $user->isPluginEnabled('cl')) {
172 $active_clients = ttGroupHelper::getActiveClients(true);
173 $form->addInput(array('type'=>'combobox',
174 'onchange'=>'fillProjectDropdown(this.value);',
176 'style'=>'width: 250px;',
178 'data'=>$active_clients,
179 'datakeys'=>array('id', 'name'),
180 'empty'=>array(''=>$i18n->get('dropdown.select'))));
181 // Note: in other modes the client list is filtered to relevant clients only. See below.
184 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
185 // Dropdown for projects assigned to user.
186 $project_list = $user->getAssignedProjects();
187 $form->addInput(array('type'=>'combobox',
188 'onchange'=>'fillTaskDropdown(this.value);',
190 'style'=>'width: 250px;',
191 'value'=>$cl_project,
192 'data'=>$project_list,
193 'datakeys'=>array('id','name'),
194 'empty'=>array(''=>$i18n->get('dropdown.select'))));
196 // Dropdown for clients if the clients plugin is enabled.
197 if ($user->isPluginEnabled('cl')) {
198 $active_clients = ttGroupHelper::getActiveClients(true);
199 // We need an array of assigned project ids to do some trimming.
200 foreach($project_list as $project)
201 $projects_assigned_to_user[] = $project['id'];
203 // Build a client list out of active clients. Use only clients that are relevant to user.
204 // Also trim their associated project list to only assigned projects (to user).
205 foreach($active_clients as $client) {
206 $projects_assigned_to_client = explode(',', $client['projects']);
207 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
208 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
210 $client['projects'] = implode(',', $intersection);
211 $client_list[] = $client;
214 $form->addInput(array('type'=>'combobox',
215 'onchange'=>'fillProjectDropdown(this.value);',
217 'style'=>'width: 250px;',
219 'data'=>$client_list,
220 'datakeys'=>array('id', 'name'),
221 'empty'=>array(''=>$i18n->get('dropdown.select'))));
225 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
226 $task_list = ttGroupHelper::getActiveTasks();
227 $form->addInput(array('type'=>'combobox',
229 'style'=>'width: 250px;',
232 'datakeys'=>array('id','name'),
233 'empty'=>array(''=>$i18n->get('dropdown.select'))));
236 // Add other controls.
237 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
238 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
239 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
240 if ($user->punch_mode && !$user->canOverridePunchMode()) {
241 // Make the start and finish fields read-only.
242 $form->getElement('start')->setEnabled(false);
243 $form->getElement('finish')->setEnabled(false);
246 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
247 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
248 if (!defined('NOTE_INPUT_HEIGHT'))
249 define('NOTE_INPUT_HEIGHT', 40);
250 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
251 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
252 if ($user->isPluginEnabled('iv'))
253 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
254 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
255 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
257 // If we have custom fields - add controls for them.
258 if ($custom_fields && $custom_fields->fields[0]) {
259 // Only one custom field is supported at this time.
260 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
261 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
262 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
263 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
264 'style'=>'width: 250px;',
266 'data'=>$custom_fields->options,
267 'empty'=>array(''=>$i18n->get('dropdown.select'))));
272 if ($request->isPost()) {
273 if ($request->getParameter('btn_submit')) {
275 // Validate user input.
276 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
277 $err->add($i18n->get('error.client'));
278 if ($custom_fields) {
279 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
281 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
282 if (!$cl_project) $err->add($i18n->get('error.project'));
284 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
285 if (!$cl_task) $err->add($i18n->get('error.task'));
287 if (strlen($cl_duration) == 0) {
288 if ($cl_start || $cl_finish) {
289 if (!ttTimeHelper::isValidTime($cl_start))
290 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
292 if (!ttTimeHelper::isValidTime($cl_finish))
293 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
294 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
295 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
298 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
299 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
300 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
302 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
303 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
306 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
307 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
309 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
310 // Finished validating user input.
312 // Prohibit creating entries in future.
313 if (!$user->future_entries) {
314 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
315 if ($selected_date->after($browser_today))
316 $err->add($i18n->get('error.future_date'));
319 // Prohibit creating entries in locked range.
320 if ($user->isDateLocked($selected_date))
321 $err->add($i18n->get('error.range_locked'));
323 // Prohibit creating another uncompleted record.
325 if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
326 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
329 // Prohibit creating an overlapping record.
331 if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
332 $err->add($i18n->get('error.overlap'));
337 $id = ttTimeHelper::insert(array(
339 'user_id' => $user_id,
340 'group_id' => $group_id,
341 'org_id' => $user->org_id,
342 'client' => $cl_client,
343 'project' => $cl_project,
345 'start' => $cl_start,
346 'finish' => $cl_finish,
347 'duration' => $cl_duration,
349 'billable' => $cl_billable));
351 // Insert a custom field if we have it.
353 if ($id && $custom_fields && $cl_cf_1) {
354 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
355 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
356 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
357 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
359 if ($id && $result) {
360 header('Location: time.php');
363 $err->add($i18n->get('error.db'));
365 } elseif ($request->getParameter('btn_stop')) {
366 // Stop button pressed to finish an uncompleted record.
367 $record_id = $request->getParameter('record_id');
368 $record = ttTimeHelper::getRecord($record_id, $user_id);
369 $browser_date = $request->getParameter('browser_date');
370 $browser_time = $request->getParameter('browser_time');
372 // Can we complete this record?
373 if ($record['date'] == $browser_date // closing today's record
374 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
375 && !ttTimeHelper::overlaps($user_id, $browser_date, $record['start'], $browser_time)) { // no overlap
376 $res = ttTimeHelper::update(array(
378 'date'=>$record['date'],
380 'client'=>$record['client_id'],
381 'project'=>$record['project_id'],
382 'task'=>$record['task_id'],
383 'start'=>$record['start'],
384 'finish'=>$browser_time,
385 'note'=>$record['comment'],
386 'billable'=>$record['billable']));
388 $err->add($i18n->get('error.db'));
390 // Cannot complete, redirect for manual edit.
391 header('Location: time_edit.php?id='.$record_id);
397 $week_total = ttTimeHelper::getTimeForWeek($user_id, $selected_date);
399 $smarty->assign('selected_date', $selected_date);
400 $smarty->assign('week_total', $week_total);
401 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user_id, $cl_date));
402 $smarty->assign('time_records', ttTimeHelper::getRecords($user_id, $cl_date));
403 $smarty->assign('client_list', $client_list);
404 $smarty->assign('project_list', $project_list);
405 $smarty->assign('task_list', $task_list);
406 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
407 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
408 $smarty->assign('timestring', $selected_date->toString($user->date_format));
409 $smarty->assign('title', $i18n->get('title.time'));
410 $smarty->assign('content_page_name', 'time.tpl');
411 $smarty->display('index.tpl');