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 // Initialize variables.
67 $cl_start = trim($request->getParameter('start'));
68 $cl_finish = trim($request->getParameter('finish'));
69 $cl_duration = trim($request->getParameter('duration'));
70 $cl_note = trim($request->getParameter('note'));
72 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->getMethod()=='POST'? null : @$_SESSION['cf_1'])));
73 $_SESSION['cf_1'] = $cl_cf_1;
75 if ($user->isPluginEnabled('iv')) {
76 if ($request->isPost()) {
77 $cl_billable = $request->getParameter('billable');
78 $_SESSION['billable'] = (int) $cl_billable;
80 if (isset($_SESSION['billable']))
81 $cl_billable = $_SESSION['billable'];
83 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
84 $cl_client = $request->getParameter('client', ($request->getMethod()=='POST'? null : @$_SESSION['client']));
85 $_SESSION['client'] = $cl_client;
86 $cl_project = $request->getParameter('project', ($request->getMethod()=='POST'? null : @$_SESSION['project']));
87 $_SESSION['project'] = $cl_project;
88 $cl_task = $request->getParameter('task', ($request->getMethod()=='POST'? null : @$_SESSION['task']));
89 $_SESSION['task'] = $cl_task;
91 // Elements of timeRecordForm.
92 $form = new Form('timeRecordForm');
94 if ($user->canManageTeam()) {
95 $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
96 if (count($user_list) > 1) {
97 $form->addInput(array('type'=>'combobox',
98 'onchange'=>'this.form.submit();',
99 'name'=>'onBehalfUser',
100 'style'=>'width: 250px;',
101 'value'=>$on_behalf_id,
103 'datakeys'=>array('id','name')));
104 $smarty->assign('on_behalf_control', 1);
108 // Dropdown for clients in MODE_TIME. Use all active clients.
109 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
110 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
111 $form->addInput(array('type'=>'combobox',
112 'onchange'=>'fillProjectDropdown(this.value);',
114 'style'=>'width: 250px;',
116 'data'=>$active_clients,
117 'datakeys'=>array('id', 'name'),
118 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
119 // Note: in other modes the client list is filtered to relevant clients only. See below.
122 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
123 // Dropdown for projects assigned to user.
124 $project_list = $user->getAssignedProjects();
125 $form->addInput(array('type'=>'combobox',
126 'onchange'=>'fillTaskDropdown(this.value);',
128 'style'=>'width: 250px;',
129 'value'=>$cl_project,
130 'data'=>$project_list,
131 'datakeys'=>array('id','name'),
132 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
134 // Dropdown for clients if the clients plugin is enabled.
135 if ($user->isPluginEnabled('cl')) {
136 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
137 // We need an array of assigned project ids to do some trimming.
138 foreach($project_list as $project)
139 $projects_assigned_to_user[] = $project['id'];
141 // Build a client list out of active clients. Use only clients that are relevant to user.
142 // Also trim their associated project list to only assigned projects (to user).
143 foreach($active_clients as $client) {
144 $projects_assigned_to_client = explode(',', $client['projects']);
145 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
146 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
148 $client['projects'] = implode(',', $intersection);
149 $client_list[] = $client;
152 $form->addInput(array('type'=>'combobox',
153 'onchange'=>'fillProjectDropdown(this.value);',
155 'style'=>'width: 250px;',
157 'data'=>$client_list,
158 'datakeys'=>array('id', 'name'),
159 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
163 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
164 $task_list = ttTeamHelper::getActiveTasks($user->team_id);
165 $form->addInput(array('type'=>'combobox',
167 'style'=>'width: 250px;',
170 'datakeys'=>array('id','name'),
171 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
174 // Add other controls.
175 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
176 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
177 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
179 if (!$user->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
180 // Make the start and finish fields read-only.
181 $form->getElement('start')->setEnable(false);
182 $form->getElement('finish')->setEnable(false);
184 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
185 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
186 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height: 40px;','value'=>$cl_note));
187 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
188 if ($user->isPluginEnabled('iv'))
189 $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable));
190 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
191 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
193 // If we have custom fields - add controls for them.
194 if ($custom_fields && $custom_fields->fields[0]) {
195 // Only one custom field is supported at this time.
196 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
197 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
198 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
199 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
200 'style'=>'width: 250px;',
202 'data'=>$custom_fields->options,
203 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
207 // Determine lock date. Time entries earlier than lock date cannot be created or modified.
208 $lock_interval = $user->lock_interval;
210 if ($lock_interval > 0) {
211 $lockdate = new DateAndTime();
212 $lockdate->decDay($lock_interval);
216 if ($request->isPost()) {
217 if ($request->getParameter('btn_submit')) {
219 // Validate user input.
220 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
221 $err->add($i18n->getKey('error.client'));
222 if ($custom_fields) {
223 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
225 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
226 if (!$cl_project) $err->add($i18n->getKey('error.project'));
228 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
229 if (!$cl_task) $err->add($i18n->getKey('error.task'));
232 if ('0' == $cl_duration)
233 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
234 elseif ($cl_start || $cl_finish) {
235 if (!ttTimeHelper::isValidTime($cl_start))
236 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
238 if (!ttTimeHelper::isValidTime($cl_finish))
239 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
240 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
241 $err->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
244 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
245 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
246 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
248 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
249 $err->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
252 if (!ttTimeHelper::isValidDuration($cl_duration))
253 $err->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
255 if (!ttValidString($cl_note, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
256 // Finished validating user input.
258 // Prohibit creating entries in future.
259 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
260 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
261 if ($selected_date->after($browser_today))
262 $err->add($i18n->getKey('error.future_date'));
265 // Prohibit creating time entries in locked interval.
266 if($lockdate && $selected_date->before($lockdate))
267 $err->add($i18n->getKey('error.period_locked'));
269 // Prohibit creating another uncompleted record.
271 if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
272 $err->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
275 // Prohibit creating an overlapping record.
277 if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
278 $err->add($i18n->getKey('error.overlap'));
283 $id = ttTimeHelper::insert(array(
285 'user_id' => $user->getActiveUser(),
286 'client' => $cl_client,
287 'project' => $cl_project,
289 'start' => $cl_start,
290 'finish' => $cl_finish,
291 'duration' => $cl_duration,
293 'billable' => $cl_billable));
295 // Insert a custom field if we have it.
297 if ($id && $custom_fields && $cl_cf_1) {
298 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
299 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
300 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
301 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
303 if ($id && $result) {
304 header('Location: time.php');
307 $err->add($i18n->getKey('error.db'));
309 } elseif ($request->getParameter('btn_stop')) {
310 // Stop button pressed to finish an uncompleted record.
311 $record_id = $request->getParameter('record_id');
312 $record = ttTimeHelper::getRecord($record_id, $user->getActiveUser());
313 $browser_date = $request->getParameter('browser_date');
314 $browser_time = $request->getParameter('browser_time');
316 // Can we complete this record?
317 if ($record['date'] == $browser_date // closing today's record
318 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
319 && !ttTimeHelper::overlaps($user->getActiveUser(), $browser_date, $record['start'], $browser_time)) { // no overlap
320 $res = ttTimeHelper::update(array(
322 'date'=>$record['date'],
323 'user_id'=>$user->getActiveUser(),
324 'client'=>$record['client_id'],
325 'project'=>$record['project_id'],
326 'task'=>$record['task_id'],
327 'start'=>$record['start'],
328 'finish'=>$browser_time,
329 'note'=>$record['comment'],
330 'billable'=>$record['billable']));
332 $err->add($i18n->getKey('error.db'));
334 // Cannot complete, redirect for manual edit.
335 header('Location: time_edit.php?id='.$record_id);
339 elseif ($request->getParameter('onBehalfUser')) {
340 if($user->canManageTeam()) {
341 unset($_SESSION['behalf_id']);
342 unset($_SESSION['behalf_name']);
344 if($on_behalf_id != $user->id) {
345 $_SESSION['behalf_id'] = $on_behalf_id;
346 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
348 header('Location: time.php');
354 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $selected_date);
356 $smarty->assign('week_total', $week_total);
357 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
358 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
359 $smarty->assign('client_list', $client_list);
360 $smarty->assign('project_list', $project_list);
361 $smarty->assign('task_list', $task_list);
362 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
363 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
364 $smarty->assign('timestring', $selected_date->toString($user->date_format));
365 $smarty->assign('title', $i18n->getKey('title.time'));
366 $smarty->assign('content_page_name', 'time.tpl');
367 $smarty->display('index.tpl');