Location: PHPKode > projects > Buzzword > buzzword-0.8.12/gallery/gallery.inc
<?php

/*
 * buzzword
 * Copyright (c) 2003 Jon Tai
 *
 * $Id: gallery.inc,v 1.16 2003/12/17 23:39:24 bradt Exp $
 *
 * This file is part of buzzword.
 *
 * buzzword is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * buzzword is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with buzzword; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * returns all gallery galleries with parent $parent
 *
 * galleries are ordered by created DESC
 */
function get_gallery_galleries($parent) {
    $galleries = array();

    $parent = (int) $parent;

    $sql = 'SELECT gallery_key ';
    $sql .= 'FROM '.DB_PREFIX.'gallery_galleries ';
    $sql .= "WHERE parent = $parent ";
    if (!defined('ADMIN_LOGGED_IN')) {
        $sql .= "AND is_private = 'N' ";
        $sql .= "AND is_hidden = 'N' ";
    }
    $sql .= 'ORDER BY created DESC';

    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)) {
        $gallery = new gallery_gallery($row['gallery_key']);
        if ( ($gallery->exists()) && ($gallery->is_accessible) && ($gallery->is_visible) )
            $galleries[] = $gallery;
    }

    return $galleries;
}

class gallery_gallery {
    function gallery_gallery($gallery_key) {
        $this->gallery_key = (int) $gallery_key;

        $row = get_record('gallery_gallery', $this->gallery_key);
        if (!$row) {
            $sql = 'SELECT UNIX_TIMESTAMP(created) AS created, UNIX_TIMESTAMP(modified) AS modified, ';
            $sql .= 'is_private, is_hidden, title, description, allow_comments, path, parent, highlight ';
            $sql .= 'FROM '.DB_PREFIX.'gallery_galleries ';
            $sql .= "WHERE gallery_key = {$this->gallery_key} ";
            $result = mysql_query($sql);
            $row = mysql_fetch_assoc($result);

            mysql_free_result($result);

            if ($row)
                cache_record('gallery_gallery', $this->gallery_key, $row);
        }

        $this->exists = (is_array($row) || ($this->gallery_key == 0) );
        $this->is_private = ($row) ? ($row['is_private'] == 'Y') : FALSE;
        $this->is_hidden = ($row) ? ($row['is_hidden'] == 'Y') : FALSE;
        $this->created = ($row) ? $row['created'] : 0;
        $this->modified = ($row) ? $row['modified'] : 0;
        $this->title = ($row) ? $row['title'] : 'galleries';
        $this->description = ($row) ? $row['description'] : '';
        $this->allow_comments = ($row) ? ($row['allow_comments'] == 'Y') : TRUE;
        $this->path = ($row) ? $row['path'] : get_pref('gallery_image_path');
        $this->parent = ($row) ? $row['parent'] : 0;
        $this->highlight = ($row) ? $row['highlight'] : 0;

        $this->is_accessible = (defined('ADMIN_LOGGED_IN')) ? TRUE : !$this->is_private;
        $this->is_visible = (defined('ADMIN_LOGGED_IN')) ? TRUE : !$this->is_hidden;

        // inherit parents' permissions
        if ($this->gallery_key > 0) {
            $this->parent = new gallery_gallery($this->parent);
            $this->path = $this->parent->path.'/'.$this->path;
            if (!$this->parent->is_accessible)
                $this->is_accessible = FALSE;
        }
    }

    function create() {
        $gallery_key = $this->gallery_key;
        $created = mysql_quote_string(date('Y-m-d H:i:s', $this->created));
        $modified = mysql_quote_string(date('Y-m-d H:i:s', $this->modified));
        $is_private = mysql_quote_string(($this->is_private) ? 'Y' : 'N');
        $is_hidden = mysql_quote_string(($this->is_hidden) ? 'Y' : 'N');
        $title = mysql_quote_string($this->title);
        $description = mysql_quote_string($this->description);
        $allow_comments = mysql_quote_string(($this->allow_comments) ? 'Y' : 'N');
        $path = mysql_quote_string(basename($this->path));
        $parent = $this->parent->gallery_key;
        $highlight = $this->highlight;

        $sql = 'INSERT INTO '.DB_PREFIX.'gallery_galleries ';
        $sql .= '(gallery_key, created, modified, is_private, is_hidden, title, description, allow_comments, path, parent, highlight) VALUES ';
        $sql .= "($gallery_key, $created, $modified, $is_private, $is_hidden, $title, $description, $allow_comments, $path, $parent, $highlight)";
        mysql_query($sql);

        $this->exists = TRUE;

        cache_relevance_keywords('gallery_gallery', $this->gallery_key, get_relevance_keywords(strtoupper($this->title).'.'.$this->title.'.'.$this->description));
        flush_record('gallery_gallery', $this->gallery_key);
    }

