xref: /plugin/popupviewer/syntax/viewer.php (revision 0b5af90042d213356c1d07f3acce23616720d749)
1*0b5af900SGerry Weißbach<?php
2*0b5af900SGerry Weißbach/**
3*0b5af900SGerry Weißbach * popoutviewer Plugin
4*0b5af900SGerry Weißbach *
5*0b5af900SGerry Weißbach * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6*0b5af900SGerry Weißbach * @author     i-net software <tools@inetsoftware.de>
7*0b5af900SGerry Weißbach * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8*0b5af900SGerry Weißbach */
9*0b5af900SGerry Weißbach
10*0b5af900SGerry Weißbach// must be run within Dokuwiki
11*0b5af900SGerry Weißbachif(!defined('DOKU_INC')) die();
12*0b5af900SGerry Weißbachif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13*0b5af900SGerry Weißbach
14*0b5af900SGerry Weißbachrequire_once(DOKU_PLUGIN.'syntax.php');
15*0b5af900SGerry Weißbach
16*0b5af900SGerry Weißbachclass syntax_plugin_popupviewer_viewer extends DokuWiki_Syntax_Plugin {
17*0b5af900SGerry Weißbach
18*0b5af900SGerry Weißbach    function getInfo(){
19*0b5af900SGerry Weißbach        return array_merge(confToHash(dirname(__FILE__).'/plugin.info.txt'), array(
20*0b5af900SGerry Weißbach				'name' => 'PopUpViewer Linking Component',
21*0b5af900SGerry Weißbach				'desc' => 'Takes a Page to be diplayed in an overlay pop-out'
22*0b5af900SGerry Weißbach				));
23*0b5af900SGerry Weißbach    }
24*0b5af900SGerry Weißbach
25*0b5af900SGerry Weißbach    function getType() { return 'substition'; }
26*0b5af900SGerry Weißbach    function getPType() { return 'normal'; }
27*0b5af900SGerry Weißbach    function getSort() { return 98; }
28*0b5af900SGerry Weißbach
29*0b5af900SGerry Weißbach    function connectTo($mode) {
30*0b5af900SGerry Weißbach
31*0b5af900SGerry Weißbach        $this->Lexer->addSpecialPattern('{{popup>[^}]+}}}}', $mode, 'plugin_popupviewer_viewer');
32*0b5af900SGerry Weißbach        $this->Lexer->addSpecialPattern('{{popup>[^}]+}}', $mode, 'plugin_popupviewer_viewer');
33*0b5af900SGerry Weißbach        $this->Lexer->addSpecialPattern('{{popupclose>[^}]+}}}}', $mode, 'plugin_popupviewer_viewer');
34*0b5af900SGerry Weißbach        $this->Lexer->addSpecialPattern('{{popupclose>[^}]+}}', $mode, 'plugin_popupviewer_viewer');
35*0b5af900SGerry Weißbach    }
36*0b5af900SGerry Weißbach
37*0b5af900SGerry Weißbach    function handle($match, $state, $pos, &$handler) {
38*0b5af900SGerry Weißbach
39*0b5af900SGerry Weißbach        $close = strstr( $match, "{{popupclose>") !== false;
40*0b5af900SGerry Weißbach
41*0b5af900SGerry Weißbach        $orig = substr($match, $close ? 13 : 8, -2);
42*0b5af900SGerry Weißbach        list($id, $name) = explode('|', $orig, 2); // find ID/Params + Name Extension
43*0b5af900SGerry Weißbach        list($name, $title) = explode('%', $name, 2); // find Name and Title
44*0b5af900SGerry Weißbach        list($id, $param) = explode('?', $id, 2); // find ID + Params
45*0b5af900SGerry Weißbach        list($w, $h) = explode('x', $param, 2); // find Size
46*0b5af900SGerry Weißbach
47*0b5af900SGerry Weißbach        return array(trim($id), $name, $title, $w, $h, $orig, $close);
48*0b5af900SGerry Weißbach    }
49*0b5af900SGerry Weißbach
50*0b5af900SGerry Weißbach    function render($mode, &$renderer, $data) {
51*0b5af900SGerry Weißbach        global $ID, $conf, $JSINFO;
52*0b5af900SGerry Weißbach
53*0b5af900SGerry Weißbach        list($id, $name, $title, $w, $h, $orig, $close, $isImageMap) = $data;
54*0b5af900SGerry Weißbach        if ( empty($id) ) { $exists = false; } else
55*0b5af900SGerry Weißbach        {
56*0b5af900SGerry Weißbach            $page   = resolve_id(getNS($ID),$id);
57*0b5af900SGerry Weißbach            $file   = mediaFN($page);
58*0b5af900SGerry Weißbach            $exists = @file_exists($file) && @is_file($file);
59*0b5af900SGerry Weißbach        }
60*0b5af900SGerry Weißbach
61*0b5af900SGerry Weißbach        $scID = sectionID(noNs($id), $renderer->headers);
62*0b5af900SGerry Weißbach        $more = 'id="' . $scID . '"';
63*0b5af900SGerry Weißbach        $script = '';
64*0b5af900SGerry Weißbach
65*0b5af900SGerry Weißbach        if ( $exists ) {
66*0b5af900SGerry Weißbach            // is Media
67*0b5af900SGerry Weißbach
68*0b5af900SGerry Weißbach            $p1 = Doku_Handler_Parse_Media($orig);
69*0b5af900SGerry Weißbach
70*0b5af900SGerry Weißbach            $p = array();
71*0b5af900SGerry Weißbach            $p['alt'] = $id;
72*0b5af900SGerry Weißbach            $p['class'] = 'popupimage';
73*0b5af900SGerry Weißbach            $p['title'] = $title;
74*0b5af900SGerry Weißbach            $p['id'] = 'popupimage_' . $scID;
75*0b5af900SGerry Weißbach            if ($p1['width']) $p['width'] = $p1['width'];
76*0b5af900SGerry Weißbach            if ($p1['height']) $p['height'] = $p1['height'];
77*0b5af900SGerry Weißbach            if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
78*0b5af900SGerry Weißbach            if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
79*0b5af900SGerry Weißbach
80*0b5af900SGerry Weißbach            $p2 = buildAttributes($p);
81*0b5af900SGerry Weißbach            if ( empty($name) ) {
82*0b5af900SGerry Weißbach                $name = '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
83*0b5af900SGerry Weißbach            } else {
84*0b5af900SGerry Weißbach                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
85*0b5af900SGerry Weißbach                $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*0b5af900SGerry Weißbach
87*0b5af900SGerry Weißbach                $name = preg_replace("%^(<img[^>]*?)>%", "$1 id=\"popupimage_$scID\">", $name);
88*0b5af900SGerry Weißbach            }
89*0b5af900SGerry Weißbach
90*0b5af900SGerry Weißbach            $more = $this->_getOnClickHandler($close, array('isImage' => true, 'id' => $id));
91*0b5af900SGerry Weißbach            $id = ml($id);
92*0b5af900SGerry Weißbach
93*0b5af900SGerry Weißbach        } else {
94*0b5af900SGerry Weißbach            // is Page
95*0b5af900SGerry Weißbach            resolve_pageid(getNS($ID),$id,$exists);
96*0b5af900SGerry Weißbach            if ( empty($name) ) {
97*0b5af900SGerry Weißbach                $name = htmlspecialchars(noNS($id),ENT_QUOTES,'UTF-8');
98*0b5af900SGerry Weißbach                if ($conf['useheading'] && $id ) {
99*0b5af900SGerry Weißbach                    $heading = p_get_first_heading($id,true);
100*0b5af900SGerry Weißbach                    if ($heading) {
101*0b5af900SGerry Weißbach                        $name = htmlspecialchars($heading,ENT_QUOTES,'UTF-8');
102*0b5af900SGerry Weißbach                    }
103*0b5af900SGerry Weißbach                }
104*0b5af900SGerry Weißbach            } else {
105*0b5af900SGerry Weißbach                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
106*0b5af900SGerry Weißbach            }
107*0b5af900SGerry Weißbach
108*0b5af900SGerry Weißbach			$data = array(
109*0b5af900SGerry Weißbach				'width' => $w,
110*0b5af900SGerry Weißbach				'height' => $h,
111*0b5af900SGerry Weißbach				'id' => $id
112*0b5af900SGerry Weißbach			);
113*0b5af900SGerry Weißbach
114*0b5af900SGerry Weißbach            // Add ID for AJAX - this.href for offline versions
115*0b5af900SGerry Weißbach            $more = $this->_getOnClickHandler($close, $data);
116*0b5af900SGerry Weißbach            $id=wl($id);
117*0b5af900SGerry Weißbach        }
118*0b5af900SGerry Weißbach
119*0b5af900SGerry Weißbach        $renderer->doc .= $this->_renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script);
120*0b5af900SGerry Weißbach
121*0b5af900SGerry Weißbach        return true;
122*0b5af900SGerry Weißbach    }
123*0b5af900SGerry Weißbach
124*0b5af900SGerry Weißbach    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
125*0b5af900SGerry Weißbach
126*0b5af900SGerry Weißbach        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
127*0b5af900SGerry Weißbach        $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*0b5af900SGerry Weißbach
129*0b5af900SGerry Weißbach        if ( !is_array($isImageMap) ) {
130*0b5af900SGerry Weißbach            return '<a href="'.$id.'" ' . $more . ' >' . $name . '</a>' . $script;
131*0b5af900SGerry Weißbach        } else {
132*0b5af900SGerry Weißbach            $return = '<area href="'.$id.'" ' . $more . '';
133*0b5af900SGerry Weißbach            $return .= ' title="'.$name.'" alt="'.$name.'"';
134*0b5af900SGerry Weißbach            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
135*0b5af900SGerry Weißbach
136*0b5af900SGerry Weißbach            return $return;
137*0b5af900SGerry Weißbach        }
138*0b5af900SGerry Weißbach    }
139*0b5af900SGerry Weißbach
140*0b5af900SGerry Weißbach    function _getOnClickHandler($close, $data=array()) {
141*0b5af900SGerry Weißbach        if ( !$close ) {
142*0b5af900SGerry Weißbach            return ' popupviewerdata="' . htmlentities(json_encode(array_filter($data))) . '"';
143*0b5af900SGerry Weißbach        } else {
144*0b5af900SGerry Weißbach            return ' popupviewerclose';
145*0b5af900SGerry Weißbach        }
146*0b5af900SGerry Weißbach    }
147*0b5af900SGerry Weißbach}
148*0b5af900SGerry Weißbach// vim:ts=4:sw=4:et:enc=utf-8:
149