-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.php
243 lines (196 loc) · 8.27 KB
/
update.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
<?php
$toProcess = [
'combined' => [
'inputFile' => "static/csv/gc-it-projects-combined.csv",
'outputFile' => "layouts/shortcodes/tabledata_combined.html",
'outputJson' => "static/js/generated/tabledata_combined.js",
'isCombined' => true,
'originDate' => "",
],
'2022' => [
'inputFile' => "static/csv/gc-it-projects-2022.csv",
'outputFile' => "layouts/shortcodes/tabledata_2022.html",
'outputJson' => "static/js/generated/tabledata_2022.js",
'isCombined' => false,
'originDate' => "April 25, 2022",
],
'2019' => [
'inputFile' => "static/csv/gc-it-projects-2019.csv",
'outputFile' => "layouts/shortcodes/tabledata_2019.html",
'outputJson' => "static/js/generated/tabledata_2019.js",
'isCombined' => false,
'originDate' => "May 1, 2019",
],
'2016' => [
'inputFile' => "static/csv/gc-it-projects-2016.csv",
'outputFile' => "layouts/shortcodes/tabledata_2016.html",
'outputJson' => "static/js/generated/tabledata_2016.js",
'isCombined' => false,
'originDate' => "June 9, 2016",
],
];
// Helper functions
function parseTotalBudget($input) {
if(! trim($input)) {
return 0;
}
$input = str_replace(['$', ',',' '], '', $input);
return intval($input);
}
function displayTotalBudget($input, $isCombined = false) {
if(! trim($input)) {
if($isCombined) {
return '';
}
else {
return '<em class="no-data-provided">(not provided)</em>';
}
}
else {
return $input;
}
}
function displayBudgetSource($input) {
if($input) {
return '<br/> <span class="text-muted pdt-budget-source">(' . $input . ")</span>";
}
else {
return '';
}
}
function parseEstimatedCompletionDate($estimatedCompletionDate) {
if(trim($estimatedCompletionDate)) {
return date("Y-m-d", strtotime($estimatedCompletionDate));
}
else {
return "0000-00-00";
}
}
function displayEstimatedCompletionDate($estimatedCompletionDate, $rawProvidedDate = "No date provided", $isCombined = false) {
if(trim($estimatedCompletionDate)) {
return str_replace(' ', ' ',$estimatedCompletionDate);
}
elseif($isCombined == false) {
return '<em class="no-data-provided">' . $rawProvidedDate . '</em>';
}
else {
// For combined data, don't list "not provided" each time
return '';
}
}
function calculateYearsRemaining($estimatedCompletionDate, $originDate) {
// $difference = strtotime($estimatedCompletionDate) - strtotime($originDate);
// Thanks to
// https://stackoverflow.com/a/5387225/756641
$d1 = new DateTime($estimatedCompletionDate);
$d2 = new DateTime($originDate);
$diff = $d2->diff($d1);
$years = round($diff->y + $diff->m / 12, 1);
// if($d1 < $d2) {
// $years = -$years;
// }
// $years += 1;
if ($years < 0) {
return 0;
}
else {
return $years;
}
}
function parseYearsRemaining($estimatedCompletionDate) {
if(! $estimatedCompletionDate) {
return 1000;
}
else {
return 0;
}
}
function cleanupDescriptions($description) {
$description = str_replace(["\n", "\t", "\r"], " ", $description);
return htmlentities($description);
}
// var_dump($array);
// Main operation
foreach($toProcess as $key => $params) {
$pathToCsvFile = $params['inputFile'];
$pathToOutputFile = $params['outputFile'];
$htmlOutput = "";
$originDate = $params['originDate'];
if(! file_exists($pathToCsvFile)) {
exit("Could not find specified CSV file in \$pathToCsvFile.\n");
}
// Thanks to
// https://stackoverflow.com/a/4801943/756641
$array = $fields = array(); $i = 0;
$handle = @fopen($pathToCsvFile, "r");
if ($handle) {
while (($row = fgetcsv($handle, 4096)) !== false) {
if (empty($fields)) {
$fields = $row;
continue;
}
foreach ($row as $k=>$value) {
$array[$i][$fields[$k]] = $value;
}
$i++;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
foreach($array as $item) {
if($params['isCombined']) {
// Process as a combined dataset
// projectName projectDescription2016 projectDescription2019 totalBudget2016 estimatedCompletionDate2016 totalBudget2019 estimatedCompletionDate2019 mostRecentBudget peakBudget hasComparisonBudgets hasComparisonDates budgetDelta budgetDeltaPercentage datesDeltaYear estimatedStatus
// deptAcronym shortcode uniqueId department latestProjectName latestDescription originalBudget latestBudget originalBudgetSource latestBudgetSource originalEstimatedCompletionDate latestEstimatedCompletionDate originalEstimatedCompletionDateSource latestEstimatedCompletionDateSource budgetDelta budgetDeltaPercentage datesDeltaYear numOfEntries estimatedStatus isOver10M isOver100M
// For now, exclude entries without a name
if($item['latestProjectName']) {
$htmlOutput .= '
<tr id="' . $item['uniqueId'] . '">
<td data-search="' . $item['deptAcronym'] . ' ' . strtolower($item['department']) . '">' . $item['department'] . '</td>
<td data-search="' . $item['uniqueId'] . ' ' . htmlentities($item['latestProjectName']) . ' ' . cleanupDescriptions($item['latestDescription']) . '"><a href="#uid=' . $item['uniqueId'] . '">' . $item['latestProjectName'] . '</a></td>
<td class="pdt-date" data-order="' . parseTotalBudget($item['originalBudget']) . '">' . displayTotalBudget($item['originalBudget'], 1) . displayBudgetSource($item['originalBudgetSource']) . '</td>
<td class="pdt-date" data-order="' . parseTotalBudget($item['latestBudget']) . '">' . displayTotalBudget($item['latestBudget'], 1) . displayBudgetSource($item['latestBudgetSource']) . '</td>
<td class="pdt-date" data-order="' . parseTotalBudget($item['budgetDelta']) . '">' . displayTotalBudget($item['budgetDelta'], 1) . '</td>
<td class="" data-order="' . floatval($item['budgetDeltaPercentage']) . '">' . $item['budgetDeltaPercentage'] . '</td>
<td data-order="' . parseEstimatedCompletionDate($item['originalEstimatedCompletionDate']) . '">' . displayEstimatedCompletionDate($item['originalEstimatedCompletionDate'], "No date provided", 1) . '</td>
<td data-order="' . parseEstimatedCompletionDate($item['latestEstimatedCompletionDate']) . '">' . displayEstimatedCompletionDate($item['latestEstimatedCompletionDate'], "No date provided", 1) . '</td>
<td>' . $item['datesDeltaYear'] . '</td>
<td>' . $item['estimatedStatus'] . '</td>
</tr>
';
}
}
else {
// Individual year entries
// deptAcronym shortcode uniqueId department projectName description totalBudget estimatedCompletionDate rawProvidedDate yearsRemaining originalDocumentOrder source asOfDate isOver10M isOver100M
// Note that if the Hugo page structure changed, this would need to be updated:
$pageUrl = '/' . $key . '/';
$htmlOutput .= '
<tr id="' . $item['uniqueId'] . '">
<td data-search="' . $item['deptAcronym'] . ' ' . strtolower($item['department']) . '">' . $item['department'] . '</td>
<td data-search="' . $item['uniqueId'] . ' ' . htmlentities($item['projectName']) . ' ' . cleanupDescriptions($item['description']) . '"><a href="' . $pageUrl . '#uid=' . $item['uniqueId'] . '">' . $item['projectName'] . '</a></td>
<td class="pdt-date" data-order="' . parseTotalBudget($item['totalBudget']) . '">' . displayTotalBudget($item['totalBudget']) . '</td>
<td data-order="' . parseEstimatedCompletionDate($item['estimatedCompletionDate']) . '">' . displayEstimatedCompletionDate($item['estimatedCompletionDate'], $item['rawProvidedDate']) . '</td>
<td>' . $item['yearsRemaining'] . '</td>
</tr>
';
}
}
file_put_contents($pathToOutputFile, $htmlOutput);
echo "Updated table HTML file - $pathToOutputFile\n";
if(isset($params['outputJson'])) {
$jsonFileHeader = "// JSON data
// This file is generated automatically
var app = app || {};
app.data = app.data || {};
app.data = ";
$indexedArray = [];
foreach($array as $item) {
$indexedArray[$item['uniqueId']] = $item;
}
file_put_contents($params['outputJson'], $jsonFileHeader . json_encode($indexedArray, JSON_PRETTY_PRINT) . ";\n");
echo "Updated table JSON file - " . $params['outputJson'] . "\n";
}
}