/
AggregationTable.php
558 lines (485 loc) · 18 KB
/
AggregationTable.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php
namespace dokuwiki\plugin\struct\meta;
/**
* Creates the table aggregation output
*
* @package dokuwiki\plugin\struct\meta
*/
class AggregationTable
{
/**
* @var string the page id of the page this is rendered to
*/
protected $id;
/**
* @var string the Type of renderer used
*/
protected $mode;
/**
* @var \Doku_Renderer the DokuWiki renderer used to create the output
*/
protected $renderer;
/**
* @var SearchConfig the configured search - gives access to columns etc.
*/
protected $searchConfig;
/**
* @var Column[] the list of columns to be displayed
*/
protected $columns;
/**
* @var Value[][] the search result
*/
protected $result;
/**
* @var int number of all results
*/
protected $resultCount;
/**
* @var string[] the result PIDs for each row
*/
protected $resultPIDs;
protected $resultRids;
protected $resultRevs;
/**
* @var array for summing up columns
*/
protected $sums;
/**
* @var bool skip full table when no results found
*/
protected $simplenone = true;
/**
* @todo we might be able to get rid of this helper and move this to SearchConfig
* @var \helper_plugin_struct_config
*/
protected $helper;
/**
* Initialize the Aggregation renderer and executes the search
*
* You need to call @see render() on the resulting object.
*
* @param string $id
* @param string $mode
* @param \Doku_Renderer $renderer
* @param SearchConfig $searchConfig
*/
public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
{
$this->id = $id;
$this->mode = $mode;
$this->renderer = $renderer;
$this->searchConfig = $searchConfig;
$this->data = $searchConfig->getConf();
$this->columns = $searchConfig->getColumns();
$this->result = $this->searchConfig->execute();
$this->resultCount = $this->searchConfig->getCount();
$this->resultPIDs = $this->searchConfig->getPids();
$this->resultRids = $this->searchConfig->getRids();
$this->resultRevs = $this->searchConfig->getRevs();
$this->helper = plugin_load('helper', 'struct_config');
}
/**
* Returns the page id for the table
*/
public function getID()
{
return $this->id;
}
/**
* Create the table on the renderer
*/
public function render()
{
// abort early if there are no results at all (not filtered)
if (!$this->resultCount && !$this->isDynamicallyFiltered() && $this->simplenone) {
$this->startScope();
$this->renderer->cdata($this->helper->getLang('none'));
$this->finishScope();
return;
}
$this->startScope();
$this->renderActiveFilters();
$rendercontext = array(
'table' => $this,
'renderer' => $this->renderer,
'search' => $this->searchConfig,
'columns' => $this->columns,
'data' => $this->result
);
\Doku_Event::createAndTrigger(
'PLUGIN_STRUCT_RENDER_AGGREGATION_TABLE',
$rendercontext,
array($this, 'renderTable')
);
// export handle
$this->renderExportControls();
$this->finishScope();
}
/**
* Render the default aggregation table
*/
public function renderTable($rendercontext)
{
$this->renderer->table_open();
// header
$this->renderer->tablethead_open();
$this->renderColumnHeaders();
$this->renderDynamicFilters();
$this->renderer->tablethead_close();
if ($this->resultCount) {
// actual data
$this->renderer->tabletbody_open();
$this->renderResult();
$this->renderer->tabletbody_close();
// footer (tfoot is develonly currently)
if (method_exists($this->renderer, 'tabletfoot_open')) $this->renderer->tabletfoot_open();
$this->renderSums();
$this->renderPagingControls();
if (method_exists($this->renderer, 'tabletfoot_close')) $this->renderer->tabletfoot_close();
} else {
// nothing found
$this->renderEmptyResult();
}
// table close
$this->renderer->table_close();
}
/**
* Adds additional info to document and renderer in XHTML mode
*
* @see finishScope()
*/
protected function startScope()
{
// unique identifier for this aggregation
$this->renderer->info['struct_table_hash'] = md5(var_export($this->data, true));
// wrapping div
if ($this->mode != 'xhtml') return;
$this->renderer->doc .= "<div class=\"structaggregation\">";
}
/**
* Closes the table and anything opened in startScope()
*
* @see startScope()
*/
protected function finishScope()
{
// remove identifier from renderer again
if (isset($this->renderer->info['struct_table_hash'])) {
unset($this->renderer->info['struct_table_hash']);
}
// wrapping div
if ($this->mode != 'xhtml') return;
$this->renderer->doc .= '</div>';
}
/**
* Displays info about the currently applied filters
*/
protected function renderActiveFilters()
{
if ($this->mode != 'xhtml') return;
$dynamic = $this->searchConfig->getDynamicParameters();
$filters = $dynamic->getFilters();
if (!$filters) return;
$fltrs = array();
foreach ($filters as $column => $filter) {
list($comp, $value) = $filter;
// display the filters in a human readable format
foreach ($this->columns as $col) {
if ($column === $col->getFullQualifiedLabel()) {
$column = $col->getTranslatedLabel();
}
}
$fltrs[] = sprintf('"%s" %s "%s"', $column, $this->helper->getLang("comparator $comp"), $value);
}
$this->renderer->doc .= '<div class="filter">';
$this->renderer->doc .= '<h4>' . sprintf($this->helper->getLang('tablefilteredby'), hsc(implode(' & ', $fltrs))) . '</h4>';
$this->renderer->doc .= '<div class="resetfilter">';
$this->renderer->internallink($this->id, $this->helper->getLang('tableresetfilter'));
$this->renderer->doc .= '</div>';
$this->renderer->doc .= '</div>';
}
/**
* Shows the column headers with links to sort by column
*/
protected function renderColumnHeaders()
{
$this->renderer->tablerow_open();
// additional column for row numbers
if (!empty($this->data['rownumbers'])) {
$this->renderer->tableheader_open();
$this->renderer->cdata('#');
$this->renderer->tableheader_close();
}
// show all headers
foreach ($this->columns as $num => $column) {
$header = '';
if (isset($this->data['headers'][$num])) {
$header = $this->data['headers'][$num];
}
// use field label if no header was set
if (blank($header)) {
if (is_a($column, 'dokuwiki\plugin\struct\meta\Column')) {
$header = $column->getTranslatedLabel();
} else {
$header = 'column ' . $num; // this should never happen
}
}
// simple mode first
if ($this->mode != 'xhtml') {
$this->renderer->tableheader_open();
$this->renderer->cdata($header);
$this->renderer->tableheader_close();
continue;
}
// still here? create custom header for more flexibility
// width setting, widths are prevalidated, no escape needed
$width = '';
if (isset($this->data['widths'][$num]) && $this->data['widths'][$num] != '-') {
$width = ' style="min-width: ' . $this->data['widths'][$num] . ';' .
'max-width: ' . $this->data['widths'][$num] . ';"';
}
// prepare data attribute for inline edits
if (
!is_a($column, '\dokuwiki\plugin\struct\meta\PageColumn') &&
!is_a($column, '\dokuwiki\plugin\struct\meta\RevisionColumn')
) {
$data = 'data-field="' . hsc($column->getFullQualifiedLabel()) . '"';
} else {
$data = '';
}
// sort indicator and link
$sortclass = '';
$sorts = $this->searchConfig->getSorts();
$dynamic = $this->searchConfig->getDynamicParameters();
$dynamic->setSort($column, true);
if (isset($sorts[$column->getFullQualifiedLabel()])) {
list(/*colname*/, $currentSort) = $sorts[$column->getFullQualifiedLabel()];
if ($currentSort) {
$sortclass = 'sort-down';
$dynamic->setSort($column, false);
} else {
$sortclass = 'sort-up';
}
}
$link = wl($this->id, $dynamic->getURLParameters());
// output XHTML header
$this->renderer->doc .= "<th $width $data>";
$this->renderer->doc .= '<a href="' . $link . '" class="' . $sortclass . '" title="' . $this->helper->getLang('sort') . '">' . hsc($header) . '</a>';
$this->renderer->doc .= '</th>';
}
$this->renderer->tablerow_close();
}
/**
* Is the result set currently dynamically filtered?
* @return bool
*/
protected function isDynamicallyFiltered()
{
if ($this->mode != 'xhtml') return false;
if (!$this->data['dynfilters']) return false;
$dynamic = $this->searchConfig->getDynamicParameters();
return (bool) $dynamic->getFilters();
}
/**
* Add input fields for dynamic filtering
*/
protected function renderDynamicFilters()
{
if ($this->mode != 'xhtml') return;
if (!$this->data['dynfilters']) return;
if (is_a($this->renderer, 'renderer_plugin_dw2pdf')) {
return;
}
global $conf;
$this->renderer->doc .= '<tr class="dataflt">';
// add extra column for row numbers
if ($this->data['rownumbers']) {
$this->renderer->doc .= '<th></th>';
}
// each column gets a form
foreach ($this->columns as $column) {
$this->renderer->doc .= '<th>';
{
$form = new \Doku_Form(array('method' => 'GET', 'action' => wl($this->id)));
unset($form->_hidden['sectok']); // we don't need it here
if (!$conf['userewrite']) $form->addHidden('id', $this->id);
// current value
$dynamic = $this->searchConfig->getDynamicParameters();
$filters = $dynamic->getFilters();
if (isset($filters[$column->getFullQualifiedLabel()])) {
list(, $current) = $filters[$column->getFullQualifiedLabel()];
$dynamic->removeFilter($column);
} else {
$current = '';
}
// Add current request params
$params = $dynamic->getURLParameters();
foreach ($params as $key => $val) {
$form->addHidden($key, $val);
}
// add input field
$key = $column->getFullQualifiedLabel() . $column->getType()->getDefaultComparator();
$form->addElement(form_makeField('text', SearchConfigParameters::$PARAM_FILTER . '[' . $key . ']', $current, ''));
$this->renderer->doc .= $form->getForm();
}
$this->renderer->doc .= '</th>';
}
$this->renderer->doc .= '</tr>';
}
/**
* Display the actual table data
*/
protected function renderResult()
{
foreach ($this->result as $rownum => $row) {
$data = array(
'id' => $this->id,
'mode' => $this->mode,
'renderer' => $this->renderer,
'searchConfig' => $this->searchConfig,
'data' => $this->data,
'rownum' => &$rownum,
'row' => &$row,
);
$evt = new \Doku_Event('PLUGIN_STRUCT_AGGREGATIONTABLE_RENDERRESULTROW', $data);
if ($evt->advise_before()) {
$this->renderResultRow($rownum, $row);
}
$evt->advise_after();
}
}
/**
* Render a single result row
*
* @param int $rownum
* @param array $row
*/
protected function renderResultRow($rownum, $row)
{
$this->renderer->tablerow_open();
// add data attribute for inline edit
if ($this->mode == 'xhtml') {
$pid = $this->resultPIDs[$rownum];
$rid = $this->resultRids[$rownum];
$rev = $this->resultRevs[$rownum];
$this->renderer->doc = substr(rtrim($this->renderer->doc), 0, -1); // remove closing '>'
$this->renderer->doc .= ' data-pid="' . hsc($pid) . '" data-rev="' . $rev . '" data-rid="' . $rid . '">';
}
// row number column
if (!empty($this->data['rownumbers'])) {
$this->renderer->tablecell_open();
$searchConfigConf = $this->searchConfig->getConf();
$this->renderer->cdata($rownum + $searchConfigConf['offset'] + 1);
$this->renderer->tablecell_close();
}
/** @var Value $value */
foreach ($row as $colnum => $value) {
$align = isset($this->data['align'][$colnum]) ? $this->data['align'][$colnum] : null;
$this->renderer->tablecell_open(1, $align);
$value->render($this->renderer, $this->mode);
$this->renderer->tablecell_close();
// summarize
if ($this->data['summarize'] && is_numeric($value->getValue())) {
if (!isset($this->sums[$colnum])) {
$this->sums[$colnum] = 0;
}
$this->sums[$colnum] += $value->getValue();
}
}
$this->renderer->tablerow_close();
}
/**
* Renders an information row for when no results were found
*/
protected function renderEmptyResult()
{
$this->renderer->tablerow_open();
$this->renderer->tablecell_open(count($this->columns) + $this->data['rownumbers'], 'center');
$this->renderer->cdata($this->helper->getLang('none'));
$this->renderer->tablecell_close();
$this->renderer->tablerow_close();
}
/**
* Add sums if wanted
*/
protected function renderSums()
{
if (empty($this->data['summarize'])) return;
$this->renderer->info['struct_table_meta'] = true;
if ($this->mode == 'xhtml') {
/** @noinspection PhpMethodParametersCountMismatchInspection */
$this->renderer->tablerow_open('summarize');
} else {
$this->renderer->tablerow_open();
}
if ($this->data['rownumbers']) {
$this->renderer->tableheader_open();
$this->renderer->tableheader_close();
}
$len = count($this->columns);
for ($i = 0; $i < $len; $i++) {
$this->renderer->tableheader_open(1, $this->data['align'][$i]);
if (!empty($this->sums[$i])) {
$this->renderer->cdata('∑ ');
$this->columns[$i]->getType()->renderValue($this->sums[$i], $this->renderer, $this->mode);
} else {
if ($this->mode == 'xhtml') {
$this->renderer->doc .= ' ';
}
}
$this->renderer->tableheader_close();
}
$this->renderer->tablerow_close();
$this->renderer->info['struct_table_meta'] = false;
}
/**
* Adds paging controls to the table
*/
protected function renderPagingControls()
{
if (empty($this->data['limit'])) return;
if ($this->mode != 'xhtml') return;
$this->renderer->info['struct_table_meta'] = true;
$this->renderer->tablerow_open();
$this->renderer->tableheader_open((count($this->columns) + ($this->data['rownumbers'] ? 1 : 0)));
$offset = $this->data['offset'];
// prev link
if ($offset) {
$prev = $offset - $this->data['limit'];
if ($prev < 0) {
$prev = 0;
}
$dynamic = $this->searchConfig->getDynamicParameters();
$dynamic->setOffset($prev);
$link = wl($this->id, $dynamic->getURLParameters());
$this->renderer->doc .= '<a href="' . $link . '" class="prev">' . $this->helper->getLang('prev') . '</a>';
}
// next link
if ($this->resultCount > $offset + $this->data['limit']) {
$next = $offset + $this->data['limit'];
$dynamic = $this->searchConfig->getDynamicParameters();
$dynamic->setOffset($next);
$link = wl($this->id, $dynamic->getURLParameters());
$this->renderer->doc .= '<a href="' . $link . '" class="next">' . $this->helper->getLang('next') . '</a>';
}
$this->renderer->tableheader_close();
$this->renderer->tablerow_close();
$this->renderer->info['struct_table_meta'] = true;
}
/**
* Adds CSV export controls
*/
protected function renderExportControls()
{
if ($this->mode != 'xhtml') return;
if (empty($this->data['csv'])) return;
if (!$this->resultCount) return;
$dynamic = $this->searchConfig->getDynamicParameters();
$params = $dynamic->getURLParameters();
$params['hash'] = $this->renderer->info['struct_table_hash'];
// FIXME apply dynamic filters
$link = exportlink($this->id, 'struct_csv', $params);
$this->renderer->doc .= '<a href="' . $link . '" class="export mediafile mf_csv">' . $this->helper->getLang('csvexport') . '</a>';
}
}