]> git.llucax.com Git - software/bife/bife-all.git/blobdiff - src/BIFE/Album.php
- Added experimental phing build file.
[software/bife/bife-all.git] / src / BIFE / Album.php
index 1bbc1d63f2ec7c6ca9504dac5396dd55327e6c1c..977949c4b927d1da76a3fdebe5385eccfded804a 100644 (file)
 // +X2C includes
 require_once 'BIFE/Widget.php';
 // ~X2C
 // +X2C includes
 require_once 'BIFE/Widget.php';
 // ~X2C
-require_once 'Album.php';
+
+require_once 'Image/Transform.php';
 
 // +X2C Class 20 :Album
 /**
 
 // +X2C Class 20 :Album
 /**
- * Photo album widget.
-[TODO: Make a better explanation]
+ * Photo album widget. [TODO: Make a better explanation]
  *
  * @access public
  */
  *
  * @access public
  */
@@ -60,17 +60,36 @@ class BIFE_Album extends BIFE_Widget {
      */
     function BIFE_Album($attrs) // ~X2C
     {
      */
     function BIFE_Album($attrs) // ~X2C
     {
+        $this->__construct($attrs);
+    }
+    // -X2C
+
+    // +X2C Operation 57
+    /**
+     * Constructor.
+     *
+     * @param  array $attrs Attributes.
+     *
+     * @return void
+     * @access public
+     */
+    function __construct($attrs) // ~X2C
+    {
+        // TODO - get defaults from an INI file.
         $defaults = array(
             'DIR'           => '.',
             'RECURSIVE'     => true,
             'THUMBSFORMAT'  => 'jpeg',
             'THUMBSDIR'     => '.thumbs',
         $defaults = array(
             'DIR'           => '.',
             'RECURSIVE'     => true,
             'THUMBSFORMAT'  => 'jpeg',
             'THUMBSDIR'     => '.thumbs',
-            'EXTENSIONS'    => 'png,jpg,jpeg,gif',
+            'EXTENSIONS'    => 'png,jpg,jpeg,PNG,JPG,JPEG',
             'SELECTED'      => '',
             'MAXROWS'       => 0,
             'COLUMNS'       => 4,
             'SELECTED'      => '',
             'MAXROWS'       => 0,
             'COLUMNS'       => 4,
+            // TODO - agregar atributo para el LINK, ver de hacer el
+            //        widget de un link.
         );
         $this->attrs = array_merge($defaults, $attrs);
         );
         $this->attrs = array_merge($defaults, $attrs);
+        $this->attrs['EXTENSIONS'] = explode(',', $this->attrs['EXTENSIONS']);
     }
     // -X2C
 
     }
     // -X2C
 
@@ -80,14 +99,184 @@ class BIFE_Album extends BIFE_Widget {
      *
      * @param  HTML_Template_Sigma &$template Template to use to render the widget.
      *
      *
      * @param  HTML_Template_Sigma &$template Template to use to render the widget.
      *
-     * @return void
+     * @return string
      * @access public
      */
     function render(&$template) // ~X2C
     {
      * @access public
      */
     function render(&$template) // ~X2C
     {
-        extract($this->attrs, EXTR_SKIP);
-        $album =& new Hook_Album($DIR, $RECURSIVE, $THUMBSFORMAT, $THUMBSDIR, $EXTENSIONS);
-        return $album->album($template, $SELECTED, $MAXROWS, $COLUMNS);
+        $template->loadTemplateFile('bife_album.html');
+        $template->addBlockFile('ITEMS', 'ITEM', 'bife_album_item.html');
+        $root = $this->attrs['DIR'];
+        $list = $this->getList();
+        $tot  = count($list);
+        $rows = ceil($tot / $this->attrs['COLUMNS']);
+        for ($row = 0; $row < $rows; $row++) {
+            for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) {
+                $cur = $row * $this->attrs['COLUMNS'] + $col;
+                if ($photo = @$list[$cur]) {
+                    $selected = ($photo['file'] === $this->attrs['SELECTED']);
+                    if (is_null($photo['thumb'])) {
+                        $photo['thumb'] = $this->makeThumb($photo['file']);
+                    }
+                    // FIXME - Si no se pudo crear el thumb, devuelve null
+                    // (ver si se agrega otro template para indicar error
+                    // o algo asi).
+                    $template->setVariable(
+                        array(
+                            'PHOTO' => $photo['file'],
+                            'DESC'  => $photo['desc'],
+                            'THUMB' => $photo['thumb'],
+                        )
+                    );
+                    $template->parse('ITEM');
+                } else {
+                    if (!@$empty) {
+                        $template->replaceBlockFile('ITEM',
+                            'bife_album_emptyitem.html', true);
+                        $empty = true;
+                    }
+                    $template->touchBlock('ITEM');
+                    $template->parse('ITEM');
+                }
+            }
+            $template->parse('FILA');
+        }
+        $template->setVariable('DESC', $this->getDescription());
+        return $template->get();
+    }
+    // -X2C
+
+    // +X2C Operation 95
+    /**
+     * Gets a list of photos with their descriptions and thumbnails.
+Returns an array of associative arrays with this keys:
+<ul>
+<li><b>file:</b> Photo filename.</li>
+<li><b>desc:</b> Photo Description.</li>
+<li><b>thumb:</b> Photo thumbnail filename.</li>
+</ul>
+     *
+     * @return array
+     * @access protected
+     */
+    function getList() // ~X2C
+    {
+        $root = $this->attrs['DIR'];
+        $exts = $this->attrs['EXTENSIONS'];
+        $format = $this->attrs['THUMBSFORMAT'];
+        $return = array();
+        $d = dir($root);
+        if ($d) {
+            while (($file = $d->read()) !== false) {
+                list($path, $name, $ext) = $this->splitFilename("$root/$file");
+                if (is_readable("$root/$file") and in_array($ext, $exts)) {
+                    $thumb = $this->getThumbFilename("$root/$file");
+                    $return[] = array(
+                        'file'  => "$root/$name",
+                        'desc'  => $name,
+                        'thumb' => is_readable($thumb) ? $thumb : null,
+                    );
+                }              
+            }
+            $d->close();
+        }
+        return $return;
+    }
+    // -X2C
+
+    // +X2C Operation 97
+    /**
+     * Creates an image thumbnail, returning his filename.
+     *
+     * @param  string $filename Filename of the image to create the thumb.
+     * @param  int $size Maximum thumbnail size.
+     *
+     * @return string
+     * @access protected
+     */
+    function makeThumb($filename, $size = 100) // ~X2C
+    {
+        $format = $this->attrs['THUMBSFORMAT'];
+        $thumb = $this->getThumbFilename($filename);
+        list($path, $name, $ext) = $this->splitFilename($thumb);
+        $img =& Image_Transform::factory('GD');
+        $img->load($filename);
+        // If image is larger than the maximum size, we resize it.
+        if ($img->img_x > $size or $img->img_y > $size ) {
+            if (!@is_dir($path) and !@mkdir($path)) {
+                return null;
+            }
+            if (PEAR::isError($img)) {
+                return null;
+            }
+            if (!$img->scale($size)) {
+                return null;
+            }
+        }
+        $img->save("$path/$name.$format", $format);
+        $img->free();
+
+        return $thumb;
+    }
+    // -X2C
+
+    // +X2C Operation 98
+    /**
+     * Returns the filename of an image thumb.
+     *
+     * @param  string $filename Filename of the image to get the thumb name.
+     *
+     * @return string
+     * @access protected
+     */
+    function getThumbFilename($filename) // ~X2C
+    {
+        $root = $this->attrs['DIR'];
+        $format = $this->attrs['THUMBSFORMAT'];
+        $thumbsdir = $this->attrs['THUMBSDIR'];
+
+        list($path, $name, $ext) = $this->splitFilename($filename);
+
+        return "$root/$thumbsdir/$name.$format";
+    }
+    // -X2C
+
+    // +X2C Operation 102
+    /**
+     * Returns the description of the album.
+     *
+     * @return string
+     * @access protected
+     */
+    function getDescription() // ~X2C
+    {
+        $root = $this->attrs['DIR'];
+        return @join('', file($file));
+    }
+    // -X2C
+
+    // +X2C Operation 100
+    /**
+     * Splits a filename returning an array with the path, name and extension.
+     *
+     * @param  string $filename Filename to split.
+     *
+     * @return array
+     * @access public
+     * @static
+     */
+    function splitFilename($filename) // ~X2C
+    {
+        $path = explode('/', $filename);
+        $file = array_pop($path);
+        $ext  = '';
+        if (strstr($file, '.')) {
+            preg_match('|([^/]+?)(\.([^\.]*))?$|', $file, $m);
+            $file = @$m[1] . ((@$m[2] == '.' ) ? '.' : '');
+            $ext  = @$m[3];
+        }
+        $dir = count($path) ? join('/', $path) : '';
+        return array($dir, $file, $ext);
     }
     // -X2C
 
     }
     // -X2C