xref: /plugin/struct/meta/AggregationCloud.php (revision 0699ff471e0a3fdbfd89b3462b1206a6cb772de2)
1<?php
2namespace dokuwiki\plugin\struct\meta;
3class AggregationCloud {
4    /**
5     * @var string the page id of the page this is rendered to
6     */
7    protected $id;
8    /**
9     * @var string the Type of renderer used
10     */
11    protected $mode;
12    /**
13     * @var \Doku_Renderer the DokuWiki renderer used to create the output
14     */
15    protected $renderer;
16    /**
17     * @var SearchConfig the configured search - gives access to columns etc.
18     */
19    protected $searchConfig;
20    /**
21     * @var Column[] the list of columns to be displayed
22     */
23    protected $columns;
24    /**
25     * @var  Value[][] the search result
26     */
27    protected $result;
28    /**
29     * @var int number of all results
30     */
31    protected $resultCount;
32    /**
33     * @var string[] the result PIDs for each row
34     */
35    protected $resultPIDs;
36    /**
37     * @var array for summing up columns
38     */
39    protected $sums;
40    /**
41     * @var bool skip full table when no results found
42     */
43    protected $simplenone = true;
44    /**
45     * @todo we might be able to get rid of this helper and move this to SearchConfig
46     * @var \helper_plugin_struct_config
47     */
48    protected $helper;
49    /**
50     * Initialize the Aggregation renderer and executes the search
51     *
52     * You need to call @see render() on the resulting object.
53     *
54     * @param string $id
55     * @param string $mode
56     * @param \Doku_Renderer $renderer
57     * @param SearchConfig $searchConfig
58     */
59    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchCloud $searchConfig) {
60        $this->id = $id;
61        $this->mode = $mode;
62        $this->renderer = $renderer;
63        $this->searchConfig = $searchConfig;
64        $this->data = $searchConfig->getConf();
65        $this->columns = $searchConfig->getColumns();
66        $this->result = $this->searchConfig->execute();
67        $this->resultCount = $this->searchConfig->getCount();
68        $this->helper = plugin_load('helper', 'struct_config');
69    }
70
71    /**
72     * Create the table on the renderer
73     */
74    public function render() {
75        $this->startScope();
76        $this->renderer->doc .= '<ul>';
77        foreach ($this->result as $result) {
78            $this->renderResult($result);
79        }
80        $this->renderer->doc .= '</ul>';
81        $this->finishScope();
82        return;
83    }
84    /**
85     * Adds additional info to document and renderer in XHTML mode
86     *
87     * @see finishScope()
88     */
89    protected function startScope() {
90        // unique identifier for this aggregation
91        $this->renderer->info['struct_cloud_hash'] = md5(var_export($this->data, true));
92        // wrapping div
93        if($this->mode != 'xhtml') return;
94        $this->renderer->doc .= "<div class=\"structaggregation cloudaggregation\">";
95    }
96    /**
97     * Closes the table and anything opened in startScope()
98     *
99     * @see startScope()
100     */
101    protected function finishScope() {
102        // remove identifier from renderer again
103        if(isset($this->renderer->info['struct_cloud_hash'])) {
104            unset($this->renderer->info['struct_cloud_hash']);
105        }
106        // wrapping div
107        if($this->mode != 'xhtml') return;
108        $this->renderer->doc .= '</div>';
109    }
110    protected function renderResult($result) {
111        /**
112         * @var Value $value
113         */
114        $value = $result['tag'];
115        $count = $result['count'];
116        if ($value->isEmpty()) {
117            return;
118        }
119
120        $raw = $value->getRawValue();
121        if (is_array($raw)) {
122            $raw = $raw[0];
123        }
124        $schema = $this->data['schemas'][0][0];
125        $col = $value->getColumn()->getLabel();
126        $this->renderer->doc .= '<li><div class="li">';
127        $this->renderer->doc .= '<div data-count="'. $count.'" class="' . $value->getColumn()->getLabel() . '">';
128        //$value->render($this->renderer, $this->mode);
129        $this->renderer->internallink("?flt[$schema.$col*~]=" . urlencode($raw),$raw);
130        if ($column < $this->resultCount) {
131            $this->renderer->doc .= ' ';
132        }
133        $this->renderer->doc .= '</div>';
134        $this->renderer->doc .= '</div></li>';
135    }
136}