/
action.php
265 lines (232 loc) · 10.6 KB
/
action.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
<?
// $Header: /cvsroot/tsheet/timesheet.php/action.php,v 1.9 2005/05/10 11:42:52 vexil Exp $
// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
if (!$authenticationManager->isLoggedIn()) {
Header("Location: login.php?redirect=$_SERVER[PHP_SELF]");
exit;
}
// Connect to database.
$dbh = dbConnect();
$contextUser = strtolower($_SESSION['contextUser']);
//load local vars from superglobals
$month = $_REQUEST['month'];
$day = $_REQUEST['day'];
$year = $_REQUEST['year'];
$client_id = $_REQUEST['client_id'];
$proj_id = $_REQUEST['proj_id'];
$task_id = $_REQUEST['task_id'];
$origin = $_REQUEST["origin"];
$destination = $_REQUEST["destination"];
$fromPopupWindow = isset($_REQUEST['fromPopupWindow']) ? $_REQUEST['fromPopupWindow']: false;
$clockonoff = isset($_REQUEST['clockonoff']) ? $_REQUEST['clockonoff']: "";
$clock_on_time_hour = isset($_REQUEST['clock_on_time_hour']) ? $_REQUEST['clock_on_time_hour']: 0;
$clock_on_time_min = isset($_REQUEST['clock_on_time_min']) ? $_REQUEST['clock_on_time_min']: 0;
$clock_off_time_hour = isset($_REQUEST['clock_off_time_hour']) ? $_REQUEST['clock_off_time_hour']: 0;
$clock_off_time_min = isset($_REQUEST['clock_off_time_min']) ? $_REQUEST['clock_off_time_min']: 0;
$log_message = isset($_REQUEST['log_message']) ? $_REQUEST['log_message']: "";
$log_message_presented = isset($_REQUEST['log_message_presented']) ? $_REQUEST['log_message_presented']: false;
$clock_on_check = isset($_REQUEST['clock_on_check']) ? $_REQUEST['clock_on_check']: "";
$clock_off_check = isset($_REQUEST['clock_off_check']) ? $_REQUEST['clock_off_check']: "";
$clock_on_radio = isset($_REQUEST['clock_on_radio']) ? $_REQUEST['clock_on_radio']: "";
$clock_off_radio = isset($_REQUEST['clock_off_radio']) ? $_REQUEST['clock_off_radio']: "";
//set the return location
$Location = "$destination?month=$month&year=$year&day=$day&destination=$destination";
if ($destination == "stopwatch.php" || $destination == "daily.php")
$Location = "$destination?client_id=$client_id&proj_id=$proj_id&task_id=$task_id&month=$month&year=$year&day=$day&destination=$destination";
//determine the action
if (empty($clockonoff)) {
if (!empty($clock_on_check) && !empty($clock_off_check))
$clockonoff = "clockonandoff";
else if (!empty($clock_on_check)) {
if ($clock_on_radio == "now")
$clockonoff = "clockonnow";
else
$clockonoff = "clockonat";
}
else if (!empty($clock_off_check)) {
if ($clock_off_radio == "now")
$clockonoff = "clockoffnow";
else
$clockonoff = "clockoffat";
}
else
errorPage("You must select at least one checkbox to indicate your action: clock on, clock off, or both.", $fromPopupWindow);
}
//call appropriate functions
if ($clockonoff == "clockonandoff")
clockonandoff();
else if ($clockonoff == "clockonat") {
$timeString = "$year-$month-$day $clock_on_time_hour:$clock_on_time_min:00";
clockon($timeString);
}
else if ($clockonoff == "clockoffat") {
$timeString = "$year-$month-$day $clock_off_time_hour:$clock_off_time_min:00";
clockoff($timeString);
}
else if ($clockonoff == "clockonnow") {
//if we're coming from the popup window then set the return location to the origin
if ($fromPopupWindow)
//set the return location
$Location = "$origin?client_id=$client_id&proj_id=$proj_id&task_id=$task_id&month=$month&year=$year&day=$day&destination=$destination";
$timeString = date("Y-m-d H:i:00");
clockon($timeString);
}
else if ($clockonoff == "clockoffnow") {
$timeString = date("Y-m-d H:i:00");
clockoff($timeString);
}
else
errorPage("Could not determine the clock on/off action. Please report this as a bug", $fromPopupWindow);
//redirects to a page where the user can enter the log message. Then returns here.
function getLogMessage() {
//import global vars
global $contextUser, $year, $month, $day, $task_id, $proj_id, $client_id, $Location;
global $origin, $destination, $clock_on_time_hour, $clock_off_time_hour,
$clock_on_time_min, $clock_off_time_min, $clockonoff;
global $log_message, $log_message_presented, $fromPopupWindow;
if ($log_message_presented == false) {
$targetWindowLocation = "log_message.php".
"?origin=$origin&destination=$destination".
"&clock_on_time_hour=$clock_on_time_hour".
"&clock_off_time_hour=$clock_off_time_hour".
"&clock_on_time_min=$clock_on_time_min".
"&clock_off_time_min=$clock_off_time_min".
"&year=$year".
"&month=$month".
"&day=$day".
"&client_id=$client_id".
"&proj_id=$proj_id".
"&task_id=$task_id".
"&clockonoff=$clockonoff";
if ($fromPopupWindow) {
//close this popup window and load the log message page in the main window.
loadMainPageAndCloseWindow($targetWindowLocation);
}
else {
Header("Location: $targetWindowLocation");
exit;
}
}
}
function clockon($timeString) {
include("table_names.inc");
//import global vars
global $contextUser, $task_id, $proj_id, $Location, $fromPopupWindow;
if (empty($Location))
errorPage("failed sanity check, location empty");
//check that we are not already clocked on
$querystring = "SELECT $TIMES_TABLE.start_time, $TASK_TABLE.name FROM ".
"$TIMES_TABLE, $TASK_TABLE WHERE ".
"uid='$contextUser' AND ".
"end_time='0' AND ".
//"start_time>='$year-$month-$day' AND ".
//"start_time<='$year-$month-$day 23:59:59' AND ".
"$TIMES_TABLE.task_id=$task_id AND ".
"$TIMES_TABLE.proj_id=$proj_id AND ".
"$TASK_TABLE.task_id=$task_id AND ".
"$TASK_TABLE.proj_id=$proj_id";
list($qh,$num) = dbQuery($querystring);
$resultset = dbResult($qh);
if ($num > 0)
errorPage("You have already clocked on for task '$resultset[name]' at $resultset[start_time]. Please clock off first.", $fromPopupWindow);
//now insert the record for this clock on
$querystring = "INSERT INTO $TIMES_TABLE (uid, start_time, proj_id,task_id) ".
"VALUES ('$contextUser','$timeString', $proj_id, $task_id)";
list($qh,$num) = dbQuery($querystring);
//now output an ok page, the redirect back
print "<html>\n";
print " <head>\n";
print " <script language=\"javascript\">\n";
print " function alertAndLoad()\n";
print " {\n";
print " alert('Clocked on successfully');\n";
if ($fromPopupWindow)
print " window.opener.location.reload();\n";
print " window.location=\"$Location\";\n";
print " }\n";
print " </script>\n";
print " </head>\n";
print " <body onLoad=\"javascript:alertAndLoad();\">\n";
print " </body>\n";
print " </html>\n";
exit;
}
function clockoff($timeString) {
include("table_names.inc");
//import global vars
global $contextUser, $year, $month, $day, $task_id, $proj_id, $Location;
global $destination, $clock_on_time_hour, $clock_off_time_hour,
$clock_on_time_min, $clock_off_time_min, $clockonoff;
global $log_message, $log_message_presented, $fromPopupWindow;
//check that we are actually clocked on
$querystring = "SELECT start_time, start_time < '$timeString' AS valid FROM $TIMES_TABLE WHERE ".
"uid='$contextUser' AND ".
"end_time=0 AND ".
//"start_time >= '$year-$month-$day' AND ".
//"start_time <= '$year-$month-$day 23:59:59' AND ".
"proj_id=$proj_id AND ".
"task_id=$task_id";
list($qh,$num) = dbQuery($querystring);
$data = dbResult($qh);
if ($num == 0)
errorPage("You are not currently clocked on. You must clock on before you can clock off.", $fromPopupWindow);
//also check that the clockoff time is after the clockon time
else if ($data["valid"] == 0)
errorPage("You must clock off <i>after</i> you clock on.", $fromPopupWindow);
//do we need to present the user with a log message screen?
if ($log_message_presented == false)
getLogMessage();
//now insert the record for this clock off
$log_message = addslashes($log_message);
$querystring = "UPDATE $TIMES_TABLE SET log_message='$log_message', end_time='$timeString' WHERE ".
"uid='$contextUser' AND ".
"proj_id=$proj_id AND ".
"end_time=0 AND ".
//"start_time >= '$year-$month-$day' AND ".
//"start_time < '$year-$month-$day 23:59:59' AND ".
"task_id=$task_id";
list($qh,$num) = dbQuery($querystring);
Header("Location: $Location");
}
function clockonandoff() {
include("table_names.inc");
//import global vars
global $contextUser, $year, $month, $day, $task_id, $proj_id, $Location;
global $destination, $clock_on_time_hour, $clock_off_time_hour,
$clock_on_time_min, $clock_off_time_min, $clockonoff;
global $log_message, $log_message_presented;
global $clock_on_radio, $clock_off_radio, $fromPopupWindow;
if ($clock_on_radio == "now" && $clock_off_radio == "now")
errorPage("You cannot clock on and off at with the same clock-on and clock-off time.", $fromPopupWindow);
//get the dates
if ($clock_on_radio == "now") {
$clock_on_time_hour = date("H");
$clock_on_time_min = date("i");
}
if ($clock_off_radio == "now") {
$clock_off_time_hour = date("H");
$clock_off_time_min = date("i");
}
//make sure we're not clocking on after clocking off
if (($clock_on_time_hour == $clock_off_time_hour) && ($clock_on_time_min > $clock_off_time_min))
errorPage("You cannot have your clock on time ($clock_on_time_hour:$clock_on_time_min) ".
"later than your clock off time ($clock_off_time_hour:$clock_off_time_min)", $fromPopupWindow);
else if ($clock_on_time_hour > $clock_off_time_hour)
errorPage("You cannot have your clock on time ($clock_on_time_hour:$clock_on_time_min) ".
"later than your clock off time ($clock_off_time_hour:$clock_off_time_min)", $fromPopupWindow);
else if (($clock_on_time_hour == $clock_off_time_hour) && ($clock_on_time_min == $clock_off_time_min))
errorPage("You cannot clock on and off with the same clock on and clock off time. on_hour=$clock_on_time_hour on_min=$clock_on_time_min off_hour=$clock_off_time_hour off_min=$clock_off_time_min", $fromPopupWindow);
if ($log_message_presented == false)
getLogMessage();
$log_message = addslashes($log_message);
$queryString = "INSERT INTO $TIMES_TABLE (uid, start_time, end_time, proj_id, task_id, log_message) ".
"VALUES ('$contextUser','$year-$month-$day $clock_on_time_hour:$clock_on_time_min:00', ".
"'$year-$month-$day $clock_off_time_hour:$clock_off_time_min:00', ".
"$proj_id, $task_id, '$log_message')";
list($qh,$num) = dbQuery($queryString);
Header("Location: $Location");
exit;
}
?>