1<?php
2/**
3 * DokuWiki Plugin datapopup (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Boehler <dev@aboehler.at>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once(DOKU_PLUGIN.'action.php');
15
16class action_plugin_datapopup extends DokuWiki_Action_Plugin {
17
18    var $dthlp = null;
19
20    function action_plugin_datapopup() {
21        $this->dthlp =& plugin_load('helper', 'data');
22    }
23
24    function register(Doku_Event_Handler $controller) {
25        global $JSINFO;
26        $JSINFO['plugin']['datapopup']['registered_types'] = $this->getConf('registered_types');
27
28        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
29    }
30
31    protected function _getFields($link, $name) {
32        if(!$this->dthlp)
33          return "The datapopup plugin reported an error. Please report.";
34
35        $sqlite = $this->dthlp->_getDB();
36        if(!$sqlite) return "The datapopup had an error retrieving the SQLite plugin. Please report.";
37        $query = "SELECT data.value FROM data JOIN pages ON data.pid=pages.pid WHERE pages.page='".$link."' AND data.key='".strtolower($name)."';";
38        $res = $sqlite->query($query);
39        $rows = $sqlite->res2arr($res);
40        return $rows[0]['value'];
41    }
42
43    public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
44        if($event->data != 'plugin_datapopup') return;
45        global $auth;
46        global $INPUT;
47
48        $link = $INPUT->str('link');
49        $link = preg_replace('/\?.*/', '', $link);
50
51        $fields = explode(',',$this->getConf('fields'));
52        $fields = array_map('trim',$fields);
53        $fields = array_filter($fields);
54
55        echo '<ul>';
56        foreach($fields as $name){
57            $val = $this->_getFields($link, $name);
58            if($val != "")
59            {
60                echo '<li class="dataov_name">';
61                echo '<div class="li">';
62                echo hsc($name);
63                echo '</div></li>';
64                echo '<li class="dataov_'.hsc($name).'">';
65                echo '<div class="li">';
66                echo hsc($val);
67                echo '</div></li>';
68            }
69        }
70        echo '</ul>';
71
72
73        $event->preventDefault();
74        $event->stopPropagation();
75    }
76
77}
78
79// vim:ts=4:sw=4:et:enc=utf-8:
80