1<?php
2/**
3 * Plugin Reliwa: Allow display your Reliwa-Items.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Juergen A. Lamers <jaloma.ac@googlemail.com>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12/**
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 */
16class syntax_plugin_reliwa extends DokuWiki_Syntax_Plugin {
17
18  //<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>
19  var $dflt = array(
20		    'user' => '',
21		    'cat' => 'book',
22		    'count' => '8',
23		    'col' => '',//'1',
24		    'rows' => '',
25		    'order' => 'random',
26		    'amazonid' => ''//'dokutransdata'
27		    );
28
29    /**
30     * return some info
31     */
32  function getInfo(){
33    return array(
34		 'author' => 'J�rgen A. Lamers',
35		 'email'  => 'jaloma.ac@googlemail.com',
36		 'date'   => '2008-01-12',
37		 'name'   => 'Reliwa Plugin',
38		 'desc'   => 'Add your ReliwaItems to your wiki
39                              Syntax: <reliwa params/>',
40		 'url'    => 'http://jaloma.ac.googlepages.com/plugin:reliwa'
41		 );
42  }
43
44    /**
45     * What kind of syntax are we?
46     */
47  function getType() { return 'substition'; }
48  function getPType() { return 'block'; }
49    /**
50     * Where to sort in to parse?
51     */
52  function getSort() { return 899; }
53
54    /**
55    * Connect pattern to lexer
56    */
57  function connectTo($mode) { //     1234567890123
58                              //     <reliwa
59    $this->Lexer->addSpecialPattern('<reliwa ?[^>\n]*/>',$mode,'plugin_reliwa');
60  }
61
62  /*
63  function postConnect() {
64    $this->Lexer->addExitPattern('</reliwa>','plugin_reliwa');
65  }*/
66
67    /**
68     * Handle the match
69     */
70  function handle($match, $state, $pos, &$handler){
71    // break matched cdata into its components
72    list($str_params,$str_points) = explode('>',substr($match,7,-9),2);
73    $gmap = $this->_extract_params($str_params);
74    return array($gmap);
75  }
76
77    /**
78     * Create output
79     */
80  function render($mode, &$renderer, $data) {
81    if ($mode == 'xhtml') {
82      list($param) = $data;
83
84  //<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>
85      $txt  = "";
86      $txt .= '<span>';
87      $txt .= '<script';
88      $txt .= ' type="text/javascript"';
89      $txt .= ' src="'.$this->getConf('url');
90      $uname = $param['user'];
91      if ($uname == '') {
92	$uname = $this->getConf('user');
93      }
94      $txt .= 'username='.$uname.'&amp;';
95      $txt .= 'cat='.$param['cat'].'&amp;';
96      $txt .= 'count='.$param['count'].'&amp;';
97      if ($param['rows'] != '') {
98	$txt .= 'rows='.$param['rows'].'&amp;';
99      }else {
100      $txt .= 'column='.$param['col'].'&amp;';
101      }
102      $txt .= 'order='.$param['order'];
103      if ($param['amazonid'] != '') {
104	$txt .= '&amp;associate_id='.$param['amazonid'];
105      }
106      $txt .= '">';
107      $txt .= '</script>';
108      $txt .= '</span>';
109      $renderer->doc .= $txt;
110    }
111    return false;
112  }
113
114  /**
115   * extract parameters for the googlemap from the parameter string
116   *
117   * @param   string    $str_params   string of key="value" pairs
118   * @return  array                   associative array of parameters key=>value
119   */
120  function _extract_params($str_params) {
121    $param = array();
122    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
123    if (sizeof($param) == 0) {
124      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
125    }
126    // parse match for instructions, break into key value pairs
127    $gmap = $this->dflt;
128    foreach($param as $kvpair) {
129      list($match,$key,$val) = $kvpair;
130      $key = strtolower($key);
131      if ($key == 'order') {
132	if (strpos('date_desc;date_asc;random',$val) === 0) {
133	  $val = 'random';
134	}
135      } else if ($key == 'cat') {
136	if ($val == 'film_seen' || $val == 'movie_seen') {
137	  $val = '6';
138	} else if ($val == 'film_wanted' || $val == 'movie_wanted') {
139	  $val = '7';
140	} else if ($val == 'film') {
141	  $val = 'movie';
142	} else if ($val == 'book_reading') {
143	  $val = '1';
144	} else if ($val == 'book_readed') {
145	  $val = '2';
146	} else if ($val == 'book_wanted') {
147	  $val = '3';
148	} else if ($val == 'music_heared' || $val == 'music_listened') {
149	  $val = '4';
150	} else if ($val == 'music_wanted') {
151	  $val = '5';
152	// 6 := Filme (gesehen)
153	// 7 := Filme (m�chte sehen)
154	// movie := Filme (alle)
155	// music := Musik (alle)
156	// 1 := B�cher (liest gerade)
157	// 2 := B�cher (gelesen)
158	// 3 := B�cher (m�chte lesen)
159	// 4 := Musik (geh�rt)
160	// book := B�cher (alle)
161	// 5 := Musik (m�chte h�ren)
162	} else if (strpos('6;7;movie;music;1;2;3;4;book;5',$val) != 0) {
163	} else {
164	  $val = 'book';
165	}
166      }
167      if (isset($gmap[$key])) $gmap[$key] = $val;
168    }
169
170    return $gmap;
171  }
172} /* CLASS */
173?>