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;
52 // Determine previous and next dates for simple navigation.
53 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
54 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
56 // Use custom fields plugin if it is enabled.
57 if (in_array('cf', explode(',', $user->plugins))) {
58 require_once('../plugins/CustomFields.class.php');
59 $custom_fields = new CustomFields($user->team_id);
60 $smarty->assign('custom_fields', $custom_fields);
63 // Initialize variables.
64 $cl_start = trim($request->getParameter('start'));
65 $cl_finish = trim($request->getParameter('finish'));
66 $cl_duration = trim($request->getParameter('duration'));
67 $cl_note = trim($request->getParameter('note'));
69 $cl_cf_1 = trim($request->getParameter('cf_1', ($request->getMethod()=='POST'? null : @$_SESSION['cf_1'])));
70 $_SESSION['cf_1'] = $cl_cf_1;
72 if (in_array('iv', explode(',', $user->plugins))) {
73 if ($request->getMethod() == 'POST') {
74 $cl_billable = $request->getParameter('billable');
75 $_SESSION['billable'] = (int) $cl_billable;
77 if (isset($_SESSION['billable']))
78 $cl_billable = $_SESSION['billable'];
80 $cl_client = $request->getParameter('client', ($request->getMethod()=='POST'? null : @$_SESSION['client']));
81 $_SESSION['client'] = $cl_client;
82 $cl_project = $request->getParameter('project', ($request->getMethod()=='POST'? null : @$_SESSION['project']));
83 $_SESSION['project'] = $cl_project;
84 $cl_task = $request->getParameter('task', ($request->getMethod()=='POST'? null : @$_SESSION['task']));
85 $_SESSION['task'] = $cl_task;
87 // Elements of timeRecordForm.
88 $form = new Form('timeRecordForm');
90 // Dropdown for clients in MODE_TIME. Use all active clients.
91 if (MODE_TIME == $user->tracking_mode && in_array('cl', explode(',', $user->plugins))) {
92 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
93 $form->addInput(array('type'=>'combobox',
94 'onchange'=>'fillProjectDropdown(this.value);',
96 'style'=>'width: 250px;',
98 'data'=>$active_clients,
99 'datakeys'=>array('id', 'name'),
100 'empty'=>array(''=>$i18n->getKey('dropdown.select'))
102 // Note: in other modes the client list is filtered to relevant clients only. See below.
105 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
106 // Dropdown for projects assigned to user.
107 $project_list = $user->getAssignedProjects();
108 $form->addInput(array('type'=>'combobox',
109 'onchange'=>'fillTaskDropdown(this.value);',
111 'style'=>'width: 250px;',
112 'value'=>$cl_project,
113 'data'=>$project_list,
114 'datakeys'=>array('id','name'),
115 'empty'=>array(''=>$i18n->getKey('dropdown.select'))
118 // Dropdown for clients if the clients plugin is enabled.
119 if (in_array('cl', explode(',', $user->plugins))) {
120 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
121 // We need an array of assigned project ids to do some trimming.
122 foreach($project_list as $project)
123 $projects_assigned_to_user[] = $project['id'];
125 // Build a client list out of active clients. Use only clients that are relevant to user.
126 // Also trim their associated project list to only assigned projects (to user).
127 foreach($active_clients as $client) {
128 $projects_assigned_to_client = explode(',', $client['projects']);
129 if (is_array($projects_assigned_to_client) && is_array($projects_assigned_to_user))
130 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
132 $client['projects'] = implode(',', $intersection);
133 $client_list[] = $client;
136 $form->addInput(array('type'=>'combobox',
137 'onchange'=>'fillProjectDropdown(this.value);',
139 'style'=>'width: 250px;',
141 'data'=>$client_list,
142 'datakeys'=>array('id', 'name'),
143 'empty'=>array(''=>$i18n->getKey('dropdown.select'))
148 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
149 $task_list = ttTeamHelper::getActiveTasks($user->team_id);
150 $form->addInput(array('type'=>'combobox',
152 'style'=>'width: 250px;',
155 'datakeys'=>array('id','name'),
156 'empty'=>array(''=>$i18n->getKey('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');"));
163 if (!$user->canManageTeam() && defined('READONLY_START_FINISH') && isTrue(READONLY_START_FINISH)) {
164 // Make the start and finish fields read-only.
165 $form->getElement('start')->setEnable(false);
166 $form->getElement('finish')->setEnable(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 (in_array('iv', explode(',', $user->plugins)))
172 $form->addInput(array('type'=>'checkbox','name'=>'billable','data'=>1,'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->getKey('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 } else if ($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->getKey('dropdown.select'))
191 // Determine lock date. Time entries earlier than lock date cannot be created or modified.
192 $lock_interval = $user->lock_interval;
194 if ($lock_interval > 0) {
195 $lockdate = new DateAndTime();
196 $lockdate->decDay($lock_interval);
200 if ($request->getMethod() == 'POST') {
201 if ($request->getParameter('btn_submit')) {
203 // Validate user input.
204 if (in_array('cl', explode(',', $user->plugins)) && in_array('cm', explode(',', $user->plugins)) && !$cl_client)
205 $errors->add($i18n->getKey('error.client'));
206 if ($custom_fields) {
207 if (!ttValidString($cl_cf_1, !$custom_fields->fields[0]['required'])) $errors->add($i18n->getKey('error.field'), $custom_fields->fields[0]['label']);
209 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
210 if (!$cl_project) $errors->add($i18n->getKey('error.project'));
212 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
213 if (!$cl_task) $errors->add($i18n->getKey('error.task'));
216 if ('0' == $cl_duration)
217 $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
218 else if ($cl_start || $cl_finish) {
219 if (!ttTimeHelper::isValidTime($cl_start))
220 $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.start'));
222 if (!ttTimeHelper::isValidTime($cl_finish))
223 $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.finish'));
224 if (!ttTimeHelper::isValidInterval($cl_start, $cl_finish))
225 $errors->add($i18n->getKey('error.interval'), $i18n->getKey('label.finish'), $i18n->getKey('label.start'));
228 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
229 $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.start'));
230 $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.finish'));
232 if ((TYPE_DURATION == $user->record_type) || (TYPE_ALL == $user->record_type))
233 $errors->add($i18n->getKey('error.empty'), $i18n->getKey('label.duration'));
236 if (!ttTimeHelper::isValidDuration($cl_duration))
237 $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.duration'));
239 if (!ttValidString($cl_note, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.note'));
240 // Finished validating user input.
242 // Prohibit creating entries in future.
243 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
244 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
245 if ($selected_date->after($browser_today))
246 $errors->add($i18n->getKey('error.future_date'));
249 // Prohibit creating time entries in locked interval.
250 if($lockdate && $selected_date->before($lockdate))
251 $errors->add($i18n->getKey('error.period_locked'));
253 // Prohibit creating another uncompleted record.
254 if ($errors->isEmpty()) {
255 if (($not_completed_rec = ttTimeHelper::getUncompleted($user->getActiveUser())) && (($cl_finish == '') && ($cl_duration == '')))
256 $errors->add($i18n->getKey('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$not_completed_rec['id']."'>".$i18n->getKey('error.goto_uncompleted')."</a>");
259 // Prohibit creating an overlapping record.
260 if ($errors->isEmpty()) {
261 if (ttTimeHelper::overlaps($user->getActiveUser(), $cl_date, $cl_start, $cl_finish))
262 $errors->add($i18n->getKey('error.overlap'));
265 if ($errors->isEmpty()) {
266 $id = ttTimeHelper::insert(array(
268 'user_id' => $user->getActiveUser(),
269 'client' => $cl_client,
270 'project' => $cl_project,
272 'start' => $cl_start,
273 'finish' => $cl_finish,
274 'duration' => $cl_duration,
276 'billable' => $cl_billable));
278 // Insert a custom field if we have it.
280 if ($id && $custom_fields && $cl_cf_1) {
281 if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_TEXT)
282 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], null, $cl_cf_1);
283 else if ($custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN)
284 $result = $custom_fields->insert($id, $custom_fields->fields[0]['id'], $cl_cf_1, null);
287 if ($id && $result) {
288 header('Location: time.php');
291 $errors->add($i18n->getKey('error.db'));
296 $smarty->assign('next_date', $next_date);
297 $smarty->assign('prev_date', $prev_date);
298 $smarty->assign('time_records', ttTimeHelper::getRecords($user->getActiveUser(), $cl_date));
299 $smarty->assign('day_total', ttTimeHelper::getTimeForDay($user->getActiveUser(), $cl_date));
300 $smarty->assign('client_list', $client_list);
301 $smarty->assign('project_list', $project_list);
302 $smarty->assign('task_list', $task_list);
303 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
304 $smarty->assign('onload', 'onLoad="fillDropdowns()"');
305 $smarty->assign('timestring', $selected_date->toString($user->date_format));
306 $smarty->assign('title', $i18n->getKey('title.time'));
307 $smarty->assign('content_page_name', 'mobile/time.tpl');
308 $smarty->display('mobile/index.tpl');