1<?php
2 /**
3 * DokuWiki Plugin tagfilter (Helper Component)
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  lisps
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class helper_plugin_pageimage extends DokuWiki_Plugin {
13
14    function getMethods() {
15        $result = array();
16        $result[] = array(
17                'name'   => 'th',
18                'desc'   => 'returns the header of the image column for pagelist',
19                'return' => array('header' => 'image'),
20                );
21        $result[] = array(
22                'name'   => 'td',
23                'desc'   => 'returns the image of a given page',
24                'params' => array('id' => 'string'),
25                'return' => array('links' => 'string'),
26                );
27
28        return $result;
29    }
30
31    /**
32     * @return string the column header for th pagelist Plugin
33     */
34    function th() {
35        return $this->getLang('image');
36    }
37
38    /**
39     * @param $id
40     * @return string the cell data for the Pagelist Plugin
41     */
42    function td($id) {
43        $height = $this->getConf('height');
44        $width = null;
45        $align = 'center';
46        $ret = '';
47        $src = $this->getImageID($id);
48
49        if(!$src) {
50            $src = $this->getConf('default_image');
51            //return '';
52        }
53
54        list($ext,$mime,$dl) = mimetype($src);
55
56        if(substr($mime,0,5) == 'image' && ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png')){
57
58            $ret .= '<a href="'.wl($id).'">';
59            //add image tag
60            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height)).'"';
61            $ret .= ' class="media'.$align.'"';
62
63            // make left/right alignment for no-CSS view work (feeds)
64            if($align == 'right') $ret .= ' align="right"';
65            if($align == 'left')  $ret .= ' align="left"';
66            $ret .= ' style="';
67            if ( !is_null($width) ) {
68                $ret .= ' width:'.hsc($width);
69			} else {
70				$ret .= ' max-width:200px;';
71			}
72
73            if ( !is_null($height) ) {
74                $ret .= ' height:'.hsc($height);
75			}
76            $ret .= '" ';
77            $ret .= ' />';
78            $ret .= '</a>';
79            return $ret;
80        }
81    }
82
83    /**
84     * returns the image related to a page
85     * first checks if pageimage is set,
86     * if not checks if in the same namespace is an image with the same name and ipg,png,jpeg extension
87     * then it checks if theres an firstimage defined
88     * ohterwise it can youse a default_image
89     *
90     * @param string $id page id
91     * @param array $flags possible flag is firstimage
92     * @return string image id
93     *
94     **/
95    function getImageID($id,$flags=array()){
96	$flags = array_merge(array('firstimage' => false),$flags);
97        $src = p_get_metadata($id,'pageimage');
98        if( !$src || ($src && !@file_exists(mediaFN($src))) ){
99            $src = $id .'.jpg';
100            if(!@file_exists(mediaFN($src))) {
101                $src = $id .'.png';
102                if(!@file_exists(mediaFN($src))) {
103                    $src = $id .'.jpeg';
104                    if(!@file_exists(mediaFN($src))) {
105                        $src = p_get_metadata($id,'relation firstimage');
106                        if(!$flags['firstimage'] || !@file_exists(mediaFN($src))) {
107                            return $this->getConf('default_image');
108                        }
109                    }
110                }
111            }
112        }
113        return $src;
114    }
115
116}
117// vim:ts=4:sw=4:et:
118