1<?php
2/**
3 * DokuWiki Plugin ajax (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  i-net software <tools@inetsoftware.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_sectionedit extends DokuWiki_Action_Plugin {
13
14    private $inited = null;
15
16    /**
17     * Registers a callback function for a given event
18     *
19     * @param Doku_Event_Handler $controller DokuWiki's event controller object
20     * @return void
21     */
22    public function register(Doku_Event_Handler $controller) {
23       $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call');
24       $controller->register_hook('ACTION_ACT_PREPROCESS', 'AFTER', $this, 'handle_suppress_default_after');
25    }
26
27    /**
28     * [Custom event handler which performs action]
29     *
30     * @param Doku_Event $event  event object by reference
31     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
32     *                           handler was registered]
33     * @return void
34     */
35    public function handle_ajax_call(Doku_Event &$event, $param) {
36
37        global  $ACT,  $INPUT, $QUERY, $ID, $REV, $DATE_AT, $IDX,
38        $DATE, $RANGE, $HIGH, $TEXT, $PRE, $SUF, $SUM, $INFO, $JSINFO;
39
40        if ( $event->data != 'sectionedit' ) return false;
41        $event->preventDefault();
42
43        $ACT = act_validate($INPUT->str('do'));
44        if ( !in_array($ACT, array('show', 'edit', 'save')) ) return;
45        $this->inited = $ACT;
46
47        // This seems super evil.
48        // if we catch all our variables, include the doku.php to do its normal magic.
49        // EXCEPT: we will catch the ACT_PREPROCESS in AFTER, outpout the current content and be done with it.
50        // This will either: show the current page ('show' or 'save') or the edit form.
51        return include_once(DOKU_INC . '/doku.php');
52    }
53
54    function handle_suppress_default_after(Doku_Event &$event, $param) {
55
56        global  $ACT,  $INPUT, $QUERY, $ID, $REV, $DATE_AT, $IDX,
57        $DATE, $RANGE, $HIGH, $TEXT, $PRE, $SUF, $SUM, $INFO, $JSINFO;
58        // If this plugin already ran into the sectionedit action, we will be inited and we can just output the template
59        // if the ACT was save, return nothing to John Snow. We need another request for show.
60        // hint: super evil. handle with care. experimental.
61        if ( is_null($this->inited) || $this->inited == 'save' ) return;
62        tpl_content();
63        exit;
64    }
65}
66
67// vim:ts=4:sw=4:et:
68