]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhotoAlbum.php
added missing 4th basepage arg at plugin->run() to almost all plugins. This caused...
[SourceForge/phpwiki.git] / lib / plugin / PhotoAlbum.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PhotoAlbum.php,v 1.2 2004-02-17 12:11:36 rurban Exp $');
3
4 /**
5  * WikiPlugin which makes an 'album' of a set of photos with optional
6  * descriptions.
7  *
8  * @author: Ted Vinke <teddy@jouwfeestje.com>
9  *
10  * Usage:
11  * <?plugin PhotoAlbum
12  *          src=http://server/textfile
13  *          mode=[column|row]
14  *          desc=true
15  *          sort=false
16  *          height=50%
17  *          width=50%
18  * ?>
19  *
20  * 'src' is a CSV textfile which separates filename and description of
21  * each photo. Photos listed in textfile have to be in same directory
22  * as the file. Descriptions are optional. The 'mode' specifies how
23  * it's displayed, 'column' means vertically, 'row' means
24  * horizontally.
25  *
26  * E.g. possible content of a valid textfile:
27  *
28  * photo-01.jpg; Me and my girlfriend
29  * photo-02.jpg
30  * christmas.gif; Merry Christmas!
31  *
32  * Only 'src' parameter is mandatory. Height and width are calculated
33  * compared to original metrics retrieved by php function
34  * getimagesize() and can be absolute or a percentage (e.g. "50%)
35  *
36  * TODO:
37  *
38  * - parse any local directory for pictures
39  * - specify picture(s) as parameter(s)
40  * - 'advanced' options such as limit pictures on one page, use more
41  *   pages, thumbnail function which enlarges pictures on same page...
42  * - implement more layout options, such as usage of tables. Currently
43  *   row mode with descriptions isn't very nice. :(
44  *
45  */
46
47 define('DESC_SEP', ";");
48
49 class WikiPlugin_PhotoAlbum
50 extends WikiPlugin
51 {
52     function getName () {
53         return _("PhotoAlbum");
54     }
55
56     function getDescription () {
57         return _("Displays a set of photos listed in a text file with optional descriptions");
58     }
59
60     function getVersion() {
61         return preg_replace("/[Revision: $]/", '',
62                             "\$Revision: 1.2 $");
63     }
64
65     function getDefaultArguments() {
66         return array('src'      => '',          // textfile
67                      'mode'     => 'row',       // 'column' or 'row'
68                      'desc'     => true,        // show descriptions
69                      'sort'     => false,       // sorted alphabetically
70                      'align'    => 'center',    // only for column mode
71                      'height'   => '',
72                      'width'    => ''
73                      );
74     }
75
76     function run($dbi, $argstr, &$request, $basepage) {
77         extract($this->getArgs($argstr, $request));
78
79         if (!$src) {
80             return $this->error(fmt("%s parameter missing", "'src'"));
81         }
82
83         if (! IsSafeURL($src)) {
84             return $this->error(_("Bad url in src: remove all of <, >, \""));
85         }
86
87         @$fp = fopen($src, "r");
88
89         if (!$fp) {
90             return $this->error(fmt("Unable to read %s ", $src));
91         }
92
93         $photos = array();
94         while ($data = fgetcsv ($fp, 1024, DESC_SEP)) {
95             if (count($data) == 0 || empty($data[0]))
96                 continue;
97
98             // otherwise when empty 'undefined index 1' PHP warning appears
99             if (empty($data[1]))
100                 $data[1] = '';
101
102             $photos[count($photos)] = array ("name" => dirname($src) . "/"
103                                                        . trim("$data[0]"),
104                                              "desc" => trim("$data[1]"),
105                                              );
106         }
107
108         fclose ($fp);
109
110         if (count($photos) == 0) {
111             return;
112         }
113
114         if ($sort) {
115             sort($photos);
116         }
117
118         $bundle = HTML("\n  "); // nicer html ouput
119
120         while (list($key, $value) = each($photos)) {
121
122             if (!$desc) { // no desciptions
123                 $value["desc"] = '';
124             }
125             $params = array('alt'    => $value["desc"],
126                             'src'    => $value["name"],
127                             'border' => "0",
128                             );
129
130             $size = @getimagesize($value["name"]);
131
132             // only if width or height are given, we add it to the
133             // image parameters
134             if (!empty($width)) {
135                 $newwidth = $this->newSize($size[0], $width);
136                 $params = array_merge($params, array("width" => $newwidth));
137             }
138
139             if (!empty($height)) {
140                 $newheight = $this->newSize($size[1], $height);
141                 $params = array_merge($params, array("height" => $newheight));
142             }
143
144             $bundle->pushcontent(HTML::img($params), "\n"); // wrap html ouput
145
146             if ($mode == 'column') {
147                 $bundle->pushcontent(HTML::p($value["desc"]));
148             }
149             else if ($mode == 'row') {
150                 $bundle->pushcontent(" ".$value["desc"]." ");
151             }
152         }
153
154         if ($mode == 'column') {
155             return HTML::div(array("align" => $align), $bundle);
156         }
157         else {
158             return $bundle;
159         }
160     }
161
162     /**
163      * Calculate the new size in pixels when the original size with a
164      * value is given.
165      *
166      * Value is either absolute value or HTML percentage e.g. "50%".
167      */
168     function newSize($oldSize, $value) {
169
170         if (substr($value, strlen($value) - 1) != "%") {
171             return $value;
172         }
173         substr_replace($value, "%", "");
174         return ($oldSize*$value)/100;
175     }
176
177 };
178
179 // $Log: not supported by cvs2svn $
180 // Revision 1.1  2003/01/05 04:21:06  carstenklapp
181 // New plugin by Ted Vinke (sf tracker patch #661189)
182 //
183
184 // For emacs users
185 // Local Variables:
186 // mode: php
187 // tab-width: 8
188 // c-basic-offset: 4
189 // c-hanging-comment-ender-p: nil
190 // indent-tabs-mode: nil
191 // End:
192 ?>