2 // vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
3 // +--------------------------------------------------------------------+
4 // | BIFE - Buil It FastEr |
5 // +--------------------------------------------------------------------+
6 // | This file is part of BIFE. |
8 // | BIFE is free software; you can redistribute it and/or modify it |
9 // | under the terms of the GNU General Public License as published by |
10 // | the Free Software Foundation; either version 2 of the License, or |
11 // | (at your option) any later version. |
13 // | BIFE is distributed in the hope that it will be useful, but |
14 // | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 // | General Public License for more details. |
18 // | You should have received a copy of the GNU General Public License |
19 // | along with Hooks; if not, write to the Free Software Foundation, |
20 // | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 // +--------------------------------------------------------------------+
22 // | Created: Wed May 17 18:16:54 ART 2003 |
23 // | Authors: Leandro Lucarella <luca@lugmen.org.ar> |
24 // +--------------------------------------------------------------------+
30 require_once 'BIFE/Widget.php';
33 require_once 'Image/Transform.php';
35 // +X2C Class 20 :Album
37 * Photo album widget. [TODO: Make a better explanation]
41 class BIFE_Album extends BIFE_Widget {
56 * @param array $attrs Attributes.
61 function BIFE_Album($attrs) // ~X2C
63 $this->__construct($attrs);
71 * @param array $attrs Attributes.
76 function __construct($attrs) // ~X2C
78 // TODO - get defaults from an INI file.
82 'THUMBSFORMAT' => 'jpeg',
83 'THUMBSDIR' => '.thumbs',
84 'EXTENSIONS' => 'png,jpg,jpeg,PNG,JPG,JPEG',
88 // TODO - agregar atributo para el LINK, ver de hacer el
91 $this->attrs = array_merge($defaults, $attrs);
92 $this->attrs['EXTENSIONS'] = explode(',', $this->attrs['EXTENSIONS']);
100 * @param HTML_Template_Sigma &$template Template to use to render the widget.
105 function render(&$template) // ~X2C
107 $template->loadTemplateFile('bife_album.html');
108 $template->addBlockFile('ITEMS', 'ITEM', 'bife_album_item.html');
109 $root = $this->attrs['DIR'];
110 $list = $this->getList();
112 $rows = ceil($tot / $this->attrs['COLUMNS']);
113 for ($row = 0; $row < $rows; $row++) {
114 for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) {
115 $cur = $row * $this->attrs['COLUMNS'] + $col;
116 if ($photo = @$list[$cur]) {
117 $selected = ($photo['file'] === $this->attrs['SELECTED']);
118 if (is_null($photo['thumb'])) {
119 $photo['thumb'] = $this->makeThumb($photo['file']);
121 // FIXME - Si no se pudo crear el thumb, devuelve null
122 // (ver si se agrega otro template para indicar error
124 $template->setVariable(
126 'PHOTO' => $photo['file'],
127 'DESC' => $photo['desc'],
128 'THUMB' => $photo['thumb'],
131 $template->parse('ITEM');
134 $template->replaceBlockFile('ITEM',
135 'bife_album_emptyitem.html', true);
138 $template->touchBlock('ITEM');
139 $template->parse('ITEM');
142 $template->parse('FILA');
144 $template->setVariable('DESC', $this->getDescription());
145 return $template->get();
151 * Gets a list of photos with their descriptions and thumbnails.
152 Returns an array of associative arrays with this keys:
154 <li><b>file:</b> Photo filename.</li>
155 <li><b>desc:</b> Photo Description.</li>
156 <li><b>thumb:</b> Photo thumbnail filename.</li>
162 function getList() // ~X2C
164 $root = $this->attrs['DIR'];
165 $exts = $this->attrs['EXTENSIONS'];
166 $format = $this->attrs['THUMBSFORMAT'];
170 while (($file = $d->read()) !== false) {
171 list($path, $name, $ext) = $this->splitFilename("$root/$file");
172 if (is_readable("$root/$file") and in_array($ext, $exts)) {
173 $thumb = $this->getThumbFilename("$root/$file");
175 'file' => "$root/$name",
177 'thumb' => is_readable($thumb) ? $thumb : null,
189 * Creates an image thumbnail, returning his filename.
191 * @param string $filename Filename of the image to create the thumb.
192 * @param int $size Maximum thumbnail size.
197 function makeThumb($filename, $size = 100) // ~X2C
199 $format = $this->attrs['THUMBSFORMAT'];
200 $thumb = $this->getThumbFilename($filename);
201 list($path, $name, $ext) = $this->splitFilename($thumb);
202 $img =& Image_Transform::factory('GD');
203 $img->load($filename);
204 // If image is larger than the maximum size, we resize it.
205 if ($img->img_x > $size or $img->img_y > $size ) {
206 if (!@is_dir($path) and !@mkdir($path)) {
209 if (PEAR::isError($img)) {
212 if (!$img->scale($size)) {
216 $img->save("$path/$name.$format", $format);
225 * Returns the filename of an image thumb.
227 * @param string $filename Filename of the image to get the thumb name.
232 function getThumbFilename($filename) // ~X2C
234 $root = $this->attrs['DIR'];
235 $format = $this->attrs['THUMBSFORMAT'];
236 $thumbsdir = $this->attrs['THUMBSDIR'];
238 list($path, $name, $ext) = $this->splitFilename($filename);
240 return "$root/$thumbsdir/$name.$format";
244 // +X2C Operation 102
246 * Returns the description of the album.
251 function getDescription() // ~X2C
253 $root = $this->attrs['DIR'];
254 return @join('', file($file));
258 // +X2C Operation 100
260 * Splits a filename returning an array with the path, name and extension.
262 * @param string $filename Filename to split.
268 function splitFilename($filename) // ~X2C
270 $path = explode('/', $filename);
271 $file = array_pop($path);
273 if (strstr($file, '.')) {
274 preg_match('|([^/]+?)(\.([^\.]*))?$|', $file, $m);
275 $file = @$m[1] . ((@$m[2] == '.' ) ? '.' : '');
278 $dir = count($path) ? join('/', $path) : '';
279 return array($dir, $file, $ext);
283 } // -X2C Class :Album