xref: /plugin/struct/meta/Aggregation.php (revision 67f70d54d911c6c842e298d4a97622e9d01815b3)
1d90aa848SAndreas Gohr<?php
2d90aa848SAndreas Gohr
3d90aa848SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
4d90aa848SAndreas Gohr
5d90aa848SAndreas Gohr/**
6d90aa848SAndreas Gohr * Base contract for Aggregations
7d90aa848SAndreas Gohr *
8d90aa848SAndreas Gohr * @package dokuwiki\plugin\struct\meta
9d90aa848SAndreas Gohr */
1000f71f17SAndreas Gohrabstract class Aggregation
1100f71f17SAndreas Gohr{
12d90aa848SAndreas Gohr    /** @var string the page id of the page this is rendered to */
13d90aa848SAndreas Gohr    protected $id;
14d90aa848SAndreas Gohr
15d90aa848SAndreas Gohr    /** @var string the Type of renderer used */
16d90aa848SAndreas Gohr    protected $mode;
17d90aa848SAndreas Gohr
18d90aa848SAndreas Gohr    /** @var \Doku_Renderer the DokuWiki renderer used to create the output */
19d90aa848SAndreas Gohr    protected $renderer;
20d90aa848SAndreas Gohr
21d90aa848SAndreas Gohr    /** @var SearchConfig the configured search - gives access to columns etc. */
22d90aa848SAndreas Gohr    protected $searchConfig;
23d90aa848SAndreas Gohr
24d90aa848SAndreas Gohr    /** @var Column[] the list of columns to be displayed */
25d90aa848SAndreas Gohr    protected $columns;
26d90aa848SAndreas Gohr
27d90aa848SAndreas Gohr    /** @var  Value[][] the search result */
28d90aa848SAndreas Gohr    protected $result;
29d90aa848SAndreas Gohr
30d90aa848SAndreas Gohr    /** @var int number of all results */
31d90aa848SAndreas Gohr    protected $resultCount;
32d90aa848SAndreas Gohr
33c2839eb2SAnna Dabrowska    /** @var string usually a div, but AggregationValue needs to be wrapped in a span */
34c2839eb2SAnna Dabrowska    protected $tagName = 'div';
35c2839eb2SAnna Dabrowska
36d90aa848SAndreas Gohr    /**
37d90aa848SAndreas Gohr     * @todo we might be able to get rid of this helper and move this to SearchConfig
38d90aa848SAndreas Gohr     * @var \helper_plugin_struct_config
39d90aa848SAndreas Gohr     */
40d90aa848SAndreas Gohr    protected $helper;
41d90aa848SAndreas Gohr
42d90aa848SAndreas Gohr    /**
43d90aa848SAndreas Gohr     * @var array the original configuration data
44d90aa848SAndreas Gohr     */
45d90aa848SAndreas Gohr    protected $data;
46d90aa848SAndreas Gohr
47d90aa848SAndreas Gohr    /**
48d90aa848SAndreas Gohr     * Initialize the Aggregation renderer and executes the search
49d90aa848SAndreas Gohr     *
50f4dbd3b0SAndreas Gohr     * You need to call startScope(), render() and finishScope() on the resulting object.
51d90aa848SAndreas Gohr     *
52f4dbd3b0SAndreas Gohr     * @param string $id The page this is rendered to
53f4dbd3b0SAndreas Gohr     * @param string $mode The renderer format
54f4dbd3b0SAndreas Gohr     * @param \Doku_Renderer $renderer The renderer to use for output
55f4dbd3b0SAndreas Gohr     * @param SearchConfig $searchConfig The configured search object to use for displaying the data
56d90aa848SAndreas Gohr     */
57d90aa848SAndreas Gohr    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
58d90aa848SAndreas Gohr    {
59d90aa848SAndreas Gohr        $this->id = $id;
60d90aa848SAndreas Gohr        $this->mode = $mode;
61d90aa848SAndreas Gohr        $this->renderer = $renderer;
62d90aa848SAndreas Gohr        $this->searchConfig = $searchConfig;
63*67f70d54SAnna Dabrowska        $this->data = $searchConfig->getConf();
64*67f70d54SAnna Dabrowska        $this->columns = $searchConfig->getColumns();
65*67f70d54SAnna Dabrowska        $this->result = $this->searchConfig->execute();
66*67f70d54SAnna Dabrowska        $this->resultCount = $this->searchConfig->getCount();
67d90aa848SAndreas Gohr        $this->helper = plugin_load('helper', 'struct_config');
68d90aa848SAndreas Gohr    }
69d90aa848SAndreas Gohr
70d90aa848SAndreas Gohr    /**
71d90aa848SAndreas Gohr     * Returns the page id the aggregation is used on
72d90aa848SAndreas Gohr     */
73d90aa848SAndreas Gohr    public function getID()
74d90aa848SAndreas Gohr    {
75d90aa848SAndreas Gohr        return $this->id;
76d90aa848SAndreas Gohr    }
77d90aa848SAndreas Gohr
78d90aa848SAndreas Gohr    /**
79af0ce8d2SAndreas Gohr     * Return the list of classes that should be added to the scope when rendering XHTML
80af0ce8d2SAndreas Gohr     *
81af0ce8d2SAndreas Gohr     * @return string[]
82af0ce8d2SAndreas Gohr     */
83af0ce8d2SAndreas Gohr    public function getScopeClasses()
84af0ce8d2SAndreas Gohr    {
85af0ce8d2SAndreas Gohr        // we're all aggregations
86af0ce8d2SAndreas Gohr        $classes = ['structaggregation'];
87af0ce8d2SAndreas Gohr
88af0ce8d2SAndreas Gohr        // which type of aggregation are we?
89af0ce8d2SAndreas Gohr        $class = get_class($this);
90af0ce8d2SAndreas Gohr        $class = substr($class, strrpos($class, "\\") + 1);
91af0ce8d2SAndreas Gohr        $class = strtolower($class);
92af0ce8d2SAndreas Gohr        $classes[] = 'struct' . $class;
93af0ce8d2SAndreas Gohr
94af0ce8d2SAndreas Gohr        // config options
95af0ce8d2SAndreas Gohr        if ($this->data['nesting']) {
96af0ce8d2SAndreas Gohr            $classes[] = 'is-nested';
97af0ce8d2SAndreas Gohr        }
98af0ce8d2SAndreas Gohr        if ($this->data['index']) {
99af0ce8d2SAndreas Gohr            $classes[] = 'is-indexed';
100af0ce8d2SAndreas Gohr        }
101af0ce8d2SAndreas Gohr
102af0ce8d2SAndreas Gohr        // custom classes
103af0ce8d2SAndreas Gohr        $classes = array_merge($classes, $this->data['classes']);
104af0ce8d2SAndreas Gohr        return $classes;
105af0ce8d2SAndreas Gohr    }
106af0ce8d2SAndreas Gohr
107af0ce8d2SAndreas Gohr    /**
108af0ce8d2SAndreas Gohr     * Render the actual output to the renderer
109d90aa848SAndreas Gohr     *
110d90aa848SAndreas Gohr     * @param bool $showNotFound show a not found message when no data available?
111d90aa848SAndreas Gohr     */
112d90aa848SAndreas Gohr    abstract public function render($showNotFound = false);
113af0ce8d2SAndreas Gohr
114af0ce8d2SAndreas Gohr    /**
115af0ce8d2SAndreas Gohr     * Adds additional info to document and renderer in XHTML mode
116af0ce8d2SAndreas Gohr     *
117af0ce8d2SAndreas Gohr     * Called before render()
118af0ce8d2SAndreas Gohr     *
119af0ce8d2SAndreas Gohr     * @see finishScope()
120af0ce8d2SAndreas Gohr     */
121af0ce8d2SAndreas Gohr    public function startScope()
122af0ce8d2SAndreas Gohr    {
123af0ce8d2SAndreas Gohr        if ($this->mode == 'xhtml') {
124af0ce8d2SAndreas Gohr            $classes = $this->getScopeClasses();
1254b00515cSAnna Dabrowska
1264b00515cSAnna Dabrowska            $hash = $this->renderer->info['struct_table_hash'] ?? '';
1274b00515cSAnna Dabrowska            $id = $hash ? " id=\"$hash\" " : '';
1284b00515cSAnna Dabrowska
129c2839eb2SAnna Dabrowska            $this->renderer->doc .= '<' . $this->tagName .  $id . ' class="' . implode(' ', $classes) . '">';
130af0ce8d2SAndreas Gohr        }
131af0ce8d2SAndreas Gohr    }
132af0ce8d2SAndreas Gohr
133af0ce8d2SAndreas Gohr    /**
134af0ce8d2SAndreas Gohr     * Closes anything opened in startScope()
135af0ce8d2SAndreas Gohr     *
136af0ce8d2SAndreas Gohr     * Called after render()
137af0ce8d2SAndreas Gohr     *
138af0ce8d2SAndreas Gohr     * @see startScope()
139af0ce8d2SAndreas Gohr     */
140af0ce8d2SAndreas Gohr    public function finishScope()
141af0ce8d2SAndreas Gohr    {
142af0ce8d2SAndreas Gohr        if ($this->mode == 'xhtml') {
143c2839eb2SAnna Dabrowska            $this->renderer->doc .= '</' . $this->tagName . '>';
144af0ce8d2SAndreas Gohr        }
145af0ce8d2SAndreas Gohr    }
146d90aa848SAndreas Gohr}
147