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