<?php
/**
 * Newsboard plugin for dokuwiki
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Arthur Lobert(arthur.lobert@thalesgroup.com)
 */
 
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');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_newsboard extends DokuWiki_Syntax_Plugin{
 
	 function getType()  { return 'substition'; }
    function getPType() { return 'block'; }
    function getSort()  { return 304; }
	
    function getInfo(){
        return array(
            'author' => 'Arthur Lobert',
            'email'  => 'arthur.lobert@thalesgroup.com',
            'date'   => '2010_09_01',
            'name'   => 'newsboard',
            'desc'   => '',
            'url'    => 'http://www.dokuwiki.org/plugin:newsboard',
        );
    }
 
    function connectTo($mode) { 
		$this->Lexer->addSpecialPattern('{{newsboard.*?}}',$mode,'plugin_newsboard'); 
	}
 
	function check_arg($match){
		$check = preg_split("/>/",$match, -1 ,PREG_SPLIT_DELIM_CAPTURE);
		if ($check[0] == "")
			return substr($match, 1, strlen($match) - 1); 
		else
			return false;
	}
	
	function minmaj($match){
		$ret = 0;
		switch ($match) {
			case "all":
			$ret = RECENTS_SKIP_DELETED;
			break;
			//default : major
			default:
			$ret = RECENTS_SKIP_DELETED | RECENTS_SKIP_MINORS ;
			break;
		}
		return $ret;
	} 
	
	function nb_arg($nbr){
		$nb = (int) $nbr;
		if ($nb < 1)
			$nbr = '10';
		return ($nbr);
	}
	
//	function parse_ligne();
	
    function handle($match, $state, $pos, &$handler){
    	$match = html_entity_decode(substr($match, 11, -2));
    	$match = $this->check_arg($match);
    	if ($match){
    		$arg = preg_split("/\|/",$match, -1 ,PREG_SPLIT_DELIM_CAPTURE);
    		$ns = $arg[0];
    		if ($ns[0] == ':')
    			$ns = substr($ns, 1);
    		$change = $this->minmaj($arg[1]);
    		$nbr = $this->nb_arg($arg[2]);
    		$modif = getRecents(0, $nbr, $ns, $change);
    		return ($modif);
    	}
    	else{
    		return false;
    	}
    }
 
function get_titre($text)
{
	$ignore = 0;
	$tab = preg_split("/======/",$text ,-1,PREG_SPLIT_DELIM_CAPTURE);
	if ($tab[2] != Null)
		return ($tab[1]);
	return (0);
}

function cut_titre($text, $titre){
	return (str_replace('======'.$titre.'======', ' ', $text));
}
   	function get_image($page)
   	{
   		$i = 0;
   		$text = rawWiki($page);
   		$tab = preg_split("/(\{\{|\}})/",$text, -1 ,PREG_SPLIT_DELIM_CAPTURE);
   		while ($tab[$i])
   		{
   			if ($tab[$i] == "{{" && preg_match("/newsboard>/", $tab[$i + 1]) == 0)
   				return $tab[$i].$tab[$i+1].$tab[$i+2];
  			$i++;
  		}
   		return 0;
   	}
    
   	function cut_text($text){
   			return (str_replace('/', ' / ', $text));
   	}
   	
    function render($mode, &$renderer, $data) {

		foreach ($data as $tic):
	
			$page = $tic[id];
			$text =  rawWikiSlices('1-500', $page);
			$text = $this->cut_text($text[1]);
			$titre = $this->get_titre($text);
			$text = $this->cut_titre($text, $titre);
			$media = $this->get_image($page);
			if ($media)
				$image =" <input type='image' border='0' src='/dokuwiki/lib/exe/fetch.php?media=".$media."' height='100' width='100'  align='left' alt=''/>"; 
			else
				$image = 0;
		 if (plugin_isdisabled('wrap') || (!$wrap = plugin_load('syntax', 'wrap_div'))) {
                msg('missing_wrapplugin', -1);
                return false;
            }
            
			$data[0] = '1';
			$data[1] = 'round box 600px  center';
            $wrap->render($mode, &$renderer, $data);
             
			$renderer->doc .="<table >";
			if ($titre)
				$renderer->doc .="
									<tr>
										<div align='center'><FONT SIZE=5>".$titre."</FONT><br /><br /></div>
									
									</tr>
									";
			else
				$renderer->doc .="
									<tr>
										<div align='center'><FONT SIZE=5>".$page."</FONT><br /><br /></div>
									
									</tr>
									";
			$renderer->doc .="<tr align='center' height='100' width='100'>";
			if ($image){
				$renderer->doc .="
								<td height='100' width='100'>
										<div    align='center'>". $image."</div>
									</td><td><div height='100' width='5%' style='margin-left:10px;'>";
									
				}
			else
				$renderer->doc .="<div   align='center' height='100' width='5%'>
									";
            $data[0] = '3';
			$data[1] = $text;
			$wrap->render($mode, &$renderer, $data);   
			$renderer->doc .= "		
          					
          							</div>
								</td></tr>	
          						<tr >
          					
          					</table>
          					<div align='center' ><a href='/dokuwiki/doku.php?id=".$page."'  class='breadcrumbs' title='".$page."' ><H2>".$this->getLang('next')."</H2></a></div></tr>
          					";
			
            $data[0] = '4';
			$data[1] = null;
            $wrap->render($mode, &$renderer, $data);
          	
            endforeach;
            return true ;
      }
}