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 (!ttAccessCheck(right_data_entry)) {
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 = $quota->get($selected_date->mYear, $selected_date->mMonth);
70 $month_total = ttTimeHelper::getTimeForMonth($user->getActiveUser(), $selected_date);
71 $minutes_left = ttTimeHelper::toMinutes($month_quota) - 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->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
191 // Make the start and finish fields read-only.
192 $form->getElement('start')->setEnabled(false);
193 $form->getElement('finish')->setEnabled(false);
196 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
197 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
198 if (!defined('NOTE_INPUT_HEIGHT'))
199 define('NOTE_INPUT_HEIGHT', 40);
200 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
201 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
202 if ($user->isPluginEnabled('iv'))
203 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
204 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
205 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
207 // If we have custom fields - add controls for them.
208 if ($custom_fields && $custom_fields->fields[0]) {
209 // Only one custom field is supported at this time.
210 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
211 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
212 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
213 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
214 'style'=>'width: 250px;',
216 'data'=>$custom_fields->options,
217 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
222 if ($request->isPost()) {
223 if ($request->getParameter('btn_submit')) {
225 // Validate user input.
226 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
227 $err->add($i18n->getKey('error.client'));
228 if ($custom_fields) {
229 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
231 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
232 if (!$cl_project) $err->add($i18n->getKey('error.project'));
234 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
235 if (!$cl_task) $err->add($i18n->getKey('error.task'));
237 if (strlen($cl_duration) == 0) {
238 if ($cl_start || $cl_finish) {
239 if (!ttTimeHelper::isValidTime($cl_start))
240 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
242 if (!ttTimeHelper::isValidTime($cl_finish))
243 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
244 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
245 $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
248 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
249 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
250 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
252 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
253 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
256 if (!ttTimeHelper::isValidDuration($cl_duration))
257 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
259 if (!ttValidString($cl_note, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
260 // Finished validating user input.
262 // Prohibit creating entries in future.
263 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
264 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
265 if ($selected_date->after($browser_today))
266 $err->add($i18n->getKey('error.future_date'));
269 // Prohibit creating entries in locked range.
270 if ($user->isDateLocked($selected_date))
271 $err->add($i18n->getKey('error.range_locked'));
273 // Prohibit creating another uncompleted record.
275 if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
276 $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
279 // Prohibit creating an overlapping record.
281 if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
282 $err->add($i18n->getKey('error.overlap'));
287 $id = ttTimeHelper::insert(array(
289 'user_id' => $user->getActiveUser(),
290 'client' => $cl_client,
291 'project' => $cl_project,
293 'start' => $cl_start,
294 'finish' => $cl_finish,
295 'duration' => $cl_duration,
297 'billable' => $cl_billable));
299 // Insert a custom field if we have it.
301 if ($id && $custom_fields && $cl_cf_1) {
302 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
303 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
304 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
305 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
307 if ($id && $result) {
308 header('Location: time.php');
311 $err->add($i18n->getKey('error.db'));
313 } elseif ($request->getParameter('btn_stop')) {
314 // Stop button pressed to finish an uncompleted record.
315 $record_id = $request->getParameter('record_id');
316 $record = ttTimeHelper::getRecord($record_id, $user->getActiveUser());
317 $browser_date = $request->getParameter('browser_date');
318 $browser_time = $request->getParameter('browser_time');
320 // Can we complete this record?
321 if ($record['date'] == $browser_date // closing today's record
322 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
323 && !ttTimeHelper::overlaps($user->getActiveUser(), $browser_date, $record['start'], $browser_time)) { // no overlap
324 $res = ttTimeHelper::update(array(
326 'date'=>$record['date'],
327 'user_id'=>$user->getActiveUser(),
328 'client'=>$record['client_id'],
329 'project'=>$record['project_id'],
330 'task'=>$record['task_id'],
331 'start'=>$record['start'],
332 'finish'=>$browser_time,
333 'note'=>$record['comment'],
334 'billable'=>$record['billable']));
336 $err->add($i18n->getKey('error.db'));
338 // Cannot complete, redirect for manual edit.
339 header('Location: time_edit.php?id='.$record_id);
343 elseif ($request->getParameter('onBehalfUser')) {
344 if($user->canManageTeam()) {
345 unset($_SESSION['behalf_id']);
346 unset($_SESSION['behalf_name']);
348 if($on_behalf_id != $user->id) {
349 $_SESSION['behalf_id'] = $on_behalf_id;
350 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
352 header('Location: time.php');
358 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
360 $smarty->assign('selected_date', $selected_date);
361 $smarty->assign('week_total', $week_total);
362 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
363 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
364 $smarty->assign('client_list', $client_list);
365 $smarty->assign('project_list', $project_list);
366 $smarty->assign('task_list', $task_list);
367 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
368 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
369 $smarty->assign('timestring', $selected_date->toString($user->date_format));
370 $smarty->assign('title', $i18n->getKey('title.time'));
371 $smarty->assign('content_page_name', 'time.tpl');
372 $smarty->display('index.tpl');