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('ttGroupHelper');
34 import('ttClientHelper');
35 import('ttTimeHelper');
36 import('DateAndTime');
39 if (!ttAccessAllowed('track_own_time')) {
40 header('Location: access_denied.php');
43 // End of access checks.
45 $user_id = $user->getUser();
47 // Initialize and store date in session.
48 $cl_date = $request->getParameter('date', @$_SESSION['date']);
49 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
50 if($selected_date->isError())
51 $selected_date = new DateAndTime(DB_DATEFORMAT);
53 $cl_date = $selected_date->toString(DB_DATEFORMAT);
54 $_SESSION['date'] = $cl_date;
56 // Determine previous and next dates for simple navigation.
57 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
58 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
60 // Use custom fields plugin if it is enabled.
61 if ($user->isPluginEnabled('cf')) {
62 require_once('../plugins/CustomFields.class.php');
63 $custom_fields = new CustomFields($user->group_id);
64 $smarty->assign('custom_fields', $custom_fields);
67 // Initialize variables.
68 $cl_start = trim($request->getParameter('start'));
69 $cl_finish = trim($request->getParameter('finish'));
70 $cl_duration = trim($request->getParameter('duration'));
71 $cl_note = trim($request->getParameter('note'));
73 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->isPost() ? null : @$_SESSION['cf_1'])));
74 $_SESSION['cf_1'] = $cl_cf_1;
76 if ($user->isPluginEnabled('iv')) {
77 if ($request->isPost()) {
78 $cl_billable = $request->getParameter('billable');
79 $_SESSION['billable'] = (int) $cl_billable;
81 if (isset($_SESSION['billable']))
82 $cl_billable = $_SESSION['billable'];
84 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
85 $_SESSION['client'] = $cl_client;
86 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
87 $_SESSION['project'] = $cl_project;
88 $cl_task = $request->getParameter('task', ($request->isPost() ? null : @$_SESSION['task']));
89 $_SESSION['task'] = $cl_task;
91 // Elements of timeRecordForm.
92 $form = new Form('timeRecordForm');
94 // Dropdown for clients in MODE_TIME. Use all active clients.
95 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
96 $active_clients = ttGroupHelper::getActiveClients(true);
97 $form->addInput(array('type'=>'combobox',
98 'onchange'=>'fillProjectDropdown(this.value);',
100 'style'=>'width: 250px;',
102 'data'=>$active_clients,
103 'datakeys'=>array('id', 'name'),
104 'empty'=>array(''=>$i18n->get('dropdown.select'))));
105 // Note: in other modes the client list is filtered to relevant clients only. See below.
108 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
109 // Dropdown for projects assigned to user.
110 $project_list = $user->getAssignedProjects();
111 $form->addInput(array('type'=>'combobox',
112 'onchange'=>'fillTaskDropdown(this.value);',
114 'style'=>'width: 250px;',
115 'value'=>$cl_project,
116 'data'=>$project_list,
117 'datakeys'=>array('id','name'),
118 'empty'=>array(''=>$i18n->get('dropdown.select'))));
120 // Dropdown for clients if the clients plugin is enabled.
121 if ($user->isPluginEnabled('cl')) {
122 $active_clients = ttGroupHelper::getActiveClients(true);
123 // We need an array of assigned project ids to do some trimming.
124 foreach($project_list as $project)
125 $projects_assigned_to_user[] = $project['id'];
127 // Build a client list out of active clients. Use only clients that are relevant to user.
128 // Also trim their associated project list to only assigned projects (to user).
129 foreach($active_clients as $client) {
130 $projects_assigned_to_client = explode(',', $client['projects']);
131 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
132 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
134 $client['projects'] = implode(',', $intersection);
135 $client_list[] = $client;
138 $form->addInput(array('type'=>'combobox',
139 'onchange'=>'fillProjectDropdown(this.value);',
141 'style'=>'width: 250px;',
143 'data'=>$client_list,
144 'datakeys'=>array('id', 'name'),
145 'empty'=>array(''=>$i18n->get('dropdown.select'))));
149 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
150 $task_list = ttTeamHelper::getActiveTasks($user->group_id);
151 $form->addInput(array('type'=>'combobox',
153 'style'=>'width: 250px;',
156 'datakeys'=>array('id','name'),
157 'empty'=>array(''=>$i18n->get('dropdown.select'))));
159 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
160 $form->addInput(array('type'=>'text','name'=>'start','value'=>$cl_start,'onchange'=>"formDisable('start');"));
161 $form->addInput(array('type'=>'text','name'=>'finish','value'=>$cl_finish,'onchange'=>"formDisable('finish');"));
162 if ($user->punch_mode && !$user->canOverridePunchMode()) {
163 // Make the start and finish fields read-only.
164 $form->getElement('start')->setEnabled(false);
165 $form->getElement('finish')->setEnabled(false);
168 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
169 $form->addInput(array('type'=>'text','name'=>'duration','value'=>$cl_duration,'onchange'=>"formDisable('duration');"));
170 $form->addInput(array('type'=>'textarea','name'=>'note','style'=>'width: 250px; height: 60px;','value'=>$cl_note));
171 if ($user->isPluginEnabled('iv'))
172 $form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable));
173 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
174 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
176 // If we have custom fields - add controls for them.
177 if ($custom_fields && $custom_fields->fields[0]) {
178 // Only one custom field is supported at this time.
179 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT) {
180 $form->addInput(array('type'=>'text','name'=>'cf_1','value'=>$cl_cf_1));
181 } elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
182 $form->addInput(array('type'=>'combobox','name'=>'cf_1',
183 'style'=>'width: 250px;',
185 'data'=>$custom_fields->options,
186 'empty'=>array(''=>$i18n->get('dropdown.select'))));
191 if ($request->isPost()) {
192 if ($request->getParameter('btn_submit')) {
194 // Validate user input.
195 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
196 $err->add($i18n->get('error.client'));
197 if ($custom_fields) {
198 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $err->add($i18n->get('error.field'), $custom_fields->fields[0]['label']);
200 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
201 if (!$cl_project) $err->add($i18n->get('error.project'));
203 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode && $user->task_required) {
204 if (!$cl_task) $err->add($i18n->get('error.task'));
206 if (strlen($cl_duration) == 0) {
207 if ($cl_start || $cl_finish) {
208 if (!ttTimeHelper::isValidTime($cl_start))
209 $err->add($i18n->get('error.field'), $i18n->get('label.start'));
211 if (!ttTimeHelper::isValidTime($cl_finish))
212 $err->add($i18n->get('error.field'), $i18n->get('label.finish'));
213 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
214 $err->add($i18n->get('error.interval'), $i18n->get('label.finish'), $i18n->get('label.start'));
217 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
218 $err->add($i18n->get('error.empty'), $i18n->get('label.start'));
219 $err->add($i18n->get('error.empty'), $i18n->get('label.finish'));
221 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
222 $err->add($i18n->get('error.empty'), $i18n->get('label.duration'));
225 if (false === ttTimeHelper::postedDurationToMinutes($cl_duration))
226 $err->add($i18n->get('error.field'), $i18n->get('label.duration'));
228 if (!ttValidString($cl_note, true)) $err->add($i18n->get('error.field'), $i18n->get('label.note'));
229 // Finished validating user input.
231 // Prohibit creating entries in future.
232 if (!$user->future_entries) {
233 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
234 if ($selected_date->after($browser_today))
235 $err->add($i18n->get('error.future_date'));
238 // Prohibit creating entries in locked range.
239 if ($user->isDateLocked($selected_date))
240 $err->add($i18n->get('error.range_locked'));
242 // Prohibit creating another uncompleted record.
244 if (($not_completed_rec = ttTimeHelper::getUncompleted($user_id)) && (($cl_finish == '') && ($cl_duration == '')))
245 $err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
248 // Prohibit creating an overlapping record.
250 if (ttTimeHelper::overlaps($user_id, $cl_date, $cl_start, $cl_finish))
251 $err->add($i18n->get('error.overlap'));
255 $id = ttTimeHelper::insert(array(
257 'user_id' => $user_id,
258 'group_id' => $user->getGroup(),
259 'org_id' => $user->org_id,
260 'client' => $cl_client,
261 'project' => $cl_project,
263 'start' => $cl_start,
264 'finish' => $cl_finish,
265 'duration' => $cl_duration,
267 'billable' => $cl_billable));
269 // Insert a custom field if we have it.
271 if ($id && $custom_fields && $cl_cf_1) {
272 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
273 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
274 elseif ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
275 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
278 if ($id && $result) {
279 header('Location: time.php');
282 $err->add($i18n->get('error.db'));
287 $smarty->assign('next_date', $next_date);
288 $smarty->assign('prev_date', $prev_date);
289 $smarty->assign('time_records', ttTimeHelper::getRecords($user_id, $cl_date));
290 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user_id, $cl_date));
291 $smarty->assign('client_list', $client_list);
292 $smarty->assign('project_list', $project_list);
293 $smarty->assign('task_list', $task_list);
294 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
295 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
296 $smarty->assign('timestring', $selected_date->toString($user->date_format));
297 $smarty->assign('title', $i18n->get('title.time'));
298 $smarty->assign('content_page_name', 'mobile/time.tpl');
299 $smarty->display('mobile/index.tpl');