<?php
/**
* Random Quotes Block
*
* Block displays on index page with a random quote. All
* quotes are stored in table mod_quotes and pulled randomly.
*
* Original by Joe Stump (miester.org),
* adapted for PHP-Nuke by Erik Sooff (hide@address.com)
* adapted for phpWebSite by Seyed Razavi
*
* @author Edward Ritter, hide@address.com
* @verson $Id: quotes.inc.php,v 1.6 2002/09/03 19:32:17 esritter Exp $
* @module quotes
* @package phpWebSite
*/
class qotd {
/**
* add - adds new quote
*
* @author Edward Ritter
* @param string qquote: the actual quote.
* @param string qauthor: the quote's author.
*/
function add($qquote, $qauthor) {
global $table_prefix;
$qquote = (!get_magic_quotes_gpc())?addslashes ($qquote):$qquote;
$qauthor = (!get_magic_quotes_gpc())?addslashes ($qauthor):$qauthor;
mysql_query ("INSERT INTO " . $table_prefix . "mod_quotes (quote, author) VALUES ('$qquote', '$qauthor')") or
die ("Couldn't add quote to table: [$qquote, $qauthor]" . mysql_error());
$boxtitle = "Quote Added";
$boxcontent = "<a href=\"./mod.php?mod=quotes&op=quotes_admin\">Return to Quotes Admin</a>";
thememainbox($boxtitle, $boxcontent);
}
/**
* modify - displays form for editing / deleting quote.
*
* @author Edward Ritter
* @param int qid: quote id.
*/
function modify($qid) {
global $table_prefix;
$get_quote = mysql_query ("SELECT quote, author FROM " . $table_prefix . "mod_quotes WHERE qid='$qid'");
list ($quote, $author) = mysql_fetch_row ($get_quote);
$boxtitle = "Edit Quote";
$boxcontent = $this->form('update',$qid, $quote, $author);
thememainbox ($boxtitle, $boxcontent);
}
/**
* delete - deletes quote
*
* @author Edward Ritter
* @param int qid: quote id.
*/
function delete($qid) {
global $confirm, $table_prefix;
if ($confirm) {
mysql_query ("DELETE FROM " . $table_prefix . "mod_quotes WHERE qid='$qid'") or
die ("Couldn't delete quote from table: " . mysql_error());
$boxtitle = "Quote Deleted";
$boxcontent = "<a href=\"./mod.php?mod=quotes&op=quotes_admin\">Return to Quotes Admin</a>";
} else {
$result = mysql_query ("SELECT quote, author FROM " . $table_prefix . "mod_quotes WHERE qid='$qid'");
list ($quote, $author) = mysql_fetch_row ($result);
// if quote is >25 characters, truncate and add ...
$string = $quote;
if (strlen ($string) > 25) {
$string = substr ($quote,0,24);
if ($string != " ") {
$last_space = strrpos ($string, " ");
$string = substr ($quote,0,$last_space);
$string .= "...";
}
}
$boxtitle = "Are you sure you wish to delete quote \"$string\" ($author)?";
$boxcontent = "<a href=\"./mod.php?mod=quotes&op=quotes_admin&adminop=delete&confirm=true&qid=$qid\">YES</a>
<a href=\"mod.php?mod=quotes&op=quotes_admin\">NO</a>";
}
thememainbox($boxtitle, $boxcontent);
}
/**
* update - updates quote in table.
*
* @author Edward Ritter
* @param int qid: quote id.
* @param string qquote: quote text
* @param string qauthor: quote author
*/
function update($qid, $qquote, $qauthor) {
global $table_prefix;
$qquote = (!get_magic_quotes_gpc())?addslashes ($qquote):$qquote;
$qauthor = (!get_magic_quotes_gpc())?addslashes ($qauthor):$qauthor;
$result = mysql_query ("UPDATE " . $table_prefix . "mod_quotes SET quote ='$qquote', author='$qauthor' WHERE qid=$qid") or
die ("Couldn't update quote in table: " . mysql_error());
$boxtitle = "Quote Updated";
$boxcontent = "<a href=\"mod.php?mod=quotes&op=quotes_admin\">Return to Quotes Admin</a>.";
thememainbox($boxtitle, $boxcontent);
}
/**
* show_block - displays quote block on index page
*
* @author Edward Ritter
* @return content: the quote and author.
*/
function show_block() {
global $table_prefix;
$result = mysql_query ("SELECT * FROM " . $table_prefix . "mod_quotes");
$num_rows = mysql_num_rows ($result);
// only proceed if we have quotes in the database
if ($result > 0 && $num_rows > 0) {
// Create the random id for the quote
srand((double)microtime()*1000000);
$random = 1;
if ($num_rows > 1) {
$random = rand(1,$num_rows);
}
/* Now that we have the random number we can pull out the record
also make sure we hit an existing quot if not we do a query again */
$havequote = 0;
while ($havequote != $random) {
$myrow = mysql_fetch_array ($result);
// make sure we stop this loop when we do have a quote
if (is_array($myrow)) {
$havequote = $havequote + 1;
}
}
// this is the start of the content for the qotd block
$content = '<div align="center"><i>';
// add the quote text and remove whitespaces at begin or end
$content .= trim($myrow[1]) . '</i><br/>';
// add the author and then close everything
$content .= '-- '.stripslashes($myrow[2]).'</div>';
} else {
// this is what we say when the quotes database is empty
$content = 'Sorry, there are no quotes in the database!';
}
// now we build the box somewhere on the side
return $content;
}
/**
* admin - admin functions for quotes block
*
* allows admin to add, modify, and delete quotes.
*
* @author Edward Ritter
* @param none
*/
function admin() {
// Build Add Form
global $table_prefix;
$boxtitle = "Add Quote";
$boxcontent .= $this->form('add');
thememainbox($boxtitle,$boxcontent);
// Build Update/Delete Form
$result = mysql_query ("SELECT * FROM " . $table_prefix . "mod_quotes");
if (mysql_num_rows ($result) > 0) {
$boxtitle = "Edit Quote";
$boxcontent = "<form action=\"./mod.php\" method=\"post\">
<select name=\"qid\">";
while (list ($qid, $quote, $author) = mysql_fetch_row ($result)) {
// if quote is >25 characters, truncate and add ...
$string = $quote;
if (strlen ($string) > 25) {
$string = substr($quote,0,24);
if ($string != " ") {
$last_space = strrpos($string, " ");
$string = substr($quote,0,$last_space);
$string .= "...";
}
}
$boxcontent .= "<option value=\"$qid\">$string ($author)</option>";
}
$boxcontent .= "</select><input type=\"hidden\" name=\"mod\" value=\"quotes\" />
<input type=\"hidden\" name=\"op\" value=\"quotes_admin\" />
<input type=\"hidden\" name=\"adminop\" value=\"modify\" />
<input type=\"submit\" value=\"Edit Quote\" /></form>";
thememainbox($boxtitle,$boxcontent);
}
}
/**
* options - options functions for quotes block
*
* allows block placement, title, view changes
*
* @author Edward Ritter
* @param none
*/
function options() {
}
/**
* form - displays form for adding or modifying a quote
*
* @author Edward Ritter
* @param string op: add or modify
* @param int qid: quote id (for modify)
* @param qquote: quote text (modify)
* @param qauthor: quote author (modify)
*/
function form($op, $qid='', $qquote='', $qauthor='') {
$content = "<form action=\"mod.php\" method=\"post\">
<input type=\"hidden\" name=\"mod\" value=\"quotes\" />";
if ($qid) {
$content .= "<input type=\"hidden\" name=\"qid\" value=\"$qid\" />";
}
$content .= "<input type=\"hidden\" name=\"op\" value=\"quotes_admin\" />
<input type=\"hidden\" name=\"adminop\" value=\"$op\" />
Quote Text: <br/>
<textarea class=\"textbox\" name=\"qquote\" cols=\"60\" rows=\"5\">$qquote</textarea>
<br />Quote Author: <input class=\"textbox\" type=\"text\" name=\"qauthor\" size=\"31\" maxlength=\"128\" value=\"$qauthor\" />";
if ($op == "update") {
$content .= " <a href=\"./mod.php?mod=quotes&op=quotes_admin&adminop=delete&qid=$qid\">delete quote</a> | ";
}
$content .= " <input type=\"submit\" value=\"$op\" /></form>";
return $content;
}
}
?>