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')) {
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 which 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 // Determine previous and next dates for simple navigation.
79 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
80 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
82 // Use custom fields plugin if it is enabled.
83 if ($user->isPluginEnabled('cf')) {
84 require_once('../plugins/CustomFields.class.php');
85 $custom_fields = new CustomFields();
86 $smarty->assign('custom_fields', $custom_fields);
89 // Initialize variables.
90 $cl_start = trim($request->getParameter('start'));
91 $cl_finish = trim($request->getParameter('finish'));
92 $cl_duration = trim($request->getParameter('duration'));
93 $cl_note = trim($request->getParameter('note'));
95 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
96 $_SESSION['cf_1'] = $cl_cf_1;
98 if ($user->isPluginEnabled('iv')) {
99 if ($request->isPost()) {
100 $cl_billable = $request->getParameter('billable');
101 $_SESSION['billable'] = (int) $cl_billable;
103 if (isset($_SESSION['billable']))
104 $cl_billable = $_SESSION['billable'];
106 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
107 $_SESSION['client'] = $cl_client;
108 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
109 $_SESSION['project'] = $cl_project;
110 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
111 $_SESSION['task'] = $cl_task;
113 // Elements of timeRecordForm.
114 $form = new Form('timeRecordForm');
115 if ($user->can('track_time')) {
116 $rank = $user->getMaxRankForGroup($group_id);
117 if ($user->can('track_own_time'))
118 $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
120 $options = array('group_id'=>$group_id,'status'=>ACTIVE,'max_rank'=>$rank);
121 $user_list = $user->getUsers($options);
122 if (count($user_list) >= 1) {
123 $form->addInput(array('type'=>'combobox',
124 'onchange'=>'document.timeRecordForm.user_changed.value=1;document.timeRecordForm.submit();',
126 'style'=>'width: 250px;',
129 'datakeys'=>array('id','name')));
130 $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
131 $smarty->assign('user_dropdown', 1);
135 // Dropdown for clients in MODE_TIME. Use all active clients.
136 if (MODE_TIME == $user->getTrackingMode() && $user->isPluginEnabled('cl')) {
137 $active_clients = ttGroupHelper::getActiveClients(true);
138 $form->addInput(array('type'=>'combobox',
139 'onchange'=>'fillProjectDropdown(this.value);',
141 'style'=>'width: 250px;',
143 'data'=>$active_clients,
144 'datakeys'=>array('id', 'name'),
145 'empty'=>array(''=>$i18n->get('dropdown.select'))));
146 // Note: in other modes the client list is filtered to relevant clients only. See below.
149 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
150 // Dropdown for projects assigned to user.
151 $project_list = $user->getAssignedProjects();
152 $form->addInput(array('type'=>'combobox',
153 'onchange'=>'fillTaskDropdown(this.value);',
155 'style'=>'width: 250px;',
156 'value'=>$cl_project,
157 'data'=>$project_list,
158 'datakeys'=>array('id','name'),
159 'empty'=>array(''=>$i18n->get('dropdown.select'))));
161 // Dropdown for clients if the clients plugin is enabled.
162 if ($user->isPluginEnabled('cl')) {
163 $active_clients = ttGroupHelper::getActiveClients(true);
164 // We need an array of assigned project ids to do some trimming.
165 foreach($project_list as $project)
166 $projects_assigned_to_user[] = $project['id'];
168 // Build a client list out of active clients. Use only clients that are relevant to user.
169 // Also trim their associated project list to only assigned projects (to user).
170 foreach($active_clients as $client) {
171 $projects_assigned_to_client = explode(',', $client['projects']);
172 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
173 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
175 $client['projects'] = implode(',', $intersection);
176 $client_list[] = $client;
179 $form->addInput(array('type'=>'combobox',
180 'onchange'=>'fillProjectDropdown(this.value);',
182 'style'=>'width: 250px;',
184 'data'=>$client_list,
185 'datakeys'=>array('id', 'name'),
186 'empty'=>array(''=>$i18n->get('dropdown.select'))));
190 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
191 $task_list = ttGroupHelper::getActiveTasks();
192 $form->addInput(array('type'=>'combobox',
194 'style'=>'width: 250px;',
197 'datakeys'=>array('id','name'),
198 'empty'=>array(''=>$i18n->get('dropdown.select'))));
200 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
201 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
202 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
203 if ($user->punch_mode && !$user->canOverridePunchMode()) {
204 // Make the start and finish fields read-only.
205 $form->getElement('start')->setEnabled(false);
206 $form->getElement('finish')->setEnabled(false);
209 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
210 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
211 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 60px;','value'=>$cl_note));
212 if ($user->isPluginEnabled('iv'))
213 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
214 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
215 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
217 // If we have custom fields - add controls for them.
218 if ($custom_fields && $custom_fields->fields[0]) {
219 // Only one custom field is supported at this time.
220 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
221 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
222 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
223 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
224 'style'=>'width: 250px;',
226 'data'=>$custom_fields->options,
227 'empty'=>array(''=>$i18n->get('dropdown.select'))));
232 if ($request->isPost()) {
233 if ($request->getParameter('btn_submit')) {
235 // Validate user input.
236 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
237 $err->add($i18n->get('error.client'));
238 if ($custom_fields) {
239 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
241 if (MODE_PROJECTS == $user->getTrackingMode() || MODE_PROJECTS_AND_TASKS == $user->getTrackingMode()) {
242 if (!$cl_project) $err->add($i18n->get('error.project'));
244 if (MODE_PROJECTS_AND_TASKS == $user->getTrackingMode() && $user->task_required) {
245 if (!$cl_task) $err->add($i18n->get('error.task'));
247 if (strlen($cl_duration) == 0) {
248 if ($cl_start || $cl_finish) {
249 if (!ttTimeHelper::isValidTime($cl_start))
250 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
252 if (!ttTimeHelper::isValidTime($cl_finish))
253 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
254 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
255 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
258 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
259 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
260 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
262 if ((TYPE_DURATION == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType()))
263 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
266 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
267 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
269 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
270 // Finished validating user input.
272 // Prohibit creating entries in future.
273 if (!$user->future_entries) {
274 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
275 if ($selected_date->after($browser_today))
276 $err->add($i18n->get('error.future_date'));
279 // Prohibit creating entries in locked range.
280 if ($user->isDateLocked($selected_date))
281 $err->add($i18n->get('error.range_locked'));
283 // Prohibit creating another uncompleted record.
285 if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
286 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
289 // Prohibit creating an overlapping record.
291 if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
292 $err->add($i18n->get('error.overlap'));
296 $id = ttTimeHelper::insert(array(
298 'user_id' => $user_id,
299 'group_id' => $group_id,
300 'org_id' => $user->org_id,
301 'client' => $cl_client,
302 'project' => $cl_project,
304 'start' => $cl_start,
305 'finish' => $cl_finish,
306 'duration' => $cl_duration,
308 'billable' => $cl_billable));
310 // Insert a custom field if we have it.
312 if ($id && $custom_fields && $cl_cf_1) {
313 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
314 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
315 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
316 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
319 if ($id && $result) {
320 header('Location: time.php');
323 $err->add($i18n->get('error.db'));
328 $smarty->assign('next_date', $next_date);
329 $smarty->assign('prev_date', $prev_date);
330 $smarty->assign('time_records', ttTimeHelper::getRecords($user_id, $cl_date));
331 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($cl_date));
332 $smarty->assign('client_list', $client_list);
333 $smarty->assign('project_list', $project_list);
334 $smarty->assign('task_list', $task_list);
335 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
336 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
337 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
338 $smarty->assign('title', $i18n->get('title.time'));
339 $smarty->assign('content_page_name', 'mobile/time.tpl');
340 $smarty->display('mobile/index.tpl');