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