xref: /plugin/struct/action/config.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
18824339dSAndreas Gohr<?php
2*d6d97f60SAnna 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
108824339dSAndreas Gohr// must be run within Dokuwiki
1118902e76SMichael Großeuse dokuwiki\plugin\struct\meta\Column;
128824339dSAndreas Gohruse dokuwiki\plugin\struct\types\AbstractBaseType;
138824339dSAndreas Gohr
148824339dSAndreas Gohrif (!defined('DOKU_INC')) die();
158824339dSAndreas Gohr
16*d6d97f60SAnna Dabrowskaclass action_plugin_struct_config extends DokuWiki_Action_Plugin
17*d6d97f60SAnna Dabrowska{
188824339dSAndreas Gohr
198824339dSAndreas Gohr    /**
208824339dSAndreas Gohr     * Registers a callback function for a given event
218824339dSAndreas Gohr     *
228824339dSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
238824339dSAndreas Gohr     * @return void
248824339dSAndreas Gohr     */
25*d6d97f60SAnna Dabrowska    public function register(Doku_Event_Handler $controller)
26*d6d97f60SAnna Dabrowska    {
278824339dSAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
288824339dSAndreas Gohr    }
298824339dSAndreas Gohr
308824339dSAndreas Gohr    /**
318824339dSAndreas Gohr     * Reconfigure config for a given type
328824339dSAndreas Gohr     *
338824339dSAndreas Gohr     * @param Doku_Event $event event object by reference
348824339dSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
358824339dSAndreas Gohr     *                           handler was registered]
368824339dSAndreas Gohr     */
37*d6d97f60SAnna Dabrowska    public function handle_ajax(Doku_Event $event, $param)
38*d6d97f60SAnna Dabrowska    {
398824339dSAndreas Gohr        if ($event->data != 'plugin_struct_config') return;
408824339dSAndreas Gohr        $event->preventDefault();
418824339dSAndreas Gohr        $event->stopPropagation();
428824339dSAndreas Gohr        global $INPUT;
438824339dSAndreas Gohr
448824339dSAndreas Gohr        $conf = json_decode($INPUT->str('conf'), true);
4518902e76SMichael Große        $typeclasses = Column::allTypes();
4618902e76SMichael Große        $class = $typeclasses[$INPUT->str('type', 'Text')];
478824339dSAndreas Gohr        /** @var AbstractBaseType $type */
488824339dSAndreas Gohr        $type = new $class($conf);
498824339dSAndreas Gohr
508824339dSAndreas Gohr        header('Content-Type: text/plain'); // we need the encoded string, not decoded by jQuery
518824339dSAndreas Gohr        echo json_encode($type->getConfig());
528824339dSAndreas Gohr    }
538824339dSAndreas Gohr}
54