This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLTableFormBoxModel.php
More file actions
348 lines (340 loc) · 14.2 KB
/
Copy pathHTMLTableFormBoxModel.php
File metadata and controls
348 lines (340 loc) · 14.2 KB
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
<?php
/**
* Copyright 2011 Felix Ostrowski, hbz
*
* This file is part of Phresnel.
*
* Phresnel is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Phresnel is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Phresnel. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Render a resource as an HTML Form.
*/
class HTMLTableFormBoxModel extends AbstractFormBoxModel {
/**
* Wrap a form for a resource.
*
* @param LibRDF_URINode $resource The resource to render a form for.
* @return string
*/
protected function _render() {
$rs = "<form method=\"POST\" action=\"\">";
$rs .= $this->_form($this->_lens, $this->_resourceURI);
$rs .= '<input type="submit" name="format" value="RDFa" />';
$rs .= '<input type="submit" name="format" value="Turtle" />';
$rs .= '<input type="submit" name="format" value="RDF/XML" />';
$rs .= '<input type="submit" name="format" value="Save" />';
$rs .= "</form>";
$doc = new DomDocument('1.0', 'UTF-8');
$doc->loadXml($rs);
$doc->formatOutput = true;
foreach ($this->_namespaces as $url => $prefix) {
$doc->documentElement->appendChild(
$doc->createAttribute("$prefix"))->appendChild(
$doc->createTextNode($url));
}
return $doc->saveXml($doc->documentElement);
}
/**
* Render a form for a resource.
*
* @param LibRDF_Node $lensURI The URI of the lens to use.
* @param LibRDF_Node $resourceURI The URI of the resource.
* @param resource $rem HTML containing the remove button.
* @return string
*/
protected function _form(LibRDF_Node $lensURI, LibRDF_Node $resourceURI, $rem = "") {
try {
$domain = $this->_lensDef->getTarget($lensURI, new
LibRDF_URINode(FRESNEL."classLensDomain"));
list ($dname, $dns) = $this->_nssplit($domain);
$d = substr($domain, 1, strlen($domain) - 2);
} catch (LibRDF_LookupError $e) {
$d = null;
}
if ($resourceURI instanceOf LibRDF_BlankNode) {
$r = substr($resourceURI, 2);
} else {
$r = substr($resourceURI, 1, -1);
}
$rs = '';
$rs .= "<table class=\"resource\">";
$lst = $this->_lensDef->getTarget($lensURI, new
LibRDF_URINode(FRESNEL."showProperties"));
$props = $this->_unlist($lst);
$rs .= "<tr><td class=\"rlabel\" colspan=\"2\">";
$rs .= "<span>$r</span>";
if ($domain) {
$rs .= sprintf('<input type="hidden" name="content[%1$s][object][%2$s][]" value="%3$s" />', $r, RDF."type", $d);
}
$rs .= "$rem</td></tr>";
foreach ($props as $prop) {
if ($prop instanceof LibRDF_BlankNode) {
$rs .= $this->_subform($resourceURI, $prop);
// TODO: Alternatively render search field
} else if ($prop instanceof LibRDF_URINode) {
$rs .= $this->_input($resourceURI, $prop);
}
}
$rs .= "</table>";
return $rs;
}
/**
* Render a text input for a property.
*
* @param LibRDF_Node $resourceURI The URI of the resource.
* @param LibRDF_URINode $prop The URI of the property.
* @return string
*/
protected function _input(LibRDF_Node $resourceURI, LibRDF_URINode $prop) {
if ($resourceURI instanceOf LibRDF_BlankNode) {
$r = substr($resourceURI, 2);
} else {
$r = substr($resourceURI, 1, -1);
}
$p = substr($prop, 1, -1);
$values = $this->_data->findStatements($resourceURI, $prop, null);
$rs = '';
try {
$format = $this->_lensDef->getSource(new
LibRDF_URINode(FRESNEL."propertyFormatDomain"),
$prop);
$label = $this->_lensDef->getTarget($format, new
LibRDF_URINode(FRESNEL."label"));
} catch(LibRDF_LookupError $e) {
$label = htmlspecialchars("$prop");
}
if ($values->current() === null) {
$rs .= "<tr>";
$rs .= "<td class=\"plabel\">$label</td>";
$rs .= sprintf('<td><input type="text" name="content[%1$s][data][%2$s][]" /></td>',
$r, $p);
$rs .= "</tr>";
} else {
foreach ($values as $val) {
$rs .= "<tr>";
$rs .= "<td class=\"plabel\">$label</td>";
$escapedVal = htmlspecialchars($val->getObject());
$rs .= sprintf( '<td><input type="text" name="content[%1$s][data][%2$s][]" value="%3$s" /></td>',
$r, $p, $escapedVal);
$rs .= "</tr>";
}
}
return $rs;
}
/**
* Render a subform for a property of a resource.
*
* @param LibRDF_Node $resourceURI The URI of the resource.
* @param LibRDF_Node $prop The URI of the property.
* @return string
*/
protected function _subform(LibRDF_Node $resourceURI, LibRDF_Node $prop) {
if ($resourceURI instanceOf LibRDF_BlankNode) {
$r = substr($resourceURI, 2);
} else {
$r = substr($resourceURI, 1, -1);
}
$sublens = $this->_lensDef->getTarget($prop, new
LibRDF_URINode(FRESNEL."sublens"));
$link = $this->_lensDef->getTarget($prop, new
LibRDF_URINode(FRESNEL."property"));
$link_s = substr($link, 1, -1);
try {
$format = $this->_lensDef->getSource(new
LibRDF_URINode(FRESNEL."propertyFormatDomain"), $link);
$label = $this->_lensDef->getTarget($format, new LibRDF_URINode(FRESNEL."label"));
} catch(LibRDF_LookupError $e) {
$label = "$prop";
}
try {
$purpose = $this->_lensDef->getTarget($sublens, new
LibRDF_URINode(FRESNEL."purpose"));
} catch(LibRDF_LookupError $e) {
$purpose = null;
}
$values = $this->_data->findStatements($resourceURI, $link, null);
$rs = '';
if ($purpose and $purpose->isEqual(new LibRDF_URINode(LM."linkLens"))) {
// LINK
$rs .= $this->_link($resourceURI, $link, $sublens, $values);
} else if ($values->current() === null) {
try {
$this->_lensDef->getTarget($prop, new
LibRDF_URINode(LM."optional", "true"));
} catch (LibRDF_LookupError $e) {
$child = new LibRDF_BlankNode();
$c = substr($child, 2);
$rem = sprintf('<input type="submit" class="remove" name="remove[%1$s][object][%2$s][%3$s]" value="X" />', $r, $link_s, $c);
// Recursively render subforms
$rs .= "<tr><td class=\"plabel\">$label";
$rs .= sprintf('<input type="hidden" name="content[%1$s][object][%2$s][]" value="%3$s" /></td>',
$r, $link_s, $c);
$rs .= '<td>';
$rs .= $this->_form($sublens, $child, $rem);
$rs .= '</td></tr>';
}
$rs .= sprintf('<tr><td colspan="2"><input type="submit" class="add" name="add[%1$s][object][%2$s]" value="%3$s hinzufügen" /></td></tr>', $r, $link_s, $label);
} else {
foreach ($values as $val) {
$child = $val->getObject();
$c = substr($child, 2);
$rs .= "<tr><td class=\"plabel\">$label";
$rs .= sprintf('<input type="hidden" name="content[%1$s][object][%2$s][]" value="%3$s" /></td>',
$r, $link_s, $c);
$rs .= '<td>';
$rem = sprintf('<input type="submit" class="remove" name="remove[%1$s][object][%2$s][%3$s]" value="X" />', $r, $link_s, $c);
$rs .= $this->_form($sublens, $child, $rem);
$rs .= '</td></tr>';
}
$rs .= sprintf('<tr><td colspan="2"><input type="submit" class="add" name="add[%1$s][object][%2$s]" value="%3$s hinzufügen" /></td></tr>', $r, $link_s, $label);
}
return $rs;
}
/**
* Render a dropdown menu to link to another resource.
*
* @param LibRDF_Node $resourceURI The source resource.
* @param LibRDF_URINode $link The property, i.e. link.
* @param LibRDF_Node $sublens The sublens to use to display the options.
* @param mixed $value Optional, defaults to null.
* @return string
*/
protected function _link(
LibRDF_Node $resourceURI,
LibRDF_URINode $link,
LibRDF_Node $sublens,
$values) {
$link_s = substr($link, 1, -1);
try {
$format = $this->_lensDef->getSource(new
LibRDF_URINode(FRESNEL."propertyFormatDomain"), $link);
$label = $this->_lensDef->getTarget($format, new LibRDF_URINode(FRESNEL."label"));
} catch(LibRDF_LookupError $e) {
$label = htmlspecialchars("$link");
}
$rs = "";
if ($resourceURI instanceOf LibRDF_BlankNode) {
$r = substr($resourceURI, 2);
} else {
$r = substr($resourceURI, 1, -1);
}
try {
$domain = $this->_lensDef->getTarget($sublens, new
LibRDF_URINode(FRESNEL."classLensDomain"));
list ($dname, $dns) = $this->_nssplit($domain);
} catch (LibRDF_LookupError $e) {
$domain = null;
}
// sublens with no properties to show are
// raw links
try {
$lst = $this->_lensDef->getTarget($sublens, new
LibRDF_URINode(FRESNEL."showProperties"));
} catch (LibRDF_LookupError $ex) {
if ($values->current() === null) {
$rs .= "<tr>";
$rs .= "<td class=\"plabel\">$label</td>";
$rs .= sprintf('<td><input type="text" name="content[%1$s][object][%2$s][]" /></td>',
$r, $link_s);
$rs .= "</tr>";
} else {
foreach ($values as $value) {
$val = $value->getObject();
$val = htmlspecialchars(substr($val, 1, strlen($val) - 2));
$rs .= "<tr>";
$rs .= "<td class=\"plabel\">$label</td>";
$rs .= sprintf('<td><input type="text" name="content[%1$s][object][%2$s][]" value="%3$s" /></td>',
$r, $link_s, $val);
$rs .= "</tr>";
}
}
return $rs;
}
try {
// Query for options
$type = new LibRDF_URINode(RDF."type");
$l = Phresnel::getLens($sublens);
$l->loadResource(new LibRDF_BlankNode());
$optModel = $l->getData();
$options = $optModel->findStatements(null, $type, $domain);
$props = $this->_unlist($lst);
// List selected options
$selected = array();
foreach ($values as $value) {
$val = $value->getObject();
$labelVals = array();
foreach ($props as $prop) {
if ($prop instanceof LibRDF_URINode) {
try {
$labelVals[] = $optModel->getTarget($val, $prop);
} catch (LibRDF_LookupError $e) {
}
}
}
$val = substr($val, 1, strlen($val) - 2);
if (empty($labelVals)) {
$labelVals[] = $val;
}
$rs .= '<tr><td>';
$rs .= $label;
$rs .= '</td><td>';
$rs .= implode($labelVals, ", ");
$rs .= sprintf('<input type="hidden" name="content[%1$s][object][%2$s][]" value="%3$s" />',
$r, $link_s, $val);
$rs .= sprintf('<input type="submit" class="remove" name="remove[%1$s][object][%2$s][%3$s]" value="X" />', $r, $link_s, $val);
$rs .= '</td>';
$rs .= '</tr>';
$selected[] = $val;
}
$available = array();
foreach ($options as $option) {
$uri = $option->getSubject();
$u = substr($uri, 1, strlen($uri) - 2);
if (in_array($u, $selected)) {
continue;
}
$labelVals = array();
foreach ($props as $prop) {
if ($prop instanceof LibRDF_URINode) {
try {
$labelVals[] = $optModel->getTarget($option->getSubject(), $prop);
} catch (LibRDF_LookupError $e) {
}
}
}
if (empty($labelVals)) {
$labelVals[] = $u;
}
$available[$u] = implode($labelVals, ", ");
}
} catch(LibRDF_LookupError $e) {
// Handle empty options, only list existing ones!
// $this->_logger->logError($e);
echo "$link";
}
if (!empty($available)) {
$rs .= '<tr><td colspan="2">';
$rs .= sprintf('<select name="link[%1$s][object][%2$s]">', $r, $link_s);
foreach ($available as $u => $l) {
$rs .= "<option value=\"$u\">";
$rs .= $l;
$rs .= "</option>";
}
$rs .= '</select>';
$rs .= '</td></tr>';
$rs .= sprintf('<tr><td colspan="2"><input type="submit" class="add" name="add[%1$s][object][%2$s]" value="%3$s hinzufügen" /></td></tr>', $r, $link_s, $label);
}
return $rs;
}
}