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