xref: /plugin/struct/meta/AggregationValue.php (revision 00f71f1792dd6eaba8ea1c269be2cb4d9d469640)
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 extends Aggregation
13{
14    /**
15     * @var Column the column to be displayed
16     */
17    protected $column;
18
19    /** @inheritdoc */
20    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
21    {
22        // limit to first result
23        $searchConfig->setLimit(1);
24        $searchConfig->setOffset(0);
25
26        parent::__construct($id, $mode, $renderer, $searchConfig);
27    }
28
29    /**
30     * Create the output on the renderer
31     *
32     * @param int $show_not_found Whether to display the default text for no records
33     */
34    public function render($show_not_found = 0)
35    {
36        $this->startScope();
37
38        // Check that we actually got a result
39        if ($this->resultCount) {
40            $this->renderValue($this->result[0]); // only one result
41        } else {
42            if ($show_not_found) {
43                $this->renderer->cdata($this->helper->getLang('none'));
44            }
45        }
46
47        $this->finishScope();
48    }
49
50    /**
51     * Adds additional info to document and renderer in XHTML mode
52     *
53     * @see finishScope()
54     */
55    protected function startScope()
56    {
57        // wrapping span
58        if ($this->mode != 'xhtml') {
59            return;
60        }
61        $this->renderer->doc .= "<span class=\"structaggregation valueaggregation\">";
62    }
63
64    /**
65     * Closes anything opened in startScope()
66     *
67     * @see startScope()
68     */
69    protected function finishScope()
70    {
71        // wrapping span
72        if ($this->mode != 'xhtml') {
73            return;
74        }
75        $this->renderer->doc .= '</span>';
76    }
77
78    /**
79     * @param Value[] $resultrow
80     */
81    protected function renderValue($resultrow)
82    {
83        foreach ($resultrow as $column => $value) {
84            if ($value->isEmpty()) {
85                continue;
86            }
87            if ($this->mode == 'xhtml') {
88                $type = 'struct_' . strtolower($value->getColumn()->getType()->getClass());
89                $this->renderer->doc .= '<span class="' . $type . '">';
90            }
91            $value->render($this->renderer, $this->mode);
92            if ($this->mode == 'xhtml') {
93                $this->renderer->doc .= '</span>';
94            }
95        }
96    }
97}
98