<?php
/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Luigi Micco <l.micco@tiscali.it>
 */

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_pagebox extends DokuWiki_Syntax_Plugin {

	function getInfo(){
		return array(
      'author' => 'Luigi micco',
      'email'  => 'l.micco@tiscali.it',
      'date'   => '2010-09-06',
      'name'   => 'Pagebox Plugin (syntax component)',
      'desc'   => 'Allow to use box like MediaWiki to show table and pages',
      'url'    => 'http://www.bitlibero.com/dokuwiki/pagebox-06.09.2010.zip',
		);
	}

	function getType(){ return 'protected'; }
	function getAllowedTypes() { return array('substition','protected','disabled','formatting'); }
	function getSort(){ return 315; }
	function getPType(){ return 'block'; }
  function connectTo($mode) {
      $this->Lexer->addSpecialPattern('{{pagebox>.+?}}', $mode, 'plugin_pagebox');
  }

  function handle($match, $state, $pos, &$handler) {
    
    $match = substr($match, 2, -2); // strip markup
    list($match, $flags) = explode('&', $match, 2);

    // break the pattern up into its constituent parts 
    list($include, $id, $section) = preg_split('/>|#/u', $match, 3); 
    return array($include, $id, explode('&', $flags)); 
 
  }
    
	function render($mode, &$renderer, $data){
		
    list($type, $page_id, $flags) = $data; 
    if ($type == "pagebox") {
      if(auth_quickaclcheck($page_id) >= AUTH_READ) {
        if($mode == 'xhtml'){

          $align = 'void';
          $width = 'auto';
          
          foreach($flags as $value) {
            if (is_numeric($value)) $width = $value.'px';
            if (strtolower($value) == 'right') $align = 'right';
            if (strtolower($value) == 'center') $align = 'center';
            if (strtolower($value) == 'left') $align = 'left';
          }  

          $renderer->doc.= '<div class="pagebox_out a'.$align.'" style="width:'.$width.'">';
          $renderer->doc.= '<div class="pagebox_in">';
          $renderer->doc.= tpl_include_page($page_id, false); 
          $renderer->doc.= '</div>';
          $renderer->doc.= '</div>';

          return true;
        }
        return false;
      }
    }
		return false;
	}
}

//Setup VIM: ex: et ts=4 enc=utf-8 :
