-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.php
146 lines (120 loc) · 4.8 KB
/
results.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
<?php
$project = $_POST['project'];
$errors = doValidation($project);
function doValidation($project = "test") {
require_once 'third_party/PHPExcel/PHPExcel.php';
$errors = array();
$projectPath = __DIR__ . "/upload/" . $project . "/";
// Ensure the project path exists
if(!file_exists($projectPath)) {
$errors[] = "Invalid project path given";
return $errors;
}
$excelFile = "";
if ($projectDirHandle = opendir($projectPath)) {
while (false !== ($entry = readdir($projectDirHandle))) {
if (pathinfo($entry, PATHINFO_EXTENSION) == "xlsx") {
$excelFile = $entry;
}
}
closedir($projectDirHandle);
}
if(empty($excelFile)) {
$errors[] = "No Media Grid file found";
return $errors;
}
$objPHPExcel = PHPExcel_IOFactory::load($projectPath . $excelFile);
// TODO Check if we can safely assume only one worksheet per file
// Iterate through all the sheets in the excel workbook and store as an array
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$arrayData[$worksheet->getTitle()] = $worksheet->toArray();
}
$mediaGrid = array();
$resCodeLabel = "";
$resCategoryLabel = "";
$resTitleLabel = "";
// Grab all the data we need for validation from the media grid spreadsheet
foreach ($arrayData['CLA Media Grid'] as $row_number => $entry) {
if($row_number == 0) {
$resCodeLabel = $entry[0];
$resCategoryLabel = $entry[4];
$resTitleLabel = $entry[6];
}
else {
// Don't include rows that have resource category 'CH: Chapter pdfs'
if($entry[4] != "CH: Chapter pdfs") {
$mediaGrid[] = array(
$resCodeLabel => trim($entry[0]),
$resCategoryLabel => $entry[4],
$resTitleLabel => $entry[6]
);
}
}
}
$contentFiles = array();
// Scan the 'Final Content' folder
if ($projectsDirHandle = opendir($projectPath . "Final Content/")) {
while (false !== ($entry = readdir($projectsDirHandle))) {
if ($entry != "." && $entry != ".." && $entry != "Chapter PDFs") {
$contentFiles[] = array(
"filename" => $entry,
"listedInMediaGrid" => FALSE
);
}
}
closedir($projectsDirHandle);
}
else {
$errors[] = "Could not open Final Content folder";
return $errors;
}
// Loop through every entry in the Media Grid, and verify that the Resource Code references a file that exists
// in the Final Content directory
for ($i = 0; $i < count($mediaGrid); $i++) {
$fileInMediaGrid = $mediaGrid[$i][$resCodeLabel];
$fileFound = FALSE;
for ($j = 0; $j < count($contentFiles); $j++) {
$contentFileName = pathinfo($contentFiles[$j]['filename'], PATHINFO_FILENAME);
if ($fileInMediaGrid == $contentFileName) {
$contentFiles[$j]['listedInMediaGrid'] = TRUE;
$fileFound = TRUE;
}
}
if (!$fileFound) {
$errors[] = "Could not find a file matching resource code " . $fileInMediaGrid . " (row " . ($i + 2) . ")";
}
}
// Loop through all the files in the Final Content list and check that have now all been marked
// as listed in the Media Grid
for($i = 0; $i < count($contentFiles) ; $i++) {
if(!$contentFiles[$i]['listedInMediaGrid']) {
$errors[] = "File " . $contentFiles[$i]['filename'] . " exists in Final Content folder but is not listed in the Media Grid";
}
}
// Loop through Media Grid and check Resource categories are all valid
for($i = 0; $i < count($mediaGrid) ; $i++) {
if(!in_array($mediaGrid[$i][$resCategoryLabel], $this->allowedResourceCategories)) {
$errors[] = "Row ". $i . ": Resource category " . $mediaGrid[$i][$resCategoryLabel] . " not allowed";
}
}
return $errors;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Validated</title>
</head>
<body>
<?php if(empty($errors)) { ?>
<p>No errors found</p>
<?php } else { ?>
<ul>
<?php foreach($errors as $errorMessage) { ?>
<ul><?php echo $errorMessage ?></ul>
<?php } ?>
</ul>
<?php } ?>
</body>
</html>