xref: /plugin/popupviewer/syntax/viewer.php (revision 53d985153b04151130573414c69b5c6cf738b1fd)
1<?php
2/**
3 * popoutviewer Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once(DOKU_PLUGIN.'syntax.php');
15
16class syntax_plugin_popupviewer_viewer extends DokuWiki_Syntax_Plugin {
17
18    private $headers = array();
19
20    function getInfo(){
21        return array_merge(confToHash(dirname(__FILE__).'/plugin.info.txt'), array(
22				'name' => 'PopUpViewer Linking Component',
23				'desc' => 'Takes a Page to be diplayed in an overlay pop-out'
24				));
25    }
26
27    function getType() { return 'substition'; }
28    function getPType() { return 'normal'; }
29    function getSort() { return 98; }
30
31    function connectTo($mode) {
32
33        $this->Lexer->addSpecialPattern('{{popup>[^}]+}}}}', $mode, 'plugin_popupviewer_viewer');
34        $this->Lexer->addSpecialPattern('{{popup>[^}]+}}', $mode, 'plugin_popupviewer_viewer');
35        $this->Lexer->addSpecialPattern('{{popupclose>[^}]+}}}}', $mode, 'plugin_popupviewer_viewer');
36        $this->Lexer->addSpecialPattern('{{popupclose>[^}]+}}', $mode, 'plugin_popupviewer_viewer');
37    }
38
39    function handle($match, $state, $pos, Doku_Handler $handler) {
40
41        $close = strstr( $match, "{{popupclose>") !== false;
42
43        $orig = substr($match, $close ? 13 : 8, -2);
44        list($id, $name) = explode('|', $orig, 2); // find ID/Params + Name Extension
45        list($name, $title) = explode('%', $name, 2); // find Name and Title
46        list($id, $param) = explode('?', $id, 2); // find ID + Params
47
48        $params = explode('&', strtolower($param));
49        $w = $h = $keepOpen = null;
50        foreach( $params as $p ) {
51            if ( $p === 'keepOpen' ) {
52                $keepOpen = true;
53            } else {
54                list($w, $h) = explode('x', $p, 2); // find Size
55            }
56        }
57
58        return array(trim($id), $name, $title, $w, $h, $orig, $close, null, $keepOpen);
59    }
60
61    function render($mode, Doku_Renderer $renderer, $data) {
62        global $ID, $conf, $JSINFO;
63
64        if ( $mode != 'xhtml' && $mode != 'metadata' ) { return true; }
65
66        list($id, $name, $title, $w, $h, $orig, $close, $isImageMap, $keepOpen) = $data;
67
68        $exists = false;
69        if ( !empty($id) ) {
70            $origID = $id;
71            resolve_mediaid(getNS($ID),$id,$exists);
72            if ( !$exists ) {
73                $id = $origID;
74            }
75        }
76
77        if($mode === 'metadata') {
78            $renderer->internalmedia($id,$title);
79            return;
80        }
81
82        $scID = sectionID(noNs($id), $this->headers);
83        $more = 'id="' . $scID . '"';
84        $script = '';
85
86        if ( $exists ) {
87            // is Media
88
89            $p1 = Doku_Handler_Parse_Media($orig);
90
91            if ( empty($name) ) {
92
93                if ( !method_exists($renderer, '_media') ) {
94                    $p = array();
95                    $p['alt'] = $id;
96                    $p['class'] = 'popupimage';
97                    $p['title'] = $title;
98                    $p['id'] = 'popupimage_' . $scID;
99                    if ($p1['width']) $p['width'] = $p1['width'];
100                    if ($p1['height']) $p['height'] = $p1['height'];
101                    if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
102                    if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
103
104                    $p2 = buildAttributes($p);
105                    $name = '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
106                } else {
107                    $name = $renderer->_media($id, ($title ? $title : ($p1['title'] ? $p1['title'] : $id) ), ' popupimage' . ($p1['align'] ? ' media' . $p1['align'] : '' ), $p1['width'], $p1['height']);
108                }
109            } else {
110                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
111                $name = trim(preg_replace("%^(\s|\r|\n)*?<a.+?>(.*)?</a>(\s|\r|\n)*?$%is", "$2", preg_replace("%^(\s|\r|\n)*?<p.*?>(.*)?</p>(\s|\r|\n)*?$%is", "$2", $name)));
112
113                $name = preg_replace("%^(<img[^>]*?)>%", "$1 id=\"popupimage_$scID\">", $name);
114            }
115
116            $more = $this->_getOnClickHandler($close, array('isImage' => true, 'id' => $id));
117            $id = ml($id);
118
119        } else {
120            // is Page
121            resolve_pageid(getNS($ID),$id,$exists);
122            if ( empty($name) ) {
123                $name = htmlspecialchars(noNS($id),ENT_QUOTES,'UTF-8');
124                if ($conf['useheading'] && $id ) {
125                    $heading = p_get_first_heading($id,true);
126                    if ($heading) {
127                        $name = htmlspecialchars($heading,ENT_QUOTES,'UTF-8');
128                    }
129                }
130            } else {
131                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
132            }
133
134			$data = array(
135				'width' => $w,
136				'height' => $h,
137				'id' => $id
138			);
139
140			if ( $keepOpen ) {
141    			$data['keepOpen'] = true;
142			}
143
144            // Add ID for AJAX - this.href for offline versions
145            $more = $this->_getOnClickHandler($close, $data);
146            $id=wl($id);
147        }
148
149        $renderer->doc .= $this->_renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script);
150
151        return true;
152    }
153
154    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
155
156        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
157        $name = trim(preg_replace("%^(\s|\r|\n)*?<a.+?>(.*)?</a>(\s|\r|\n)*?$%is", "$2", preg_replace("%^(\s|\r|\n)*?<p.*?>(.*)?</p>(\s|\r|\n)*?$%is", "$2", $name)));
158
159        if ( !is_array($isImageMap) ) {
160            return '<a href="'.$id.'" ' . trim($more) . ' >' . $name . '</a>' . $script;
161        } else {
162            $return = '<area href="'.$id.'" ' . trim($more) . '';
163            $return .= ' title="'.$name.'" alt="'.$name.'"';
164            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
165
166            return $return;
167        }
168    }
169
170    function _getOnClickHandler($close, $data=array()) {
171        if ( !$close ) {
172            return ' data-popupviewer="' . htmlentities(json_encode(array_filter($data))) . '"';
173        } else {
174            return ' data-popupviewerclose';
175        }
176    }
177
178	/**
179	 * Implements API from imagemap
180	 */
181    function convertToImageMapArea($imagemap, $data, $pos) {
182
183        list($id, $name, $title, $w, $h, $orig, $close) = $data;
184
185        if ( !preg_match('/^(.*)@([^@]+)$/u', array_pop(explode('|', $name)), $match)) {
186            return;
187        }
188
189        $coords = explode(',',$match[2]);
190        if (count($coords) == 3) {
191            $shape = 'circle';
192        } elseif (count($coords) == 4) {
193            $shape = 'rect';
194        } elseif (count($coords) >= 6) {
195            $shape = 'poly';
196        } else {
197            return;
198        }
199
200        $coords = array_map('trim', $coords);
201        $name = trim($match[1]);
202        $imagemap->CallWriter->writeCall(array('plugin', array('popupviewer_viewer', array($id, $name, $title, $w, $h, $orig, $close, array('shape' => $shape, 'coords' => join(',',$coords))), DOKU_LEXER_MATCHED), $pos));
203    }
204}
205// vim:ts=4:sw=4:et:enc=utf-8:
206