-
Notifications
You must be signed in to change notification settings - Fork 124
/
puncher.php
339 lines (307 loc) · 13.4 KB
/
puncher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/* Copyright (c) Anuko International Ltd. https://www.anuko.com
License: See license.txt */
require_once('initialize.php');
import('form.Form');
import('ttUserHelper');
import('ttGroupHelper');
import('ttClientHelper');
import('ttTimeHelper');
import('ttDate');
// Access check.
if (!ttAccessAllowed('track_own_time')) {
header('Location: access_denied.php');
exit();
}
if (!$user->isPluginEnabled('pu')) {
header('Location: feature_disabled.php');
exit();
}
// If we are passed in browser_today, make sure it is in correct format.
$browser_today = null; // Reused below beyond access checks.
if ($request->isPost()) {
// Validate that browser_today parameter is in correct format.
$browser_today = $request->getParameter('browser_today');
if ($browser_today && !ttValidDbDateFormatDate($browser_today)) {
header('Location: access_denied.php');
exit();
}
}
// If we are passed in browser_time, make sure it is in correct format.
$browser_time = null; // Reused below beyond access checks.
if ($request->isPost()) {
// Validate that browser_time parameter is in correct format.
$browser_time = $request->getParameter('browser_time');
if ($browser_time && !ttValidTime($browser_time)) {
header('Location: access_denied.php');
exit();
}
}
// End of access checks.
$showClient = $user->isPluginEnabled('cl');
$showBillable = $user->isPluginEnabled('iv');
$trackingMode = $user->getTrackingMode();
$showProject = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
$showTask = MODE_PROJECTS_AND_TASKS == $trackingMode;
$taskRequired = false;
if ($showTask) $taskRequired = $user->getConfigOption('task_required');
$oneUncompleted = $user->getConfigOption('one_uncompleted');
// Initialize $cl_date.
$date_today = new ttDate($browser_today); // Initialize to browser today if we are passed it in, otherwise server today.
$cl_date = $date_today->toString();
// Use custom fields plugin if it is enabled.
if ($user->isPluginEnabled('cf')) {
require_once('plugins/CustomFields.class.php');
$custom_fields = new CustomFields();
$smarty->assign('custom_fields', $custom_fields);
}
// Obtain first uncompleted record for today. If there are multiples, we operate with first found.
$uncompletedToday = ttTimeHelper::getFirstUncompletedForDate($user->getUser(), $date_today->toString());
$enable_controls = ($uncompletedToday == null);
// Obtain first found uncompleted record, not necessarily for today. Used to prohibit entry in "One uncompleted" mode.
$uncompleted = ttTimeHelper::getUncompleted($user->getUser());
// Initialize variables.
$cl_start = $browser_time;
$cl_finish = $browser_time;
$cl_duration = $cl_note = null;
// Disabled controls are not posted. Therefore, && $enable_controls condition in several places below.
// This allows us to get values from session when controls are disabled and reset to null when not.
$cl_billable = 1;
if ($user->isPluginEnabled('iv')) {
$cl_billable = $request->getParameter('billable', ($request->isPost() && $enable_controls ? null : @$_SESSION['billable']));
$_SESSION['billable'] = $cl_billable;
}
$cl_client = $request->getParameter('client', ($request->isPost() && $enable_controls ? null : @$_SESSION['client']));
$_SESSION['client'] = $cl_client;
$cl_project = $request->getParameter('project', ($request->isPost() && $enable_controls ? null : @$_SESSION['project']));
$_SESSION['project'] = $cl_project;
$cl_task = $request->getParameter('task', ($request->isPost() && $enable_controls ? null : @$_SESSION['task']));
$_SESSION['task'] = $cl_task;
// Handle time custom fields.
$timeCustomFields = array();
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$control_name = 'time_field_'.$timeField['id'];
$cl_control_name = $request->getParameter($control_name, ($request->isPost() && $enable_controls ? null : @$_SESSION[$control_name]));
$_SESSION[$control_name] = $cl_control_name;
$timeCustomFields[$timeField['id']] = array('field_id' => $timeField['id'],
'control_name' => $control_name,
'label' => $timeField['label'],
'type' => $timeField['type'],
'required' => $timeField['required'],
'value' => trim($cl_control_name));
}
}
// Elements of timeRecordForm.
$form = new Form('timeRecordForm');
// Dropdown for clients in MODE_TIME. Use all active clients.
if (MODE_TIME == $trackingMode && $showClient) {
$active_clients = ttGroupHelper::getActiveClients(true);
$form->addInput(array('type'=>'combobox',
'onchange'=>'fillProjectDropdown(this.value);',
'name'=>'client',
'enable'=>$enable_controls,
'value'=>$cl_client,
'data'=>$active_clients,
'datakeys'=>array('id', 'name'),
'empty'=>array(''=>$i18n->get('dropdown.select'))));
// Note: in other modes the client list is filtered to relevant clients only. See below.
}
// Billable checkbox.
if ($showBillable) {
$form->addInput(array('type'=>'checkbox','name'=>'billable','value'=>$cl_billable,'enable'=>$enable_controls));
}
// If we have time custom fields - add controls for them.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($custom_fields->timeFields as $timeField) {
$field_name = 'time_field_'.$timeField['id'];
if ($timeField['type'] == CustomFields::TYPE_TEXT) {
$form->addInput(array('type'=>'text',
'name'=>$field_name,
'enable'=>$enable_controls,
'value'=>$timeCustomFields[$timeField['id']]['value']));
} elseif ($timeField['type'] == CustomFields::TYPE_DROPDOWN) {
$form->addInput(array('type'=>'combobox','name'=>$field_name,
'data'=>CustomFields::getOptions($timeField['id']),
'value'=>$timeCustomFields[$timeField['id']]['value'],
'enable'=>$enable_controls,
'empty'=>array(''=>$i18n->get('dropdown.select'))));
}
}
}
// If we show project dropdown, add controls for project and client.
$project_list = $client_list = array();
if ($showProject) {
// Dropdown for projects assigned to user.
$project_list = $user->getAssignedProjects();
$form->addInput(array('type'=>'combobox',
'onchange'=>'fillTaskDropdown(this.value);',
'name'=>'project',
'enable'=>$enable_controls,
'value'=>$cl_project,
'data'=>$project_list,
'datakeys'=>array('id','name'),
'empty'=>array(''=>$i18n->get('dropdown.select'))));
// Client dropdown.
if ($showClient) {
$active_clients = ttGroupHelper::getActiveClients(true);
// We need an array of assigned project ids to do some trimming.
foreach($project_list as $project)
$projects_assigned_to_user[] = $project['id'];
// Build a client list out of active clients. Use only clients that are relevant to user.
// Also trim their associated project list to only assigned projects (to user).
foreach($active_clients as $client) {
$projects_assigned_to_client = explode(',', $client['projects']);
$intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
if ($intersection) {
$client['projects'] = implode(',', $intersection);
$client_list[] = $client;
}
}
$form->addInput(array('type'=>'combobox',
'onchange'=>'fillProjectDropdown(this.value);',
'name'=>'client',
'enable'=>$enable_controls,
'value'=>$cl_client,
'data'=>$client_list,
'datakeys'=>array('id', 'name'),
'empty'=>array(''=>$i18n->get('dropdown.select'))));
}
}
// Task dropdown.
$task_list = array();
if ($showTask) {
$task_list = ttGroupHelper::getActiveTasks();
$form->addInput(array('type'=>'combobox',
'name'=>'task',
'enable'=>$enable_controls,
'value'=>$cl_task,
'data'=>$task_list,
'datakeys'=>array('id','name'),
'empty'=>array(''=>$i18n->get('dropdown.select'))));
}
// A hidden control for today's date from user's browser.
$form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on button click.
// A hidden control for current time from user's browser.
$form->addInput(array('type'=>'hidden','name'=>'browser_time','value'=>'')); // User current time, which gets filled in on button click.
// Start and stop buttons.
$enable_start = $uncompletedToday ? false : true;
if (!$uncompletedToday)
$form->addInput(array('type'=>'submit','name'=>'btn_start','onclick'=>'browser_today.value=get_date();browser_time.value=get_time()','value'=>$i18n->get('button.start'),'enable'=>$enable_start));
else
$form->addInput(array('type'=>'submit','name'=>'btn_stop','onclick'=>'browser_today.value=get_date();browser_time.value=get_time()','value'=>$i18n->get('button.stop'),'enable'=>!$enable_start));
// Submit.
if ($request->isPost()) {
if ($request->getParameter('btn_start')) {
// Start button clicked. We need to create a new uncompleted record with only the start time.
$cl_finish = null;
// Validate user input.
if ($showClient && $user->isOptionEnabled('client_required') && !$cl_client)
$err->add($i18n->get('error.client'));
// Validate input in time custom fields.
if (isset($custom_fields) && $custom_fields->timeFields) {
foreach ($timeCustomFields as $timeField) {
// Validation is the same for text and dropdown fields.
if (!ttValidString($timeField['value'], !$timeField['required'])) $err->add($i18n->get('error.field'), htmlspecialchars($timeField['label']));
}
}
if ($showProject) {
if (!$cl_project) $err->add($i18n->get('error.project'));
}
if ($showTask && $taskRequired) {
if (!$cl_task) $err->add($i18n->get('error.task'));
}
// Finished validating user input.
// Prohibit creating entries in future.
if (!$user->isOptionEnabled('future_entries')) {
// $date_today is already browser_today, just check if the date we are using is after server tomorrow.
$server_tomorrow = new ttDate();
$server_tomorrow->incrementDay();
if ($date_today->after($server_tomorrow))
$err->add($i18n->get('error.future_date'));
}
// Prohibit creating time entries in locked interval.
if ($user->isDateLocked($date_today))
$err->add($i18n->get('error.range_locked'));
// Prohibit creating another uncompleted record.
if ($err->no() && $uncompleted && $oneUncompleted) {
$err->add($i18n->get('error.uncompleted_exists')." <a href = 'time_edit.php?id=".$uncompleted['id']."'>".$i18n->get('error.goto_uncompleted')."</a>");
}
// Prohibit creating an overlapping record.
if ($err->no()) {
if (ttTimeHelper::overlaps($user->getUser(), $cl_date, $cl_start, $cl_finish))
$err->add($i18n->get('error.overlap'));
}
if ($err->no()) {
$id = ttTimeHelper::insert(array(
'date' => $cl_date,
'client' => $cl_client,
'project' => $cl_project,
'task' => $cl_task,
'start' => $cl_start,
'finish' => $cl_finish,
'duration' => $cl_duration,
'note' => $cl_note,
'billable' => $cl_billable));
// Insert time custom fields if we have them.
$result = true;
if ($id && isset($custom_fields) && $custom_fields->timeFields) {
$result = $custom_fields->insertTimeFields($id, $timeCustomFields);
}
if ($id && $result) {
header('Location: puncher.php');
exit();
}
$err->add($i18n->get('error.db'));
}
}
if ($request->getParameter('btn_stop')) {
// Stop button clicked. We need to finish an uncompleted record in progress.
$record = ttTimeHelper::getRecord($uncompletedToday['id']);
// Can we complete this record?
if (ttTimeHelper::isValidInterval($record['start'], $cl_finish) // finish time is greater than start time
&& !ttTimeHelper::overlaps($user->getUser(), $cl_date, $record['start'], $cl_finish)) { // no overlap
$res = ttTimeHelper::update(array(
'id'=>$record['id'],
'date'=>$cl_date,
'client'=>$record['client_id'],
'project'=>$record['project_id'],
'task'=>$record['task_id'],
'start'=>$record['start'],
'finish'=>$cl_finish,
'note'=>$record['comment'],
'billable'=>$record['billable']));
if ($res) {
header('Location: puncher.php');
exit();
} else
$err->add($i18n->get('error.db'));
} else {
// Cannot complete, redirect for manual edit.
header('Location: time_edit.php?id='.$record['id']);
exit();
}
}
} // isPost
$week_total = ttTimeHelper::getTimeForWeek($date_today);
$timeRecords = ttTimeHelper::getRecords($cl_date);
$smarty->assign('week_total', $week_total);
$smarty->assign('uncompleted_today', $uncompletedToday);
$smarty->assign('show_client', $showClient);
$smarty->assign('show_billable', $showBillable);
$smarty->assign('show_project', $showProject);
$smarty->assign('show_task', $showTask);
$smarty->assign('task_required', $taskRequired);
$smarty->assign('day_total', ttTimeHelper::getTimeForDay($cl_date));
$smarty->assign('time_records', $timeRecords);
$smarty->assign('show_record_custom_fields', $user->isOptionEnabled('record_custom_fields'));
$smarty->assign('show_start', true);
$smarty->assign('client_list', $client_list);
$smarty->assign('project_list', $project_list);
$smarty->assign('task_list', $task_list);
$smarty->assign('forms', array($form->getName()=>$form->toArray()));
$smarty->assign('onload', 'onLoad="fillDropdowns()"');
$smarty->assign('timestring', $date_today->toString($user->getDateFormat()));
$smarty->assign('title', $i18n->get('title.puncher'));
$smarty->assign('content_page_name', 'puncher.tpl');
$smarty->display('index.tpl');