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     * Registers a callback function for a given event
17     *
18     * @param Doku_Event_Handler $controller DokuWiki's event controller object
19     * @return void
20     */
21    public function register(Doku_Event_Handler $controller)
22    {
23        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
24        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'addJsinfo');
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    /**
52     * Add config options to JSINFO
53     *
54     * @param Doku_Event $event
55     */
56    public function addJsinfo(Doku_Event $event)
57    {
58        global $JSINFO;
59        $JSINFO['plugins']['struct']['disableDeleteSerial'] = $this->getConf('disableDeleteSerial');
60    }
61}
62