xref: /plugin/struct/meta/AggregationList.php (revision 76a74c230f7336dce2880f047cb3dbd1a2e8f0e7)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5/**
6 * Class AggregationList
7 *
8 * @package dokuwiki\plugin\struct\meta
9 */
10class AggregationList
11{
12
13    /**
14     * @var string the page id of the page this is rendered to
15     */
16    protected $id;
17    /**
18     * @var string the Type of renderer used
19     */
20    protected $mode;
21    /**
22     * @var \Doku_Renderer the DokuWiki renderer used to create the output
23     */
24    protected $renderer;
25    /**
26     * @var SearchConfig the configured search - gives access to columns etc.
27     */
28    protected $searchConfig;
29
30    /**
31     * @var Column[] the list of columns to be displayed
32     */
33    protected $columns;
34
35    /**
36     * @var  Value[][] the search result
37     */
38    protected $result;
39
40    /**
41     * @var int number of all results
42     */
43    protected $resultColumnCount;
44
45    /**
46     * Initialize the Aggregation renderer and executes the search
47     *
48     * You need to call @see render() on the resulting object.
49     *
50     * @param string $id
51     * @param string $mode
52     * @param \Doku_Renderer $renderer
53     * @param SearchConfig $searchConfig
54     */
55    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
56    {
57        $this->id = $id;
58        $this->mode = $mode;
59        $this->renderer = $renderer;
60        $this->searchConfig = $searchConfig;
61        $this->data = $searchConfig->getConf();
62        $this->columns = $searchConfig->getColumns();
63
64        $this->result = $this->searchConfig->execute();
65        $this->resultColumnCount = count($this->columns);
66        $this->resultPIDs = $this->searchConfig->getPids();
67    }
68
69    /**
70     * Create the list on the renderer
71     */
72    public function render()
73    {
74
75        $this->startScope();
76
77        $this->renderer->listu_open();
78
79        foreach ($this->result as $result) {
80            $this->renderer->listitem_open(1);
81            $this->renderer->listcontent_open();
82            $this->renderListItem($result);
83            $this->renderer->listcontent_close();
84            $this->renderer->listitem_close();
85        }
86
87        $this->renderer->listu_close();
88
89        $this->finishScope();
90
91        return;
92    }
93
94    /**
95     * Adds additional info to document and renderer in XHTML mode
96     *
97     * @see finishScope()
98     */
99    protected function startScope()
100    {
101        // wrapping div
102        if ($this->mode != 'xhtml') return;
103        $this->renderer->doc .= "<div class=\"structaggregation listaggregation\">";
104    }
105
106    /**
107     * Closes anything opened in startScope()
108     *
109     * @see startScope()
110     */
111    protected function finishScope()
112    {
113        // wrapping div
114        if ($this->mode != 'xhtml') return;
115        $this->renderer->doc .= '</div>';
116    }
117
118    /**
119     * @param $resultrow
120     */
121    protected function renderListItem($resultrow)
122    {
123        $sepbyheaders = $this->searchConfig->getConf()['sepbyheaders'];
124        $headers = $this->searchConfig->getConf()['headers'];
125
126        /**
127         * @var Value $value
128         */
129        foreach ($resultrow as $column => $value) {
130            if ($value->isEmpty()) {
131                continue;
132            }
133            if ($sepbyheaders && !empty($headers[$column])) {
134                if ($this->mode == 'xhtml') {
135                    $this->renderer->doc .= '<span class="struct_header">' . hsc($headers[$column]) . '</span>';
136                } else {
137                    $this->renderer->cdata($headers[$column]);
138                }
139            }
140            if ($this->mode == 'xhtml') {
141                $type = 'struct_' . strtolower($value->getColumn()->getType()->getClass());
142                $this->renderer->doc .= '<div class="' . $type . '">';
143            }
144            $value->render($this->renderer, $this->mode);
145            if ($column < $this->resultColumnCount) {
146                $this->renderer->cdata(' ');
147            }
148            if ($this->mode == 'xhtml') {
149                $this->renderer->doc .= '</div>';
150            }
151        }
152    }
153}
154