<?php
/**
 * Info Plugin: Simple related namespaces plugin
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 * @author     Christophe Gragnic <christophegragnic@yahoo.fr>
 */
// 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.'syntax.php');

class syntax_plugin_nsrelation extends DokuWiki_Syntax_Plugin {

    var $relatedns = array();
    var $relatedns_display = array();
    var $rootns   = '';

    /**
     * Initialize
     */
    function syntax_plugin_nsrelation(){
        // load wanted ns in the relation into array
        $this->relatedns = array_unique(array_filter(explode('|',$this->getConf('nsrelations'))));
        $this->relatedns_display = array_unique(array_filter(explode('|',$this->getConf('nsrelationsdisplay'))));
        // if there is not enough display strings, fill with raw namespaces
        if (count($this->relatedns_display) < $this->relatedns)
            for($i=count($this->relatedns_display);$i<count($this->relatedns);$i++)
                $this->relatedns_display[] = $this->relatedns[$i];

        $this->rootns = cleanID($this->getConf('nsrelationrootns'));
        if($this->rootns) $this->rootns .= ':';
    }

    /**
     * return some info
     */
    function getInfo(){
        return confToHash(dirname(__FILE__).'/info.txt');
    }

    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }

    /**
     * Where to sort in?
     */
    function getSort(){
        return 155;
    }


    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('~~NO_NSREL~~',$mode,'plugin_nsrelation');
    }


    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        return array('no_nsrel');
    }

    /**
     * Create output
     */
    function render($format, &$renderer, $data) {
        // store info in metadata
        if($format == 'metadata'){
            $renderer->meta['plugin']['nsrelation']['no_nsrel'] = true;
        }
        return false;
    }

    /**
     * Returns a link to the wanted and related namespace
     */
    function _buildNSRelLink($ns,$idpart){
        global $conf;
        global $saved_conf;
        $link = ':'.$this->rootns.$ns.':'.$idpart;
        $name = $this->relatedns_display[array_search($ns,$this->relatedns)];
        return html_wikilink($link,$name);
    }

    /**
     * Displays the available and configured namespaces. Needs to be placed in the template.
     */
    function _showNSRelations(){
        global $ACT;
        global $ID;
        global $conf;

        if($ACT != 'show') return;
        if($this->rootns && strpos($ID,$this->rootns) !== 0) return;
        $meta = p_get_metadata($ID);
        if($meta['plugin']['nsrelation']['no_nsrel']) return;

        $currentns = '';
        foreach($this->relatedns as $ns){
            if(strpos($ID,$this->rootns.$ns) === 0 ){
                $currentns = $ns;
                break;
            }
        }
        if($currentns == '') return;

        $rx = '/^'.$this->rootns.'(('.join('|',$this->relatedns).'):)?/';
        $idpart = preg_replace($rx,'',$ID);

        $out  = '<div class="plugin_nsrelation">';
        $out .= '<span>'.$this->getLang('nsrelations');
        if($this->getConf('about')){
            $out .= '<sup>'.html_wikilink($this->getConf('about'),'?').'</sup>';
        }
        $out .= ':</span> ';
        $out .= '<ul>';
        foreach($this->relatedns as $ns){
            $out .= '  <li'.($ns==$currentns?' class="current"':'').'><div class="li">'.$this->_buildNSRelLink($ns,$idpart).'</div></li>';
        }
        $out .= '</ul>';
        $out .= '</div>';

        return $out;
    }

}

//Setup VIM: ex: et ts=4 enc=utf-8 :
