xref: /plugin/struct/action/config.php (revision 748e747f37aa44250ee32847b5fc3ff1e47f0835)
18824339dSAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
38824339dSAndreas Gohr/**
48824339dSAndreas Gohr * DokuWiki Plugin struct (Action Component)
58824339dSAndreas Gohr *
68824339dSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
78824339dSAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
88824339dSAndreas Gohr */
98824339dSAndreas Gohr
1018902e76SMichael Großeuse dokuwiki\plugin\struct\meta\Column;
118824339dSAndreas Gohruse dokuwiki\plugin\struct\types\AbstractBaseType;
128824339dSAndreas Gohr
13d6d97f60SAnna Dabrowskaclass action_plugin_struct_config extends DokuWiki_Action_Plugin
14d6d97f60SAnna Dabrowska{
158824339dSAndreas Gohr
168824339dSAndreas Gohr    /**
178824339dSAndreas Gohr     * Registers a callback function for a given event
188824339dSAndreas Gohr     *
198824339dSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
208824339dSAndreas Gohr     * @return void
218824339dSAndreas Gohr     */
22d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
23d6d97f60SAnna Dabrowska    {
24*748e747fSAnna Dabrowska        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax');
258824339dSAndreas Gohr    }
268824339dSAndreas Gohr
278824339dSAndreas Gohr    /**
288824339dSAndreas Gohr     * Reconfigure config for a given type
298824339dSAndreas Gohr     *
308824339dSAndreas Gohr     * @param Doku_Event $event event object by reference
318824339dSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
328824339dSAndreas Gohr     *                           handler was registered]
338824339dSAndreas Gohr     */
34*748e747fSAnna Dabrowska    public function handleAjax(Doku_Event $event, $param)
35d6d97f60SAnna Dabrowska    {
368824339dSAndreas Gohr        if ($event->data != 'plugin_struct_config') return;
378824339dSAndreas Gohr        $event->preventDefault();
388824339dSAndreas Gohr        $event->stopPropagation();
398824339dSAndreas Gohr        global $INPUT;
408824339dSAndreas Gohr
418824339dSAndreas Gohr        $conf = json_decode($INPUT->str('conf'), true);
4218902e76SMichael Große        $typeclasses = Column::allTypes();
4318902e76SMichael Große        $class = $typeclasses[$INPUT->str('type', 'Text')];
448824339dSAndreas Gohr        /** @var AbstractBaseType $type */
458824339dSAndreas Gohr        $type = new $class($conf);
468824339dSAndreas Gohr
478824339dSAndreas Gohr        header('Content-Type: text/plain'); // we need the encoded string, not decoded by jQuery
488824339dSAndreas Gohr        echo json_encode($type->getConfig());
498824339dSAndreas Gohr    }
508824339dSAndreas Gohr}
51