xref: /plugin/struct/action/ajax.php (revision 914921fb92301c70150ca8f040ad4d23523f569c) !
1*914921fbSAndreas Gohr<?php
2*914921fbSAndreas Gohr/**
3*914921fbSAndreas Gohr * DokuWiki Plugin struct (Action Component)
4*914921fbSAndreas Gohr *
5*914921fbSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*914921fbSAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7*914921fbSAndreas Gohr */
8*914921fbSAndreas Gohr
9*914921fbSAndreas Gohr// must be run within Dokuwiki
10*914921fbSAndreas Gohruse plugin\struct\meta\Schema;
11*914921fbSAndreas Gohruse plugin\struct\meta\StructException;
12*914921fbSAndreas Gohr
13*914921fbSAndreas Gohrif(!defined('DOKU_INC')) die();
14*914921fbSAndreas Gohr
15*914921fbSAndreas Gohrclass action_plugin_struct_ajax extends DokuWiki_Action_Plugin {
16*914921fbSAndreas Gohr
17*914921fbSAndreas Gohr    /**
18*914921fbSAndreas Gohr     * Registers a callback function for a given event
19*914921fbSAndreas Gohr     *
20*914921fbSAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
21*914921fbSAndreas Gohr     * @return void
22*914921fbSAndreas Gohr     */
23*914921fbSAndreas Gohr    public function register(Doku_Event_Handler $controller) {
24*914921fbSAndreas Gohr        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
25*914921fbSAndreas Gohr    }
26*914921fbSAndreas Gohr
27*914921fbSAndreas Gohr    /**
28*914921fbSAndreas Gohr     * Pass Ajax call to a type
29*914921fbSAndreas Gohr     *
30*914921fbSAndreas Gohr     * @param Doku_Event $event event object by reference
31*914921fbSAndreas Gohr     * @param mixed $param [the parameters passed as fifth argument to register_hook() when this
32*914921fbSAndreas Gohr     *                           handler was registered]
33*914921fbSAndreas Gohr     * @return bool
34*914921fbSAndreas Gohr     */
35*914921fbSAndreas Gohr    public function handle_ajax(Doku_Event $event, $param) {
36*914921fbSAndreas Gohr        if($event->data != 'plugin_struct') return;
37*914921fbSAndreas Gohr        $event->preventDefault();
38*914921fbSAndreas Gohr        $event->stopPropagation();
39*914921fbSAndreas Gohr
40*914921fbSAndreas Gohr        header('Content-Type: application/json');
41*914921fbSAndreas Gohr        try {
42*914921fbSAndreas Gohr            $result = $this->executeTypeAjax();
43*914921fbSAndreas Gohr        } catch(StructException $e) {
44*914921fbSAndreas Gohr            $result = array(
45*914921fbSAndreas Gohr                'error' => $e->getMessage()
46*914921fbSAndreas Gohr            );
47*914921fbSAndreas Gohr            http_status(500);
48*914921fbSAndreas Gohr        }
49*914921fbSAndreas Gohr
50*914921fbSAndreas Gohr        $json = new JSON;
51*914921fbSAndreas Gohr        echo $json->encode($result);
52*914921fbSAndreas Gohr    }
53*914921fbSAndreas Gohr
54*914921fbSAndreas Gohr    /**
55*914921fbSAndreas Gohr     * Check the input variables and run the AJAX call
56*914921fbSAndreas Gohr     *
57*914921fbSAndreas Gohr     * @throws StructException
58*914921fbSAndreas Gohr     * @return mixed
59*914921fbSAndreas Gohr     */
60*914921fbSAndreas Gohr    protected function executeTypeAjax() {
61*914921fbSAndreas Gohr        global $INPUT;
62*914921fbSAndreas Gohr
63*914921fbSAndreas Gohr        $col = $INPUT->str('column');
64*914921fbSAndreas Gohr        if(blank($col)) throw new StructException('No column provided');
65*914921fbSAndreas Gohr        list($schema, $colname) = explode('.', $col, 2);
66*914921fbSAndreas Gohr        if(blank($schema) || blank($colname)) throw new StructException('Column format is wrong');
67*914921fbSAndreas Gohr
68*914921fbSAndreas Gohr        $schema = new Schema($schema);
69*914921fbSAndreas Gohr        if(!$schema->getId()) throw new StructException('Unknown Schema');
70*914921fbSAndreas Gohr
71*914921fbSAndreas Gohr        $column = $schema->findColumn($colname);
72*914921fbSAndreas Gohr        if($column === false) throw new StructException('Column not found');
73*914921fbSAndreas Gohr
74*914921fbSAndreas Gohr        return $column->getType()->handleAjax();
75*914921fbSAndreas Gohr    }
76*914921fbSAndreas Gohr}
77