<?php
/*
* buzzword
* Copyright (c) 2003 Jon Tai
*
* $Id: link.inc 347 2004-04-21 07:49:30Z 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 links with parent $parent
*
* links are ordered by created DESC
*/
function get_links_links($parent) {
$links = array();
$parent = (int) $parent;
$sql = 'SELECT link_key ';
$sql .= 'FROM '.DB_PREFIX.'links_links ';
$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)) {
$link = new links_link($row['link_key']);
if ( ($link->exists()) && ($link->is_accessible) && ($link->is_visible) )
$links[] = $link;
}
return $links;
}
class links_link {
function links_link($link_key) {
$this->link_key = (int) $link_key;
$row = get_record('links_link', $this->link_key);
if (!$row) {
$sql = 'SELECT UNIX_TIMESTAMP(created) AS created, UNIX_TIMESTAMP(modified) AS modified, ';
$sql .= 'is_private, is_hidden, title, url, description, parent ';
$sql .= 'FROM '.DB_PREFIX.'links_links ';
$sql .= "WHERE link_key = {$this->link_key} ";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
if ($row)
cache_record('links_link', $this->link_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'] : 'link';
$this->url = ($row) ? $row['url'] : '';
$this->description = ($row) ? $row['description'] : '';
$this->parent = ($row) ? $row['parent'] : 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
$this->parent = new links_category($this->parent);
if (!$this->parent->is_accessible)
$this->is_accessible = FALSE;
}
function create() {
$link_key = $this->link_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);
$url = mysql_quote_string($this->format_url());
$description = mysql_quote_string($this->description);
$parent = $this->parent->category_key;
$sql = 'INSERT INTO '.DB_PREFIX.'links_links ';
$sql .= '(link_key, created, modified, is_private, is_hidden, title, url, description, parent) VALUES ';
$sql .= "($link_key, $created, $modified, $is_private, $is_hidden, $title, $url, $description, $parent)";
mysql_query($sql);
$this->exists = TRUE;
cache_relevance_keywords('links_link', $this->link_key, get_relevance_keywords(strtoupper($this->title).'.'.$this->title.'.'.$this->description));
flush_record('links_link', $this->link_key);
}
function update() {
$link_key = $this->link_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);
$url = mysql_quote_string($this->format_url());
$description = mysql_quote_string($this->description);
$parent = $this->parent->category_key;
$sql = 'UPDATE '.DB_PREFIX.'links_links SET ';
$sql .= "created = $created, ";
$sql .= "modified = $modified, ";
$sql .= "is_private = $is_private, ";
$sql .= "is_hidden = $is_hidden, ";
$sql .= "title = $title, ";
$sql .= "url = $url, ";
$sql .= "description = $description, ";
$sql .= "parent = $parent ";
$sql .= "WHERE link_key = $link_key";
mysql_query($sql);
cache_relevance_keywords('links_link', $this->link_key, get_relevance_keywords(strtoupper($this->title).'.'.$this->title.'.'.$this->description));
flush_record('links_link', $this->link_key);
}
function destroy() {
$sql = 'DELETE FROM '.DB_PREFIX.'links_links ';
$sql .= "WHERE link_key = {$this->link_key} ";
mysql_query($sql);
$this->exists = FALSE;
$comments = get_links_link_comments($this);
foreach ($comments as $comment)
$comment->destroy();
flush_relevance_keywords('links_link', $this->link_key);
flush_record('links_link', $this->link_key);
}
// whether or not the link exists
function exists() {
return ($this->is_accessible) ? $this->exists : FALSE;
}
function get_display_restrictions() {
if ($this->is_private)
return 'link is private';
if ($this->is_hidden)
return 'link 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_url() {
return '<a href="'.$this->url.'" target="link_'.$this->link_key.'">'.$this->url.'</a>';
}
function get_display_short_url() {
$url = parse_url($this->url);
return $url['host'];
}
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() {
$relevance = "<p>\n";
$relevance .= $this->get_display_terse_relevance();
$relevance .= $this->get_display_short_url()."<br>\n";
$relevance .= "</p>\n";
return $relevance;
}
function get_display_terse_relevance() {
return "<a href=\"../links/link.php?l={$this->link_key}\">".$this->get_display_short_title()."</a><br>\n";
}
function format_url() {
$url = $this->url;
// if no protocol is specified, use HTTP
if (!preg_match('/^[a-z]+:/i', $url))
$url = 'http://'.$url;
// if url is a domain without a trailing /, add one
if (preg_match('/^[a-z]+:\/\/[^\/]+$/i', $url))
$url .= '/';
return $url;
}
}
?>