xref: /plugin/struct/action/config.php (revision dfe3ae67e2cbc66413b67e0194e82ef51f46af29)
1<?php
2
3/**
4 * DokuWiki Plugin struct (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10use dokuwiki\plugin\struct\meta\Column;
11use dokuwiki\plugin\struct\types\AbstractBaseType;
12
13class action_plugin_struct_config extends DokuWiki_Action_Plugin
14{
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    {
24        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
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 handleAjax(Doku_Event $event, $param)
35    {
36        if ($event->data != 'plugin_struct_config') return;
37        $event->preventDefault();
38        $event->stopPropagation();
39        global $INPUT;
40
41        $conf = json_decode($INPUT->str('conf'), true);
42        $typeclasses = Column::allTypes();
43        $class = $typeclasses[$INPUT->str('type', 'Text')];
44        /** @var AbstractBaseType $type */
45        $type = new $class($conf);
46
47        header('Content-Type: text/plain'); // we need the encoded string, not decoded by jQuery
48        echo json_encode($type->getConfig());
49    }
50}
51