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 // +X2C Class 20 :Album
35 * Photo album widget. [TODO: Make a better explanation]
39 class BIFE_Album extends BIFE_Widget {
54 * @param array $attrs Attributes.
59 function BIFE_Album($attrs) // ~X2C
61 $this->__construct($attrs);
69 * @param array $attrs Attributes.
74 function __construct($attrs) // ~X2C
76 // TODO - get defaults from an INI file.
80 'THUMBSFORMAT' => 'jpeg',
81 'THUMBSDIR' => '.thumbs',
82 'EXTENSIONS' => 'png,jpg,jpeg,PNG,JPG,JPEG',
87 $this->attrs = array_merge($defaults, $attrs);
88 $this->attrs['EXTENSIONS'] = explode(',', $this->attrs['EXTENSIONS']);
96 * @param HTML_Template_Sigma &$template Template to use to render the widget.
101 function render(&$template) // ~X2C
103 $template->loadTemplateFile('bife_album.html');
104 $template->addBlockFile('ITEMS', 'ITEM', 'bife_album_item.html');
105 $root = $this->attrs['DIR'];
106 $list = $this->getList();
108 $rows = ceil($tot / $this->attrs['COLUMNS']);
109 for ($row = 0; $row < $rows; $row++) {
110 for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) {
111 $cur = $row * $this->attrs['COLUMNS'] + $col;
112 if ($photo = @$list[$cur]) {
113 $selected = ($photo['file'] === $this->attrs['SELECTED']);
114 if (is_null($photo['thumb'])) {
115 $photo['thumb'] = $this->makeThumb($photo['file']);
117 // FIXME - Si no se pudo crear el thumb, devuelve null
118 // (ver si se agrega otro template para indicar error
120 $template->setVariable(
122 'PHOTO' => $photo['file'],
123 'DESC' => $photo['desc'],
124 'THUMB' => $photo['thumb'],
127 $template->parse('ITEM');
130 $template->replaceBlockFile('ITEM',
131 'bife_album_emptyitem.html', true);
134 $template->touchBlock('ITEM');
135 $template->parse('ITEM');
138 $template->parse('FILA');
140 $template->setVariable('DESC', $this->getDescription());
141 return $template->get();
147 * Gets a list of photos with their descriptions and thumbnails.
148 Returns an array of associative arrays with this keys:
150 <li><b>file:</b> Photo filename.</li>
151 <li><b>desc:</b> Photo Description.</li>
152 <li><b>thumb:</b> Photo thumbnail filename.</li>
158 function getList() // ~X2C
160 $root = $this->attrs['DIR'];
161 $exts = $this->attrs['EXTENSIONS'];
162 $format = $this->attrs['THUMBSFORMAT'];
166 while (($file = $d->read()) !== false) {
167 list($path, $name, $ext) = $this->splitFilename("$root/$file");
168 if (is_readable("$root/$file") and in_array($ext, $exts)) {
169 $thumb = $this->getThumbFilename("$root/$file");
171 'file' => "$root/$file",
173 'thumb' => is_readable($thumb) ? $thumb : null,
185 * Creates an image thumbnail, returning his filename.
187 * @param string $filename Filename of the image to create the thumb.
188 * @param int $size Maximum thumbnail size.
193 function makeThumb($filename, $size = 100) // ~X2C
195 $format = $this->attrs['THUMBSFORMAT'];
196 $thumb = $this->getThumbFilename($filename);
197 list($path, $name, $ext) = $this->splitFilename($thumb);
198 $img =& Image_Transform::factory('GD');
199 $img->load($filename);
200 // If image is larger than the maximum size, we resize it.
201 if ($img->img_x > $size or $img->img_y > $size ) {
202 if (!@is_dir($path) and !@mkdir($path)) {
205 if (PEAR::isError($img)) {
208 if (!$img->scale($size)) {
212 $img->save("$path/$name.$format", $format);
221 * Returns the filename of an image thumb.
223 * @param string $filename Filename of the image to get the thumb name.
228 function getThumbFilename($filename) // ~X2C
230 $root = $this->attrs['DIR'];
231 $format = $this->attrs['THUMBSFORMAT'];
232 $thumbsdir = $this->attrs['THUMBSDIR'];
234 list($path, $name, $ext) = $this->splitFilename($filename);
236 return "$root/$thumbsdir/$name.$format";
240 // +X2C Operation 102
242 * Returns the description of the album.
247 function getDescription() // ~X2C
249 $root = $this->attrs['DIR'];
250 return @join('', file($file));
254 // +X2C Operation 100
256 * Splits a filename returning an array with the path, name and extension.
258 * @param string $filename Filename to split.
264 function splitFilename($filename) // ~X2C
266 $path = explode('/', $filename);
267 $file = array_pop($path);
269 if (strstr($file, '.')) {
270 preg_match('|([^/]+?)(\.([^\.]*))?$|', $file, $m);
271 $file = @$m[1] . ((@$m[2] == '.' ) ? '.' : '');
274 $dir = count($path) ? join('/', $path) : '';
275 return array($dir, $file, $ext);
279 } // -X2C Class :Album