xref: /plugin/struct/meta/AggregationValue.php (revision 0549dcc5bc88d4f9d923acdd09931d8d51be7097)
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        $this->result = $result[0];
85
86        // Load helper
87        $this->helper = plugin_load('helper', 'struct_config');
88    }
89
90    /**
91     * Create the output on the renderer
92     *
93     * @param int $show_not_found Whether to display the default text for no records
94     */
95    public function render($show_not_found = 0)
96    {
97        $this->startScope();
98
99        // Check that we actually got a result
100        if ($this->resultCount) {
101            $this->renderValue($this->result);
102        } else {
103            if ($show_not_found) {
104                $this->renderer->cdata($this->helper->getLang('none'));
105            }
106        }
107
108        $this->finishScope();
109
110        return;
111    }
112
113    /**
114     * Adds additional info to document and renderer in XHTML mode
115     *
116     * @see finishScope()
117     */
118    protected function startScope()
119    {
120        // wrapping span
121        if ($this->mode != 'xhtml') {
122            return;
123        }
124        $this->renderer->doc .= "<span class=\"structaggregation valueaggregation\">";
125    }
126
127    /**
128     * Closes anything opened in startScope()
129     *
130     * @see startScope()
131     */
132    protected function finishScope()
133    {
134        // wrapping span
135        if ($this->mode != 'xhtml') {
136            return;
137        }
138        $this->renderer->doc .= '</span>';
139    }
140
141    /**
142     * @param $resultrow
143     */
144    protected function renderValue($resultrow)
145    {
146        // @var  Value  $value
147        foreach ($resultrow as $column => $value) {
148            if ($value->isEmpty()) {
149                continue;
150            }
151            if ($this->mode == 'xhtml') {
152                $type = 'struct_' . strtolower($value->getColumn()->getType()->getClass());
153                $this->renderer->doc .= '<span class="' . $type . '">';
154            }
155            $value->render($this->renderer, $this->mode);
156            if ($this->mode == 'xhtml') {
157                $this->renderer->doc .= '</span>';
158            }
159        }
160    }
161}
162