xref: /plugin/struct/action/config.php (revision 6445472824d3796a70da99c1f8f7a396d777813b)
1<?php
2/**
3 * DokuWiki Plugin struct (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10use dokuwiki\plugin\struct\types\AbstractBaseType;
11
12if(!defined('DOKU_INC')) die();
13
14class action_plugin_struct_config extends DokuWiki_Action_Plugin {
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');
24    }
25
26    /**
27     * Reconfigure config for a given type
28     *
29     * @param Doku_Event $event event object by reference
30     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
31     *                           handler was registered]
32     */
33    public function handle_ajax(Doku_Event $event, $param) {
34        if($event->data != 'plugin_struct_config') return;
35        $event->preventDefault();
36        $event->stopPropagation();
37        global $INPUT;
38
39        $conf = json_decode($INPUT->str('conf'), true);
40        /** @var AbstractBaseType $type */
41        $class = 'dokuwiki\\plugin\\struct\\types\\' . $INPUT->str('type', 'Text');
42        $type = new $class($conf);
43
44        header('Content-Type: text/plain'); // we need the encoded string, not decoded by jQuery
45        echo json_encode($type->getConfig());
46    }
47
48}
49