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

/*
 * buzzword
 * Copyright (c) 2003 Jon Tai
 *
 * $Id: entry.inc 20 2004-04-27 23:53:26Z brad $
 *
 * 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
 */

function get_planet_entries($parent) {
    $entries = array();

    $sql = 'SELECT entry_key ';
    $sql .= 'FROM '.DB_PREFIX.'planet_entries ';
    $sql .= "WHERE parent = $parent ";
    $sql .= 'ORDER BY created DESC';

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

    return $entries;
}

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

    $sql = 'SELECT entry_key ';
    $sql .= 'FROM '.DB_PREFIX.'planet_entries ';
    $sql .= 'WHERE 1 ';

    $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 planet_entry($row['entry_key']);
        if ($entry->exists())
            $entries[] = $entry;
    }

    return $entries;
}

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

        $row = get_record('planet_entry', $this->entry_key);
        if (!$row) {
            $sql = 'SELECT UNIX_TIMESTAMP(created) AS created, ';
            $sql .= 'title, body, trackback_url, parent ';
            $sql .= 'FROM '.DB_PREFIX.'planet_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('planet_entry', $this->entry_key, $row);
        }

        $this->exists = is_array($row);
        $this->created = ($row) ? $row['created'] : 0;
        $this->title = ($row) ? $row['title'] : 'entry';
        $this->body = ($row) ? $row['body'] : '';
        $this->trackback_url = ($row) ? $row['trackback_url'] : '';
        $this->parent = ($row) ? $row['parent'] : 0;

        $this->is_accessible = TRUE;	// needed for relevance
        $this->is_visible = TRUE;	// needed for relevance

        $this->parent = new planet_subscription($this->parent);
        if (!$this->parent->is_accessible)
            $this->is_accessible = FALSE;
    }

    function create() {
        $entry_key = $this->entry_key;
        $created = mysql_quote_string(date('Y-m-d H:i:s', $this->created));
        $title = mysql_quote_string($this->title);
        $body = mysql_quote_string($this->body);
        $trackback_url = mysql_quote_string($this->trackback_url);
        $parent = $this->parent->subscription_key;

        $sql = 'INSERT INTO '.DB_PREFIX.'planet_entries ';
        $sql .= '(entry_key, created, title, body, trackback_url, parent) VALUES ';
        $sql .= "($entry_key, $created, $title, $body, $trackback_url, $parent)";
        mysql_query($sql);

        $this->exists = TRUE;

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

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

        $this->exists = FALSE;

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

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

    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($this->body);
    }

    function get_display_short_body($length = FALSE) {
        return ($length === FALSE) ?
            str_trunc($this->body, get_pref('buzzword_short_desc_length'), FALSE):
            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_trackback_url() {
        return '<a href="'.$this->trackback_url.'" target="planet_'.$this->entry_key.'">'.$this->trackback_url.'</a>';
    }

    function get_display_short_trackback_url() {
        $url = parse_url($this->trackback_url);
        return $url['host'];
    }

    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() {
        $relevance = "<a href=\"{$this->trackback_url}\">";
        $relevance .= $this->get_display_short_title();
        $relevance .= "</a><br>\n";
        return $relevance;
    }
}

?>
Return current item: Buzzword