1<?php
2/**
3 *
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Jürgen 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/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_jalbum_jalbum extends DokuWiki_Syntax_Plugin {
18
19  /**
20   * default parameters of the jalbum tag
21   */
22  var $dflt = array(
23		    'width' => 20,
24		    'height' => 20,
25		    'align' => 'top',
26			'albumid' => 'off',
27		    'albumdir' => 'off',
28		    'albumtitle' => 'off',
29		    'albumshow' => 'off',
30			'jpgtohtml' => 'on'
31		    );
32
33  /**
34   * return some info
35   */
36  function getInfo(){
37    return array(
38		 'author' => 'Juergen A.Lamers',
39		 'email'  => 'jaloma.ac@googlemail.com',
40		 'date'   => @file_get_contents(DOKU_PLUGIN . 'jalbum/VERSION'),
41		 'name'   => 'jalbum',
42		 'desc'   => 'Einfacher Zugriff auf Alben und Thumbs von JAlbum',
43		 'url'    => 'http://wiki.dokuwiki.org/plugin:jalbum'
44		 );
45  }
46
47  /**
48   * What kind of syntax are we?
49   */
50  function getType(){
51    return 'substition';
52  }
53
54  function getSort(){ return 305; }
55
56  function connectTo($mode) {
57    $this->Lexer->addSpecialPattern('<jalbum ?[^>\n]*>.*?</jalbum>',$mode,'plugin_jalbum_jalbum');
58  }
59
60
61  /**
62   * Handle the match
63   */
64  function handle($match, $state, $pos, &$handler){
65    list($str_params,$str_images) = explode('>',substr($match,7,-9),2);
66    $thumb = $this->_extract_params($str_params);
67    $imgs = $this->_extract_images($str_images);
68    return array($thumb,$imgs);
69  }
70
71  /**
72   * Create output
73   */
74  function render($mode, &$renderer, $data) {
75    global $conf;
76    if ($mode == 'xhtml') {
77      list($param,$imgs) = $data;
78      if (isset($param['albumid']) && $param['albumid'] != 'off') {
79      	$txt = '';
80		$url = 'http://jalbum.net/browse/user/album/'.$param['albumid'];
81	    $target = "target='extern' ";
82		$fullview .= '<a '.$target.'href="'.$url.'">';
83		$fullview .= $param['albumtitle'];
84		$fullview .= '</a>';
85      	if (isset($param['albumshow']) && $param['albumshow'] != 'off') {
86      		$width = $param['width'];
87			if ($width == 20) {
88				$width = 800;
89			}
90			$height = $param['height'];
91			if ($height == 20) {
92				$height = '640';
93			}
94			$txt .= '<div class="jalbumnet">';
95			$txt .= '<iframe align="left" valign="top" title="'.$param['albumtitle'].'"'.
96			' src="'.$url.'"'.
97			' width="'.$width.'"'.
98			' height="'.$height.'"'.
99			' style="width:'.$param['width'].'; height: '.$param['height'].'"'.
100			'>'.
101			'</iframe>'	;
102			$txt .= '<br/>Fullview: '.$fullview;
103			$txt .= '</div>';
104   		} else 	{
105			$txt .= $fullview;
106		}
107		$renderer->doc .= $txt;
108      } elseif (isset($param['albumdir']) && $param['albumdir'] != 'off') {
109		$url = $this->getConf('baseurl').$param['albumdir'];
110		if (isset($this->getConf['target'])) {
111	  		$target = 'target="'.$this->getConf['target'].'" ';
112		} else {
113	  		$target = '';
114		}
115		$txt .= '<a '.$target.'href="'.$url.'">';
116		$txt .= $param['albumtitle'];
117		$txt .= '</a>';
118		$renderer->doc .= $txt;
119      } else {
120		foreach($imgs as $img) {
121	  		list($image_path,$image_name,$alttxt) = $img;
122	  //	  $img_href = str_ireplace('jpg','html',$image_name);
123	  		$alttxt = strip_tags($alttxt);
124	  		$img_href = str_replace('JPG','html',$image_name);
125	  		$txt = "";
126	  		if (isset($this->getConf['target'])) {
127	    		$target = 'target="'.$this->getConf['target'].'" ';
128	  		} else {
129	    		$target = '';
130	  		}
131			if ($param['jpgtohtml'] == 'on') {
132	  			$txt .= '<a '.$target.' href="'.$this->getConf('baseurl').$image_path.'/slides/'.$img_href.'">';
133			}
134	  		$txt .= '<img border="0" src="'.$this->getConf('baseurl').$image_path.'/thumbs/'.$image_name.'" alt="'.$alttxt.'"';
135	  		if (isset($param['width'])) {
136	    		$txt .= ' width="'.$param['width'].'"';
137	  		}
138	  		if (isset($param['align'])) {
139	    		$txt .= ' align="'.$param['align'].'"';
140	  		} else {
141	    		$txt .= ' align="'.$this->dflt['align'].'"';
142	  		}
143	  		if (isset($param['height'])) {
144	    		$txt .= ' height="'.$param['height'].'"';
145	  		} else {
146	    		$txt .= ' height="'.$this->dflt['height'].'"';
147	  		}
148	  		$txt .= '/>';//IMG
149			if ($param['jpgtohtml'] == 'on') {
150	  			$txt .= '</a>';
151			}
152	  		$renderer->doc .= $txt;
153		}
154      }
155      return true;
156    }
157    return false;
158  }
159
160  /**
161   * extract parameters for jalbum from the parameter string
162   *
163   * @param   string    $str_params   string of key="value" or key='value' pairs
164   * @return  array                   associative array of parameters key=>value
165   */
166  function _extract_params($str_params) {
167    $param = array();
168    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
169    if (sizeof($param) == 0) {
170      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
171    }
172    // parse match for instructions, break into key value pairs
173    $thumb = $this->dflt;
174    foreach($param as $kvpair) {
175      list($match,$key,$val) = $kvpair;
176//      $key = strtolower($key);
177      if (isset($thumb[$key])) $thumb[$key] = $val;
178    }
179    return $thumb;
180  }
181
182
183  /**
184   * extract images from the wiki syntax data
185   *
186   * @param   string    $str_images   multi-line string of imagepath,imagename,description triplets
187   * @return  array                   multi-dimensional array of imagepath,imagename,description triplets
188   */
189  function _extract_images($str_images) {
190    $image = array();
191    preg_match_all('/^(.*),(.*),(.*?)$/um',$str_images,$image,PREG_SET_ORDER);
192
193    $overlay = array();
194    foreach ($image as $pt) {
195      list($match,$imgpath,$img,$text) = $pt;
196
197      $text = addslashes(str_replace("\n","",p_render("xhtml",p_get_instructions($text),$info)));
198
199      $overlay[] = array($imgpath,$img,$text);
200    }
201    return $overlay;
202  }
203} // class
204