1<?php
2/**
3 * DokuWiki Plugin rowmove (Ajax Component)
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author lisps
7 * @author peterfromearth
8 */
9if (!defined('DOKU_INC')) die();
10class action_plugin_rowmove extends DokuWiki_Action_Plugin {
11
12    /**
13     * Register the eventhandlers
14     */
15    function register(Doku_Event_Handler $controller) {
16        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
17        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, '_ajax_call');
18    }
19
20    /**
21     * Inserts the toolbar button
22     */
23    function insert_button(Doku_Event $event, $param) {
24        $event->data[] = array(
25            'type'   => 'format',
26            'title' => 'Rowmove',
27            'icon'   => '../../plugins/rowmove/images/toolicon.png',
28            'sample' => 'CHECK HELP',
29            'open' => '<rowmove>',
30            'close'=>'',
31            'insert'=>'',
32        );
33    }
34
35    function _ajax_call(Doku_Event $event, $param) {
36        if ($event->data !== 'plugin_rowmove') {
37            return;
38        }
39        //no other ajax call handlers needed
40        $event->stopPropagation();
41        $event->preventDefault();
42
43        /* @var $INPUT \Input */
44        global $INPUT;
45
46        #Variables
47        $tablenr = $INPUT->int('index');
48        $index   = $INPUT->int('idx_row');
49        $index2  = $INPUT->int('idx_row2');
50
51        /* @var $Hajax \helper_plugin_ajaxedit */
52        $Hajax = $this->loadHelper('ajaxedit');
53
54        $data=$Hajax->getWikiPage();
55
56        $zeilen = explode("\n",$data);
57
58        $itable = -1; //Tabllen zähler
59        $irow = 0;	//Zeilenzähler
60        $key1=array();
61        $key2=array();
62        $col_notclosed=0;
63        $pagemod = 0;
64        foreach($zeilen as $key=>$zeile) { //durchlaufen der Zeilen
65            if( preg_match("/^[\|\^].*/",$zeile) || $col_notclosed) { //Am Anfang der Zeile ein "|" oder "^" heist Tablle
66                if(preg_match("/^[\|\^].*/",$zeile) && !$pagemod && $col_notclosed) { //Doch am Anfang einer Zeile, aber nur wenn pagemod=aus und zeile nicht geschlossen
67                    $col_notclosed = 0;
68                    $irow++;
69                }
70
71                if($irow == 0 && !$col_notclosed) $itable++; //Tabellenanfang gefunden
72                if(($itable == $tablenr && $irow == $index)) {
73                    $key1[]=$key;
74                } //1. Zeile gefunden
75                if($itable == $tablenr && $irow == $index2){
76                    $key2[]=$key;
77                } //2. Zeile gefunden
78
79                if(!in_array(substr(trim($zeile),-1),array('|','^'))){ //kein Zeilenendezeichen vorhanden
80                    $col_notclosed = 1;
81
82                    if(substr_count($zeile,'pagemod')%2 == 1 && (strpos($zeile,'<pagemod') !== false || strpos($zeile,'</pagemod') !== false)) {
83                        if($pagemod) { //ende von pagemod
84                            $pagemod = 0;
85                            $irow++;
86                            $col_notclosed = 0;
87
88                        } else { //pagemod beginnt
89                            $pagemod = 1;
90                        }
91                    }
92                } else { //normales Zeilenende
93                    $col_notclosed = 0;
94                    $irow++;
95                }
96
97
98            } else { //Tabllenende
99                $col_notclosed = 0;
100                $irow=0;
101            }
102        }
103
104        //Vertauschen der Zeilen
105        $copy1 = array_slice($zeilen,reset($key1),count($key1));
106        $copy2 = array_slice($zeilen,reset($key2),count($key2));
107        array_splice($zeilen,reset($key1),count($key1),$copy2);
108
109        array_splice($zeilen,reset($key2),count($key2),$copy1);
110
111        //Zusammenfügen
112        $data = implode("\n",$zeilen);
113
114        $summary ="Row Change Table:".$tablenr." Row:".$index." to Row:".$index2." ";
115        $Hajax->saveWikiPage($data,$summary,true);
116    }
117}
118