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