<?php
/**
 * Plugin Reliwa: Allow display your Reliwa-Items.
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Juergen A. Lamers <jaloma.ac@googlemail.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_reliwa extends DokuWiki_Syntax_Plugin {

  //<script type="text/javascript" src="http://www.reliwa.de/api/badge?username=jaloma&cat=book&count=6&column=2&order=date_desc&associate_id=dokutransdata"></script>
  var $dflt = array(
		    'user' => '',
		    'cat' => 'book',
		    'count' => '8',
		    'col' => '',//'1',
		    'rows' => '',
		    'order' => 'random',
		    'amazonid' => ''//'dokutransdata'
		    );
  
    /**
     * return some info
     */
  function getInfo(){
    return array(
		 'author' => 'Jürgen A. Lamers',
		 'email'  => 'jaloma.ac@googlemail.com',
		 'date'   => '2008-01-12',
		 'name'   => 'Reliwa Plugin',
		 'desc'   => 'Add your ReliwaItems to your wiki
                              Syntax: <reliwa params/>',
		 'url'    => 'http://jaloma.ac.googlepages.com/plugin:reliwa'
		 );
  }

    /**
     * What kind of syntax are we?
     */
  function getType() { return 'substition'; }
  function getPType() { return 'block'; }
    /**
     * Where to sort in to parse?
     */
  function getSort() { return 899; } 

    /**
    * Connect pattern to lexer
    */
  function connectTo($mode) { //     1234567890123
                              //     <reliwa
    $this->Lexer->addSpecialPattern('<reliwa ?[^>\n]*/>',$mode,'plugin_reliwa'); 
  }

  /*
  function postConnect() {
    $this->Lexer->addExitPattern('</reliwa>','plugin_reliwa');
  }*/

    /**
     * Handle the match
     */
  function handle($match, $state, $pos, &$handler){
    // break matched cdata into its components
    list($str_params,$str_points) = explode('>',substr($match,7,-9),2);
    $gmap = $this->_extract_params($str_params);
    return array($gmap);
  }

    /**
     * Create output
     */
  function render($mode, &$renderer, $data) {
    if ($mode == 'xhtml') {
      list($param) = $data;

  //<script type="text/javascript" src="http://www.reliwa.de/api/badge?username=jaloma&cat=book&count=6&column=2&order=date_desc&associate_id=dokutransdata"></script>
      $txt  = "";
      $txt .= '<span>';
      $txt .= '<script';
      $txt .= ' type="text/javascript"';
      $txt .= ' src="'.$this->getConf('url');
      $uname = $param['user'];
      if ($uname == '') {
	$uname = $this->getConf('user');
      }
      $txt .= 'username='.$uname.'&amp;';
      $txt .= 'cat='.$param['cat'].'&amp;';
      $txt .= 'count='.$param['count'].'&amp;';
      if ($param['rows'] != '') {
	$txt .= 'rows='.$param['rows'].'&amp;';
      }else {
      $txt .= 'column='.$param['col'].'&amp;';
      }
      $txt .= 'order='.$param['order'];
      if ($param['amazonid'] != '') {
	$txt .= '&amp;associate_id='.$param['amazonid'];
      }
      $txt .= '">';
      $txt .= '</script>';
      $txt .= '</span>';
      $renderer->doc .= $txt;
    }
    return false;
  }
  
  /**
   * extract parameters for the googlemap from the parameter string
   *
   * @param   string    $str_params   string of key="value" pairs
   * @return  array                   associative array of parameters key=>value
   */
  function _extract_params($str_params) {
    $param = array();
    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
    if (sizeof($param) == 0) {
      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
    }
    // parse match for instructions, break into key value pairs      
    $gmap = $this->dflt;
    foreach($param as $kvpair) {
      list($match,$key,$val) = $kvpair;
      $key = strtolower($key);
      if ($key == 'order') {
	if (strpos('date_desc;date_asc;random',$val) === 0) {
	  $val = 'random';
	}
      } else if ($key == 'cat') {
	if ($val == 'film_seen' || $val == 'movie_seen') {
	  $val = '6';
	} else if ($val == 'film_wanted' || $val == 'movie_wanted') {
	  $val = '7';
	} else if ($val == 'film') {
	  $val = 'movie';
	} else if ($val == 'book_reading') {
	  $val = '1';
	} else if ($val == 'book_readed') {
	  $val = '2';
	} else if ($val == 'book_wanted') {
	  $val = '3';
	} else if ($val == 'music_heared' || $val == 'music_listened') {
	  $val = '4';
	} else if ($val == 'music_wanted') {
	  $val = '5';
	// 6 := Filme (gesehen)
	// 7 := Filme (m�chte sehen)
	// movie := Filme (alle)
	// music := Musik (alle)
	// 1 := B�cher (liest gerade)
	// 2 := B�cher (gelesen)
	// 3 := B�cher (m�chte lesen)
	// 4 := Musik (geh�rt)
	// book := B�cher (alle)
	// 5 := Musik (m�chte h�ren)
	} else if (strpos('6;7;movie;music;1;2;3;4;book;5',$val) != 0) {
	} else {
	  $val = 'book';
	}
      }
      if (isset($gmap[$key])) $gmap[$key] = $val;        
    }

    return $gmap;
  }
} /* CLASS */
?>