action.php
author Andreas Boehler <andreas@aboehler.at>
Tue, 23 Sep 2014 21:36:49 +0200
changeset 4 dffe53229d26
parent 3 88d936428bed
child 5 11ccac7668ec
permissions -rw-r--r--
Updated Plugin Info
<?php
/**
 * DokuWiki Plugin datapopup (Action Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Andreas Boehler <dev@aboehler.at>
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');

require_once(DOKU_PLUGIN.'action.php');

class action_plugin_datapopup extends DokuWiki_Action_Plugin {

    var $dthlp = null;

    function action_plugin_datapopup() {
        $this->dthlp =& plugin_load('helper', 'data');
    }

    function register(&$controller) {
        global $JSINFO;
        $JSINFO['plugin']['datapopup']['registered_types'] = $this->getConf('registered_types');
        
        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
    }

    protected function _getFields($link, $name) {
        if(!$this->dthlp)
          return "The datapopup plugin reported an error. Please report.";
        
        $sqlite = $this->dthlp->_getDB();
        if(!$sqlite) return "The datapopup had an error retrieving the SQLite plugin. Please report.";
        $query = "SELECT data.value FROM data JOIN pages ON data.pid=pages.pid WHERE pages.page='".$link."' AND data.key='".strtolower($name)."';";
        $res = $sqlite->query($query);
        $rows = $sqlite->res2arr($res);
        return $rows[0]['value'];
    }
    
    public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
        if($event->data != 'plugin_datapopup') return;    
        global $auth;
        global $INPUT;

        $link = $INPUT->str('link');
        $link = preg_replace('/\?.*/', '', $link);
        
        $fields = explode(',',$this->getConf('fields'));
        $fields = array_map('trim',$fields);
        $fields = array_filter($fields);

        echo '<ul>';
        foreach($fields as $name){
            $val = $this->_getFields($link, $name);
            if($val != "")
            {
                echo '<li class="dataov_name">';
                echo '<div class="li">';
                echo hsc($name);
                echo '</div></li>';
                echo '<li class="dataov_'.hsc($name).'">';
                echo '<div class="li">';
                echo hsc($val);
                echo '</div></li>';
            }
        }
        echo '</ul>';                
    
        
        $event->preventDefault();
        $event->stopPropagation();
    }        

}

// vim:ts=4:sw=4:et:enc=utf-8: