1<?php
2/**
3 * Strata Basic, table plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Brend Wanders <b.wanders@utwente.nl>
7 */
8
9if (!defined('DOKU_INC')) die('Meh.');
10
11/**
12 * Table syntax for basic query handling.
13 */
14class syntax_plugin_strata_table extends syntax_plugin_strata_select {
15    function connectTo($mode) {
16        $this->Lexer->addSpecialPattern('<table'.$this->helper->fieldsShortPattern().'* *>\s*?\n.+?\n\s*?</table>',$mode, 'plugin_strata_table');
17    }
18
19    function getUISettingUI($hasUIBlock) {
20        return array('choices' => array('none' => array('none', 'no', 'n'), 'generic' => array('generic', 'g'), 'table' => array('table', 't')), 'default' => 'table');
21    }
22
23    function handleHeader($header, &$result, &$typemap) {
24        return preg_replace('/(^<table)|( *>$)/','',$header);
25    }
26
27    function render($mode, Doku_Renderer $R, $data) {
28        if($data == array() || isset($data['error'])) {
29            if($mode == 'xhtml' || $mode == 'odt') {
30                $R->table_open();
31                $R->tablerow_open();
32                $R->tablecell_open();
33                $this->displayError($mode, $R, $data);
34                $R->tablecell_close();
35                $R->tablerow_close();
36                $R->table_close();
37            }
38            return;
39        }
40
41        $query = $this->prepareQuery($data['query']);
42
43        // execute the query
44        $result = $this->triples->queryRelations($query);
45
46        // prepare all columns
47        foreach($data['fields'] as $meta) {
48            $fields[] = array(
49                'variable'=>$meta['variable'],
50                'caption'=>$meta['caption'],
51                'type'=>$this->util->loadType($meta['type']),
52                'typeName'=>$meta['type'],
53                'hint'=>$meta['hint'],
54                'aggregate'=>$this->util->loadAggregate($meta['aggregate']),
55                'aggregateHint'=>$meta['aggregateHint']
56            );
57        }
58
59        if($mode == 'xhtml' || $mode == 'odt') {
60            // render header
61            $this->ui_container_open($mode, $R, $data, array('strata-container', 'strata-container-table'));
62            $R->table_open();
63            if($mode == 'xhtml') { $R->doc .= '<thead>'.DOKU_LF; }
64            $R->tablerow_open();
65
66            // render all columns
67            foreach($fields as $f) {
68                $R->tableheader_open();
69                if($mode == 'xhtml') { $R->doc .= '<span class="strata-caption" data-field="'.hsc($f['variable']).'">'; }
70                $R->cdata($f['caption']);
71                if($mode == 'xhtml') { $R->doc .= '</span>'.DOKU_LF; }
72                $R->tableheader_close();
73            }
74            $R->tablerow_close();
75            if($mode == 'xhtml') { $R->doc .= '</thead>'.DOKU_LF; }
76
77            if($result != false) {
78                // render each row
79                $itemcount = 0;
80                foreach($result as $row) {
81                    if($mode == 'xhtml') { $R->doc .= '<tbody class="strata-item" data-strata-order="'.($itemcount++).'">'.DOKU_LF; }
82                    $R->tablerow_open();
83                    foreach($fields as $f) {
84                        $R->tablecell_open();
85                        $this->util->renderField($mode, $R, $this->triples, $f['aggregate']->aggregate($row[$f['variable']],$f['aggregateHint']), $f['typeName'], $f['hint'], $f['type'], $f['variable']);
86                        $R->tablecell_close();
87                    }
88                    $R->tablerow_close();
89                    if($mode == 'xhtml') { $R->doc .= '</tbody>'.DOKU_LF; }
90                }
91                $result->closeCursor();
92            } else {
93                $R->tablerow_open();
94                $R->tablecell_open(count($fields));
95                $R->emphasis_open();
96                $R->cdata(sprintf($this->helper->getLang('content_error_explanation'),'Strata table'));
97                $R->emphasis_close();
98                $R->tablecell_close();
99                $R->tablerow_close();
100            }
101
102            $R->table_close();
103            $this->ui_container_close($mode, $R);
104
105            return true;
106        } elseif($mode == 'metadata') {
107            if($result == false) return;
108
109            // render all rows in metadata mode to enable things like backlinks
110            foreach($result as $row) {
111                foreach($fields as $f) {
112                    $this->util->renderField($mode, $R, $this->triples, $f['aggregate']->aggregate($row[$f['variable']],$f['aggregateHint']), $f['typeName'], $f['hint'], $f['type'], $f['variable']);
113                }
114            }
115            $result->closeCursor();
116
117            return true;
118        }
119
120        return false;
121    }
122}
123