1<?php
2/**
3 * Plugin PhotoWidget: Allow Display a Gallery3, Flicker, Picassa Photo Widget in a wiki page.
4 * photowidget.swf is from Roytanck.com ( http://www.roytanck.com/get-my-flickr-widget/ )
5 *
6 * @license		GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author		Jonathan Tsai <tryweb@ichiayi.com>
8 * 2012/4/13
9 *	1. Release first version
10 * 2012/4/14
11 *  1. Add Album Name Function : Get <photowidget feed='xx'>Album Name</photowidget>
12 */
13
14if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16require_once(DOKU_PLUGIN.'syntax.php');
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_photowidget extends DokuWiki_Syntax_Plugin {
22
23  var $dflt = array(
24		    'feed' => 'off',
25		    'width' => 300,
26		    'height' => 300
27		    );
28
29    /**
30     * return some info
31     */
32  function getInfo(){
33    return array(
34		'author' => 'Jonathan Tsai',
35		'email'  => 'tryweb@ichiayi.com',
36		'date'   => '2012-04-14',
37		'name'   => 'photowidget Plugin',
38		'desc'   => 'Add Photo Widget to your wiki
39                              Syntax: <photowidget params>Album Name</photowidget>',
40		'url'    => 'http://www.dokuwiki.org/plugin:photowidget'
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 900; }
53
54    /**
55    * Connect pattern to lexer
56    */
57  function connectTo($mode) {
58    $this->Lexer->addSpecialPattern('<photowidget ?[^>\n]*>.*?</photowidget>',$mode,'plugin_photowidget');
59  }
60
61
62  function postConnect() {
63    $this->Lexer->addExitPattern('</photowidget>','plugin_photowidget');
64  }
65
66    /**
67     * Handle the match
68     */
69  function handle($match, $state, $pos, &$handler){
70    // break matched cdata into its components
71    list($str_params,$str_albumname) = explode('>',substr($match,13,-14),2);
72    $gmap = $this->_extract_params($str_params);
73	$gmap['name']=$str_albumname;
74
75    return array($gmap);
76  }
77
78    /**
79     * Create output
80     */
81  function render($mode, &$renderer, $data) {
82    if ($mode == 'xhtml') {
83		list($param) = $data;
84
85		$w_width = str_replace("px","",$param['width']);
86		$w_height = str_replace("px","",$param['height']);
87		$str_name =(isset($param['name']) && trim($param['name'])!='')?$param['name']:'';
88		$w_height0=($str_name!='')?$w_height+25:$w_height;
89		$renderer->doc .= '<div class="g-block-content" style="width:'.$w_width.'px;height:'.$w_height0.'px;border:1px solid #ACD7F5;padding:5px;">';
90		$renderer->doc .= '<ccenter>'.$str_name.'</center>';
91		if (isset($param['feed']) && $param['feed'] != 'off') {
92			$renderer->doc .= $this->photowidget_iframe($param, $w_width, $w_height);
93		}
94		$renderer->doc .= '</div>';
95	}
96	return false;
97  }
98
99
100	// 17:58 2012/4/13
101	function photowidget_iframe($param, $p_width, $p_height)  {
102
103		$w_swfLoc = DOKU_BASE.'lib/plugins/photowidget/photowidget.swf';
104		$w_feedurl = 'http://'.$_SERVER['HTTP_HOST'].DOKU_BASE.'lib/plugins/photowidget/photowidget.php?feed='.urlencode(str_replace("&", "_andand_",$param['feed']));
105
106		$txt = '<div><object type="application/x-shockwave-flash" data="'.$w_swfLoc.'" width="'.$p_width.'" height="'.$p_height.'">';
107		$txt .= '<param name="movie" value="'.$w_swfLoc.'" />';
108		$txt .= '<param name="bgcolor" value="#ffffff" />';
109		$txt .= '<param name="AllowScriptAccess" value="always" />';
110		$txt .= '<param name="flashvars" value="feed='.$w_feedurl.'" />';
111		$txt .= '<p>This widget requires Flash Player 9 or better</p>';
112		$txt .= '</object></div>';
113		return $txt;
114	}
115
116
117  /**
118   * extract parameters for the googlemap from the parameter string
119   *
120   * @param   string    $str_params   string of key="value" pairs
121   * @return  array                   associative array of parameters key=>value
122   */
123  function _extract_params($str_params) {
124    $param = array();
125    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
126    if (sizeof($param) == 0) {
127      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
128    }
129    // parse match for instructions, break into key value pairs
130    $gmap = $this->dflt;
131    foreach($param as $kvpair) {
132      list($match,$key,$val) = $kvpair;
133//    $key = strtolower($key);
134      if (isset($gmap[$key])) $gmap[$key] = $val;
135    }
136
137    return $gmap;
138  }
139} /* CLASS */
140