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

/*
 * buzzword
 * Copyright (c) 2003 Jon Tai
 *
 * $Id: entry.inc 345 2004-04-21 07:38:53Z jon $
 *
 * 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 blog entries
 *
 * entries are ordered by created DESC
 */
function get_blog_entries() {
    $entries = array();

    $sql = 'SELECT entry_key ';
    $sql .= 'FROM '.DB_PREFIX.'blog_entries ';
    $sql .= 'WHERE 1 ';
    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)) {
        $entry = new blog_entry($row['entry_key']);
        if ( ($entry->exists()) && ($entry->is_accessible) && ($entry->is_visible) )
            $entries[] = $entry;
    }

    return $entries;
}

/**
 * returns up to $limit blog entries between $start and $end
 *
 * entries are ordered by created DESC
 */
function get_blog_entries_by_date($start, $end, $limit = 0) {
    $entries = array();

    $sql = 'SELECT entry_key ';
    $sql .= 'FROM '.DB_PREFIX.'blog_entries ';
    $sql .= 'WHERE 1 ';
    if (!defined('ADMIN_LOGGED_IN')) {
        $sql .= "AND is_private = 'N' ";
        $sql .= "AND is_hidden = 'N' ";
    }

    $start = mysql_quote_string(date('Y-m-d H:i:s', $start));
    $end = mysql_quote_string(date('Y-m-d H:i:s', $end));
    $limit = ($limit > 0) ? 'LIMIT '.((int) $limit) : '';

    $sql .= "AND created >= $start ";
    $sql .= "AND created < $end ";
    $sql .= 'ORDER BY created DESC ';
    $sql .= $limit;

    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)) {
        $entry = new blog_entry($row['entry_key']);
        if ( ($entry->exists()) && ($entry->is_accessible) && ($entry->is_visible) )
            $entries[] = $entry;
    }

    return $entries;
}

class blog_entry {
    function blog_entry($entry_key) {
        $this->entry_key = (int) $entry_key;

        $row = get_record('blog_entry', $this->entry_key);
        if (!$row) {
            $sql = 'SELECT UNIX_TIMESTAMP(created) AS created, UNIX_TIMESTAMP(modified) AS modified, ';
            $sql .= 'is_private, is_hidden, title, body, allow_comments ';
            $sql .= 'FROM '.DB_PREFIX.'blog_entries ';
            $sql .= "WHERE entry_key = {$this->entry_key} ";
            $result = mysql_query($sql);
            $row = mysql_fetch_assoc($result);

            mysql_free_result($result);

            if ($row)
                cache_record('blog_entry', $this->entry_key, $row);
        }

        $this->exists = is_array($row);
        $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'] : 'entry';
        $this->body = ($row) ? $row['body'] : '';
        $this->allow_comments = ($row) ? ($row['allow_comments'] == 'Y') : TRUE;

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

    function create() {
        $entry_key = $this->entry_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);
        $body = mysql_quote_string($this->body);
        $allow_comments = mysql_quote_string(($this->allow_comments) ? 'Y' : 'N');

        $sql = 'INSERT INTO '.DB_PREFIX.'blog_entries ';
        $sql .= '(entry_key, created, modified, is_private, is_hidden, title, body, allow_comments) VALUES ';
        $sql .= "($entry_key, $created, $modified, $is_private, $is_hidden, $title, $body, $allow_comments)";
        mysql_query($sql);

        $this->exists = TRUE;

        cache_relevance_keywords('blog_entry', $this->entry_key, get_relevance_keywords(strtoupper($this->title).'.'.$this->title.'.'.$this->body));
        flush_record('blog_entry', $this->entry_key);
    }

    function update() {
        $entry_key = $this->entry_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);
        $body = mysql_quote_string($this->body);
        $allow_comments = mysql_quote_string(($this->allow_comments) ? 'Y' : 'N');

        $sql = 'UPDATE '.DB_PREFIX.'blog_entries SET ';
        $sql .= "created = $created, ";
        $sql .= "modified = $modified, ";
        $sql .= "is_private = $is_private, ";
        $sql .= "is_hidden = $is_hidden, ";
        $sql .= "title = $title, ";
        $sql .= "body = $body, ";
        $sql .= "allow_comments = $allow_comments ";
        $sql .= "WHERE entry_key = $entry_key";
        mysql_query($sql);

        cache_relevance_keywords('blog_entry', $this->entry_key, get_relevance_keywords(strtoupper($this->title).'.'.$this->title.'.'.$this->body));
        flush_record('blog_entry', $this->entry_key);
    }

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

        $this->exists = FALSE;

        $comments = get_blog_entry_comments($this);
        foreach ($comments as $comment)
            $comment->destroy();

        flush_relevance_keywords('blog_entry', $this->entry_key);
        flush_record('blog_entry', $this->entry_key);
    }

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

    function get_display_restrictions() {
        if ($this->is_private)
            return 'entry is private';
        if ($this->is_hidden)
            return 'entry 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_body() {
        return longtext_hook(nl2p($this->body));
    }

    function get_display_short_body($length = FALSE) {
        return ($length === FALSE) ?
            nl2p(str_trunc($this->body, get_pref('buzzword_short_desc_length'), FALSE)):
            nl2p(str_trunc($this->body, $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() {
        $relevance = "<p>\n";
        $relevance .= $this->get_display_terse_relevance();
        $relevance .= $this->get_display_date_created()."<br>\n";
        $relevance .= "</p>\n";
        return $relevance;
    }

    function get_display_terse_relevance() {
        return "<a href=\"../blog/entry.php?e={$this->entry_key}\">".$this->get_display_short_title()."</a><br>\n";
    }
}

?>
Return current item: Buzzword