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        global $ID;
42
43        $close = strstr( $match, "{{popupclose>") !== false;
44
45        $orig = substr($match, $close ? 13 : 8, -2);
46        $partId = explode('|', $orig, 2); // find ID/Params + Name Extension
47        $id = $partId[0];
48        $name = $partId[1] ?? '';
49        $partName = explode('%', $name, 2); // find Name and Title
50        $name = $partName[0];
51        $title = $part[1] ?? '';
52        $partParam = explode('?', $id, 2); // find ID + Params
53        $id = $partParam[0];
54        $param = $partParam[1] ?? '';
55
56        $params = explode('&', strtolower($param));
57        $w = $h = $keepOpen = null;
58        foreach( $params as $p ) {
59            if ( strtolower( $p ) === 'keepopen' ) {
60                $keepOpen = true;
61            } else {
62                list($w, $h) = explode('x', $p, 2); // find Size
63            }
64        }
65
66        $id = trim($id);
67        $exists = false;
68        if ( !empty($id) ) {
69            $origID = $id;
70            resolve_mediaid(getNS($ID),$id,$exists);
71            if ( !$exists ) {
72                $id = $origID;
73            }
74        }
75
76        $p1 = Doku_Handler_Parse_Media($orig);
77        return array($id, $name, $title, $w, $h, $orig, $close, null, $keepOpen, $exists, $p1);
78    }
79
80    function render($mode, Doku_Renderer $renderer, $data) {
81        global $ID, $conf, $JSINFO;
82
83        if ( $mode != 'xhtml' && $mode != 'metadata' ) { return true; }
84
85        list($id, $name, $title, $w, $h, $orig, $close, $isImageMap, $keepOpen, $exists, $p1) = $data;
86
87        if($mode === 'metadata') {
88            $renderer->internalmedia($id,$title);
89            return;
90        }
91
92        $scID = sectionID(noNs($id), $this->headers);
93        $more = 'id="' . $scID . '"';
94        $script = '';
95
96        if ( $exists ) {
97            // is Media
98            if ( empty($name) ) {
99
100                if ( !method_exists($renderer, '_media') ) {
101                    $p = array();
102                    $p['alt'] = $id;
103                    $p['class'] = 'popupimage';
104                    $p['title'] = $title;
105                    $p['id'] = 'popupimage_' . $scID;
106                    if ($p1['width']) $p['width'] = $p1['width'];
107                    if ($p1['height']) $p['height'] = $p1['height'];
108                    if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
109                    if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
110
111                    $p2 = buildAttributes($p);
112                    $name = '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
113                } else {
114                    $name = $renderer->_media($id, ($title ? $title : ($p1['title'] ? $p1['title'] : $id) ), ' popupimage' . ($p1['align'] ? ' media' . $p1['align'] : '' ), $p1['width'], $p1['height']);
115                }
116            } else {
117                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
118                $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)));
119
120                $name = preg_replace("%^(<img[^>]*?)>%", "$1 id=\"popupimage_$scID\">", $name);
121            }
122
123            $more = $this->_getOnClickHandler($close, array('isImage' => true, 'id' => $id));
124            $id = ml($id);
125
126        } else {
127            // is Page
128            resolve_pageid(getNS($ID),$id,$exists);
129            if ( empty($name) ) {
130                $name = htmlspecialchars(noNS($id),ENT_QUOTES,'UTF-8');
131                if ($conf['useheading'] && $id ) {
132                    $heading = p_get_first_heading($id,true);
133                    if ($heading) {
134                        $name = htmlspecialchars($heading,ENT_QUOTES,'UTF-8');
135                    }
136                }
137            } else {
138                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
139            }
140
141            $data = array(
142                'width' => $w,
143                'height' => $h,
144                'id' => $id
145            );
146
147            if ( $keepOpen ) {
148                $data['keepOpen'] = true;
149            }
150
151            // Add ID for AJAX - this.href for offline versions
152            $more = $this->_getOnClickHandler($close, $data);
153            $id=wl($id);
154        }
155
156        $renderer->doc .= $this->_renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, 'popupviewer');
157
158        return true;
159    }
160
161    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
162
163        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
164        $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)));
165
166        if ( !is_array($isImageMap) ) {
167            return '<a href="'.$id.'" ' . trim($more) . ' >' . $name . '</a>' . $script;
168        } else {
169            $return = '<area href="'.$id.'" ' . trim($more) . '';
170            $return .= ' title="'.$name.'" alt="'.$name.'"';
171            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
172
173            return $return;
174        }
175    }
176
177    function _getOnClickHandler($close, $data=array()) {
178        if ( !$close ) {
179            return ' data-popupviewer="' . htmlentities(json_encode(array_filter($data))) . '"';
180        } else {
181            return ' data-popupviewerclose';
182        }
183    }
184
185    /**
186     * Implements API from imagemap
187     */
188    function convertToImageMapArea($imagemap, $data, $pos) {
189
190        list($id, $name, $title, $w, $h, $orig, $close) = $data;
191
192        if ( !preg_match('/^(.*)@([^@]+)$/u', array_pop(explode('|', $name)), $match)) {
193            return;
194        }
195
196        $coords = explode(',',$match[2]);
197        if (count($coords) == 3) {
198            $shape = 'circle';
199        } elseif (count($coords) == 4) {
200            $shape = 'rect';
201        } elseif (count($coords) >= 6) {
202            $shape = 'poly';
203        } else {
204            return;
205        }
206
207        $coords = array_map('trim', $coords);
208        $name = trim($match[1]);
209        $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));
210    }
211}
212// vim:ts=4:sw=4:et:enc=utf-8:
213