xref: /plugin/struct/meta/AggregationList.php (revision ea5ad12ac615a67b2c935ce7472ece1c00189ba7)
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->doc .= '<ul>';
76
77        foreach ($this->result as $result) {
78            $this->renderListItem($result);
79        }
80
81        $this->renderer->doc .= '</ul>';
82
83        $this->finishScope();
84
85        return;
86    }
87
88    /**
89     * Adds additional info to document and renderer in XHTML mode
90     *
91     * @see finishScope()
92     */
93    protected function startScope() {
94        // wrapping div
95        if($this->mode != 'xhtml') return;
96        $this->renderer->doc .= "<div class=\"structaggregation listaggregation\">";
97    }
98
99    /**
100     * Closes anything opened in startScope()
101     *
102     * @see startScope()
103     */
104    protected function finishScope() {
105        // wrapping div
106        if($this->mode != 'xhtml') return;
107        $this->renderer->doc .= '</div>';
108    }
109
110    /**
111     * @param $resultrow
112     */
113    protected function renderListItem($resultrow) {
114        $this->renderer->doc .= '<li><div class="li">';
115        $sepbyheaders = $this->searchConfig->getConf()['sepbyheaders'];
116        $headers = $this->searchConfig->getConf()['headers'];
117
118        /**
119         * @var Value $value
120         */
121        foreach ($resultrow as $column => $value) {
122            if ($value->isEmpty()) {
123                continue;
124            }
125            if ($sepbyheaders && !empty($headers[$column])) {
126                $this->renderer->doc .= '<span class="struct_header">' . hsc($headers[$column]) . '</span>';
127            }
128            $type = 'struct_' . strtolower($value->getColumn()->getType()->getClass());
129            $this->renderer->doc .= '<div class="' . $type . '">';
130            $value->render($this->renderer, $this->mode);
131            if ($column < $this->resultColumnCount) {
132                $this->renderer->doc .= ' ';
133            }
134            $this->renderer->doc .= '</div>';
135        }
136
137        $this->renderer->doc .= '</div></li>';
138    }
139}
140