<?php
/**
 * Gallery Manager
 *
 * This class is meant to be used to read gallery items from the database.
 *
 * @author     Thomas Meyer
 * @license    http://www.gnu.org/licenses/gpl.txt
 *
 * @package    yana
 * @subpackage plugins
 */

/**
 * Gallery
 *
 * @package     yana
 * @subpackage  plugins
 */
class GalleryManager extends Utility
{

    /**
     * Connection to data source (API)
     *
     * @access  private
     * @static
     * @var     DBStream  Database-API with Query-Builder (also works with text-files)
     */
    private static $database = null;

    /**
     * Returns the database connection
     *
     * @access  protected
     * @static
     * @return  DBStream
     * @ignore
     */
    protected static function getDatabase()
    {
        if (!isset(self::$database)) {
            self::$database = Yana::connect("mediadb");
        }
        return self::$database;
    }

    /**
     * get gallery
     *
     * @access  public
     * @static
     * @param   int  $id
     * @return  StdClass
     */
    public static function getGallery($id)
    {
        assert('is_int($id); // Wrong argument type argument 1. Integer expected');
        $id = (int) $id;
        if (!isset(self::$galleries[$id])) {
            $db = self::getDatabase();
            $where = array(
                array('user_created', '=', YanaUser::getUserName()),
                'or',
                array('media_public', '=', true)
            );
            $data = $db->select("mediafolder.$id", $where);
            if (empty($data)) {
                throw new NotFoundException("No such gallery: $id");
            }
            $gallery = new StdClass();
            $gallery->id = $id;
            $gallery->name = $data['MEDIAFOLDER_NAME'];
            $gallery->description = $data['MEDIAFOLDER_DESCRIPTION'];
            $gallery->location = $data['MEDIAFOLDER_LOCATION'];
            self::$galleries[$id] = $gallery;
        }
        return self::$galleries[$id];
    }
}
?>