xref: /plugin/popupviewer/syntax/viewer.php (revision 252878ea9b67e9760d1f2fc41bb670af655a2401)
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        list($id, $name, $title, $w, $h, $orig, $close, $isImageMap, $keepOpen) = $data;
65
66        $exists = false;
67        if ( !empty($id) ) {
68            $origID = $id;
69            resolve_mediaid(getNS($ID),$id,$exists);
70            if ( !$exists ) {
71                $id = $origID;
72                resolve_pageid(getNS($ID),$id);
73            }
74        }
75
76        $scID = sectionID(noNs($id), $this->headers);
77        $more = 'id="' . $scID . '"';
78        $script = '';
79
80        if ( $exists ) {
81            // is Media
82
83            $p1 = Doku_Handler_Parse_Media($orig);
84
85            $p = array();
86            $p['alt'] = $id;
87            $p['class'] = 'popupimage';
88            $p['title'] = $title;
89            $p['id'] = 'popupimage_' . $scID;
90            if ($p1['width']) $p['width'] = $p1['width'];
91            if ($p1['height']) $p['height'] = $p1['height'];
92            if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
93            if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
94
95            $p2 = buildAttributes($p);
96            if ( empty($name) ) {
97                $name = '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
98            } else {
99                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
100                $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)));
101
102                $name = preg_replace("%^(<img[^>]*?)>%", "$1 id=\"popupimage_$scID\">", $name);
103            }
104
105            $more = $this->_getOnClickHandler($close, array('isImage' => true, 'id' => $id));
106            $id = ml($id);
107
108        } else {
109            // is Page
110            resolve_pageid(getNS($ID),$id,$exists);
111            if ( empty($name) ) {
112                $name = htmlspecialchars(noNS($id),ENT_QUOTES,'UTF-8');
113                if ($conf['useheading'] && $id ) {
114                    $heading = p_get_first_heading($id,true);
115                    if ($heading) {
116                        $name = htmlspecialchars($heading,ENT_QUOTES,'UTF-8');
117                    }
118                }
119            } else {
120                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
121            }
122
123			$data = array(
124				'width' => $w,
125				'height' => $h,
126				'id' => $id
127			);
128
129			if ( $keepOpen ) {
130    			$data['keepOpen'] = true;
131			}
132
133            // Add ID for AJAX - this.href for offline versions
134            $more = $this->_getOnClickHandler($close, $data);
135            $id=wl($id);
136        }
137
138        $renderer->doc .= $this->_renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script);
139
140        return true;
141    }
142
143    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
144
145        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
146        $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)));
147
148        if ( !is_array($isImageMap) ) {
149            return '<a href="'.$id.'" ' . trim($more) . ' >' . $name . '</a>' . $script;
150        } else {
151            $return = '<area href="'.$id.'" ' . trim($more) . '';
152            $return .= ' title="'.$name.'" alt="'.$name.'"';
153            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
154
155            return $return;
156        }
157    }
158
159    function _getOnClickHandler($close, $data=array()) {
160        if ( !$close ) {
161            return ' data-popupviewer="' . htmlentities(json_encode(array_filter($data))) . '"';
162        } else {
163            return ' data-popupviewerclose';
164        }
165    }
166
167	/**
168	 * Implements API from imagemap
169	 */
170    function convertToImageMapArea($imagemap, $data, $pos) {
171
172        list($id, $name, $title, $w, $h, $orig, $close) = $data;
173
174        if ( !preg_match('/^(.*)@([^@]+)$/u', array_pop(explode('|', $name)), $match)) {
175            return;
176        }
177
178        $coords = explode(',',$match[2]);
179        if (count($coords) == 3) {
180            $shape = 'circle';
181        } elseif (count($coords) == 4) {
182            $shape = 'rect';
183        } elseif (count($coords) >= 6) {
184            $shape = 'poly';
185        } else {
186            return;
187        }
188
189        $coords = array_map('trim', $coords);
190        $name = trim($match[1]);
191        $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));
192    }
193}
194// vim:ts=4:sw=4:et:enc=utf-8:
195