xref: /plugin/struct/meta/Aggregation.php (revision ba7f5789bbbcab95d7a655f6ec50a97b731b40d5)
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
27c2839eb2SAnna Dabrowska    /** @var string usually a div, but AggregationValue needs to be wrapped in a span */
28c2839eb2SAnna Dabrowska    protected $tagName = 'div';
29c2839eb2SAnna Dabrowska
30d90aa848SAndreas Gohr    /**
31d90aa848SAndreas Gohr     * @todo we might be able to get rid of this helper and move this to SearchConfig
32d90aa848SAndreas Gohr     * @var \helper_plugin_struct_config
33d90aa848SAndreas Gohr     */
34d90aa848SAndreas Gohr    protected $helper;
35d90aa848SAndreas Gohr
36d90aa848SAndreas Gohr    /**
37d90aa848SAndreas Gohr     * @var array the original configuration data
38d90aa848SAndreas Gohr     */
39d90aa848SAndreas Gohr    protected $data;
40d90aa848SAndreas Gohr
41d90aa848SAndreas Gohr    /**
42d90aa848SAndreas Gohr     * Initialize the Aggregation renderer and executes the search
43d90aa848SAndreas Gohr     *
44f4dbd3b0SAndreas Gohr     * You need to call startScope(), render() and finishScope() on the resulting object.
45d90aa848SAndreas Gohr     *
46f4dbd3b0SAndreas Gohr     * @param string $id The page this is rendered to
47f4dbd3b0SAndreas Gohr     * @param string $mode The renderer format
48f4dbd3b0SAndreas Gohr     * @param \Doku_Renderer $renderer The renderer to use for output
49f4dbd3b0SAndreas Gohr     * @param SearchConfig $searchConfig The configured search object to use for displaying the data
50d90aa848SAndreas Gohr     */
51d90aa848SAndreas Gohr    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $searchConfig)
52d90aa848SAndreas Gohr    {
53d90aa848SAndreas Gohr        $this->id = $id;
54d90aa848SAndreas Gohr        $this->mode = $mode;
55d90aa848SAndreas Gohr        $this->renderer = $renderer;
56d90aa848SAndreas Gohr        $this->searchConfig = $searchConfig;
57*67f70d54SAnna Dabrowska        $this->data = $searchConfig->getConf();
58*67f70d54SAnna Dabrowska        $this->columns = $searchConfig->getColumns();
59d90aa848SAndreas Gohr        $this->helper = plugin_load('helper', 'struct_config');
60d90aa848SAndreas Gohr    }
61d90aa848SAndreas Gohr
62d90aa848SAndreas Gohr    /**
63d90aa848SAndreas Gohr     * Returns the page id the aggregation is used on
64d90aa848SAndreas Gohr     */
65d90aa848SAndreas Gohr    public function getID()
66d90aa848SAndreas Gohr    {
67d90aa848SAndreas Gohr        return $this->id;
68d90aa848SAndreas Gohr    }
69d90aa848SAndreas Gohr
70d90aa848SAndreas Gohr    /**
71af0ce8d2SAndreas Gohr     * Return the list of classes that should be added to the scope when rendering XHTML
72af0ce8d2SAndreas Gohr     *
73af0ce8d2SAndreas Gohr     * @return string[]
74af0ce8d2SAndreas Gohr     */
75af0ce8d2SAndreas Gohr    public function getScopeClasses()
76af0ce8d2SAndreas Gohr    {
77af0ce8d2SAndreas Gohr        // we're all aggregations
78af0ce8d2SAndreas Gohr        $classes = ['structaggregation'];
79af0ce8d2SAndreas Gohr
80af0ce8d2SAndreas Gohr        // which type of aggregation are we?
81af0ce8d2SAndreas Gohr        $class = get_class($this);
82af0ce8d2SAndreas Gohr        $class = substr($class, strrpos($class, "\\") + 1);
83af0ce8d2SAndreas Gohr        $class = strtolower($class);
84af0ce8d2SAndreas Gohr        $classes[] = 'struct' . $class;
85af0ce8d2SAndreas Gohr
86af0ce8d2SAndreas Gohr        // config options
87af0ce8d2SAndreas Gohr        if ($this->data['nesting']) {
88af0ce8d2SAndreas Gohr            $classes[] = 'is-nested';
89af0ce8d2SAndreas Gohr        }
90af0ce8d2SAndreas Gohr        if ($this->data['index']) {
91af0ce8d2SAndreas Gohr            $classes[] = 'is-indexed';
92af0ce8d2SAndreas Gohr        }
93af0ce8d2SAndreas Gohr
94af0ce8d2SAndreas Gohr        // custom classes
95af0ce8d2SAndreas Gohr        $classes = array_merge($classes, $this->data['classes']);
96af0ce8d2SAndreas Gohr        return $classes;
97af0ce8d2SAndreas Gohr    }
98af0ce8d2SAndreas Gohr
99af0ce8d2SAndreas Gohr    /**
100af0ce8d2SAndreas Gohr     * Render the actual output to the renderer
101d90aa848SAndreas Gohr     *
102d90aa848SAndreas Gohr     * @param bool $showNotFound show a not found message when no data available?
103d90aa848SAndreas Gohr     */
104d90aa848SAndreas Gohr    abstract public function render($showNotFound = false);
105af0ce8d2SAndreas Gohr
106af0ce8d2SAndreas Gohr    /**
107af0ce8d2SAndreas Gohr     * Adds additional info to document and renderer in XHTML mode
108af0ce8d2SAndreas Gohr     *
109af0ce8d2SAndreas Gohr     * Called before render()
110af0ce8d2SAndreas Gohr     *
111af0ce8d2SAndreas Gohr     * @see finishScope()
112af0ce8d2SAndreas Gohr     */
113af0ce8d2SAndreas Gohr    public function startScope()
114af0ce8d2SAndreas Gohr    {
115af0ce8d2SAndreas Gohr        if ($this->mode == 'xhtml') {
116af0ce8d2SAndreas Gohr            $classes = $this->getScopeClasses();
1174b00515cSAnna Dabrowska
1184b00515cSAnna Dabrowska            $hash = $this->renderer->info['struct_table_hash'] ?? '';
1194b00515cSAnna Dabrowska            $id = $hash ? " id=\"$hash\" " : '';
1204b00515cSAnna Dabrowska
121c2839eb2SAnna Dabrowska            $this->renderer->doc .= '<' . $this->tagName .  $id . ' class="' . implode(' ', $classes) . '">';
122af0ce8d2SAndreas Gohr        }
123af0ce8d2SAndreas Gohr    }
124af0ce8d2SAndreas Gohr
125af0ce8d2SAndreas Gohr    /**
126af0ce8d2SAndreas Gohr     * Closes anything opened in startScope()
127af0ce8d2SAndreas Gohr     *
128af0ce8d2SAndreas Gohr     * Called after render()
129af0ce8d2SAndreas Gohr     *
130af0ce8d2SAndreas Gohr     * @see startScope()
131af0ce8d2SAndreas Gohr     */
132af0ce8d2SAndreas Gohr    public function finishScope()
133af0ce8d2SAndreas Gohr    {
134af0ce8d2SAndreas Gohr        if ($this->mode == 'xhtml') {
135c2839eb2SAnna Dabrowska            $this->renderer->doc .= '</' . $this->tagName . '>';
136af0ce8d2SAndreas Gohr        }
137af0ce8d2SAndreas Gohr    }
138d90aa848SAndreas Gohr}
139