1<?php
2/**
3 * Strata Basic, data entry 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 * List syntax for basic query handling.
13 */
14class syntax_plugin_strata_list extends syntax_plugin_strata_select {
15    function connectTo($mode) {
16        $this->Lexer->addSpecialPattern('<list'.$this->helper->fieldsShortPattern().'* *>\s*?\n.+?\n\s*?</list>',$mode, 'plugin_strata_list');
17    }
18
19    function handleHeader($header, &$result, &$typemap) {
20        return preg_replace('/(^<list)|( *>$)/','',$header);
21    }
22
23    function render($mode, Doku_Renderer $R, $data) {
24        if($data == array() || isset($data['error'])) {
25            if($mode == 'xhtml' || $mode == 'odt') {
26                $R->listu_open();
27                $R->listitem_open(1);
28                $R->listcontent_open();
29                $this->displayError($mode, $R, $data);
30                $R->listcontent_close();
31                $R->listitem_close();
32                $R->listu_close();
33            }
34
35            return;
36        }
37
38        $query = $this->prepareQuery($data['query']);
39
40        // execute the query
41        $result = $this->triples->queryRelations($query);
42
43        if($result == false) {
44            if($mode == 'xhtml' || $mode == 'odt') {
45                $R->listu_open();
46                $R->listitem_open(1);
47                $R->listcontent_open();
48                $R->emphasis_open();
49                $R->cdata(sprintf($this->helper->getLang('content_error_explanation'),'Strata list'));
50                $R->emphasis_close();
51                $R->listcontent_close();
52                $R->listitem_close();
53                $R->listu_close();
54            }
55
56            return;
57        }
58
59        // prepare all 'columns'
60        $fields = array();
61        foreach($data['fields'] as $meta) {
62            $fields[] = array(
63                'variable'=>$meta['variable'],
64                'caption'=>$meta['caption'],
65                'type'=>$this->util->loadType($meta['type']),
66                'typeName'=>$meta['type'],
67                'hint'=>$meta['hint'],
68                'aggregate'=>$this->util->loadAggregate($meta['aggregate']),
69                'aggergateHint'=>$meta['aggregateHint']
70            );
71        }
72
73
74        if($mode == 'xhtml' || $mode == 'odt') {
75            // render header
76            $this->ui_container_open($mode, $R, $data, array('strata-container', 'strata-container-list'));
77
78            $this->util->renderCaptions($mode, $R, $fields);
79
80            $R->listu_open();
81
82            // render each row
83            $itemcount = 0;
84            foreach($result as $row) {
85                if($mode == 'xhtml') {
86                    $R->doc .= '<li class="level1 strata-item" data-strata-order="'.($itemcount++).'">'.DOKU_LF;
87                } else {
88                    $R->listitem_open(1);
89                }
90                $R->listcontent_open();
91
92                $fieldCount = 0;
93
94                foreach($fields as $f) {
95                    $values = $f['aggregate']->aggregate($row[$f['variable']], $f['aggregateHint']);
96                    if(!count($values)) continue;
97                    if($fieldCount>1) $R->cdata('; ');
98                    if($fieldCount==1) $R->cdata(' (');
99                    $this->util->renderField($mode, $R, $this->triples, $values, $f['typeName'], $f['hint'], $f['type'], $f['variable']);
100                    $fieldCount++;
101                }
102
103                if($fieldCount>1) $R->cdata(')');
104
105                $R->listcontent_close();
106                if($mode == 'xhtml') {
107                    $R->doc.= '</li>'.DOKU_LF;
108                } else {
109                    $R->listitem_close();
110                }
111            }
112            $result->closeCursor();
113
114            $R->listu_close();
115            $this->ui_container_close($mode, $R);
116
117            return true;
118        } elseif($mode == 'metadata') {
119            // render all rows in metadata mode to enable things like backlinks
120            foreach($result as $row) {
121                foreach($fields as $f) {
122                    $this->util->renderField($mode, $R, $this->triples, $f['aggregate']->aggregate($row[$f['variable']],$f['aggregateHint']), $f['typeName'], $f['hint'], $f['type'], $f['variable']);
123                }
124            }
125            $result->closeCursor();
126
127            return true;
128        }
129
130        return false;
131    }
132}
133