    function update() {
        $gallery_key = $this->gallery_key;
        $created = mysql_quote_string(date('Y-m-d H:i:s', $this->created));
        $modified = mysql_quote_string(date('Y-m-d H:i:s', $this->modified));
        $is_private = mysql_quote_string(($this->is_private) ? 'Y' : 'N');
        $is_hidden = mysql_quote_string(($this->is_hidden) ? 'Y' : 'N');
        $title = mysql_quote_string($this->title);
        $description = mysql_quote_string($this->description);
        $allow_comments = mysql_quote_string(($this->allow_comments) ? 'Y' : 'N');
        $path = mysql_quote_string(basename($this->path));
        $parent = $this->parent->gallery_key;
        $highlight = $this->highlight;

        $sql = 'UPDATE '.DB_PREFIX.'gallery_galleries SET ';
        $sql .= "created = $created, ";
        $sql .= "modified = $modified, ";
        $sql .= "is_private = $is_private, ";
        $sql .= "is_hidden = $is_hidden, ";
        $sql .= "title = $title, ";
        $sql .= "description = $description, ";
        $sql .= "allow_comments = $allow_comments, ";
        $sql .= "path = $path, ";
        $sql .= "parent = $parent, ";
        $sql .= "highlight = $highlight ";
        $sql .= "WHERE gallery_key = $gallery_key";
        mysql_query($sql);

        cache_relevance_keywords('gallery_gallery', $this->gallery_key, get_relevance_keywords(strtoupper($this->title).'.'.$this->title.'.'.$this->description));
        flush_record('gallery_gallery', $this->gallery_key);
    }

    function destroy() {
        $sql = 'DELETE FROM '.DB_PREFIX.'gallery_galleries ';
        $sql .= "WHERE gallery_key = {$this->gallery_key} ";
        mysql_query($sql);

        $this->exists = FALSE;

        $galleries = get_gallery_galleries($this->gallery_key);
        foreach ($galleries as $gallery)
            $gallery->destroy();

        $images = get_gallery_images($this->gallery_key);
        foreach ($images as $image)
            $image->destroy();

        flush_relevance_keywords('gallery_gallery', $this->gallery_key);
        flush_record('gallery_gallery', $this->gallery_key);
    }

    // whether or not the gallery exists
    function exists() {
        return ($this->is_accessible) ? ($this->exists && is_dir($this->path)) : FALSE;
    }

    function get_display_restrictions() {
        if ($this->is_private)
            return 'gallery is private';
        if ($this->is_hidden)
            return 'gallery is hidden';
        return 'no restrictions';
    }

    function get_display_title() {
        return $this->title;
    }

    function get_display_short_title($length = FALSE) {
        return ($length === FALSE) ?
            str_trunc($this->title, get_pref('buzzword_short_title_length'), TRUE):
            str_trunc($this->title, $length, TRUE);
    }

    function get_display_description() {
        return longtext_hook(nl2p($this->description));
    }

    function get_display_short_description($length = FALSE) {
        return ($length === FALSE) ?
            nl2p(str_trunc($this->description, get_pref('buzzword_short_desc_length'), FALSE)):
            nl2p(str_trunc($this->description, $length, FALSE));
    }

    function get_display_date_created() {
        return date(get_pref('buzzword_date_format'), $this->created);
    }

    function get_display_time_created() {
        return date(get_pref('buzzword_time_format'), $this->created);
    }
    
    function get_display_date_modified() {
        return date(get_pref('buzzword_date_format'), $this->modified);
    }

    function get_display_time_modified() {
        return date(get_pref('buzzword_time_format'), $this->modified);
    }

    function get_display_relevance() {
        $highlight = new gallery_image($this->highlight);

        $relevance = "<p style=\"clear: both;\">\n";
        $relevance .= "<a href=\"../gallery/index.php?g={$this->gallery_key}\"><img \n";
        $relevance .= " src=\"../gallery/thumb.php?i={$highlight->image_key}\" ".$highlight->get_alt()." \n";
        $relevance .= " border=\"0\" ".$highlight->get_thumb_half_size()." class=\"gallery-thumb-halfsize\"></a>\n";
        $relevance .= "<a href=\"../gallery/index.php?g={$this->gallery_key}\">".$this->get_display_short_title(get_pref('buzzword_short_title_length') - round(get_pref('gallery_thumb_width') / 16))."</a><br>\n";
        $relevance .= number_format($this->get_num_children())." images<br>\n";
        $relevance .= "</p>\n";
        return $relevance;
    }

