<?php
/**
 * enable menuPop Up things
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Gerry Weißbach <gerry.w@gammaproduction.de>
 */

if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_menuPopUp extends DokuWiki_Syntax_Plugin {
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Gerry Weissbach',
            'email'  => 'gerry.w@gammaproduction.de',
            'date'   => '2006-01-28',
            'name'   => 'Menu Pop Up',
            'desc'   => 'Pops up an DIV - Block with more Navigational Information (can be used for more) 
            			Syntax: {{popup>[[link]]?pageName(?width/height)}}
            			All Options ar optional.
            			',
            'url'    => 'http://wiki.gammaproduction.de/dokuwiki/plugins/menupopup',
        );
    }

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

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


    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('\{\{popup>[^}]*\}\}',$mode, 'plugin_menuPopUp');
    }


    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
    	
        $match = substr($match, 8, -2); //strip markup from start and end
        
        $data = array();

        //handle params
        list($data['link'], $data['page'], $params) = explode('?', $match, 3);

        //max thumb dimensions
        if(preg_match('/\b(\d+)(%|px|pt|em)\/(\d+)(%|px|pt|em)\b/i',$params,$match)) {
            $data['width'] = $match[1].$match[2];
            $data['height'] = $match[3].$match[4];
        }

        if(preg_match('/\bvertical\b/i',$params,$match)) {
            $data['vertical'] = 1;
        }
        else
            $data['vertical'] = 0;
        
/*        if(preg_match('/\b(onmouseover|onclick)\b/i',$params,$match)) {
            $data['event'] = $match[1];
        }*/
        
        if (empty($data['event'])) $data['event'] = 'onmouseover';

		if (empty($data['link']) || empty($data['page'])) return false;
		
     	return $data;
    }

    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {

       	$renderer->info['cache'] = FALSE;
        if($mode == 'xhtml'){
        	
		$popUp = new menuPopUp($data);
		$renderer->doc .= $popUp->html();

		return true;
        }
        return false;
    }
    
}

/**
 * @author    Gerry Weißbach <gerry.w@gammaporduction.de>
 */

if (!class_exists('menuPopUp')) {
	class menuPopUp {
		
		var $link;
		var $page;
		var $width;
		var $width_lower;
		var $height;
		var $event;
		var $vertical;
		var $linkAlign;
		
		function menuPopUp($data)
		{
			
			$this->link = $data['link'];
			$this->page = $data['page'];
			$this->width = $data['width'];
			$this->height = $data['height'];
			$this->event = $data['event'];
			$this->vertical = $data['vertical'];
			
			$substract = array	(
							'%' => 10,
							'px' => 50,
							'pt' => 25,
							'em' => 1
						);

		        if(preg_match('/\b(\d+)(%|px|pt|em)\b/i',$this->width,$match)) {
				$this->width_lower = ($match[1]-$substract[$match[2]]).$match[2];
		        }
		        else
		        	$this->width_lower = $this->width;


			if ( preg_match('/^[\s]{2}/', $this->link) ) {
				$this->link = preg_replace('/^\s{2}/', '', $this->link);
				$this->linkAlign .= ' left';
			}
		
			if ( preg_match('/[\s]{2}$/', $this->link) ) {
				$this->link = preg_replace('/\s{2}$/', '', $this->link);
				$this->linkAlign .= ' right';
			}
		}

		function html()
		{
			
			$includePage = new pageInclude($this->page);
			
			$output = '<div class="menuContainer'.$this->linkAlign.'" onmouseover="this.className=\'menuContainerOver'.$this->linkAlign.'\'" onmouseout="this.className=\'menuContainer'.$this->linkAlign.'\';">'; // id="'.$this->link.'">';

			$output .= '<div class="menuLink'.($this->vertical?' vertical':'').'">'.p_render('xhtml', p_get_instructions($this->link), $info);
			
			$output .= '<div class="menuContent'.($this->vertical?' vertical':'').'" style="width:'.(empty($this->width)?'150px':$this->width).'; '.(empty($this->height)?'':'max-height:'.$this->height.';').( preg_match('/right/', $this->linkAlign)? ' margin-left:-'.(empty($this->width_lower)?'125px':$this->width_lower).';' : '' ) . '">';
			$output .= $includePage->html();
			$output .= '</div>';
			
			$output .= '</div></div>';
			return $output;
		}
	}
}

/* Include the inner Page */
if (!class_exists('pageInclude')) {
	class pageInclude {
		
		var $page;
		
		function pageInclude($page)
		{
		        global $ID;
		        global $filechain;
		 
		        resolve_pageid(getNS($ID),$page,$exists); // resolve shortcuts
		 
		        // check for existence and permission
//		        if ((auth_quickaclcheck($page) < 1)) echo "noauth";
		        if ((auth_quickaclcheck($page) < 1)) return false;
		 
		        // check for and establish start of $filechain
		        if (!isset($filechain)) $filechain[] = $ID;
		 
		        // don't allow the same file to be included more than once
//		        if (in_array($page, $filechain)) "filechain";
		        if (in_array($page, $filechain)) return false;
		 
		        // add included page to the filechain
		        $filechain[] = $page;
		        
		        $this->page = $page;
		}
		
		function html()
		{
			global $conf;

			$file = wikiFN($this->page);
			return p_render('xhtml', p_cached_instructions($file), $info).$this->incl_editButton($this->page, $file, auth_quickaclcheck($this->page));
		}

		function incl_editButton($id, $file, $perm){
			$ret = '';
			
			if (@file_exists($file)){
			if (($perm >= AUTH_EDIT) && (is_writable($file))) $ret = '<div class="secedit">'.html_btn('edit', $id, '', array('do' => 'edit'), 'post').'</div>';
			} elseif ($perm >= AUTH_CREATE){
			$ret = '<div class="secedit">'.html_btn('create', $id, '', array('do' => 'edit'), 'post').'</div>';
			}
			return $ret;
		}
	}
}

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