1<?php
2/**
3 * Strata Basic, table plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     François KAAG (francois.kaag@cardynal.fr)
7 * Derived from the original table command
8 */
9
10if (!defined('DOKU_INC')) die('Meh.');
11
12/**
13 * Radar syntax for basic query handling.
14 */
15class syntax_plugin_strata_radar extends syntax_plugin_strata_select {
16    function connectTo($mode) {
17        $this->Lexer->addSpecialPattern('<radar'.$this->helper->fieldsShortPattern().'* *>\s*?\n.+?\n\s*?</radar>',$mode, 'plugin_strata_radar');
18    }
19
20    function getUISettingUI($hasUIBlock) {
21        return array('choices' => array('none' => array('none', 'no', 'n'), 'generic' => array('generic', 'g'), 'radar' => array('radar', 't')), 'default' => 'radar');
22    }
23
24    function handleHeader($header, &$result, &$typemap) {
25        return preg_replace('/(^<radar)|( *>$)/','',$header);
26    }
27
28    function render($mode, Doku_Renderer $R, $data) {
29        if($data == array() || isset($data['error'])) {
30            if($mode == 'xhtml' || $mode == 'odt') {
31                $R->table_open();
32                $R->tablerow_open();
33                $R->tablecell_open();
34                $this->displayError($mode, $R, $data);
35                $R->tablecell_close();
36                $R->tablerow_close();
37                $R->table_close();
38            }
39            return;
40        }
41
42        $query = $this->prepareQuery($data['query']);
43
44        // execute the query
45        $result = $this->triples->queryRelations($query);
46
47        // prepare all columns
48        foreach($data['fields'] as $meta) {
49            $fields[] = array(
50                'variable'=>$meta['variable'],
51                'caption'=>$meta['caption'],
52                'type'=>$this->util->loadType($meta['type']),
53                'typeName'=>$meta['type'],
54                'hint'=>$meta['hint'],
55                'aggregate'=>$this->util->loadAggregate($meta['aggregate']),
56                'aggregateHint'=>$meta['aggregateHint']
57            );
58        }
59
60        if($mode == 'xhtml' || $mode == 'odt') {
61			$labelSet=array_column($fields,'caption');
62			$labelSet[]=$labelSet[0];
63			$labels=json_encode($labelSet);
64            if($mode == 'xhtml') { $R->doc .= '</thead>'.DOKU_LF; }
65            if($result != false) {
66                // render each row
67                $itemcount = 0;
68                foreach($result as $row) {
69					$valueSet=[];
70                    foreach($fields as $f) {
71						$valueSet[]= intval($f['aggregate']->aggregate($row[$f['variable']],$f['aggregateHint'])[0]);
72                    }
73					$valueSet[]=$valueSet[0];
74					$values = json_encode($valueSet);
75           if($mode == 'xhtml') { $R->doc .= '</tbody>'.DOKU_LF; }
76                }
77                $result->closeCursor();
78            } else {
79				$R->table_open();
80                $R->tablerow_open();
81                $R->tablecell_open(count($fields));
82                $R->emphasis_open();
83                $R->cdata(sprintf($this->helper->getLang('content_error_explanation'),'Strata table'));
84                $R->emphasis_close();
85                $R->tablecell_close();
86                $R->tablerow_close();
87				$R->table_close();
88            }
89			$uid=uniqid();
90
91			$R->doc .= "
92<script>function draw() {
93data = [{type: 'scatterpolar',r: ".$values.",theta: ".$labels.",fill: 'toself'}]
94layout = {polar: {radialaxis: {visible: true,range: [0, 100]}},showlegend: false}
95graph = document.getElementById('".$uid."');
96Plotly.newPlot(graph, data, layout)
97}
98window.onload=draw;
99</script>
100<style>svg {height : auto}</style>
101<div id='".$uid."'></div>";
102            return true;
103        } elseif($mode == 'metadata') {
104            if($result == false) return;
105
106            // render all rows in metadata mode to enable things like backlinks
107            foreach($result as $row) {
108                foreach($fields as $f) {
109                    $this->util->renderField($mode, $R, $this->triples, $f['aggregate']->aggregate($row[$f['variable']],$f['aggregateHint']), $f['typeName'], $f['hint'], $f['type'], $f['variable']);
110                }
111            }
112            $result->closeCursor();
113
114            return true;
115        }
116
117        return false;
118    }
119}
120