xref: /plugin/struct/meta/AggregationList.php (revision 8925ba298f12cecb837c2c249b3a592d289eabe2)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5
6/**
7 * Class AggregationList
8 *
9 * @package dokuwiki\plugin\struct\meta
10 */
11class AggregationList {
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        $this->id = $id;
57        $this->mode = $mode;
58        $this->renderer = $renderer;
59        $this->searchConfig = $searchConfig;
60        $this->data = $searchConfig->getConf();
61        $this->columns = $searchConfig->getColumns();
62
63        $this->result = $this->searchConfig->execute();
64        $this->resultColumnCount = count($this->columns);
65        $this->resultPIDs = $this->searchConfig->getPids();
66    }
67
68    /**
69     * Create the list on the renderer
70     */
71    public function render() {
72
73        $this->startScope();
74
75        $this->renderer->listu_open();
76
77        foreach ($this->result as $result) {
78            $this->renderer->listitem_open(1);
79            $this->renderer->listcontent_open();
80            $this->renderListItem($result);
81            $this->renderer->listcontent_close();
82            $this->renderer->listitem_close();
83        }
84
85        $this->renderer->listu_close();
86
87        $this->finishScope();
88
89        return;
90    }
91
92    /**
93     * Adds additional info to document and renderer in XHTML mode
94     *
95     * @see finishScope()
96     */
97    protected function startScope() {
98        // wrapping div
99        if($this->mode != 'xhtml') return;
100        $this->renderer->doc .= "<div class=\"structaggregation listaggregation\">";
101    }
102
103    /**
104     * Closes anything opened in startScope()
105     *
106     * @see startScope()
107     */
108    protected function finishScope() {
109        // wrapping div
110        if($this->mode != 'xhtml') return;
111        $this->renderer->doc .= '</div>';
112    }
113
114    /**
115     * @param $resultrow
116     */
117    protected function renderListItem($resultrow) {
118        $sepbyheaders = $this->searchConfig->getConf()['sepbyheaders'];
119        $headers = $this->searchConfig->getConf()['headers'];
120
121        /**
122         * @var Value $value
123         */
124        foreach ($resultrow as $column => $value) {
125            if ($value->isEmpty()) {
126                continue;
127            }
128            if ($sepbyheaders && !empty($headers[$column])) {
129                if ($this->mode == 'xhtml') {
130                    $this->renderer->doc .= '<span class="struct_header">' . hsc($headers[$column]) . '</span>';
131                } else {
132                    $this->renderer->cdata($headers[$column]);
133                }
134            }
135            if ($this->mode == 'xhtml') {
136                $type = 'struct_' . strtolower($value->getColumn()->getType()->getClass());
137                $this->renderer->doc .= '<div class="' . $type . '">';
138            }
139            $value->render($this->renderer, $this->mode);
140            if ($column < $this->resultColumnCount) {
141                $this->renderer->cdata(' ');
142            }
143            if ($this->mode == 'xhtml') {
144                $this->renderer->doc .= '</div>';
145            }
146        }
147
148    }
149}
150