xref: /plugin/popupviewer/syntax/viewer.php (revision ce73262edf620f8efa32367d865f4adf81dfdf31)
10b5af900SGerry Weißbach<?php
20b5af900SGerry Weißbach/**
30b5af900SGerry Weißbach * popoutviewer Plugin
40b5af900SGerry Weißbach *
50b5af900SGerry Weißbach * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
60b5af900SGerry Weißbach * @author     i-net software <tools@inetsoftware.de>
70b5af900SGerry Weißbach * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
80b5af900SGerry Weißbach */
90b5af900SGerry Weißbach
100b5af900SGerry Weißbach// must be run within Dokuwiki
110b5af900SGerry Weißbachif(!defined('DOKU_INC')) die();
120b5af900SGerry Weißbachif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
130b5af900SGerry Weißbach
140b5af900SGerry Weißbachclass syntax_plugin_popupviewer_viewer extends DokuWiki_Syntax_Plugin {
150b5af900SGerry Weißbach
1616153982SGerry Weißbach    private $headers = array();
1716153982SGerry Weißbach
180b5af900SGerry Weißbach    function getInfo(){
190b5af900SGerry Weißbach        return array_merge(confToHash(dirname(__FILE__).'/plugin.info.txt'), array(
200b5af900SGerry Weißbach                'name' => 'PopUpViewer Linking Component',
210b5af900SGerry Weißbach                'desc' => 'Takes a Page to be diplayed in an overlay pop-out'
220b5af900SGerry Weißbach                ));
230b5af900SGerry Weißbach    }
240b5af900SGerry Weißbach
250b5af900SGerry Weißbach    function getType() { return 'substition'; }
260b5af900SGerry Weißbach    function getPType() { return 'normal'; }
270b5af900SGerry Weißbach    function getSort() { return 98; }
280b5af900SGerry Weißbach
290b5af900SGerry Weißbach    function connectTo($mode) {
300b5af900SGerry Weißbach
310b5af900SGerry Weißbach        $this->Lexer->addSpecialPattern('{{popup>[^}]+}}}}', $mode, 'plugin_popupviewer_viewer');
320b5af900SGerry Weißbach        $this->Lexer->addSpecialPattern('{{popup>[^}]+}}', $mode, 'plugin_popupviewer_viewer');
330b5af900SGerry Weißbach        $this->Lexer->addSpecialPattern('{{popupclose>[^}]+}}}}', $mode, 'plugin_popupviewer_viewer');
340b5af900SGerry Weißbach        $this->Lexer->addSpecialPattern('{{popupclose>[^}]+}}', $mode, 'plugin_popupviewer_viewer');
350b5af900SGerry Weißbach    }
360b5af900SGerry Weißbach
3717068d0fSAndreas Gohr    function handle($match, $state, $pos, Doku_Handler $handler) {
380b5af900SGerry Weißbach
39*1f6f3fcaSYury Ovsyannikov        global $ID;
40*1f6f3fcaSYury Ovsyannikov
410b5af900SGerry Weißbach        $close = strstr( $match, "{{popupclose>") !== false;
420b5af900SGerry Weißbach
430b5af900SGerry Weißbach        $orig = substr($match, $close ? 13 : 8, -2);
44*1f6f3fcaSYury Ovsyannikov        $partId = explode('|', $orig, 2); // find ID/Params + Name Extension
45*1f6f3fcaSYury Ovsyannikov        $id = $partId[0];
46*1f6f3fcaSYury Ovsyannikov        $name = $partId[1] ?? '';
47*1f6f3fcaSYury Ovsyannikov        $partName = explode('%', $name, 2); // find Name and Title
48*1f6f3fcaSYury Ovsyannikov        $name = $partName[0];
49*1f6f3fcaSYury Ovsyannikov        $title = $part[1] ?? '';
50*1f6f3fcaSYury Ovsyannikov        $partParam = explode('?', $id, 2); // find ID + Params
51*1f6f3fcaSYury Ovsyannikov        $id = $partParam[0];
52*1f6f3fcaSYury Ovsyannikov        $param = $partParam[1] ?? '';
530b5af900SGerry Weißbach
541b06b0ceSGerry Weißbach        $params = explode('&', strtolower($param));
551b06b0ceSGerry Weißbach        $w = $h = $keepOpen = null;
561b06b0ceSGerry Weißbach        foreach( $params as $p ) {
57aab2f4b2SGerry Weißbach            if ( strtolower( $p ) === 'keepopen' ) {
581b06b0ceSGerry Weißbach                $keepOpen = true;
591b06b0ceSGerry Weißbach            } else {
601b06b0ceSGerry Weißbach                list($w, $h) = explode('x', $p, 2); // find Size
611b06b0ceSGerry Weißbach            }
621b06b0ceSGerry Weißbach        }
631b06b0ceSGerry Weißbach
64dea8a38dSGerry Weißbach        $id = trim($id);
6514a3d58eSGerry Weißbach        $exists = false;
6614a3d58eSGerry Weißbach        if ( !empty($id) ) {
6714a3d58eSGerry Weißbach            $origID = $id;
6814a3d58eSGerry Weißbach            resolve_mediaid(getNS($ID),$id,$exists);
69252878eaSlisps            if ( !$exists ) {
7014a3d58eSGerry Weißbach                $id = $origID;
7114a3d58eSGerry Weißbach            }
720b5af900SGerry Weißbach        }
730b5af900SGerry Weißbach
74dea8a38dSGerry Weißbach        $p1 = Doku_Handler_Parse_Media($orig);
75dea8a38dSGerry Weißbach        return array($id, $name, $title, $w, $h, $orig, $close, null, $keepOpen, $exists, $p1);
76dea8a38dSGerry Weißbach    }
77dea8a38dSGerry Weißbach
78dea8a38dSGerry Weißbach    function render($mode, Doku_Renderer $renderer, $data) {
79dea8a38dSGerry Weißbach        global $ID, $conf, $JSINFO;
80dea8a38dSGerry Weißbach
81dea8a38dSGerry Weißbach        if ( $mode != 'xhtml' && $mode != 'metadata' ) { return true; }
82dea8a38dSGerry Weißbach
83dea8a38dSGerry Weißbach        list($id, $name, $title, $w, $h, $orig, $close, $isImageMap, $keepOpen, $exists, $p1) = $data;
84dea8a38dSGerry Weißbach
8553d98515Slisps        if($mode === 'metadata') {
8653d98515Slisps            $renderer->internalmedia($id,$title);
8753d98515Slisps            return;
8853d98515Slisps        }
8953d98515Slisps
9016153982SGerry Weißbach        $scID = sectionID(noNs($id), $this->headers);
910b5af900SGerry Weißbach        $more = 'id="' . $scID . '"';
920b5af900SGerry Weißbach        $script = '';
930b5af900SGerry Weißbach
940b5af900SGerry Weißbach        if ( $exists ) {
950b5af900SGerry Weißbach            // is Media
96c780bbc5SGerry Weißbach            if ( empty($name) ) {
97c780bbc5SGerry Weißbach
98c780bbc5SGerry Weißbach                if ( !method_exists($renderer, '_media') ) {
990b5af900SGerry Weißbach                    $p = array();
1000b5af900SGerry Weißbach                    $p['alt'] = $id;
1010b5af900SGerry Weißbach                    $p['class'] = 'popupimage';
1020b5af900SGerry Weißbach                    $p['title'] = $title;
1030b5af900SGerry Weißbach                    $p['id'] = 'popupimage_' . $scID;
1040b5af900SGerry Weißbach                    if ($p1['width']) $p['width'] = $p1['width'];
1050b5af900SGerry Weißbach                    if ($p1['height']) $p['height'] = $p1['height'];
1060b5af900SGerry Weißbach                    if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
1070b5af900SGerry Weißbach                    if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
1080b5af900SGerry Weißbach
1090b5af900SGerry Weißbach                    $p2 = buildAttributes($p);
1100b5af900SGerry Weißbach                    $name = '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
1110b5af900SGerry Weißbach                } else {
112c780bbc5SGerry Weißbach                    $name = $renderer->_media($id, ($title ? $title : ($p1['title'] ? $p1['title'] : $id) ), ' popupimage' . ($p1['align'] ? ' media' . $p1['align'] : '' ), $p1['width'], $p1['height']);
113c780bbc5SGerry Weißbach                }
114c780bbc5SGerry Weißbach            } else {
1150b5af900SGerry Weißbach                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
1160b5af900SGerry 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)));
1170b5af900SGerry Weißbach
1180b5af900SGerry Weißbach                $name = preg_replace("%^(<img[^>]*?)>%", "$1 id=\"popupimage_$scID\">", $name);
1190b5af900SGerry Weißbach            }
1200b5af900SGerry Weißbach
1210b5af900SGerry Weißbach            $more = $this->_getOnClickHandler($close, array('isImage' => true, 'id' => $id));
1220b5af900SGerry Weißbach            $id = ml($id);
1230b5af900SGerry Weißbach
1240b5af900SGerry Weißbach        } else {
1250b5af900SGerry Weißbach            // is Page
1260b5af900SGerry Weißbach            resolve_pageid(getNS($ID),$id,$exists);
1270b5af900SGerry Weißbach            if ( empty($name) ) {
1280b5af900SGerry Weißbach                $name = htmlspecialchars(noNS($id),ENT_QUOTES,'UTF-8');
1290b5af900SGerry Weißbach                if ($conf['useheading'] && $id ) {
1300b5af900SGerry Weißbach                    $heading = p_get_first_heading($id,true);
1310b5af900SGerry Weißbach                    if ($heading) {
1320b5af900SGerry Weißbach                        $name = htmlspecialchars($heading,ENT_QUOTES,'UTF-8');
1330b5af900SGerry Weißbach                    }
1340b5af900SGerry Weißbach                }
1350b5af900SGerry Weißbach            } else {
1360b5af900SGerry Weißbach                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
1370b5af900SGerry Weißbach            }
1380b5af900SGerry Weißbach
1390b5af900SGerry Weißbach            $data = array(
1400b5af900SGerry Weißbach                'width' => $w,
1410b5af900SGerry Weißbach                'height' => $h,
1420b5af900SGerry Weißbach                'id' => $id
1430b5af900SGerry Weißbach            );
1440b5af900SGerry Weißbach
1451b06b0ceSGerry Weißbach            if ( $keepOpen ) {
1461b06b0ceSGerry Weißbach                $data['keepOpen'] = true;
1471b06b0ceSGerry Weißbach            }
1481b06b0ceSGerry Weißbach
1490b5af900SGerry Weißbach            // Add ID for AJAX - this.href for offline versions
1500b5af900SGerry Weißbach            $more = $this->_getOnClickHandler($close, $data);
1510b5af900SGerry Weißbach            $id=wl($id);
1520b5af900SGerry Weißbach        }
1530b5af900SGerry Weißbach
154f7024f21SGerry Weißbach        $renderer->doc .= $this->_renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, 'popupviewer');
1550b5af900SGerry Weißbach
1560b5af900SGerry Weißbach        return true;
1570b5af900SGerry Weißbach    }
1580b5af900SGerry Weißbach
1590b5af900SGerry Weißbach    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
1600b5af900SGerry Weißbach
1610b5af900SGerry Weißbach        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
1620b5af900SGerry 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)));
1630b5af900SGerry Weißbach
1640b5af900SGerry Weißbach        if ( !is_array($isImageMap) ) {
165ee88561dSGerry Weißbach            return '<a href="'.$id.'" ' . trim($more) . ' >' . $name . '</a>' . $script;
1660b5af900SGerry Weißbach        } else {
167ee88561dSGerry Weißbach            $return = '<area href="'.$id.'" ' . trim($more) . '';
1680b5af900SGerry Weißbach            $return .= ' title="'.$name.'" alt="'.$name.'"';
1690b5af900SGerry Weißbach            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
1700b5af900SGerry Weißbach
1710b5af900SGerry Weißbach            return $return;
1720b5af900SGerry Weißbach        }
1730b5af900SGerry Weißbach    }
1740b5af900SGerry Weißbach
1750b5af900SGerry Weißbach    function _getOnClickHandler($close, $data=array()) {
1760b5af900SGerry Weißbach        if ( !$close ) {
17771189f11SGerry Weißbach            return ' data-popupviewer="' . htmlentities(json_encode(array_filter($data))) . '"';
1780b5af900SGerry Weißbach        } else {
17971189f11SGerry Weißbach            return ' data-popupviewerclose';
1800b5af900SGerry Weißbach        }
1810b5af900SGerry Weißbach    }
182a8a154ebSGerry Weißbach
183a8a154ebSGerry Weißbach    /**
184a8a154ebSGerry Weißbach     * Implements API from imagemap
185a8a154ebSGerry Weißbach     */
186a8a154ebSGerry Weißbach    function convertToImageMapArea($imagemap, $data, $pos) {
187a8a154ebSGerry Weißbach
188a8a154ebSGerry Weißbach        list($id, $name, $title, $w, $h, $orig, $close) = $data;
189a8a154ebSGerry Weißbach
190a8a154ebSGerry Weißbach        if ( !preg_match('/^(.*)@([^@]+)$/u', array_pop(explode('|', $name)), $match)) {
191a8a154ebSGerry Weißbach            return;
192a8a154ebSGerry Weißbach        }
193a8a154ebSGerry Weißbach
194a8a154ebSGerry Weißbach        $coords = explode(',',$match[2]);
195a8a154ebSGerry Weißbach        if (count($coords) == 3) {
196a8a154ebSGerry Weißbach            $shape = 'circle';
197a8a154ebSGerry Weißbach        } elseif (count($coords) == 4) {
198a8a154ebSGerry Weißbach            $shape = 'rect';
199a8a154ebSGerry Weißbach        } elseif (count($coords) >= 6) {
200a8a154ebSGerry Weißbach            $shape = 'poly';
201a8a154ebSGerry Weißbach        } else {
202a8a154ebSGerry Weißbach            return;
203a8a154ebSGerry Weißbach        }
204a8a154ebSGerry Weißbach
205a8a154ebSGerry Weißbach        $coords = array_map('trim', $coords);
206a8a154ebSGerry Weißbach        $name = trim($match[1]);
207a8a154ebSGerry Weißbach        $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));
208a8a154ebSGerry Weißbach    }
2090b5af900SGerry Weißbach}
2100b5af900SGerry Weißbach// vim:ts=4:sw=4:et:enc=utf-8:
211