xref: /plugin/struct/action/config.php (revision 18902e76725e821be86e59cc933b8203e6687851)
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\meta\Column;
11use dokuwiki\plugin\struct\types\AbstractBaseType;
12
13if(!defined('DOKU_INC')) die();
14
15class action_plugin_struct_config extends DokuWiki_Action_Plugin {
16
17    /**
18     * Registers a callback function for a given event
19     *
20     * @param Doku_Event_Handler $controller DokuWiki's event controller object
21     * @return void
22     */
23    public function register(Doku_Event_Handler $controller) {
24        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
25    }
26
27    /**
28     * Reconfigure config for a given type
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     */
34    public function handle_ajax(Doku_Event $event, $param) {
35        if($event->data != 'plugin_struct_config') return;
36        $event->preventDefault();
37        $event->stopPropagation();
38        global $INPUT;
39
40        $conf = json_decode($INPUT->str('conf'), true);
41        $typeclasses = Column::allTypes();
42        $class = $typeclasses[$INPUT->str('type', 'Text')];
43        /** @var AbstractBaseType $type */
44        $type = new $class($conf);
45
46        header('Content-Type: text/plain'); // we need the encoded string, not decoded by jQuery
47        echo json_encode($type->getConfig());
48    }
49
50}
51