1<?php
2// must be run within DokuWiki
3if(!defined('DOKU_INC')) die();
4
5if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
6require_once DOKU_PLUGIN.'syntax.php';
7
8
9/**
10 * All DokuWiki plugins to extend the parser/rendering mechanism
11 * need to inherit from this class
12 */
13class syntax_plugin_dtable extends DokuWiki_Syntax_Plugin {
14
15    function getPType(){
16       return 'block';
17    }
18
19    function getType() { return 'container'; }
20    function getSort() { return 400; }
21    function getAllowedTypes() {return array('container','formatting','substition');}
22
23    function connectTo($mode) { $this->Lexer->addEntryPattern('<dtab[0-9][0-9]>(?=.*</dtable>)',$mode,'plugin_dtable'); }
24    function postConnect() { $this->Lexer->addExitPattern('</dtable>','plugin_dtable'); }
25
26
27    function handle($match, $state, $pos, Doku_Handler $handler) {
28		global $INFO;
29        switch ($state) {
30          case DOKU_LEXER_ENTER :
31	      try {
32		  		$table_nr = (int) substr($match, 5, 2);
33                return array($state, array($pos, $table_nr, p_get_metadata($INFO['id'], 'dtable_pages')));
34	      } catch(Exception $e)
35	      {
36		  return array($state, false);
37	      }
38
39          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
40          case DOKU_LEXER_EXIT :       return array($state, '');
41        }
42        return array();
43    }
44
45    function render($mode, Doku_Renderer $renderer, $data) {
46	global $ID;
47	if($mode == 'xhtml')
48	{
49	   list($state,$match) = $data;
50	   switch ($state) {
51	     case DOKU_LEXER_ENTER :
52
53		if($match != false)
54		{
55		    if (auth_quickaclcheck($ID) >= AUTH_EDIT)
56		    {
57			$dtable =& plugin_load('helper', 'dtable');
58
59			$pos = $match[0];
60			$table_nr = $match[1];
61			$dtable_pages = $match[2];
62
63			$id = $dtable_pages[$table_nr];
64			$filepath = wikiFN( $id );
65
66			$start_line = $dtable->line_nr($pos, $filepath) ;
67
68			//search for first row
69			$file_cont = explode("\n", io_readWikiPage($filepath, $id));
70
71			$start_line++;
72
73			$i = $start_line;
74			while( $i <  count($file_cont) && strpos($file_cont[$i], '|') !== 0 && strpos($file_cont[$i], '^') !== 0)
75				$i += 1;
76			$start_line = $i;
77
78
79			$raw_lines = '';
80
81			while( $i <  count($file_cont) && strpos( $file_cont[ $i ], '</dtable>' ) !== 0 )
82			{
83				$raw_lines .= $file_cont[$i]."\n";
84				$i++;
85			}
86
87			$lines = $dtable->rows($raw_lines, $id, $start_line);
88
89			$json = new JSON();
90
91			$renderer->doc .= '<form class="dtable dynamic_form" id="dtable_'.$start_line.'_'.$id.'" action="'.$DOKU_BASE.'lib/exe/ajax.php" method="post" data-table="'.htmlspecialchars($json->encode($lines)).'">';
92			$renderer->doc .= '<input type="hidden" class="dtable_field" value="dtable" name="call">';
93
94			//This is needed to correct linkwiz behaviour.
95			$renderer->doc .= '<input type="hidden" class="dtable_field" value="'.$id.'" name="id">';
96
97		    }
98		}
99	    break;
100
101	    case DOKU_LEXER_UNMATCHED :  $renderer->doc .= $renderer->_xmlEntities($match); break;
102	    case DOKU_LEXER_EXIT :
103		if (auth_quickaclcheck($ID) >= AUTH_EDIT)
104		    $renderer->doc .= "</form>";
105
106	    break;
107	   }
108	   return true;
109	}
110        return false;
111    }
112}
113