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