1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5/**
6 * Class AggregationValue
7 *
8 * @package dokuwiki\plugin\struct\meta
9 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
10 * @author  Iain Hallam <iain@nineworlds.net>
11 */
12class AggregationValue
13{
14    /**
15     * @var string the page id of the page this is rendered to
16     */
17    protected $id;
18    /**
19     * @var string the Type of renderer used
20     */
21    protected $mode;
22    /**
23     * @var Doku_Renderer the DokuWiki renderer used to create the output
24     */
25    protected $renderer;
26    /**
27     * @var SearchConfig the configured search - gives access to columns etc.
28     */
29    protected $searchConfig;
30
31    /**
32     * @var Column the column to be displayed
33     */
34    protected $column;
35
36    /**
37     * @var  Value[][] the search result
38     */
39    protected $result;
40
41    /**
42     * @var int number of all results
43     */
44    protected $resultCount;
45
46    /**
47     * @todo we might be able to get rid of this helper and move this to SearchConfig
48     * @var helper_plugin_struct_config
49     */
50    protected $helper;
51
52    /**
53     * Initialize the Aggregation renderer and executes the search
54     *
55     * You need to call @param string $id
56     * @param string $mode
57     * @param Doku_Renderer $renderer
58     * @param SearchConfig $searchConfig
59     * @see render() on the resulting object.
60     *
61     */
62    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
63    {
64        // Parameters
65        $this->id = $id;
66        $this->mode = $mode;
67        $this->renderer = $renderer;
68        $this->searchConfig = $searchConfig;
69
70        // Search info
71        $this->data = $this->searchConfig->getConf();
72        $columns = $this->searchConfig->getColumns();
73        $this->column = $columns[0];
74
75        // limit to first result
76        $this->searchConfig->setLimit(1);
77        $this->searchConfig->setOffset(0);
78
79        // Run the search
80        $result = $this->searchConfig->execute();
81        $this->resultCount = $this->searchConfig->getCount();
82
83        // Change from two-dimensional array with one entry to one-dimensional array
84        if (sizeof($result) > 0)
85            $this->result = $result[0];
86
87        // Load helper
88        $this->helper = plugin_load('helper', 'struct_config');
89    }
90
91    /**
92     * Create the output on the renderer
93     *
94     * @param int $show_not_found Whether to display the default text for no records
95     */
96    public function render($show_not_found = 0)
97    {
98        $this->startScope();
99
100        // Check that we actually got a result
101        if ($this->resultCount) {
102            $this->renderValue($this->result);
103        } else {
104            if ($show_not_found) {
105                $this->renderer->cdata($this->helper->getLang('none'));
106            }
107        }
108
109        $this->finishScope();
110
111        return;
112    }
113
114    /**
115     * Adds additional info to document and renderer in XHTML mode
116     *
117     * @see finishScope()
118     */
119    protected function startScope()
120    {
121        // wrapping span
122        if ($this->mode != 'xhtml') {
123            return;
124        }
125        $this->renderer->doc .= "<span class=\"structaggregation valueaggregation\">";
126    }
127
128    /**
129     * Closes anything opened in startScope()
130     *
131     * @see startScope()
132     */
133    protected function finishScope()
134    {
135        // wrapping span
136        if ($this->mode != 'xhtml') {
137            return;
138        }
139        $this->renderer->doc .= '</span>';
140    }
141
142    /**
143     * @param $resultrow
144     */
145    protected function renderValue($resultrow)
146    {
147        // @var  Value  $value
148        foreach ($resultrow as $column => $value) {
149            if ($value->isEmpty()) {
150                continue;
151            }
152            if ($this->mode == 'xhtml') {
153                $type = 'struct_' . strtolower($value->getColumn()->getType()->getClass());
154                $this->renderer->doc .= '<span class="' . $type . '">';
155            }
156            $value->render($this->renderer, $this->mode);
157            if ($this->mode == 'xhtml') {
158                $this->renderer->doc .= '</span>';
159            }
160        }
161    }
162}
163