xref: /plugin/struct/meta/AggregationCloud.php (revision fa04b28c8e5db215f2d38751b0b1b540f2561b9a)
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, SearchConfig $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->search(); //$this->searchConfig->execute();
67        //$this->resultCount = $this->searchConfig->getCount();
68        //$this->resultPIDs = $this->searchConfig->getPids();
69        $this->helper = plugin_load('helper', 'struct_config');
70    }
71
72    public function search() {
73        $QB = new QueryBuilder;
74        $schema = $this->data['schemas'][0][0];
75        $colref = $this->columns[0]->getColref();
76        if ($this->columns[0]->getType()->isMulti()) {
77            $table = 'multi_' . $schema;
78            $col = 'value';
79            $QB->filters()->whereAnd('T1.colref='.$colref);
80        } else {
81            $table = 'data_' . $schema;
82            $col = 'col' . $colref;
83        }
84        $QB->addTable($table, 'T1');
85        $QB->addSelectColumn('T1', $col, 'tag');
86        $QB->addSelectStatement("COUNT(T1.$col)", 'count');
87        $QB->filters()->whereAnd('T1.latest=1');
88        $QB->addGroupByStatement('tag');
89        $QB->addOrderBy('tag');
90        /*
91          if ($min=$this->data['min']) {
92          $QB->filters()->whereAnd("count > $min");
93          }
94        */
95        $sql = $QB->getSQL();
96        $db = plugin_load('helper', 'struct_db')->getDB();
97        $res=$db->query($sql[0], $sql[1]);
98        $results = $db->res2arr($res);
99        $db->res_close($res);
100        foreach ($results as &$result) {
101            $result['tag'] = new Value($this->columns[0], $result['tag']);
102        }
103        return $results;
104    }
105
106    /**
107     * Create the table on the renderer
108     */
109    public function render() {
110        $this->startScope();
111        $this->renderer->doc .= '<ul>';
112        foreach ($this->result as $result) {
113            $this->renderResult($result);
114        }
115        $this->renderer->doc .= '</ul>';
116        $this->finishScope();
117        return;
118    }
119    /**
120     * Adds additional info to document and renderer in XHTML mode
121     *
122     * @see finishScope()
123     */
124    protected function startScope() {
125        // unique identifier for this aggregation
126        $this->renderer->info['struct_cloud_hash'] = md5(var_export($this->data, true));
127        // wrapping div
128        if($this->mode != 'xhtml') return;
129        $this->renderer->doc .= "<div class=\"structaggregation cloudaggregation\">";
130    }
131    /**
132     * Closes the table and anything opened in startScope()
133     *
134     * @see startScope()
135     */
136    protected function finishScope() {
137        // remove identifier from renderer again
138        if(isset($this->renderer->info['struct_cloud_hash'])) {
139            unset($this->renderer->info['struct_cloud_hash']);
140        }
141        // wrapping div
142        if($this->mode != 'xhtml') return;
143        $this->renderer->doc .= '</div>';
144    }
145    protected function renderResult($result) {
146        /**
147         * @var Value $value
148         */
149        $value = $result['tag'];
150        $count = $result['count'];
151        if ($value->isEmpty()) {
152            return;
153        }
154
155        $raw = $value->getRawValue();
156        if (is_array($raw)) {
157            $raw = $raw[0];
158        }
159        $schema = $this->data['schemas'][0][0];
160        $col = $value->getColumn()->getLabel();
161        $this->renderer->doc .= '<li><div class="li">';
162        $this->renderer->doc .= '<div data-count="'. $count.'" class="' . $value->getColumn()->getLabel() . '">';
163        //$value->render($this->renderer, $this->mode);
164        $this->renderer->internallink("?flt[$schema.$col*~]=$raw",$raw);
165        if ($column < $this->resultCount) {
166            $this->renderer->doc .= ' ';
167        }
168        $this->renderer->doc .= '</div>';
169        $this->renderer->doc .= '</div></li>';
170    }
171}