xref: /plugin/popupviewer/syntax/viewer.php (revision 0b5af90042d213356c1d07f3acce23616720d749)
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    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, &$handler) {
38
39        $close = strstr( $match, "{{popupclose>") !== false;
40
41        $orig = substr($match, $close ? 13 : 8, -2);
42        list($id, $name) = explode('|', $orig, 2); // find ID/Params + Name Extension
43        list($name, $title) = explode('%', $name, 2); // find Name and Title
44        list($id, $param) = explode('?', $id, 2); // find ID + Params
45        list($w, $h) = explode('x', $param, 2); // find Size
46
47        return array(trim($id), $name, $title, $w, $h, $orig, $close);
48    }
49
50    function render($mode, &$renderer, $data) {
51        global $ID, $conf, $JSINFO;
52
53        list($id, $name, $title, $w, $h, $orig, $close, $isImageMap) = $data;
54        if ( empty($id) ) { $exists = false; } else
55        {
56            $page   = resolve_id(getNS($ID),$id);
57            $file   = mediaFN($page);
58            $exists = @file_exists($file) && @is_file($file);
59        }
60
61        $scID = sectionID(noNs($id), $renderer->headers);
62        $more = 'id="' . $scID . '"';
63        $script = '';
64
65        if ( $exists ) {
66            // is Media
67
68            $p1 = Doku_Handler_Parse_Media($orig);
69
70            $p = array();
71            $p['alt'] = $id;
72            $p['class'] = 'popupimage';
73            $p['title'] = $title;
74            $p['id'] = 'popupimage_' . $scID;
75            if ($p1['width']) $p['width'] = $p1['width'];
76            if ($p1['height']) $p['height'] = $p1['height'];
77            if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
78            if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
79
80            $p2 = buildAttributes($p);
81            if ( empty($name) ) {
82                $name = '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
83            } else {
84                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
85                $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)));
86
87                $name = preg_replace("%^(<img[^>]*?)>%", "$1 id=\"popupimage_$scID\">", $name);
88            }
89
90            $more = $this->_getOnClickHandler($close, array('isImage' => true, 'id' => $id));
91            $id = ml($id);
92
93        } else {
94            // is Page
95            resolve_pageid(getNS($ID),$id,$exists);
96            if ( empty($name) ) {
97                $name = htmlspecialchars(noNS($id),ENT_QUOTES,'UTF-8');
98                if ($conf['useheading'] && $id ) {
99                    $heading = p_get_first_heading($id,true);
100                    if ($heading) {
101                        $name = htmlspecialchars($heading,ENT_QUOTES,'UTF-8');
102                    }
103                }
104            } else {
105                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
106            }
107
108			$data = array(
109				'width' => $w,
110				'height' => $h,
111				'id' => $id
112			);
113
114            // Add ID for AJAX - this.href for offline versions
115            $more = $this->_getOnClickHandler($close, $data);
116            $id=wl($id);
117        }
118
119        $renderer->doc .= $this->_renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script);
120
121        return true;
122    }
123
124    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
125
126        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
127        $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)));
128
129        if ( !is_array($isImageMap) ) {
130            return '<a href="'.$id.'" ' . $more . ' >' . $name . '</a>' . $script;
131        } else {
132            $return = '<area href="'.$id.'" ' . $more . '';
133            $return .= ' title="'.$name.'" alt="'.$name.'"';
134            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
135
136            return $return;
137        }
138    }
139
140    function _getOnClickHandler($close, $data=array()) {
141        if ( !$close ) {
142            return ' popupviewerdata="' . htmlentities(json_encode(array_filter($data))) . '"';
143        } else {
144            return ' popupviewerclose';
145        }
146    }
147}
148// vim:ts=4:sw=4:et:enc=utf-8:
149