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		$documenttype_command = substr($match, 12, -2); //strip markup
66
67        // if the command is using a pipe separator, the mediapath file must be extracted. As of now, the text behind the pipe will be ignored!
68        $params = explode("|", $documenttype_command);
69
70        $mediapath_file = $params[0];
71
72        $caption = $mediapath_file;
73
74        if (sizeof($params) > 1) {
75            $caption = $params[1];
76        }
77
78		$thumb = new thumbnail($mediapath_file,$this);
79		if ($thumb->create()) {
80			return array($mediapath_file,$thumb->getMediapath(),$caption);
81		}
82
83		return array($mediapath_file);
84    }
85
86    /**
87     * Render xhtml output or metadata
88     *
89     * @param string        $mode     Renderer mode (supported modes: xhtml)
90     * @param Doku_Renderer $renderer The renderer
91     * @param array         $data     The data from the handler() function
92     *
93     * @return bool If rendering was successful.
94     */
95    public function render($mode, Doku_Renderer $renderer, $data)
96    {
97		list ($mediapath_file, $mediapath_thumbnail, $caption) = $data;
98
99        if ($mode == 'xhtml') {
100
101			// check if a thumbnail file was found
102			if (!$mediapath_thumbnail) {
103				if ($this->getConf('show_missing_thumb_error')) {
104					$renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file;
105					return true;
106				} else {
107					return false;
108				}
109			}
110
111			$src = ml($mediapath_thumbnail,array());
112
113			$i          = array();
114
115			$i['title'] = $mediapath_file;
116			$i['style'] = "max-width:".$this->getConf('thumb_max_dimension')."px;max-height:".$this->getConf('thumb_max_dimension')."px";
117
118			$iatt = buildAttributes($i);
119
120			$renderer->doc .= 	($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') .
121								'<img src="'.$src.'" title="'.$caption.'" '.$iatt.' />' .
122								($this->getConf('link_to_media_file') ? '</a>' : '');
123            return true;
124
125        } elseif ($mode == 'odt') {
126
127			// TODO: yet to implement
128			$renderer->cdata("");
129			return true;
130
131		}
132
133        return false;
134    }
135}