1<?php
2
3/**
4 * InlineEdit Action Plugin
5 *
6 *
7 * @author     peterfromearth
8 */
9
10if (!defined('DOKU_INC')) die();
11class action_plugin_inlineedit extends DokuWiki_Action_Plugin {
12
13    /**
14     * Register the eventhandlers
15     */
16    function register(Doku_Event_Handler $controller) {
17//         $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());
18        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, '_ajax_call');
19    }
20
21    /**
22     * Inserts the toolbar button
23     */
24    function insert_button(Doku_Event $event, $param) {
25        $event->data[] = array(
26            'type'   => 'format',
27            'title' => 'InlineEdit',
28            'icon'   => '../../plugins/inlineedit/images/inlineedit.png',
29            'sample' => '',
30            'open' => '<inlineedit>',
31            'close'=>'</inlineedit>',
32            'insert'=>'',
33        );
34    }
35
36    function _ajax_call(Doku_Event $event, $param) {
37        if ($event->data !== 'plugin_inlineedit') {
38            return;
39        }
40        //no other ajax call handlers needed
41        $event->stopPropagation();
42        $event->preventDefault();
43
44        /* @var $INPUT \Input */
45        global $INPUT;
46
47        $itemPos = $INPUT->int('id'); //input index on the server
48        $input_str = trim($INPUT->str('input')); //input string
49
50        /* @var $Hajax \helper_plugin_ajaxedit */
51        $Hajax = $this->loadHelper('ajaxedit');
52
53        $data=$Hajax->getWikiPage(); //
54
55        //find "our" fsinput fields
56        $found=explode("<inlineedit>",$data);
57
58        //find pagemods
59        @preg_match_all('=<pagemod.*?>[\w\W]*?</pagemod>=',$data,$pagemod_found, PREG_OFFSET_CAPTURE);
60
61        $calc_index = -1;
62        if ($itemPos < count($found)) {
63            for($index = 0; $index < count($found); $index++) {
64                $offset = 0;
65                for($ii = 0; $ii<=$index; $ii++) {
66                    $offset += strlen($found[$ii])+8;
67                }
68
69                $pagemod_found_flag = 0;
70                foreach($pagemod_found[0] as $pagemod_f) { //check if fsinput is in pagemod area
71                    if($offset > $pagemod_f[1] && $offset< ($pagemod_f[1]+strlen($pagemod_f[0]))) {
72                        $pagemod_found_flag = 1;
73                    }
74//                     print_r([$index,$calc_index,$input_index,$offset,$pagemod_f,($pagemod_f[1]+strlen($pagemod_f[0]))]);
75                }
76                if(!$pagemod_found_flag) { //we are looking for fsinput outside of pagemod
77                    $calc_index++;
78                }
79                if($calc_index == $itemPos){
80                    break ;
81                }
82            }
83
84            $found[$index+1] = ltrim($found[$index+1]);
85            $stop=strpos($found[$index+1],"</inlineedit>");
86            if ($stop === FALSE) {
87                $Hajax->error('Cannot find object, please contact your admin!');
88            }
89            else {
90                $oldstr = substr($found[$index+1],0,$stop);
91
92                $newstr=$input_str;
93
94                if($stop == 0){
95                    $found[$index+1]= $newstr.$found[$index+1];
96                }
97                else {
98                    $found[$index+1]=str_replace($oldstr,$newstr,$found[$index+1]);
99                }
100            }
101
102            $displayStr = $newstr ? $newstr : '...';
103            $data=implode("<inlineedit>",$found);
104            $param = array(
105                    'text'        => $displayStr,
106                    'msg'        => $this->getLang('success'),
107            );
108            $summary = "Inlineedit ".$itemPos." changed from ".$oldstr." to ".$newstr;
109            $Hajax->saveWikiPage($data,$summary,false,$param);
110        }
111    }
112
113}
114