-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreport.php
350 lines (340 loc) · 13.4 KB
/
report.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
340
341
342
343
344
345
346
347
348
349
350
<?php
require_once 'config.php';
require_once 'reportlib.php';
require_once "predis/autoload.php";
require_once 'SupOAuthClient.php';
require_once 'AuthJiraCert.php';
if (isset($_GET["logout"]) && $_GET["logout"] == "true")
{
setcookie($COOKIE_PREFIX."_jira_oauth_token", "", time() - 3600);
setcookie($COOKIE_PREFIX."_jira_oauth_secret", "", time() - 3600);
setcookie($COOKIE_PREFIX."_jira_oauth_token", "", time() - 3600, '/');
setcookie($COOKIE_PREFIX."_jira_oauth_secret", "", time() - 3600, '/');
echo "Logged out successfully. Thank you!";
}
else if(!isset($_COOKIE[$COOKIE_PREFIX."_jira_oauth_token"]) || !isset($_COOKIE[$COOKIE_PREFIX."_jira_oauth_secret"]))
{
$obj = new AuthJiraCert();
$obj->run();
}
else
{
// Get connection to redis server
$redis = new Predis\Client(array('host' => 'redis'));
$obj = new AuthJiraCert();
$client = new SupOAuthClient($obj->consumerKey, $obj->privateKeyFile, $_COOKIE[$COOKIE_PREFIX."_jira_oauth_token"], $_COOKIE[$COOKIE_PREFIX."_jira_oauth_secret"]);
$url = $obj->jiraBaseUrl . 'rest/api/2/myself';
$myself = $client->performRequest($url, array("expand"=>"groups"), "GET");
$authorizedReporter = false;
for ($i = 0; $i < count($myself["groups"]["items"]); $i++)
{
if ($myself["groups"]["items"][$i]["name"] == "timereporting")
{
$authorizedReporter = true;
}
}
if ($authorizedReporter)
{
if ($_GET["output"] != "json")
{
echo "<html><head><title>".$REPORTING_WEBSITE_TITLE."</title>";
echo "<style>";
echo "table.niceborder { border-collapse: collapse; }";
echo "table.niceborder,th.niceborder,td.niceborder { border: 1px solid black; }";
echo "form{ display:inline; margin:0px; padding:0px;}";
echo "#buttonpair { overflow: hidden; }";
echo "#buttonpair input { float:right }";
echo "</style>";
echo $EXTRA_HEAD_HTML;
echo "</head><body>";
echo "<p align=\"right\">";
if (!$redis->exists($myself["key"].'_projects'))
{
$url = $obj->jiraBaseUrl . 'rest/api/2/project';
$projectListJson = json_encode($client->performRequest($url, null, "GET"));
$projectList = json_decode($projectListJson);
$redis->set($myself["key"].'_projects', $projectListJson);
$redis->expire($myself["key"].'_projects', 600);
}
else
{
$projectList = json_decode($redis->get($myself["key"].'_projects'));
}
echo "<b>Projects:</b> ";
for ($i = 0; $i < count($projectList); $i++)
{
if ($projectList[$i]->key != "TEST")
{
echo "<a href=\"https://".$JIRA_DOMAIN."/projects/".$projectList[$i]->key."/issues\">".$projectList[$i]->key."</a> ";
}
}
echo "<b> – ".$myself["displayName"]." – </b>";
echo "<a href=\"index.php\">Home</a> – ";
echo "<a href=\"index.php?logout=true\">Logout</a><br/><br/></p>";
}
// Generate timecard
$startTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$endTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$startTime->modify('first day of last month')->setTime(0,0,0);
$dataStartTime = $startTime->format('U');
$dataEndTime = $endTime->format('U');
$period = $_GET["period"];
if ($_GET["begin_period"] && $_GET["end_period"])
{
$period = $_GET["begin_period"].' - '.$_GET["end_period"];
}
if (strpos($period, ' - ') !== FALSE)
{
$parts = explode(' - ', $period);
$startTime = new DateTime($parts[0], new DateTimeZone($DEFAULT_TIMEZONE));
$startTime->setTime(0,0,0);
$endTime = new DateTime($parts[1], new DateTimeZone($DEFAULT_TIMEZONE));
$endTime->setTime(23,59,59);
}
else if ($_GET["period"] == "This Week")
{
$startTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$startTime->modify('Last Sunday')->setTime(0,0,0);
$startTime->modify('+1 day')->setTime(0,0,0);
$endTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$endTime->modify('Next Sunday')->setTime(0,0,0);
$endTime->modify('+1 day')->setTime(0,0,0);
}
else if ($_GET["period"] == "Last Week")
{
$startTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$startTime->modify('Last Sunday')->setTime(0,0,0);
$startTime->modify('Last Sunday')->setTime(0,0,0);
$startTime->modify('+1 day')->setTime(0,0,0);
$endTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$endTime->modify('Last Sunday')->setTime(0,0,0);
$endTime->modify('+1 day')->setTime(0,0,0);
}
else if ($_GET["period"] == "This Month")
{
$startTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$startTime->modify('first day of this month')->setTime(0,0,0);
$endTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$endTime->modify('last day of this month')->setTime(23,59,59);
}
else if ($_GET["period"] == "Last Month")
{
$startTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$startTime->modify('first day of last month')->setTime(0,0,0);
$endTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$endTime->modify('first day of this month')->setTime(0,0,0);
}
else if ($_GET["period"] == "Year to Date")
{
$startTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$startTime->setDate($startTime->format('Y'), 1, 1)->setTime(0,0,0);
}
else if ($_GET["period"] == "Last Year")
{
$startTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$startTime->setDate($startTime->format('Y'), 1, 1)->setTime(0,0,0);
$startTime->modify('-1 year');
$endTime = new DateTime("now", new DateTimeZone($DEFAULT_TIMEZONE));
$endTime->setDate($endTime->format('Y'), 1, 1)->setTime(0,0,0);
}
$reportStartTime = (int) $startTime->format('U');
$reportEndTime = (int) $endTime->format('U');
//echo $reportStartTime. " " . $startTime->format("Y/m/d H:i:s");
//echo "<br/>";
//echo $reportEndTime. " " . $endTime->format("Y/m/d H:i:s");
$items = $redis->zRangeByScore('issue.wl.s.index', (int) $reportStartTime, (int) $reportEndTime);
$itemObjs = array();
if (count($items) > 0)
{
$itemObjs = $redis->mGet($items);
}
$users = array();
$decodedObjs = array();
for ($i = 0; $i < count($itemObjs); $i++)
{
$obj = json_decode($itemObjs[$i]);
if (array_search($obj->a, $users) === FALSE)
{
$users[] = $obj->a;
}
$decodedObjs[] = $obj;
}
if ($_GET["output"] != "json")
{
$reports = array("Project Summary", "Billing Summary", "User Summary", "Individual Entries");
echo "<h3>Current Report:</h3><form action=\"".$_SERVER['REQUEST_URI']."\" method=\"GET\"><table cellpadding=\"10\"><tr>";
echo "<td valign=\"top\">Report:<br/><select name=\"report\" onChange=\"this.form.submit();\">";
for ($i = 0; $i < count($reports); $i++)
{
echo "<option value=\"".$reports[$i]."\"".($_GET["report"] == $reports[$i]?" selected=\"selected\"":"").">".$reports[$i]."</option>";
}
echo "</select>";
echo "<br/><br/>Period:<br/><select name=\"period\" onChange=\"this.form.submit();\">";
$periods = array("This Week", "Last Week", "This Month", "Last Month", "Year to Date", "Last Year");
for ($i = 0; $i < count($periods); $i++)
{
echo "<option value=\"".$periods[$i]."\"".($_GET["period"] == $periods[$i]?" selected=\"selected\"":"").">".$periods[$i]."</option>";
}
echo "</select>";
echo "<br/><br/><input type=\"text\" size=\"8\" name=\"begin_period\" value=\"".$_GET["begin_period"]."\" onChange=\"this.form.submit();\"> to <input type=\"text\" size=\"8\" name=\"end_period\" value=\"".$_GET["end_period"]."\" onChange=\"this.form.submit();\">";
echo "<br/><br/>Grouping:<br/><select name=\"grouping\" onChange=\"this.form.submit();\">";
$groupings = array("Daily", "Weekly", "Monthly", "Yearly");
for ($i = 0; $i < count($groupings); $i++)
{
echo "<option value=\"".$groupings[$i]."\"".($_GET["grouping"] == $groupings[$i]?" selected=\"selected\"":"").">".$groupings[$i]."</option>";
}
echo "</select></td>";
echo "<td>User(s):<br/><select name=\"user[]\" size=\"10\" multiple=\"on\" onChange=\"this.form.submit();\">";
for ($i = 0; $i < count($users); $i++)
{
echo "<option value=\"".$users[$i]."\"".(gettype($_GET["user"]) == "array" && array_search($users[$i], $_GET["user"]) !== FALSE?" selected=\"on\"": "").">".$users[$i]."</option>";
}
echo "</select></td>";
echo "<td valign=\"top\">Merge:<br/><select name=\"merge\" onChange=\"this.form.submit();\">";
$merge = array("Split Users", "Merge Users");
for ($i = 0; $i < count($merge); $i++)
{
echo "<option value=\"".$merge[$i]."\"".($_GET["merge"] == $merge[$i]?" selected=\"selected\"":"").">".$merge[$i]."</option>";
}
echo "</select>";
echo "<br/><br/>Transpose:<br/><select name=\"transpose\" onChange=\"this.form.submit();\">";
$transpose = array("Off", "On");
for ($i = 0; $i < count($transpose); $i++)
{
echo "<option value=\"".$transpose[$i]."\"".($_GET["transpose"] == $transpose[$i]?" selected=\"selected\"":"").">".$transpose[$i]."</option>";
}
echo "</select>";
echo "<br/><br/>Task Filter:<br/><input type=\"text\" size=\"15\" value=\"".htmlentities($_GET["taskfilter"])."\" name=\"taskfilter\" onChange=\"this.form.submit();\">";
echo "</td>";
echo "<td>Project(s):<br/><select name=\"project[]\" size=\"10\" multiple=\"on\" onChange=\"this.form.submit();\">";
for ($i = 0; $i < count($projectList); $i++)
{
echo "<option value=\"".$projectList[$i]->key."\"".(gettype($_GET["project"]) == "array" && array_search($projectList[$i]->key, $_GET["project"]) !== FALSE?" selected=\"on\"": "").">".$projectList[$i]->key."</option>";
}
echo "</select></td>";
echo "</tr></table>";
}
if (!$_GET["report"])
{
if ($_GET["output"] != "json")
{
echo "<input type=\"submit\" value=\"Run\"></form>";
}
}
else
{
if ($_GET["output"] != "json")
{
echo "</form>";
}
$userList = $users;
if ($_GET["merge"] == "Merge Users" || $_GET["report"] == "User Summary")
{
if (gettype($_GET["user"]) == "array")
{
$userList = array("all selected users");
}
else
{
$userList = array("all users");
}
}
else if (gettype($_GET["user"]) == "array")
{
$userList = $_GET["user"];
}
$jsonResult = array();
for ($u = 0; $u < count($userList); $u++)
{
$ledgerStr = "";
$issueReference = array();
$dataReference = array();
$includeUserColumn = false;
if ($_GET["report"] == "Individual Entries" && $_GET["merge"] == "Merge Users")
{
$includeUserColumn = true;
}
for ($i = 0; $i < count($decodedObjs); $i++)
{
$obj = $decodedObjs[$i];
if ($obj->s >= $reportStartTime && $obj->s < $reportEndTime)
{
if ($userList[$u] == "all users" || $obj->a == $userList[$u] || ($userList[$u] == "all selected users" && array_search($obj->a, $_GET["user"]) !== FALSE))
{
$wlkey = $items[$i];
$parts = explode(".", $wlkey);
$subparts = explode("-", $parts[1]);
if ((!$_GET["taskfilter"] && gettype($_GET["project"]) != "array")
|| ($_GET["taskfilter"] && strpos($_GET["taskfilter"].",", $parts[1].",") !== FALSE)
|| (gettype($_GET["project"]) == "array" && array_search($subparts[0], $_GET["project"]) !== FALSE))
{
$ledgerStr = preg_replace("/billing:[A-Z]*_/", "billing:", $ledgerStr.$obj->l)."\n";
// There can be multiple transactions if the worklog entry spans multiple days
for ($j = 0; $j < (substr_count($ledgerStr, "\n\n")+1); $j++)
{
$issueReference[] = $parts[1];
$dataReference[] = $obj;
}
}
}
}
}
if ($_GET["grouping"] == "Daily")
{
$aggregation = "-D";
}
else if ($_GET["grouping"] == "Weekly")
{
$aggregation = "-W";
}
else if ($_GET["grouping"] == "Monthly")
{
$aggregation = "-M";
}
else if ($_GET["grouping"] == "Yearly")
{
$aggregation = "-Y";
}
if ($_GET["report"] == "Project Summary")
{
$ledgerreturn = runHledger("-f - bal -O csv -T ".$aggregation, $ledgerStr, $ledgerCsv);
$result = generateLedgerTable($ledgerCsv, $_GET["transpose"] == "On" ? true : false);
}
else if ($_GET["report"] == "Billing Summary")
{
$ledgerreturn = runHledger("-f - bal --pivot billing -O csv -T ".$aggregation, $ledgerStr, $ledgerCsv);
$result = generateLedgerTable($ledgerCsv, $_GET["transpose"] == "On" ? true : false);
}
else if ($_GET["report"] == "User Summary")
{
$ledgerreturn = runHledger("-f - bal --pivot user -O csv -T ".$aggregation, $ledgerStr, $ledgerCsv);
$result = generateLedgerTable($ledgerCsv, $_GET["transpose"] == "On" ? true : false);
}
else if ($_GET["report"] == "Individual Entries")
{
$ledgerreturn = runHledger("-f - reg -O csv ", $ledgerStr, $ledgerCsv);
$result = generateLedgerTable($ledgerCsv, false, $issueReference, $dataReference, $includeUserColumn);
}
if ($_GET["output"] != "json")
{
echo "<h3>".$_GET["report"]." for ".$userList[$u].":</h3>".$result->output;
echo "<button onClick=\"this.nextSibling.nextSibling.style.display = '';\">Get CSV</button><br/><textarea style=\"width: 90%; height: 500px;display: none\">".$result->csv."</textarea>";
}
else
{
$jsonResult[$userList[$u]] = array_map("str_getcsv", explode("\n", trim($result->csv)));
}
}
if ($_GET["output"] == "json")
{
header('Content-Type: application/json');
echo json_encode($jsonResult);
}
}
}
else
{
echo "You do not have permission to do reports. Please see the jira administrator for access.";
}
}
?>