xref: /plugin/popupviewer/syntax/viewer.php (revision a8a154eb0ff7588a8fc102146b4f47536c0e90e4)
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ßbachrequire_once(DOKU_PLUGIN.'syntax.php');
150b5af900SGerry Weißbach
160b5af900SGerry Weißbachclass syntax_plugin_popupviewer_viewer extends DokuWiki_Syntax_Plugin {
170b5af900SGerry 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
370b5af900SGerry Weißbach    function handle($match, $state, $pos, &$handler) {
380b5af900SGerry Weißbach
390b5af900SGerry Weißbach        $close = strstr( $match, "{{popupclose>") !== false;
400b5af900SGerry Weißbach
410b5af900SGerry Weißbach        $orig = substr($match, $close ? 13 : 8, -2);
420b5af900SGerry Weißbach        list($id, $name) = explode('|', $orig, 2); // find ID/Params + Name Extension
430b5af900SGerry Weißbach        list($name, $title) = explode('%', $name, 2); // find Name and Title
440b5af900SGerry Weißbach        list($id, $param) = explode('?', $id, 2); // find ID + Params
450b5af900SGerry Weißbach        list($w, $h) = explode('x', $param, 2); // find Size
460b5af900SGerry Weißbach
470b5af900SGerry Weißbach        return array(trim($id), $name, $title, $w, $h, $orig, $close);
480b5af900SGerry Weißbach    }
490b5af900SGerry Weißbach
500b5af900SGerry Weißbach    function render($mode, &$renderer, $data) {
510b5af900SGerry Weißbach        global $ID, $conf, $JSINFO;
520b5af900SGerry Weißbach
530b5af900SGerry Weißbach        list($id, $name, $title, $w, $h, $orig, $close, $isImageMap) = $data;
540b5af900SGerry Weißbach        if ( empty($id) ) { $exists = false; } else
550b5af900SGerry Weißbach        {
560b5af900SGerry Weißbach            $page   = resolve_id(getNS($ID),$id);
570b5af900SGerry Weißbach            $file   = mediaFN($page);
580b5af900SGerry Weißbach            $exists = @file_exists($file) && @is_file($file);
590b5af900SGerry Weißbach        }
600b5af900SGerry Weißbach
610b5af900SGerry Weißbach        $scID = sectionID(noNs($id), $renderer->headers);
620b5af900SGerry Weißbach        $more = 'id="' . $scID . '"';
630b5af900SGerry Weißbach        $script = '';
640b5af900SGerry Weißbach
650b5af900SGerry Weißbach        if ( $exists ) {
660b5af900SGerry Weißbach            // is Media
670b5af900SGerry Weißbach
680b5af900SGerry Weißbach            $p1 = Doku_Handler_Parse_Media($orig);
690b5af900SGerry Weißbach
700b5af900SGerry Weißbach            $p = array();
710b5af900SGerry Weißbach            $p['alt'] = $id;
720b5af900SGerry Weißbach            $p['class'] = 'popupimage';
730b5af900SGerry Weißbach            $p['title'] = $title;
740b5af900SGerry Weißbach            $p['id'] = 'popupimage_' . $scID;
750b5af900SGerry Weißbach            if ($p1['width']) $p['width'] = $p1['width'];
760b5af900SGerry Weißbach            if ($p1['height']) $p['height'] = $p1['height'];
770b5af900SGerry Weißbach            if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
780b5af900SGerry Weißbach            if ($p1['align']) $p['class'] .= ' media' . $p1['align'];
790b5af900SGerry Weißbach
800b5af900SGerry Weißbach            $p2 = buildAttributes($p);
810b5af900SGerry Weißbach            if ( empty($name) ) {
820b5af900SGerry Weißbach                $name = '<img src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2.'/>';
830b5af900SGerry Weißbach            } else {
840b5af900SGerry Weißbach                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
850b5af900SGerry 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)));
860b5af900SGerry Weißbach
870b5af900SGerry Weißbach                $name = preg_replace("%^(<img[^>]*?)>%", "$1 id=\"popupimage_$scID\">", $name);
880b5af900SGerry Weißbach            }
890b5af900SGerry Weißbach
900b5af900SGerry Weißbach            $more = $this->_getOnClickHandler($close, array('isImage' => true, 'id' => $id));
910b5af900SGerry Weißbach            $id = ml($id);
920b5af900SGerry Weißbach
930b5af900SGerry Weißbach        } else {
940b5af900SGerry Weißbach            // is Page
950b5af900SGerry Weißbach            resolve_pageid(getNS($ID),$id,$exists);
960b5af900SGerry Weißbach            if ( empty($name) ) {
970b5af900SGerry Weißbach                $name = htmlspecialchars(noNS($id),ENT_QUOTES,'UTF-8');
980b5af900SGerry Weißbach                if ($conf['useheading'] && $id ) {
990b5af900SGerry Weißbach                    $heading = p_get_first_heading($id,true);
1000b5af900SGerry Weißbach                    if ($heading) {
1010b5af900SGerry Weißbach                        $name = htmlspecialchars($heading,ENT_QUOTES,'UTF-8');
1020b5af900SGerry Weißbach                    }
1030b5af900SGerry Weißbach                }
1040b5af900SGerry Weißbach            } else {
1050b5af900SGerry Weißbach                $name = trim(p_render($mode, p_get_instructions(trim($name)), $info));
1060b5af900SGerry Weißbach            }
1070b5af900SGerry Weißbach
1080b5af900SGerry Weißbach			$data = array(
1090b5af900SGerry Weißbach				'width' => $w,
1100b5af900SGerry Weißbach				'height' => $h,
1110b5af900SGerry Weißbach				'id' => $id
1120b5af900SGerry Weißbach			);
1130b5af900SGerry Weißbach
1140b5af900SGerry Weißbach            // Add ID for AJAX - this.href for offline versions
1150b5af900SGerry Weißbach            $more = $this->_getOnClickHandler($close, $data);
1160b5af900SGerry Weißbach            $id=wl($id);
1170b5af900SGerry Weißbach        }
1180b5af900SGerry Weißbach
1190b5af900SGerry Weißbach        $renderer->doc .= $this->_renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script);
1200b5af900SGerry Weißbach
1210b5af900SGerry Weißbach        return true;
1220b5af900SGerry Weißbach    }
1230b5af900SGerry Weißbach
1240b5af900SGerry Weißbach    function _renderFinalPopupImage($id, $exists, $more, $name, $isImageMap, $script, $class='') {
1250b5af900SGerry Weißbach
1260b5af900SGerry Weißbach        $more .= ' class="wikilink' . ($exists?1:2) . (!empty($class) ? ' ' . $class : '' ). '"';
1270b5af900SGerry 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)));
1280b5af900SGerry Weißbach
1290b5af900SGerry Weißbach        if ( !is_array($isImageMap) ) {
1300b5af900SGerry Weißbach            return '<a href="'.$id.'" ' . $more . ' >' . $name . '</a>' . $script;
1310b5af900SGerry Weißbach        } else {
1320b5af900SGerry Weißbach            $return = '<area href="'.$id.'" ' . $more . '';
1330b5af900SGerry Weißbach            $return .= ' title="'.$name.'" alt="'.$name.'"';
1340b5af900SGerry Weißbach            $return .= ' shape="'.$isImageMap['shape'].'" coords="'.$isImageMap['coords'].'" />' . $script;
1350b5af900SGerry Weißbach
1360b5af900SGerry Weißbach            return $return;
1370b5af900SGerry Weißbach        }
1380b5af900SGerry Weißbach    }
1390b5af900SGerry Weißbach
1400b5af900SGerry Weißbach    function _getOnClickHandler($close, $data=array()) {
1410b5af900SGerry Weißbach        if ( !$close ) {
1420b5af900SGerry Weißbach            return ' popupviewerdata="' . htmlentities(json_encode(array_filter($data))) . '"';
1430b5af900SGerry Weißbach        } else {
1440b5af900SGerry Weißbach            return ' popupviewerclose';
1450b5af900SGerry Weißbach        }
1460b5af900SGerry Weißbach    }
147*a8a154ebSGerry Weißbach
148*a8a154ebSGerry Weißbach	/**
149*a8a154ebSGerry Weißbach	 * Implements API from imagemap
150*a8a154ebSGerry Weißbach	 */
151*a8a154ebSGerry Weißbach    function convertToImageMapArea($imagemap, $data, $pos) {
152*a8a154ebSGerry Weißbach
153*a8a154ebSGerry Weißbach        list($id, $name, $title, $w, $h, $orig, $close) = $data;
154*a8a154ebSGerry Weißbach
155*a8a154ebSGerry Weißbach        if ( !preg_match('/^(.*)@([^@]+)$/u', array_pop(explode('|', $name)), $match)) {
156*a8a154ebSGerry Weißbach            return;
157*a8a154ebSGerry Weißbach        }
158*a8a154ebSGerry Weißbach
159*a8a154ebSGerry Weißbach        $coords = explode(',',$match[2]);
160*a8a154ebSGerry Weißbach        if (count($coords) == 3) {
161*a8a154ebSGerry Weißbach            $shape = 'circle';
162*a8a154ebSGerry Weißbach        } elseif (count($coords) == 4) {
163*a8a154ebSGerry Weißbach            $shape = 'rect';
164*a8a154ebSGerry Weißbach        } elseif (count($coords) >= 6) {
165*a8a154ebSGerry Weißbach            $shape = 'poly';
166*a8a154ebSGerry Weißbach        } else {
167*a8a154ebSGerry Weißbach            return;
168*a8a154ebSGerry Weißbach        }
169*a8a154ebSGerry Weißbach
170*a8a154ebSGerry Weißbach        $coords = array_map('trim', $coords);
171*a8a154ebSGerry Weißbach        $name = trim($match[1]);
172*a8a154ebSGerry 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));
173*a8a154ebSGerry Weißbach    }
1740b5af900SGerry Weißbach}
1750b5af900SGerry Weißbach// vim:ts=4:sw=4:et:enc=utf-8:
176