1<?php
2/**
3 * Make images zoomable
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Heiko H�bel
7 * Uses Cloud Zoom, Copyright (c) 2010 R Ceccor, www.professorcloud.com
8 * and jQuery (jQuery.org)
9 */
10
11if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15class syntax_plugin_zoom extends DokuWiki_Syntax_Plugin {
16
17    /**
18     * What kind of syntax are we?
19     */
20    function getType(){
21        return 'substition';
22    }
23
24    /**
25     * What about paragraphs?
26     */
27    function getPType(){
28        return 'block';
29    }
30
31    /**
32     * Where to sort in?
33     */
34    function getSort(){
35        return 301;
36    }
37
38
39    /**
40     * Connect pattern to lexer
41     */
42    function connectTo($mode) {
43        $this->Lexer->addSpecialPattern('\{\{zoom>[^}]*\}\}',$mode,'plugin_zoom');
44    }
45
46    /**
47     * Handle the match
48     */
49    function handle($match, $state, $pos, &$handler){
50        global $ID;
51
52        $data = array(
53            'width'         => 500,
54            'height'        => 250,
55        );
56
57        $match = substr($match,7,-2); //strip markup from start and end
58
59		// alignment
60        $data['align'] = 0;
61        if (substr($match,0,1) == ' ') {
62			if (substr($match,-1,1) == ' ') {
63				$data['align'] = 3;
64			}
65			else
66			{
67				$data['align'] = 1;
68			}
69		}
70		elseif (substr($match,-1,1) == ' ') {
71			$data['align'] = 2;
72		}
73		elseif (substr($match,0,1) == '*') {
74			$match = substr($match,1);
75			if (substr($match,-1,1) == '*') {
76				$match = substr($match,0,-1);
77				$data['align'] = 3;
78			}
79			else
80			{
81				$data['align'] = 4;
82			}
83		}
84		elseif (substr($match,-1,1) == '*') {
85				$match = substr($match,0,-1);
86				$data['align'] = 5;
87		}
88
89        // extract params
90        list($img,$all_params) = explode('?',$match,2);
91		// extract params
92		list($params,$ext_params) = explode('&',$all_params,2);
93        $img = trim($img);
94		//remove unwanted quotes and other chars
95		$ext_params = str_replace(chr(34),"",$ext_params);
96		$ext_params = str_replace(chr(47),"",$ext_params);
97		$ext_params = str_replace(chr(92),"",$ext_params);
98		if (!isset($ext_params) || empty($ext_params) || strlen($ext_params) < 5) {
99			$data['ext_params'] = "position: 'inside', adjustX: -1, adjustY: -1, showTitle: false";
100		}
101		else
102		{
103			if(strpos($ext_params,"position")=== false){
104				$data['ext_params'] = "position:'inside', adjustX:-1, adjustY:-1, showTitle:false, " . trim($ext_params);
105			}
106			else
107			{
108				$data['ext_params'] = "showTitle:false, " . trim($ext_params);
109			}
110		}
111        // resolving relatives
112        $data['image'] = resolve_id(getNS($ID),$img);
113
114        $file = mediaFN($data['image']);
115        list($data['imageWidth'],$data['imageHeight']) = @getimagesize($file);
116
117        // size
118        if(preg_match('/\b(\d+)[xX](\d+)\b/',$params,$match)){
119            $data['width']  = $match[1];
120            $data['height'] = $match[2];
121        }
122		else
123		{
124			if(preg_match('/\b[xX](\d+)\b/',$params,$match)){
125				$data['height']  = $match[1];
126				$data['width'] = $match[1]*$data['imageWidth']/$data['imageHeight'];
127			}
128			else if(preg_match('/\b(\d+)\b/',$params,$match)){
129				$data['width']  = $match[1];
130				$data['height'] = $match[1]*$data['imageHeight']/$data['imageWidth'];
131			}
132		}
133        return $data;
134    }
135
136    /**
137     * Create output
138     */
139    function render($mode, &$R, $data) {
140        if($mode != 'xhtml') return false;
141        global $ID;
142
143		$align = '';
144		switch ($data['align']) {
145			case 1:
146				$align = 'cloud-zoom-block-right';
147				break;
148			case 2:
149				$align = 'cloud-zoomblock-left';
150				break;
151			case 3:
152				$style = 'width:' . $data['width'] . 'px;';
153				$align = 'cloud-zoom-center';
154				break;
155			case 4:
156				$align = 'cloud-zoom-float-right';
157				break;
158			case 5:
159				$align = 'cloud-zoom-float-left';
160				break;
161		}
162        $img = '<div style="' . $style . '" class="' . $align . '"><div style="position:relative;"><a href="'.ml($data['image'], array('w'=>$data['imageWidth'],'h'=>$data['imageHeight'])).'" class="cloud-zoom" rel="' . $data['ext_params'] .'"><img src="'.
163                    ml($data['image'], array('w'=>$data['width'],'h'=>$data['height'])).'" width="'.
164                    $data['width'].'" height="'.$data['height'].'" alt="" /></a></div></div>';
165
166        $R->doc .= $img;
167        return true;
168    }
169
170}
171
172