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');
38 if (!ttAccessCheck(right_data_entry)) {
39 header('Location: access_denied.php');
43 // Initialize and store date in session.
44 $cl_date = $request->getParameter('date', @$_SESSION['date']);
45 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
46 if($selected_date->isError())
47 $selected_date = new DateAndTime(DB_DATEFORMAT);
49 $cl_date = $selected_date->toString(DB_DATEFORMAT);
50 $_SESSION['date'] = $cl_date;
51 // TODO: for time page we may limit the day to today only.
53 // Use custom fields plugin if it is enabled.
54 if (in_array('cf', explode(',', $user->plugins))) {
55 require_once('../plugins/CustomFields.class.php');
56 $custom_fields = new CustomFields($user->team_id);
57 $smarty->assign('custom_fields', $custom_fields);
60 // Initialize variables.
61 $cl_start = trim($request->getParameter('browser_time'));
62 $cl_finish = trim($request->getParameter('browser_time'));
64 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->getMethod()=='POST'? null : @$_SESSION['cf_1'])));
65 $_SESSION['cf_1'] = $cl_cf_1;
67 if (in_array('iv', explode(',', $user->plugins))) {
68 if ($request->getMethod() == 'POST') {
69 $cl_billable = $request->getParameter('billable');
70 $_SESSION['billable'] = (int) $cl_billable;
72 if (isset($_SESSION['billable']))
73 $cl_billable = $_SESSION['billable'];
75 $cl_client = $request->getParameter('client', @$_SESSION['client']);
76 $_SESSION['client'] = $cl_client;
77 $cl_project = $request->getParameter('project', @$_SESSION['project']);
78 $_SESSION['project'] = $cl_project;
79 $cl_task = $request->getParameter('task', @$_SESSION['task']);
80 $_SESSION['task'] = $cl_task;
82 // Obtain uncompleted record. Assumtion is that only 1 uncompleted record is allowed.
83 $uncompleted = ttTimeHelper::getUncompleted($user->getActiveUser());
84 $enable_controls = ($uncompleted == null);
86 // Elements of timeRecordForm.
87 $form = new Form('timerRecordForm');
89 // Dropdown for clients in MODE_TIME. Use all active clients.
90 if (MODE_TIME == $user->tracking_mode && in_array('cl', explode(',', $user->plugins))) {
91 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
92 $form->addInput(array('type'=>'combobox',
93 'onchange'=>'fillProjectDropdown(this.value);',
95 'style'=>'width: 250px;',
96 'enable'=>$enable_controls,
98 'data'=>$active_clients,
99 'datakeys'=>array('id', 'name'),
100 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
101 // Note: in other modes the client list is filtered to relevant clients only. See below.
104 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
105 // Dropdown for projects assigned to user.
106 $project_list = $user->getAssignedProjects();
107 $form->addInput(array('type'=>'combobox',
108 'onchange'=>'fillTaskDropdown(this.value);',
110 'style'=>'width: 250px;',
111 'enable'=>$enable_controls,
112 'value'=>$cl_project,
113 'data'=>$project_list,
114 'datakeys'=>array('id','name'),
115 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
117 // Dropdown for clients if the clients plugin is enabled.
118 if (in_array('cl', explode(',', $user->plugins))) {
119 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
120 // We need an array of assigned project ids to do some trimming.
121 foreach($project_list as $project)
122 $projects_assigned_to_user[] = $project['id'];
124 // Build a client list out of active clients. Use only clients that are relevant to user.
125 // Also trim their associated project list to only assigned projects (to user).
126 foreach($active_clients as $client) {
127 $projects_assigned_to_client = explode(',', $client['projects']);
128 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
130 $client['projects'] = implode(',', $intersection);
131 $client_list[] = $client;
134 $form->addInput(array('type'=>'combobox',
135 'onchange'=>'fillProjectDropdown(this.value);',
137 'style'=>'width: 250px;',
138 'enable'=>$enable_controls,
140 'data'=>$client_list,
141 'datakeys'=>array('id', 'name'),
142 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
146 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
147 $task_list = ttTeamHelper::getActiveTasks($user->team_id);
148 $form->addInput(array('type'=>'combobox',
150 'style'=>'width: 250px;',
151 'enable'=>$enable_controls,
154 'datakeys'=>array('id','name'),
155 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
157 if (in_array('iv', explode(',', $user->plugins)))
158 $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'value'=>$cl_billable,'enable'=>$enable_controls));
159 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on button click.
160 $form->addInput(array('type'=>'hidden','name'=>'browser_time','value'=>'')); // User current time, which gets filled in on button click.
161 $enable_start = $uncompleted ? false : true;
163 $form->addInput(array('type'=>'submit','name'=>'btn_start','onclick'=>'browser_time.value=get_time()','value'=>$i18n->getKey('label.start'),'enable'=>$enable_start));
165 $form->addInput(array('type'=>'submit','name'=>'btn_stop','onclick'=>'browser_time.value=get_time()','value'=>$i18n->getKey('label.finish'),'enable'=>!$enable_start));
167 // If we have custom fields - add controls for them.
168 if ($custom_fields && $custom_fields->fields[0]) {
169 // Only one custom field is supported at this time.
170 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
171 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
172 } else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
173 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
174 'style'=>'width: 250px;',
176 'data'=>$custom_fields->options,
177 'empty'=>array(''=>$i18n->getKey('dropdown.select'))
182 // Determine lock date. Time entries earlier than lock date cannot be created or modified.
183 $lock_interval = $user->lock_interval;
185 if ($lock_interval > 0) {
186 $lockdate = new DateAndTime();
187 $lockdate->decDay($lock_interval);
191 if ($request->getMethod() == 'POST') {
192 if ($request->getParameter('btn_start')) {
193 // Start button clicked. We need to create a new uncompleted record with only the start time.
196 // Validate user input.
197 if (in_array('cl', explode(',', $user->plugins)) && in_array('cm', explode(',', $user->plugins)) && !$cl_client)
198 $errors->add($i18n->getKey('error.client'));
199 if ($custom_fields) {
200 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $errors->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
202 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
203 if (!$cl_project) $errors->add($i18n->getKey('error.project'));
205 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
206 if (!$cl_task) $errors->add($i18n->getKey('error.task'));
208 // Finished validating user input.
210 // Prohibit creating entries in future.
211 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
212 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
213 if ($selected_date->after($browser_today))
214 $errors->add($i18n->getKey('error.future_date'));
217 // Prohibit creating time entries in locked interval.
218 if($lockdate && $selected_date->before($lockdate))
219 $errors->add($i18n->getKey('error.period_locked'));
221 // Prohibit creating another uncompleted record.
222 if ($errors->no() && $uncompleted) {
223 $errors->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
226 // Prohibit creating an overlapping record.
228 if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
229 $errors->add($i18n->getKey('error.overlap'));
233 $id = ttTimeHelper::insert(array(
235 'user_id' => $user->getActiveUser(),
236 'client' => $cl_client,
237 'project' => $cl_project,
239 'start' => $cl_start,
240 'finish' => $cl_finish,
241 'duration' => $cl_duration,
243 'billable' => $cl_billable));
245 // Insert a custom field if we have it.
247 if ($id && $custom_fields && $cl_cf_1) {
248 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
249 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
250 else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
251 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
254 if ($id && $result) {
255 header('Location: timer.php');
258 $errors->add($i18n->getKey('error.db'));
261 if ($request->getParameter('btn_stop')) {
262 // Stop button clicked. We need to finish an uncompleted record in progress.
263 $record = ttTimeHelper::getRecord($uncompleted['id'], $user->getActiveUser());
265 // Can we complete this record?
266 if (ttTimeHelper::isValidInterval($record['start'], $cl_finish) // finish time is greater than start time
267 && !ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $record['start'], $cl_finish)) { // no overlap
268 $res = ttTimeHelper::update(array(
271 'user_id'=>$user->getActiveUser(),
272 'client'=>$record['client_id'],
273 'project'=>$record['project_id'],
274 'task'=>$record['task_id'],
275 'start'=>$record['start'],
276 'finish'=>$cl_finish,
277 'note'=>$record['comment'],
278 'billable'=>$record['billable']));
280 header('Location: timer.php');
283 $errors->add($i18n->getKey('error.db'));
285 // Cannot complete, redirect for manual edit.
286 header('Location: time_edit.php?id='.$record['id']);
292 $week_total = ttTimeHelper::getTimeForWeek($user->getActiveUser(), $cl_date);
293 $smarty->assign('week_total', $week_total);
295 $smarty->assign('uncompleted', $uncompleted);
299 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
300 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
301 $smarty->assign('client_list', $client_list);
302 $smarty->assign('project_list', $project_list);
303 $smarty->assign('task_list', $task_list);
304 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
305 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
306 $smarty->assign('timestring', $selected_date->toString($user->date_format));
307 $smarty->assign('title', $i18n->getKey('title.time'));
308 $smarty->assign('content_page_name', 'mobile/timer.tpl');
309 $smarty->display('mobile/index.tpl');