xref: /plugin/struct/meta/AggregationCloud.php (revision bb348952b33dec6acfc8eba6238b61f0ee413a94)
1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5class AggregationCloud {
6
7    /**
8     * @var string the page id of the page this is rendered to
9     */
10    protected $id;
11
12    /**
13     * @var string the Type of renderer used
14     */
15    protected $mode;
16
17    /**
18     * @var \Doku_Renderer the DokuWiki renderer used to create the output
19     */
20    protected $renderer;
21
22    /**
23     * @var SearchConfig the configured search - gives access to columns etc.
24     */
25    protected $searchConfig;
26
27    /**
28     * @var Column[] the list of columns to be displayed
29     */
30    protected $columns;
31
32    /**
33     * @var  Value[][] the search result
34     */
35    protected $result;
36
37    /**
38     * @var int number of all results
39     */
40    protected $resultCount;
41
42    /**
43     * Initialize the Aggregation renderer and executes the search
44     *
45     * You need to call @see render() on the resulting object.
46     *
47     * @param string $id
48     * @param string $mode
49     * @param \Doku_Renderer $renderer
50     * @param SearchConfig $searchConfig
51     */
52    public function __construct($id, $mode, \Doku_Renderer $renderer, SearchCloud $searchConfig) {
53        $this->id = $id;
54        $this->mode = $mode;
55        $this->renderer = $renderer;
56        $this->searchConfig = $searchConfig;
57        $this->data = $searchConfig->getConf();
58        $this->columns = $searchConfig->getColumns();
59        $this->result = $this->searchConfig->execute();
60        $this->resultCount = $this->searchConfig->getCount();
61
62        $this->max = $this->result[0]['count'];
63        $this->min = end($this->result)['count'];
64    }
65
66    /**
67     * Create the cloud on the renderer
68     */
69    public function render() {
70
71        $this->sortResults();
72
73        $this->startScope();
74        $this->startList();
75        foreach ($this->result as $result) {
76            $this->renderTag($result);
77        }
78        $this->finishList();
79        $this->finishScope();
80        return;
81    }
82
83    /**
84     * Adds additional info to document and renderer in XHTML mode
85     *
86     * @see finishScope()
87     */
88    protected function startScope() {
89        // wrapping div
90        if($this->mode != 'xhtml') return;
91        $this->renderer->doc .= "<div class=\"structcloud\">";
92    }
93
94    /**
95     * Closes the table and anything opened in startScope()
96     *
97     * @see startScope()
98     */
99    protected function finishScope() {
100        // wrapping div
101        if($this->mode != 'xhtml') return;
102        $this->renderer->doc .= '</div>';
103    }
104
105    /**
106     * Render a tag of the cloud
107     *
108     * @param ['tag' => Value, 'count' => int] $result
109     */
110    protected function renderTag($result) {
111        /**
112         * @var Value $value
113         */
114        $value = $result['tag'];
115        $count = $result['count'];
116        if ($value->isEmpty()) {
117            return;
118        }
119
120        $type = strtolower($value->getColumn()->getType()->getClass());
121        $weight = $this->getWeight($count, $this->min, $this->max);
122
123        if (!empty($this->data['target'])) {
124            $target = $this->data['target'];
125        } else {
126            global $INFO;
127            $target = $INFO['id'];
128        }
129
130        $tagValue = $value->getDisplayValue();
131        if (empty($tagValue)) {
132            $tagValue = $value->getRawValue();
133        }
134        if (is_array($tagValue)) {
135            $tagValue = $tagValue[0];
136        }
137        $key = $value->getColumn()->getFullQualifiedLabel() . '*~';
138        $filter = SearchConfigParameters::$PARAM_FILTER . "[$key]=" . urlencode($tagValue);
139
140        $this->renderer->listitem_open(1);
141        $this->renderer->listcontent_open();
142
143        if($this->mode == 'xhtml') {
144            $this->renderer->doc .= "<div style='font-size:$weight%' data-count='$count' class='cloudtag struct_$type'>";
145        }
146
147        $value->renderAsTagCloudLink($this->renderer, $this->mode, $target, $filter, $weight);
148
149        if($this->mode == 'xhtml') {
150            $this->renderer->doc .= '</div>';
151        }
152
153        $this->renderer->listcontent_close();
154        $this->renderer->listitem_close();
155    }
156
157    /**
158     * This interpolates the weight between 70 and 150 based on $min, $max and $current
159     *
160     * @param int $current
161     * @param int $min
162     * @param int $max
163     * @return int
164     */
165    protected function getWeight($current, $min, $max) {
166        if ($min == $max) {
167            return 100;
168        }
169        return round(($current - $min)/($max - $min) * 80 + 70);
170    }
171
172    /**
173     * Sort the list of results
174     */
175    protected function sortResults() {
176        usort($this->result, function ($a, $b) {
177            $asort = $a['tag']->getColumn()->getType()->getSortString($a['tag']);
178            $bsort = $b['tag']->getColumn()->getType()->getSortString($b['tag']);
179            if ($asort < $bsort) {
180                return -1;
181            }
182            if ($asort > $bsort) {
183                return 1;
184            }
185            return 0;
186        });
187    }
188
189    protected function startList() {
190        $this->renderer->listu_open();
191    }
192
193    protected function finishList() {
194        $this->renderer->listu_close();
195    }
196}
197