<?php
/*
* buzzword
* Copyright (c) 2003 Jon Tai
*
* $Id: category.inc 362 2004-04-28 21:10:39Z 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 links categories
*
* categories are ordered by created DESC
*/
function get_links_categories() {
$categories = array();
$sql = 'SELECT category_key ';
$sql .= 'FROM '.DB_PREFIX.'links_categories ';
$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)) {
$category = new links_category($row['category_key']);
if ( ($category->exists()) && ($category->is_accessible) && ($category->is_visible) )
$categories[] = $category;
}
return $categories;
}
class links_category {
function links_category($category_key) {
$this->category_key = (int) $category_key;
$row = get_record('links_category', $this->category_key);
if (!$row) {
$sql = 'SELECT UNIX_TIMESTAMP(created) AS created, UNIX_TIMESTAMP(modified) AS modified, ';
$sql .= 'is_private, is_hidden, title, description, allow_comments ';
$sql .= 'FROM '.DB_PREFIX.'links_categories ';
$sql .= "WHERE category_key = {$this->category_key} ";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
if ($row)
cache_record('links_category', $this->category_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'] : 'category';
$this->description = ($row) ? $row['description'] : '';
$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() {
$category_key = $this->category_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');
$sql = 'INSERT INTO '.DB_PREFIX.'links_categories ';
$sql .= '(category_key, created, modified, is_private, is_hidden, title, description, allow_comments) VALUES ';
$sql .= "($category_key, $created, $modified, $is_private, $is_hidden, $title, $description, $allow_comments)";
mysql_query($sql);
$this->exists = TRUE;
flush_record('links_category', $this->category_key);
}
function update() {
$category_key = $this->category_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');
$sql = 'UPDATE '.DB_PREFIX.'links_categories 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 .= "WHERE category_key = $category_key";
mysql_query($sql);
flush_record('links_category', $this->category_key);
}
function destroy() {
$sql = 'DELETE FROM '.DB_PREFIX.'links_categories ';
$sql .= "WHERE category_key = {$this->category_key} ";
mysql_query($sql);
$this->exists = FALSE;
$links = get_links_links($this->category_key);
foreach ($links as $link)
$link->destroy();
flush_record('links_category', $this->category_key);
}
// whether or not the category exists
function exists() {
return ($this->is_accessible) ? $this->exists : FALSE;
}
function get_display_restrictions() {
if ($this->is_private)
return 'category is private';
if ($this->is_hidden)
return 'category 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) ?
longtext_hook(nl2p(str_trunc($this->description, get_pref('buzzword_short_desc_length'), FALSE))):
longtext_hook(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_num_children() {
$sql = 'SELECT COUNT(link_key) AS count FROM '.DB_PREFIX.'links_links ';
$sql .= 'WHERE parent = '.$this->category_key;
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
return $row['count'];
}
}
?>