1<?php
2/**
3 * Table Plot Syntax Plugin
4 *
5 *  Plots a table using JQPlot libraries
6 *
7 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author     Tom Cafferty <tcafferty@glocalfocal.com>
9 */
10if(!defined('DOKU_INC')) define('DOKU_INC',(dirname(__FILE__).'/../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13require_once (DOKU_INC.'inc/parserutils.php');
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class syntax_plugin_tableplot extends DokuWiki_Syntax_Plugin {
20
21    function getInfo() {
22        return array(
23            'author' => 'Tom Cafferty',
24            'email'  => 'tcafferty@glocalfocal.com',
25            'date'   => '2011-12-29',
26            'name'   => 'tableplot',
27            'desc'   => 'Integrate JQPlot to plot a table ',
28            'url'    => 'http://www.dokuwiki.org/plugin:tableplot'
29        );
30    }
31    /**
32     * What kind of syntax are we?
33     */
34    function getType(){
35        return 'substition';
36    }
37
38    function getPType(){
39        return 'block';
40    }
41
42    /**
43     * Where to sort in?
44     */
45    function getSort(){
46        return 160;
47    }
48
49    /**
50     * Connect pattern to lexer
51     */
52    function connectTo($mode) {
53      $this->Lexer->addSpecialPattern('<tableplot>.*?</tableplot>',$mode,'plugin_tableplot');
54    }
55
56    /**
57     * Handle the match
58     */
59    function handle($match, $state, $pos, &$handler){
60        parse_str($match, $return);
61        return $return;
62    }
63
64/**
65 *
66 * Create timeline output
67 *
68 * @author   Tom Cafferty <tcafferty@glocalfocal.com>
69 *
70 */
71    function render($mode, &$renderer, $data) {
72      global $INFO;
73      global $ID;
74      global $conf;
75
76      // store meta info for this page
77      if($mode == 'metadata'){
78        $renderer->meta['plugin']['tableplot'] = true;
79        return true;
80      }
81
82      if($mode != 'xhtml') return false;
83
84      $series      = "series:'";
85      $idstr       = "id:'";
86      $orient      = "orient:'";
87      $firstSeries = "firstSeries:'";
88      $lastSeries  = "lastSeries:'";
89      $labels      = "labels:'";
90      $xaxis       = "xaxis:'";
91      $dataStart   = "dataStart:'";
92      $dataEnd     = "dataEnd:'";
93      $position    = "position:'";
94      $width       = "width:'";
95      $height      = "height:'";
96      $placement   = "placement:'";
97      $dataTransform  = "dataTransform:";
98      $labelTransform = "labelTransform:";
99      $xaxisTransform = "xaxisTransform:";
100
101      // Initialize settings from user input or conf file
102      if (isset($data['orient']))
103        $orient .= $data['orient'] . "',";
104      else
105        $orient .= $this->getConf('orient'). "',";
106
107      if (isset($data['series']))
108        $series .= $data['series'] . "',";
109      else
110        $series .= $this->getConf('series'). "',";
111
112      if (isset($data['firstSeries']))
113        $firstSeries .= $data['firstSeries'] . "',";
114      else
115        $firstSeries = '';
116
117       if (isset($data['lastSeries']))
118        $lastSeries .= $data['lastSeries']. "',";
119      else
120        $lastSeries = '';
121
122      if (isset($data['labels']))
123        $labels .= $data['labels']. "',";
124      else
125        $labels = '';
126
127      if (isset($data['xaxis']))
128        $xaxis .= $data['xaxis']. "',";
129      else
130        $xaxis = '';
131
132       if (isset($data['dataStart']))
133        $dataStart .= $data['dataStart']. "',";
134      else
135        $dataStart = '';
136
137       if (isset($data['dataEnd']))
138        $dataEnd .= $data['dataEnd']. "',";
139      else
140        $dataEnd = '';
141
142       if (isset($data['position']))
143        $position .= $data['position']. "',";
144      else
145        $position = '';
146
147       if (isset($data['width']))
148        $width .= $data['width']. "',";
149      else
150        $width = '';
151
152       if (isset($data['height']))
153        $height .= $data['height']. "',";
154      else
155        $height = '';
156
157       if (isset($data['placement']))
158        $placement .= $data['placement']. "',";
159      else
160        $placement = '';
161
162       if (isset($data['dataTransform']))
163        $dataTransform .= $data['dataTransform']. ",";
164      else
165        $dataTransform = '';
166
167       if (isset($data['labelTransform']))
168        $labelTransform .= $data['labelTransform']. ",";
169      else
170        $labelTransform = '';
171
172       if (isset($data['xaxisTransform']))
173        $xaxisTransform .= $data['xaxisTransform']. ",";
174      else
175        $xaxisTransform = '';
176
177       if (isset($data['plotArgs']))
178        $plotArgs = $data['plotArgs'];
179      else
180        $plotArgs = '';
181
182       if (isset($data['id'])) {
183           $id = $data['id'];
184           $idstr .= $data['id']."'";
185       }
186      else {
187          $id = $this->getConf('id');
188          $idstr .= $id."'";
189      }
190
191    // invoke graph Table
192      $grafArgs = '<script type="text/javascript"> jQuery(\'#'.$id.'\').tablePlot({'.$orient .$series. $firstSeries . $lastSeries . $labels . $xaxis .$dataStart .$dataEnd . $position .$width .$height .$dataTransform .$labelTransform .$xaxisTransform .$placement .$idstr;
193      $allArgs = rtrim($grafArgs,',') . '},' .$plotArgs;
194      $cmd = rtrim($allArgs,',') . '); </script>';
195
196      $renderer->doc .= $cmd;
197	  return true;
198    }
199}