xref: /plugin/struct/action/ajax.php (revision 9dfe4ab9e4aeda253f7504b2f05a184dce5b678f)
1914921fbSAndreas Gohr<?php
2914921fbSAndreas Gohr/**
3914921fbSAndreas Gohr * DokuWiki Plugin struct (Action Component)
4914921fbSAndreas Gohr *
5914921fbSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6914921fbSAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7914921fbSAndreas Gohr */
8914921fbSAndreas Gohr
9914921fbSAndreas Gohr// must be run within Dokuwiki
10914921fbSAndreas Gohruse plugin\struct\meta\Schema;
11914921fbSAndreas Gohruse plugin\struct\meta\StructException;
12914921fbSAndreas Gohr
13914921fbSAndreas Gohrif(!defined('DOKU_INC')) die();
14914921fbSAndreas Gohr
15914921fbSAndreas Gohrclass action_plugin_struct_ajax extends DokuWiki_Action_Plugin {
16914921fbSAndreas Gohr
17914921fbSAndreas Gohr    /**
18914921fbSAndreas Gohr     * Registers a callback function for a given event
19914921fbSAndreas Gohr     *
20914921fbSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
21914921fbSAndreas Gohr     * @return void
22914921fbSAndreas Gohr     */
23914921fbSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
24914921fbSAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
25914921fbSAndreas Gohr    }
26914921fbSAndreas Gohr
27914921fbSAndreas Gohr    /**
28914921fbSAndreas Gohr     * Pass Ajax call to a type
29914921fbSAndreas Gohr     *
30914921fbSAndreas Gohr     * @param Doku_Event $event event object by reference
31914921fbSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
32914921fbSAndreas Gohr     *                           handler was registered]
33914921fbSAndreas Gohr     * @return bool
34914921fbSAndreas Gohr     */
35914921fbSAndreas Gohr    public function handle_ajax(Doku_Event $event, $param) {
36914921fbSAndreas Gohr        if($event->data != 'plugin_struct') return;
37914921fbSAndreas Gohr        $event->preventDefault();
38914921fbSAndreas Gohr        $event->stopPropagation();
39*9dfe4ab9SAndreas Gohr        global $conf;
40914921fbSAndreas Gohr
41914921fbSAndreas Gohr        header('Content-Type: application/json');
42914921fbSAndreas Gohr        try {
43914921fbSAndreas Gohr            $result = $this->executeTypeAjax();
44914921fbSAndreas Gohr        } catch(StructException $e) {
45914921fbSAndreas Gohr            $result = array(
46*9dfe4ab9SAndreas Gohr                'error' => $e->getMessage().' '.basename($e->getFile()).':'.$e->getLine()
47914921fbSAndreas Gohr            );
48*9dfe4ab9SAndreas Gohr            if($conf['allowdebug']) {
49*9dfe4ab9SAndreas Gohr                $result['stacktrace'] = $e->getTraceAsString();
50*9dfe4ab9SAndreas Gohr            }
51914921fbSAndreas Gohr            http_status(500);
52914921fbSAndreas Gohr        }
53914921fbSAndreas Gohr
54914921fbSAndreas Gohr        $json = new JSON;
55914921fbSAndreas Gohr        echo $json->encode($result);
56914921fbSAndreas Gohr    }
57914921fbSAndreas Gohr
58914921fbSAndreas Gohr    /**
59914921fbSAndreas Gohr     * Check the input variables and run the AJAX call
60914921fbSAndreas Gohr     *
61914921fbSAndreas Gohr     * @throws StructException
62914921fbSAndreas Gohr     * @return mixed
63914921fbSAndreas Gohr     */
64914921fbSAndreas Gohr    protected function executeTypeAjax() {
65914921fbSAndreas Gohr        global $INPUT;
66914921fbSAndreas Gohr
67914921fbSAndreas Gohr        $col = $INPUT->str('column');
68914921fbSAndreas Gohr        if(blank($col)) throw new StructException('No column provided');
69914921fbSAndreas Gohr        list($schema, $colname) = explode('.', $col, 2);
70914921fbSAndreas Gohr        if(blank($schema) || blank($colname)) throw new StructException('Column format is wrong');
71914921fbSAndreas Gohr
72914921fbSAndreas Gohr        $schema = new Schema($schema);
73914921fbSAndreas Gohr        if(!$schema->getId()) throw new StructException('Unknown Schema');
74914921fbSAndreas Gohr
75914921fbSAndreas Gohr        $column = $schema->findColumn($colname);
76914921fbSAndreas Gohr        if($column === false) throw new StructException('Column not found');
77914921fbSAndreas Gohr
78914921fbSAndreas Gohr        return $column->getType()->handleAjax();
79914921fbSAndreas Gohr    }
80914921fbSAndreas Gohr}
81