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 $userChanged = $request->getParameter('user_changed'); // Reused in multiple places below.
52 if ($userChanged && !($user->can('track_time') && $user->isUserValid($request->getParameter('user')))) {
53 header('Location: access_denied.php'); // Group changed, but no rght or wrong user id.
57 // End of access checks.
59 // Determine user for whom we display this page.
60 if ($request->isPost() && $userChanged) {
61 $user_id = $request->getParameter('user');
62 $user->setOnBehalfUser($user_id);
64 $user_id = $user->getUser();
67 $group_id = $user->getGroup();
69 // Initialize and store date in session.
70 $cl_date = $request->getParameter('date', @$_SESSION['date']);
71 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
72 if($selected_date->isError())
73 $selected_date = new DateAndTime(DB_DATEFORMAT);
75 $cl_date = $selected_date->toString(DB_DATEFORMAT);
76 $_SESSION['date'] = $cl_date;
78 // Use custom fields plugin if it is enabled.
79 if ($user->isPluginEnabled('cf')) {
80 require_once('plugins/CustomFields.class.php');
81 $custom_fields = new CustomFields();
82 $smarty->assign('custom_fields', $custom_fields);
85 if ($user->isPluginEnabled('mq')){
86 require_once('plugins/MonthlyQuota.class.php');
87 $quota = new MonthlyQuota();
88 $month_quota_minutes = $quota->getUserQuota($selected_date->mYear, $selected_date->mMonth);
89 $quota_minutes_from_1st = $quota->getUserQuotaFrom1st($selected_date);
90 $month_total = ttTimeHelper::getTimeForMonth($selected_date);
91 $month_total_minutes = ttTimeHelper::toMinutes($month_total);
92 $balance_left = $quota_minutes_from_1st - $month_total_minutes;
93 $minutes_left = $month_quota_minutes - $month_total_minutes;
95 $smarty->assign('month_total', $month_total);
96 $smarty->assign('month_quota', ttTimeHelper::toAbsDuration($month_quota_minutes));
97 $smarty->assign('over_balance', $balance_left < 0);
98 $smarty->assign('balance_remaining', ttTimeHelper::toAbsDuration($balance_left));
99 $smarty->assign('over_quota', $minutes_left < 0);
100 $smarty->assign('quota_remaining', ttTimeHelper::toAbsDuration($minutes_left));
103 // Initialize variables.
104 $cl_start = trim($request->getParameter('start'));
105 $cl_finish = trim($request->getParameter('finish'));
106 $cl_duration = trim($request->getParameter('duration'));
107 $cl_note = trim($request->getParameter('note'));
109 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
110 $_SESSION['cf_1'] = $cl_cf_1;
112 if ($user->isPluginEnabled('iv')) {
113 if ($request->isPost()) {
114 $cl_billable = $request->getParameter('billable');
115 $_SESSION['billable'] = (int) $cl_billable;
117 if (isset($_SESSION['billable']))
118 $cl_billable = $_SESSION['billable'];
120 //$on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id'])? $_SESSION['behalf_id'] : $user->id));
121 //$on_behalf_group_id = $request->getParameter('onBehalfGroup', (isset($_SESSION['behalf_group_id'])? $_SESSION['behalf_group_id'] : $user->group_id));
122 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
123 $_SESSION['client'] = $cl_client;
124 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
125 $_SESSION['project'] = $cl_project;
126 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
127 $_SESSION['task'] = $cl_task;
129 // Elements of timeRecordForm.
130 $form = new Form('timeRecordForm');
131 if ($user->can('track_time')) {
132 $rank = $user->getMaxRankForGroup($group_id);
133 if ($user->can('track_own_time'))
134 $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
136 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
137 $user_list = $user->getUsers($options);
138 if (count($user_list) >= 1) {
139 $form->addInput(array('type'=>'combobox',
140 'onchange'=>'document.timeRecordForm.user_changed.value=1;document.timeRecordForm.submit();',
142 'style'=>'width: 250px;',
145 'datakeys'=>array('id','name')));
146 $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
147 $smarty->assign('user_dropdown', 1);
151 // Dropdown for clients in MODE_TIME. Use all active clients.
152 if (MODE_TIME == $user->getTrackingMode() && $user->isPluginEnabled('cl')) {
153 $active_clients = ttGroupHelper::getActiveClients(true);
154 $form->addInput(array('type'=>'combobox',
155 'onchange'=>'fillProjectDropdown(this.value);',
157 'style'=>'width: 250px;',
159 'data'=>$active_clients,
160 'datakeys'=>array('id', 'name'),
161 'empty'=>array(''=>$i18n->get('dropdown.select'))));
162 // Note: in other modes the client list is filtered to relevant clients only. See below.
165 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
166 // Dropdown for projects assigned to user.
167 $project_list = $user->getAssignedProjects();
168 $form->addInput(array('type'=>'combobox',
169 'onchange'=>'fillTaskDropdown(this.value);',
171 'style'=>'width: 250px;',
172 'value'=>$cl_project,
173 'data'=>$project_list,
174 'datakeys'=>array('id','name'),
175 'empty'=>array(''=>$i18n->get('dropdown.select'))));
177 // Dropdown for clients if the clients plugin is enabled.
178 if ($user->isPluginEnabled('cl')) {
179 $active_clients = ttGroupHelper::getActiveClients(true);
180 // We need an array of assigned project ids to do some trimming.
181 foreach($project_list as $project)
182 $projects_assigned_to_user[] = $project['id'];
184 // Build a client list out of active clients. Use only clients that are relevant to user.
185 // Also trim their associated project list to only assigned projects (to user).
186 foreach($active_clients as $client) {
187 $projects_assigned_to_client = explode(',', $client['projects']);
188 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
189 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
191 $client['projects'] = implode(',', $intersection);
192 $client_list[] = $client;
195 $form->addInput(array('type'=>'combobox',
196 'onchange'=>'fillProjectDropdown(this.value);',
198 'style'=>'width: 250px;',
200 'data'=>$client_list,
201 'datakeys'=>array('id', 'name'),
202 'empty'=>array(''=>$i18n->get('dropdown.select'))));
206 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
207 $task_list = ttGroupHelper::getActiveTasks();
208 $form->addInput(array('type'=>'combobox',
210 'style'=>'width: 250px;',
213 'datakeys'=>array('id','name'),
214 'empty'=>array(''=>$i18n->get('dropdown.select'))));
217 // Add other controls.
218 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
219 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
220 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
221 if ($user->punch_mode && !$user->canOverridePunchMode()) {
222 // Make the start and finish fields read-only.
223 $form->getElement('start')->setEnabled(false);
224 $form->getElement('finish')->setEnabled(false);
227 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
228 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
229 if (!defined('NOTE_INPUT_HEIGHT'))
230 define('NOTE_INPUT_HEIGHT', 40);
231 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 600px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_note));
232 $form->addInput(array('type'=>'calendar','name'=>'date','value'=>$cl_date)); // calendar
233 if ($user->isPluginEnabled('iv'))
234 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
235 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
236 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
238 // If we have custom fields - add controls for them.
239 if ($custom_fields && $custom_fields->fields[0]) {
240 // Only one custom field is supported at this time.
241 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
242 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
243 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
244 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
245 'style'=>'width: 250px;',
247 'data'=>$custom_fields->options,
248 'empty'=>array(''=>$i18n->get('dropdown.select'))));
253 if ($request->isPost()) {
254 if ($request->getParameter('btn_submit')) {
256 // Validate user input.
257 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
258 $err->add($i18n->get('error.client'));
259 if ($custom_fields) {
260 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
262 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
263 if (!$cl_project) $err->add($i18n->get('error.project'));
265 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode() && $user->task_required) {
266 if (!$cl_task) $err->add($i18n->get('error.task'));
268 if (strlen($cl_duration) == 0) {
269 if ($cl_start || $cl_finish) {
270 if (!ttTimeHelper::isValidTime($cl_start))
271 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
273 if (!ttTimeHelper::isValidTime($cl_finish))
274 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
275 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
276 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
279 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
280 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
281 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
283 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
284 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
287 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
288 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
290 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
291 if (!ttTimeHelper::canAdd()) $err->add($i18n->get('error.expired'));
292 // Finished validating user input.
294 // Prohibit creating entries in future.
295 if (!$user->future_entries) {
296 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
297 if ($selected_date->after($browser_today))
298 $err->add($i18n->get('error.future_date'));
301 // Prohibit creating entries in locked range.
302 if ($user->isDateLocked($selected_date))
303 $err->add($i18n->get('error.range_locked'));
305 // Prohibit creating another uncompleted record.
307 if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
308 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
311 // Prohibit creating an overlapping record.
313 if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
314 $err->add($i18n->get('error.overlap'));
319 $id = ttTimeHelper::insert(array(
321 'user_id' => $user_id,
322 'group_id' => $group_id,
323 'org_id' => $user->org_id,
324 'client' => $cl_client,
325 'project' => $cl_project,
327 'start' => $cl_start,
328 'finish' => $cl_finish,
329 'duration' => $cl_duration,
331 'billable' => $cl_billable));
333 // Insert a custom field if we have it.
335 if ($id && $custom_fields && $cl_cf_1) {
336 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
337 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
338 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
339 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
341 if ($id && $result) {
342 header('Location: time.php');
345 $err->add($i18n->get('error.db'));
347 } elseif ($request->getParameter('btn_stop')) {
348 // Stop button pressed to finish an uncompleted record.
349 $record_id = $request->getParameter('record_id');
350 $record = ttTimeHelper::getRecord($record_id);
351 $browser_date = $request->getParameter('browser_date');
352 $browser_time = $request->getParameter('browser_time');
354 // Can we complete this record?
355 if ($record['date'] == $browser_date // closing today's record
356 && ttTimeHelper::isValidInterval($record['start'], $browser_time) // finish time is greater than start time
357 && !ttTimeHelper::overlaps($user_id, $browser_date, $record['start'], $browser_time)) { // no overlap
358 $res = ttTimeHelper::update(array(
360 'date'=>$record['date'],
362 'client'=>$record['client_id'],
363 'project'=>$record['project_id'],
364 'task'=>$record['task_id'],
365 'start'=>$record['start'],
366 'finish'=>$browser_time,
367 'note'=>$record['comment'],
368 'billable'=>$record['billable']));
370 $err->add($i18n->get('error.db'));
372 // Cannot complete, redirect for manual edit.
373 header('Location: time_edit.php?id='.$record_id);
379 $week_total = ttTimeHelper::getTimeForWeek($selected_date);
381 $smarty->assign('selected_date', $selected_date);
382 $smarty->assign('week_total', $week_total);
383 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($cl_date));
384 $smarty->assign('time_records', ttTimeHelper::getRecords($user_id, $cl_date));
385 $smarty->assign('client_list', $client_list);
386 $smarty->assign('project_list', $project_list);
387 $smarty->assign('task_list', $task_list);
388 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
389 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
390 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
391 $smarty->assign('title', $i18n->get('title.time'));
392 $smarty->assign('content_page_name', 'time.tpl');
393 $smarty->display('index.tpl');