    function get_display_terse_relevance() {
        return "<a href=\"../gallery/index.php?g={$this->gallery_key}\">".$this->get_display_short_title()."</a><br>\n";
    }

    // sync up db to filesystem
    function rescan() {
        if (!$this->exists())
            return FALSE;

        // add db entries for images/galleries that don't already have one
        $dir = dir($this->path);
        while ($obj = $dir->read()) {
            if ($obj[0] != '.') {
                $path = mysql_quote_string($obj);

                if (is_dir($this->path.'/'.$obj)) {
                    $sql = 'SELECT gallery_key ';
                    $sql .= 'FROM '.DB_PREFIX.'gallery_galleries ';
                    $sql .= "WHERE parent = {$this->gallery_key} ";
                    $sql .= "AND path = $path ";
                    $result = mysql_query($sql);
                    $row = mysql_fetch_assoc($result);

                    if (!$row) {
                        // gallery has no db entry, add one
                        $gallery = new gallery_gallery(mysql_unique_key(DB_PREFIX.'gallery_galleries', 'gallery_key'));
                        $gallery->created = filemtime($this->path.'/'.$obj.'/.');
                        $gallery->modified = $gallery->created;
                        $gallery->is_private = FALSE;
                        $gallery->is_hidden = FALSE;
                        $gallery->title = $obj;
                        $gallery->description = '';
                        $gallery->allow_comments = TRUE;
                        $gallery->path = $obj;
                        $gallery->parent = $this;
                        $gallery->highlight = 0;
                        $gallery->create();
                    }
                } else if ( (is_file($this->path.'/'.$obj)) && (preg_match('/\.(gif|jpe?g|png|bmp|swf|tiff?)$/i', $obj)) ) {
                    $sql = 'SELECT image_key ';
                    $sql .= 'FROM '.DB_PREFIX.'gallery_images ';
                    $sql .= "WHERE parent = {$this->gallery_key} ";
                    $sql .= "AND path = $path ";
                    $result = mysql_query($sql);
                    $row = mysql_fetch_assoc($result);

                    if (!$row) {
                        // image has no db entry, add one
                        $image = new gallery_image(mysql_unique_key(DB_PREFIX.'gallery_images', 'image_key'));
                        $image->is_private = FALSE;
                        $image->is_hidden = FALSE;
                        $image->title = $obj;
                        $image->description = '';
                        $image->path = $obj;
                        $image->parent = $this;
                        $image->orientation = 0;
                        $image->create();
                    }
 
                }

            }
        }
        $dir->close();

        // remove db entries for files that have been deleted
        if (defined('ADMIN_LOGGED_IN')) {
            $sql = 'SELECT gallery_key ';
            $sql .= 'FROM '.DB_PREFIX.'gallery_galleries ';
            $result = mysql_query($sql);
            while ($row = mysql_fetch_assoc($result)) {
                $gallery = new gallery_gallery($row['gallery_key']);
                if (!$gallery->exists()) 
                    $gallery->destroy();
            }

            $sql = 'SELECT image_key ';
            $sql .= 'FROM '.DB_PREFIX.'gallery_images ';
            $result = mysql_query($sql);
            while ($row = mysql_fetch_assoc($result)) {
                $image = new gallery_image($row['image_key']);
                if (!$image->exists()) 
                    $image->destroy();
            }
        }
    }

    function get_num_children() {
        $sql = 'SELECT COUNT(image_key) AS count FROM '.DB_PREFIX.'gallery_images ';
        $sql .= 'WHERE parent = '.$this->gallery_key;
        $result = mysql_query($sql);
        $row = mysql_fetch_assoc($result);

        if ($row['count'])
            return number_format($row['count']).' images';

        $sql = 'SELECT COUNT(gallery_key) AS count FROM '.DB_PREFIX.'gallery_galleries ';
        $sql .= 'WHERE parent = '.$this->gallery_key;
        $result = mysql_query($sql);
        $row = mysql_fetch_assoc($result);

        return number_format($row['count']).' sub-galleries';
    }
}

?>
Return current item: Buzzword