<?php
/**
* Multi Engine Search Block
*
* Search box with updatable engines. Admin can
* turn on specific engines and make one the default.
* Also has built in ability to search the site. All
* engines are stored in the database for easy updating.
*
* @author Edward Ritter <hide@address.com>
* @version $Id: search.inc.php,v 1.7 2002/09/06 06:09:26 esritter Exp $
* @module search
* @package phpWebSite
*/
class search_box {
/**
* show_block() - displays search block on index page
*
* @author Edward Ritter
* @return content: the search form.
*/
function show_block() {
global $table_prefix;
$content .= "<form name=\"searchbox\" method=\"post\">
<input type=\"text\" size=\"12\" maxlength=\"255\" name=\"query\" />
<input type=\"image\" src=\"./mod/search/go.gif\"
onclick=\"open_search_result(document.searchbox.query.value,
document.searchbox.engine.options[document.searchbox.engine.selectedIndex].value);\"/>
<br /><select name=\"engine\">\n";
$result = mysql_query ("SELECT id, name, fav FROM " .$table_prefix.
"mod_search_data WHERE flag = '1' ORDER BY name");
if (mysql_num_rows ($result) > 0 ) {
while (list ($id, $name, $fav) = mysql_fetch_row ($result)) {
$content .= "<option value=\"$id\"" .
(($fav)?" selected=\"selected\"":''). ">$name</option>\n";
}
$content .= "</select></form>";
}
return $content;
}
/**
* show_result - fetches query string from db and displays results
*
* @author Edward Ritter
* @param string query: query to pass on engine's query string.
* @param int engine: engine to use to do the search.
*/
function show_result($query, $engine) {
global $table_prefix;
$result = mysql_query ("SELECT query FROM " . $table_prefix .
"mod_search_data WHERE id = '$engine'");
list ($query_string) = mysql_fetch_row ($result);
header ("Location: " . $query_string . urlencode($query));
}
/**
* admin - admin functions for search block
*
* allows admin to add, modify, delete engines.
*
* @author Edward Ritter
* @param none
*/
function admin() {
global $table_prefix;
//Build Add form
$boxtitle = "Add Search Engine";
$boxcontent = $this->form('add');
themesidebox($boxtitle, $boxcontent);
//Build Update/Delete Form
$boxtitle = "Modify Search Engines";
$result = mysql_query ("SELECT * FROM " . $table_prefix .
"mod_search_data ORDER BY name");
if (mysql_num_rows ($result) > 0 ) {
while (list ($id, $name, $qstring, $flag, $fav) =
mysql_fetch_row ($result)) {
$boxstuff .= $this->form('modify', $id, $name, $qstring, $flag, $fav);
}
}
themesidebox($boxtitle, $boxstuff);
}
/**
* add - adds new search engine
*
* @author Edward Ritter
* @param string name: name of search engine
* @param string qstring: query string to send to engine. Usually a URL.
* @param bool flag: whether engine is on or off of the drop down menu in the form.
* @param bool fav: the default engine that shows up in the drop down menu. Unique
*/
function add($name, $qstring, $flag, $fav) {
global $table_prefix;
if ($fav == 1) {
mysql_query ("UPDATE " . $table_prefix . "mod_search_data
SET fav='0' WHERE fav='1'");
}
mysql_query ("INSERT INTO " . $table_prefix . "mod_search_data VALUES
(NULL, '$name', '$qstring', '$flag', '$fav')") or
die ("Couldn't add engine to table: " . mysql_error());
header ("Location:./mod.php?mod=search&op=search_admin");
}
/**
* modify - changes info for an engine
*
* @author Edward Ritter
* @param int id: id # of search engine
* @param string name: name of search engine
* @param string qstring: query string to send to engine. Usually a URL.
* @param bool flag: whether engine is on or off of the drop down menu in the form.
* @param bool fav: the default engine that shows up in the drop down menu. Unique
*/
function modify($id, $name, $qstring, $flag, $fav) {
global $table_prefix;
if ($fav == 1) {
mysql_query ("UPDATE " . $table_prefix . "
mod_search_data SET fav='0' WHERE fav='1'");
}
mysql_query ("UPDATE " . $table_prefix . "mod_search_data
SET name='$name',query='$qstring',flag='$flag',fav='$fav'
WHERE id='$id'") or
die ("Unable to update engine: " .mysql_error());
header ("Location:./mod.php?mod=search&op=search_admin");
}
/**
* delete - removes engine from table
*
* @author Edward Ritter
* @param int id: id # of search engine
*/
function delete($id) {
global $confirm, $table_prefix;
if ($confirm) {
mysql_query ("DELETE FROM " . $table_prefix . "mod_search_data
WHERE id='$id'") or
die ("Couldn't delete engine from table: " . mysql_error());
header ("Location:./mod.php?mod=search&op=search_admin");
} else {
$result = mysql_query ("SELECT name FROM " . $table_prefix .
"mod_search_data WHERE id='$id'");
list ($name) = mysql_fetch_row ($result);
$boxtitle = "Are you sure you wish to delete $name ?";
$boxcontent = "<a href = \"mod.php?mod=search&op=search_admin&adminop=delete&confirm=true&id=$id\">YES</a>
<a href= \"mod.php?mod=search&op=search_admin\">NO</a>";
themesidebox($boxtitle,$boxcontent);
}
}
/**
* form - displays form for adding or modifying an engine
*
* @author Edward Ritter
* @param string op: add or modify
* @param int id: id # of search engine
* @param string name: name of search engine
* @param string qstring: query string to send to engine. Usually a URL.
* @param bool flag: whether engine is on or off of the drop down menu in the form.
* @param bool fav: the default engine that shows up in the drop down menu. Unique
*/
function form($op, $id='', $name='', $qstring='', $flag='', $fav='') {
$checked = " checked=\"checked\"";
$content = "<form action=\"mod.php\" method=\"post\">\n
<input type=\"hidden\" name=\"mod\" value=\"search\" />\n
<input type=\"hidden\" name=\"op\" value=\"search_admin\" />\n
<input type=\"hidden\" name=\"adminop\" value=\"$op\" />\n";
if ($id) {
$content .= "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
}
$content .= "Name: <input type=\"text\" name=\"name\" value=\"$name\" size=\"10\" />
Querystring: <input type=\"text\" name=\"qstring\" value=\"$qstring\" />
Active: <input type=\"checkbox\" name=\"flag\" value=\"1\"" .
(($flag)?$checked:'')." />\n
Favorite: <input type=\"radio\" name=\"fav\" value=\"1\"" .
(($fav)?$checked:'')." /><input type=\"submit\" value=\"$op\" />\n";
if ($op == "modify") {
$content .= " | <a href=\"mod.php?mod=search&op=search_admin&adminop=delete&id=$id\">delete</a> ";
}
$content .= "</form>";
return $content;
}
}
?>