1<?php
2/**
3 * DokuWiki Plugin mediathumbnails (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Thomas Schäfer <thomas.schaefer@itschert.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14require('thumbnail.php');
15
16class syntax_plugin_mediathumbnails extends DokuWiki_Syntax_Plugin {
17
18	/**
19     * @return string Syntax mode type
20     */
21    public function getType()
22    {
23        return 'substition';
24    }
25
26    /**
27     * @return string Paragraph type
28     */
29    public function getPType()
30    {
31        return 'normal';
32    }
33
34    /**
35     * @return int Sort order - Low numbers go before high numbers
36     */
37    public function getSort()
38    {
39        return 1;
40    }
41
42    /**
43     * Connect lookup pattern to lexer.
44     *
45     * @param string $mode Parser mode
46     */
47    public function connectTo($mode)
48    {
49		$this->Lexer->addSpecialPattern("{{thumbnail>.+?}}", $mode, substr(get_class($this), 7));
50	}
51
52    /**
53     * Handle matches of the mediathumbnails syntax
54     *
55     * @param string       $match   The match of the syntax
56     * @param int          $state   The state of the handler
57     * @param int          $pos     The position in the document
58     * @param Doku_Handler $handler The handler
59     *
60     * @return array Data for the renderer
61     */
62    public function handle($match, $state, $pos, Doku_Handler $handler)
63    {
64		// Locate the given media file and check if it can be opened as zip
65		$mediapath_file = substr($match, 12, -2); //strip markup
66
67		$thumb = new thumbnail($mediapath_file,$this);
68		if ($thumb->create()) {
69			return array($mediapath_file,$thumb->getMediapath());
70		}
71
72		return array($mediapath_file);
73    }
74
75    /**
76     * Render xhtml output or metadata
77     *
78     * @param string        $mode     Renderer mode (supported modes: xhtml)
79     * @param Doku_Renderer $renderer The renderer
80     * @param array         $data     The data from the handler() function
81     *
82     * @return bool If rendering was successful.
83     */
84    public function render($mode, Doku_Renderer $renderer, $data)
85    {
86		list ($mediapath_file, $mediapath_thumbnail) = $data;
87
88        if ($mode == 'xhtml') {
89
90			// check if a thumbnail file was found
91			if (!$mediapath_thumbnail) {
92				if ($this->getConf('show_missing_thumb_error')) {
93					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file;
94					return true;
95				} else {
96					return false;
97				}
98			}
99
100			$src = ml($mediapath_thumbnail,array());
101
102			$i          = array();
103
104			$i['title'] = $mediapath_file;
105			$i['style'] = "max-width:".$this->getConf('thumb_max_dimension')."px;max-height:".$this->getConf('thumb_max_dimension')."px";
106
107			$iatt = buildAttributes($i);
108
109			$renderer->doc .= 	($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') .
110								'<img src="'.$src.'" '.$iatt.' />' .
111								($this->getConf('link_to_media_file') ? '</a>' : '');
112            return true;
113
114        } elseif ($mode == 'odt') {
115
116			// TODO: yet to implement
117			$renderer->cdata("");
118			return true;
119
120		}
121
122        return false;
123    }
124}