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->getMethod()=='POST'? 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->getMethod()=='POST'? null : @$_SESSION['client']));
97 $_SESSION['client'] = $cl_client;
98 $cl_project = $request->getParameter('project', ($request->getMethod()=='POST'? null : @$_SESSION['project']));
99 $_SESSION['project'] = $cl_project;
100 $cl_task = $request->getParameter('task', ($request->getMethod()=='POST'? 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');"));
191 if (!$user->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
192 // Make the start and finish fields read-only.
193 $form->getElement('start')->setEnabled(false);
194 $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 (strlen($cl_duration) == 0) {
235 if ($cl_start || $cl_finish) {
236 if (!ttTimeHelper::isValidTime($cl_start))
237 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
239 if (!ttTimeHelper::isValidTime($cl_finish))
240 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
241 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
242 $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
245 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
246 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
247 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
249 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
250 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
253 if (!ttTimeHelper::isValidDuration($cl_duration))
254 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
256 if (!ttValidString($cl_note, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
257 // Finished validating user input.
259 // Prohibit creating entries in future.
260 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
261 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
262 if ($selected_date->after($browser_today))
263 $err->add($i18n->getKey('error.future_date'));
266 // Prohibit creating entries in locked range.
267 if ($user->isDateLocked($selected_date))
268 $err->add($i18n->getKey('error.range_locked'));
270 // Prohibit creating another uncompleted record.
272 if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
273 $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
276 // Prohibit creating an overlapping record.
278 if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
279 $err->add($i18n->getKey('error.overlap'));
284 $id = ttTimeHelper::insert(array(
286 'user_id' => $user->getActiveUser(),
287 'client' => $cl_client,
288 'project' => $cl_project,
290 'start' => $cl_start,
291 'finish' => $cl_finish,
292 'duration' => $cl_duration,
294 'billable' => $cl_billable));
296 // Insert a custom field if we have it.
298 if ($id && $custom_fields && $cl_cf_1) {
299 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
300 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
301 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
302 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
304 if ($id && $result) {
305 header('Location: time.php');
308 $err->add($i18n->getKey('error.db'));
310 } elseif ($request->getParameter('btn_stop')) {
311 // Stop button pressed to finish an uncompleted record.
312 $record_id = $request->getParameter('record_id');
313 $record = ttTimeHelper::getRecord($record_id, $user->getActiveUser());
314 $browser_date = $request->getParameter('browser_date');
315 $browser_time = $request->getParameter('browser_time');
317 // Can we complete this record?
318 if ($record['date'] == $browser_date // closing today's record
319 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
320 && !ttTimeHelper::overlaps($user->getActiveUser(), $browser_date, $record['start'], $browser_time)) { // no overlap
321 $res = ttTimeHelper::update(array(
323 'date'=>$record['date'],
324 'user_id'=>$user->getActiveUser(),
325 'client'=>$record['client_id'],
326 'project'=>$record['project_id'],
327 'task'=>$record['task_id'],
328 'start'=>$record['start'],
329 'finish'=>$browser_time,
330 'note'=>$record['comment'],
331 'billable'=>$record['billable']));
333 $err->add($i18n->getKey('error.db'));
335 // Cannot complete, redirect for manual edit.
336 header('Location: time_edit.php?id='.$record_id);
340 elseif ($request->getParameter('onBehalfUser')) {
341 if($user->canManageTeam()) {
342 unset($_SESSION['behalf_id']);
343 unset($_SESSION['behalf_name']);
345 if($on_behalf_id != $user->id) {
346 $_SESSION['behalf_id'] = $on_behalf_id;
347 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
349 header('Location: time.php');
355 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
357 $smarty->assign('week_total', $week_total);
358 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
359 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
360 $smarty->assign('client_list', $client_list);
361 $smarty->assign('project_list', $project_list);
362 $smarty->assign('task_list', $task_list);
363 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
364 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
365 $smarty->assign('timestring', $selected_date->toString($user->date_format));
366 $smarty->assign('title', $i18n->getKey('title.time'));
367 $smarty->assign('content_page_name', 'time.tpl');
368 $smarty->display('index.tpl');