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('ttTeamHelper');
33 import('ttClientHelper');
34 import('ttTimeHelper');
35 import('DateAndTime');
37 // This is a now removed check whether user browser supports cookies.
38 // if (!isset($_COOKIE['tt_PHPSESSID'])) {
39 // This test gives a false-positive if user goes directly to this page
40 // as from a desktop shortcut (on first request only).
41 // die ("Your browser's cookie functionality is turned off. Please turn it on.");
45 if (!ttAccessAllowed('track_own_time')) {
46 header('Location: access_denied.php');
50 // Initialize and store date in session.
51 $cl_date = $request->getParameter('date', @$_SESSION['date']);
52 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
53 if($selected_date->isError())
54 $selected_date = new DateAndTime(DB_DATEFORMAT);
56 $cl_date = $selected_date->toString(DB_DATEFORMAT);
57 $_SESSION['date'] = $cl_date;
59 // Use custom fields plugin if it is enabled.
60 if ($user->isPluginEnabled('cf')) {
61 require_once('plugins/CustomFields.class.php');
62 $custom_fields = new CustomFields($user->team_id);
63 $smarty->assign('custom_fields', $custom_fields);
66 if ($user->isPluginEnabled('mq')){
67 require_once('plugins/MonthlyQuota.class.php');
68 $quota = new MonthlyQuota();
69 $month_quota_minutes = $quota->get($selected_date->mYear, $selected_date->mMonth);
70 $month_total = ttTimeHelper::getTimeForMonth($user->getActiveUser(), $selected_date);
71 $minutes_left = $month_quota_minutes - ttTimeHelper::toMinutes($month_total);
73 $smarty->assign('month_total', $month_total);
74 $smarty->assign('over_quota', $minutes_left < 0);
75 $smarty->assign('quota_remaining', ttTimeHelper::toAbsDuration($minutes_left));
78 // Initialize variables.
79 $cl_start = trim($request->getParameter('start'));
80 $cl_finish = trim($request->getParameter('finish'));
81 $cl_duration = trim($request->getParameter('duration'));
82 $cl_note = trim($request->getParameter('note'));
84 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
85 $_SESSION['cf_1'] = $cl_cf_1;
87 if ($user->isPluginEnabled('iv')) {
88 if ($request->isPost()) {
89 $cl_billable = $request->getParameter('billable');
90 $_SESSION['billable'] = (int) $cl_billable;
92 if (isset($_SESSION['billable']))
93 $cl_billable = $_SESSION['billable'];
95 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
96 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
97 $_SESSION['client'] = $cl_client;
98 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
99 $_SESSION['project'] = $cl_project;
100 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
101 $_SESSION['task'] = $cl_task;
103 // Elements of timeRecordForm.
104 $form = new Form('timeRecordForm');
106 if ($user->canManageTeam()) {
107 $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
108 if (count($user_list) > 1) {
109 $form->addInput(array('type'=>'combobox',
110 'onchange'=>'this.form.submit();',
111 'name'=>'onBehalfUser',
112 'style'=>'width: 250px;',
113 'value'=>$on_behalf_id,
115 'datakeys'=>array('id','name')));
116 $smarty->assign('on_behalf_control', 1);
120 // Dropdown for clients in MODE_TIME. Use all active clients.
121 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
122 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
123 $form->addInput(array('type'=>'combobox',
124 'onchange'=>'fillProjectDropdown(this.value);',
126 'style'=>'width: 250px;',
128 'data'=>$active_clients,
129 'datakeys'=>array('id', 'name'),
130 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
131 // Note: in other modes the client list is filtered to relevant clients only. See below.
134 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
135 // Dropdown for projects assigned to user.
136 $project_list = $user->getAssignedProjects();
137 $form->addInput(array('type'=>'combobox',
138 'onchange'=>'fillTaskDropdown(this.value);',
140 'style'=>'width: 250px;',
141 'value'=>$cl_project,
142 'data'=>$project_list,
143 'datakeys'=>array('id','name'),
144 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
146 // Dropdown for clients if the clients plugin is enabled.
147 if ($user->isPluginEnabled('cl')) {
148 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
149 // We need an array of assigned project ids to do some trimming.
150 foreach($project_list as $project)
151 $projects_assigned_to_user[] = $project['id'];
153 // Build a client list out of active clients. Use only clients that are relevant to user.
154 // Also trim their associated project list to only assigned projects (to user).
155 foreach($active_clients as $client) {
156 $projects_assigned_to_client = explode(',', $client['projects']);
157 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
158 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
160 $client['projects'] = implode(',', $intersection);
161 $client_list[] = $client;
164 $form->addInput(array('type'=>'combobox',
165 'onchange'=>'fillProjectDropdown(this.value);',
167 'style'=>'width: 250px;',
169 'data'=>$client_list,
170 'datakeys'=>array('id', 'name'),
171 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
175 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
176 $task_list = ttTeamHelper::getActiveTasks($user->team_id);
177 $form->addInput(array('type'=>'combobox',
179 'style'=>'width: 250px;',
182 'datakeys'=>array('id','name'),
183 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
186 // Add other controls.
187 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
188 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
189 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
190 if ($user->punch_mode && // Punch mode enabled for team.
191 !($user->behalf_id && $user->can('override_punch_mode'))) { // Cannot override for lower roles.
192 // Make the start and finish fields read-only.
193 $form->getElement('start')->setEnabled(false);
194 $form->getElement('finish')->setEnabled(false);
197 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
198 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
199 if (!defined('NOTE_INPUT_HEIGHT'))
200 define('NOTE_INPUT_HEIGHT', 40);
201 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
202 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
203 if ($user->isPluginEnabled('iv'))
204 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
205 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
206 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
208 // If we have custom fields - add controls for them.
209 if ($custom_fields && $custom_fields->fields[0]) {
210 // Only one custom field is supported at this time.
211 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
212 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
213 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
214 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
215 'style'=>'width: 250px;',
217 'data'=>$custom_fields->options,
218 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
223 if ($request->isPost()) {
224 if ($request->getParameter('btn_submit')) {
226 // Validate user input.
227 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
228 $err->add($i18n->getKey('error.client'));
229 if ($custom_fields) {
230 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
232 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
233 if (!$cl_project) $err->add($i18n->getKey('error.project'));
235 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
236 if (!$cl_task) $err->add($i18n->getKey('error.task'));
238 if (strlen($cl_duration) == 0) {
239 if ($cl_start || $cl_finish) {
240 if (!ttTimeHelper::isValidTime($cl_start))
241 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
243 if (!ttTimeHelper::isValidTime($cl_finish))
244 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
245 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
246 $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
249 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
250 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
251 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
253 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
254 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
257 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
258 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
260 if (!ttValidString($cl_note, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
261 // Finished validating user input.
263 // Prohibit creating entries in future.
264 if (!$user->future_entries) {
265 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
266 if ($selected_date->after($browser_today))
267 $err->add($i18n->getKey('error.future_date'));
270 // Prohibit creating entries in locked range.
271 if ($user->isDateLocked($selected_date))
272 $err->add($i18n->getKey('error.range_locked'));
274 // Prohibit creating another uncompleted record.
276 if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
277 $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
280 // Prohibit creating an overlapping record.
282 if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
283 $err->add($i18n->getKey('error.overlap'));
288 $id = ttTimeHelper::insert(array(
290 'user_id' => $user->getActiveUser(),
291 'client' => $cl_client,
292 'project' => $cl_project,
294 'start' => $cl_start,
295 'finish' => $cl_finish,
296 'duration' => $cl_duration,
298 'billable' => $cl_billable));
300 // Insert a custom field if we have it.
302 if ($id && $custom_fields && $cl_cf_1) {
303 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
304 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
305 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
306 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
308 if ($id && $result) {
309 header('Location: time.php');
312 $err->add($i18n->getKey('error.db'));
314 } elseif ($request->getParameter('btn_stop')) {
315 // Stop button pressed to finish an uncompleted record.
316 $record_id = $request->getParameter('record_id');
317 $record = ttTimeHelper::getRecord($record_id, $user->getActiveUser());
318 $browser_date = $request->getParameter('browser_date');
319 $browser_time = $request->getParameter('browser_time');
321 // Can we complete this record?
322 if ($record['date'] == $browser_date // closing today's record
323 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
324 && !ttTimeHelper::overlaps($user->getActiveUser(), $browser_date, $record['start'], $browser_time)) { // no overlap
325 $res = ttTimeHelper::update(array(
327 'date'=>$record['date'],
328 'user_id'=>$user->getActiveUser(),
329 'client'=>$record['client_id'],
330 'project'=>$record['project_id'],
331 'task'=>$record['task_id'],
332 'start'=>$record['start'],
333 'finish'=>$browser_time,
334 'note'=>$record['comment'],
335 'billable'=>$record['billable']));
337 $err->add($i18n->getKey('error.db'));
339 // Cannot complete, redirect for manual edit.
340 header('Location: time_edit.php?id='.$record_id);
344 elseif ($request->getParameter('onBehalfUser')) {
345 if($user->canManageTeam()) {
346 unset($_SESSION['behalf_id']);
347 unset($_SESSION['behalf_name']);
349 if($on_behalf_id != $user->id) {
350 $_SESSION['behalf_id'] = $on_behalf_id;
351 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
353 header('Location: time.php');
359 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
361 $smarty->assign('selected_date', $selected_date);
362 $smarty->assign('week_total', $week_total);
363 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
364 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
365 $smarty->assign('client_list', $client_list);
366 $smarty->assign('project_list', $project_list);
367 $smarty->assign('task_list', $task_list);
368 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
369 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
370 $smarty->assign('timestring', $selected_date->toString($user->date_format));
371 $smarty->assign('title', $i18n->getKey('title.time'));
372 $smarty->assign('content_page_name', 'time.tpl');
373 $smarty->display('index.tpl');