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\Extension\ActionPlugin;
11use dokuwiki\Extension\EventHandler;
12use dokuwiki\Extension\Event;
13use dokuwiki\plugin\struct\meta\Column;
14use dokuwiki\plugin\struct\types\AbstractBaseType;
15
16class action_plugin_struct_config extends ActionPlugin
17{
18    /**
19     * Registers a callback function for a given event
20     *
21     * @param EventHandler $controller DokuWiki's event controller object
22     * @return void
23     */
24    public function register(EventHandler $controller)
25    {
26        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
27        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addJsinfo');
28    }
29
30    /**
31     * Reconfigure config for a given type
32     *
33     * @param 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 handleAjax(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, 512, JSON_THROW_ON_ERROR);
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(), JSON_THROW_ON_ERROR);
52    }
53
54    /**
55     * Add config options to JSINFO
56     *
57     * @param Event $event
58     */
59    public function addJsinfo(Event $event)
60    {
61        global $JSINFO;
62        $JSINFO['plugins']['struct']['disableDeleteSerial'] = $this->getConf('disableDeleteSerial');
63    }
64}
65