-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable.php
270 lines (213 loc) · 7.73 KB
/
table.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
<?php
/**
* Base class for a comparison table
*/
class ComparisonTable {
protected $database; // reference to the database handler
protected $tablePrefix = ""; // the prefix of the tables in the database
// constructor
public function __construct($database, $tablePrefix) {
$this->database = $database;
$this->tablePrefix = $tablePrefix;
}
// this function returns a selector for comments (this doesn't make sense without the table)
public function outputSelector() {
$output = "";
$output .= "<form class='selector' id='" . $this->tablePrefix . "-selector'>";
$columns = $this->getColumnHeaders();
foreach ($columns as $column)
$output .= "<label><input type='checkbox' checked value='" . $column["name"] . "'>" . $column["name"] . "</label>";
$output .= "</form>";
return $output;
}
// this function returns the whole table (TODO would __toString be reasonable for this?)
public function outputTable() {
$output = "";
$output .= "<table id='" . $this->tablePrefix . "-table'>";
// header of the table
$output .= "<thead>";
$output .= "<tr>";
// empty table cell
$output .= "<th></th>";
$columns = $this->getColumnHeaders();
foreach ($columns as $column) {
// the column headers
$output .= $this->outputColumnHeader($column);
}
$output .= "</tr>";
$output .= "</thead>";
// body of the table
$output .= "<tbody>";
$rows = $this->getRowHeaders();
$relations = $this->getRelations();
foreach ($rows as $row) {
$output .= "<tr>";
// the row headers
$output .= $this->outputRowHeader($row);
foreach ($columns as $column) {
// the main table cells
$output .= $this->outputRelation($relations[$row["name"]][$column["name"]]);
}
$output .= "</tr>";
}
$output .= "</tbody>";
$output .= "</table>";
return $output;
}
// output a column header
protected function outputColumnHeader($column) {
// this is the default, no frills
return "<th data-name='" . $column["name"] . "'>" . $column["name"] . "</th>";
}
protected function outputRelation($relation) {
// this is the default, no frills
$output = "";
if (empty($relation)) // empty string implies that there is no relation in the database
$output .= "<td></td>";
else
$output .= "<td>✓</td>";
return $output;
}
// output a row header
protected function outputRowHeader($row) {
// this is the default, no frills
return "<th>" . $row["name"] . "</th>";
}
// get the elements in the column headers
private function getColumnHeaders() {
// TODO caching this hardly seems worth the effort?
$sql = $this->database->prepare("SELECT * FROM [" . $this->getTableName("columns") . "]");
if ($sql->execute())
return $sql->fetchAll();
return array();
}
// get the elements in the row headers
private function getRowHeaders() {
// TODO caching this hardly seems worth the effort?
$sql = $this->database->prepare("SELECT * FROM [" . $this->getTablename("rows") . "]");
if ($sql->execute()) {
$rows = $sql->fetchAll();
usort($rows, function($a, $b) { return strcasecmp(str_replace("é", "e", $a["name"]), str_replace("é", "e", $b["name"])); });
return $rows;
}
return array();
}
// get the relations for the comparison table
private function getRelations() {
$rows = $this->getRowHeaders();
$columns = $this->getColumnHeaders();
$relations = array();
// initialise the table with empty strings (= no relation)
foreach ($rows as $row) {
foreach ($columns as $column) {
$relations[$row["name"]][$column["name"]] = "";
}
}
$query = "";
$query .= "SELECT [" . $this->getTableName("rows") . "].name, [" . $this->getTableName("columns") . "].name, [" . $this->getTableName("relations") . "].* ";
$query .= "FROM [" . $this->getTableName("rows") . "], [" . $this->getTableName("columns") . "], [" . $this->getTableName("relations") . "] ";
$query .= "WHERE [" . $this->getTableName("rows") . "].id = [" . $this->getTableName("relations") . "].row ";
$query .= "AND [" . $this->getTableName("columns") . "].id = [" . $this->getTableName("relations") . "].column";
$sql = $this->database->prepare($query);
if ($sql->execute()) {
$result = $sql->fetchAll();
// TODO check these indices (!)
foreach ($result as $relation)
$relations[$relation[0]][$relation[1]] = $relation;
}
return $relations;
}
// get the table name, based on the prefix and the required table
private function getTableName($table) {
return $this->tablePrefix . "-" . $table;
}
// print the mark corresponding the status field
protected function printMark($relation) {
$output = "";
// if the tag is not empty we make the symbol clickable
if (!empty($relation["tag"]))
$output .= "<a href='" . StacksLinks::tag($relation["tag"]) . "'>";
// choose the correct symbol
switch ($relation["status"]) {
case "true":
$output .= "✓";
break;
case "false":
$output .= "✗";
break;
case "unknown":
$output .= "?";
break;
default:
$output .= $relation["status"];
//exit("should not happen"); # TODO improve
}
// if the tag is not empty we make the symbol clickable
if (!empty($relation["tag"]))
$output .= "</a>";
return $output;
}
// print a cell containing a check mark
protected function printMarkCell($relation) {
$output = "";
// turn non-standard status into true
if (in_array($relation["status"], array("true", "false", "unknown")))
$class = $relation["status"];
else
$class = "true";
if (!empty($relation["tag"]))
$output .= "<td class='" . $class . "' data-tag='" . StacksLinks::tag($relation["tag"]) . "'>";
else
$output .= "<td class='" . $class . "'>";
$output .= $this->printMark($relation) . "</td>"; // TODO change this to static again but I don't know syntax to call static functions in the same class...
return $output;
}
// print a cell containing a definition
protected function printDefinition($row) {
$output = "";
if (!empty($row["tag"]))
$output .= "<th data-tag='" . $row["tag"] . "' data-name='" . $row["name"] . "'><a href='" . StacksLinks::tag($row["tag"]) . "'>" . $row["name"] . "</a></th>";
else
$output .= "<th data-name='" . $row["name"] . "'>" . $row["name"] . "</th>";
return $output;
}
}
/**
* Instance of a comparison table
*
* This table indicates which properties of morphisms are preserved by which operations
*/
class MorphismPropertiesPreservationTable extends ComparisonTable {
public function __construct($database) {
global $config;
parent::__construct($database, "morphism-properties-preservation");
}
protected function outputRelation($relation) {
$output = "";
if (empty($relation)) // empty string implies that there is no relation in the database
$output .= "<td></td>";
else
$output .= parent::printMarkCell($relation);
return $output;
}
protected function outputColumnHeader($row) {
$output = "";
if (!empty($row["text"]))
$output .= "<th data-text='" . $row["text"] . "' data-name='" . $row["name"] . "'>" . $row["name"] . "</th>";
else
$output .= "<th data-name='" . $row["name"] . "'>" . $row["name"] . "</th>";
return $output;
}
protected function outputRowHeader($row) {
return parent::printDefinition($row);
}
}
/**
* Simple utility class providing links to the Stacks project website
*/
class StacksLinks {
public static function tag($tag) {
return "https://stacks.math.columbia.edu/tag/" . $tag;
}
}
?>