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