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