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');
15require_once(DOKU_INC.'inc/parser/handler.php');
16
17class syntax_plugin_phosphor_phosphor extends DokuWiki_Syntax_Plugin {
18
19    private $headers = array();
20
21    function getInfo(){
22        return array_merge(confToHash(dirname(__FILE__).'/info.txt'), array(
23                'name' => 'PhosPhor - movie embedding',
24                'desc' => 'See: http://www.divergentmedia.com/support/documentation/Phosphor'
25                ));
26    }
27
28    function getType() { return 'substition'; }
29    function getPType() { return 'block'; }
30    function getSort() { return 1; }
31
32    function connectTo($mode) {
33
34        // {{phosphor> de:products:helpdesk:mobile:phosphor:openticketandshowimage:openticketandshowimage.jpg |background=de:products:helpdesk:mobile:phosphor:iphone-landscape-masked.png?600&background-inset-left=139px&background-inset-top=72px&content-frame-zoom=0.6699&background-overlay=de:products:helpdesk:mobile:phosphor:iphone-landscape-masked-overlay.png |}}
35        // {{phosphor>$image$|$title$}}
36        // params:
37        //            class
38        //            prepend
39        $this->Lexer->addSpecialPattern('{{phosphor>[^}]+}}', $mode, 'plugin_phosphor_phosphor');
40    }
41
42    function handle($match, $state, $pos, Doku_Handler $handler) {
43
44        $orig = substr($match, 11, -2);
45        list($id, $background, $title) = explode('|', $orig, 3); // find ID/Params + Name Extension
46        list($id, $param) = explode('?', $id, 2); // find ID + Params
47        $paramlist = array_merge(explode('&', $param), explode('&', $background));
48
49        $params = array();
50        foreach($paramlist as $param)
51        {
52            list($n, $v) = explode('=', $param);
53            $params[$n] = trim($v);
54        }
55
56        return array(trim($id), $title, $params, $orig);
57    }
58
59    function render($mode, Doku_Renderer $renderer, $data) {
60
61        if ($mode == 'xhtml') {
62            $this->phosphorContent($renderer, $data);
63            return true;
64        }
65
66        return false;
67    }
68
69    function phosphorContent(&$renderer, $data, $returnOnly = false, $tag='img')
70    {
71        global $ID;
72
73        list($id, $title, $params, $orig) = $data;
74        if ( empty($id) ) { $exists = false; } else
75        {
76            $page   = resolve_id(getNS($ID),$id);
77            $file   = mediaFN($page);
78            $exists = @file_exists($file) && @is_file($file);
79        }
80
81        $scID = sectionID(noNs($id), $this->headers);
82        $more = 'id="' . $scID . '"';
83        $script = '';
84
85        if ( $exists ) {
86            // is Media
87
88            $p1 = Doku_Handler_Parse_Media($orig);
89
90            $p = array();
91            $p['alt'] = $title;
92            $params['class'] .= ' phosphor';
93            $p['class'] = $params['class'];
94
95/*
96            $p['class'] = 'phosphor';
97            if ( !empty($params['class']) ) {
98                unset($params['class']);
99            }
100*/
101            $p['title'] = $title;
102            $p['id'] = 'anim_target_' . $scID;
103
104            $p['imageArray'] = $this->getImageArray($id, $params['prepend']);
105
106            $name = array_shift(explode('.', noNS($id), 2));
107            $p['callback'] = 'phosphorCallback_' . $name;
108            $p['json'] = $this->existingMediaFile(getNS($id), $name . '_animationdata.jsonp' );
109
110            if ($p1['width']) $p['width'] = $p1['width'];
111            if ($p1['height']) $p['height'] = $p1['height'];
112            if ($p1['title'] && !$p['title']) { $p['title'] = $p1['title']; $p['alt'] = $p1['title']; }
113            if ($p1['align']) $params['class'] .= ' media' . $p1['align'];
114            if ($params['speed']) $p['speed'] = $params['speed'];
115            if ($params['loop']) $p['looping'] = $params['loop'];
116
117            $p2 = buildAttributes($p);
118            $content = '<' . $tag . ' src="' . ml($id, array( 'w' => $p['width'], 'h' => $p['height'] ) ) . '" '.$p2;
119            $content .= ($tag == 'img' ? '/>' : '></' . $tag . '>');
120
121            if ( $returnOnly ) {
122                return $content;
123            }
124
125            $this->backgroundContainer($renderer, $params, $content);
126        }
127    }
128
129    function backgroundContainer(&$renderer, &$params, $content='')
130    {
131        if ( !empty($params['background']) ) {
132
133            $background = Doku_Handler_Parse_Media($params['background']);
134
135            $style = 'background-image:url(' . ml($background['src'], array( 'w' => $background['width'], 'h' => $background['height'] ) ) . ');';
136            if ( !empty($background['width']) ) {
137
138                if ( empty($background['height']) ) {
139                     $info = @getimagesize(mediaFN($background['src'])); //get original size
140                     $background['height'] = round(($background['width'] * $info[1]) / MAX(0.001, $info[0]));
141                }
142
143                $style .= 'background-size:' . $background['width'] . 'px ' . $background['height'] . 'px;';
144                $style .= 'width:' . (!empty($params['width'])?$params['width']:$background['width'] . 'px') .'; height:' . $background['height'] . 'px;';
145            }
146
147            $style2 = 'padding-top:' . $params['background-inset-top'] . ';';
148            $style2 .= 'padding-left:' . $params['background-inset-left'] . ';';
149
150            $zoom = 'zoom="' . $params['content-frame-zoom'] . '"';
151            $params['class'] .= ' hidden';
152
153            if ( !empty($params['background-overlay']) ) {
154                $overlay = 'overlay="' . ml($params['background-overlay'], array( 'w' => $background['width'], 'h' => $background['height'] ) ) . '"';
155            }
156
157            if ( !empty($params['loop']) && $params['loop'] == "true" ) {
158                $loop = 'loop="true"';
159            }
160        }
161
162        $renderer->doc .= '<span class="' . trim($params['class']) . '" style="' . $style . '" ' . (!empty($zoom)?$zoom:'') . ' ' . (!empty($overlay)?$overlay:'') . ' ' . (!empty($loop)?$loop:'') . ' >';
163        $renderer->doc .= '<span style="' . $style2 . '" class="phcontent">';
164        $renderer->doc .= $content;
165        $renderer->doc .= '</span></span>';
166    }
167
168    function getImageArray($baseID, $prepend)
169    {
170        if ( empty($prepend) ) {
171            $prepend = "_atlas";
172        }
173
174        $return = array();
175        $ns = getNS($baseID);
176        $baseID = noNS($baseID);
177        $counter = 0;
178        list($name, $ext) = explode('.', $baseID, 2);
179
180        do {
181            if ( !is_null($page = $this->existingMediaFile($ns, $name . $prepend. sprintf('%03u', $counter) . '.' . $ext)) ) {
182                $counter ++;
183                $return[] = $page;
184            } else {
185                break;
186            }
187        } while (1==1);
188
189        return implode(',', $return);
190    }
191
192    function existingMediaFile($ns, $name)
193    {
194        $page = resolve_id($ns, $name);
195        $file   = mediaFN($page);
196        $exists = @file_exists($file) && @is_file($file);
197        if ( $exists ) {
198            return ml($page);
199        }
200
201        return null;
202    }
203}
204// vim:ts=4:sw=4:et:enc=utf-8:
205