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    /**
16     * @var string the page id of the page this is rendered to
17     */
18    protected $id;
19    /**
20     * @var string the Type of renderer used
21     */
22    protected $mode;
23    /**
24     * @var Doku_Renderer the DokuWiki renderer used to create the output
25     */
26    protected $renderer;
27    /**
28     * @var SearchConfig the configured search - gives access to columns etc.
29     */
30    protected $searchConfig;
31
32    /**
33     * @var Column the column to be displayed
34     */
35    protected $column;
36
37    /**
38     * @var  Value[][] the search result
39     */
40    protected $result;
41
42    /**
43     * @var int number of all results
44     */
45    protected $resultCount;
46
47    /**
48     * @todo we might be able to get rid of this helper and move this to SearchConfig
49     * @var helper_plugin_struct_config
50     */
51    protected $helper;
52
53    /**
54     * Initialize the Aggregation renderer and executes the search
55     *
56     * You need to call @see render() on the resulting object.
57     *
58     * @param  string         $id
59     * @param  string         $mode
60     * @param  Doku_Renderer  $renderer
61     * @param  SearchConfig   $searchConfig
62     */
63    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
64    {
65        // Parameters
66        $this->id = $id;
67        $this->mode = $mode;
68        $this->renderer = $renderer;
69        $this->searchConfig = $searchConfig;
70
71        // Search info
72        $this->data = $this->searchConfig->getConf();
73        $columns = $this->searchConfig->getColumns();
74        $this->column = $columns[0];
75
76        // limit to first result
77        $this->searchConfig->setLimit(1);
78        $this->searchConfig->setOffset(0);
79
80        // Run the search
81        $result = $this->searchConfig->execute();
82        $this->resultCount = $this->searchConfig->getCount();
83
84        // Change from two-dimensional array with one entry to one-dimensional array
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