Location: PHPKode > projects > Emilda > emilda-1.2.3/src/adv_search.php
<?php

//-----------------------------------------------------------------------------
//
// Copyright (C) 2003-2005 Oy Realnode Ab
//
//-----------------------------------------------------------------------------
//
// adv_search.php
//     Part of the Emilda Project (http://www.emilda.org/)
//
// Description
//     Advanced Search.
//
// Authors
//     Christoffer Landtman <landtman (at) realnode com>
//     Erik Berglund <berglund (at) realnode com>
//     Mattias Nordstrom <nordstrom (at) realnode com>
//
//-----------------------------------------------------------------------------
//
// This program 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.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//-----------------------------------------------------------------------------
//
// $Id: adv_search.php,v 1.64.2.4 2005/06/06 15:21:14 eberglun Exp $
//
//-----------------------------------------------------------------------------

// Id used to identify this page within functions.
$PageID = "ADV_SEARCH";

require_once "common.inc";

require_once "config.inc";
require_once "constants.inc";
require_once "db.inc";
require_once "error.inc";
require_once "layout.inc";
require_once "search.inc";
require_once "language.inc";

$onload = "hideOptions();";
layout_header(array('onload' => $onload));

layout_page_title();

?>

<form name='search' action='adv_search.php' method='get'>
	<input type='hidden' name='id' value='<?php echo $_REQUEST['id']?>'>
	<input type='hidden' name='auth' value='<?php echo $_REQUEST['auth']?>'>
	<table cellpadding="6" cellspacing="0" border="0">

    	<?php

			for ($i=1; $i<=$config["adv_search_rows"]; $i++) {
			
				// Start row
				print "<tr>\n";
				
				// Join function
				print "<td align='right'>\n";
				if($i == 1) {
					print _("Search in");
				} else {
					$select_or = (isset($_REQUEST["join_operator_$i"]) && $_REQUEST["join_operator_$i"] == "||") ? " selected='selected'" : "";
					$select_andnot = (isset($_REQUEST["join_operator_$i"]) && $_REQUEST["join_operator_$i"] == "##") ? " selected='selected'" : "";
					print "	<select name='join_operator_$i'>\n
								<option value='&amp;&amp;'>"._("And")."</option>\n
								<option value='||'$select_or>"._("Or")."</option>\n
								<option value='##'$select_andnot>"._("And Not")."</option>\n
							</select>\n";
					print "&nbsp;"._("in");
				}
				print "</td>\n";
				
				// Searchable fields
				print "<td>\n";
				print "<select name='field_$i'>\n";
				foreach ($MARC_searchable as $searchable)
				{
					$select = (isset($_REQUEST["field_$i"]) && $_REQUEST["field_$i"] == $searchable) ? " selected='selected'" : "";
					print "<option value='$searchable'$select>"._($MARC_config_desc[$searchable])."</option>\n";
				}
				print "</select>\n";
				print "</td>\n";
				
				// Input field
				print "<td>"._("for")."</td>\n";
				print "<td>\n";
				$value = (isset($_REQUEST["query_$i"])) ? htmlspecialchars(stripcslashes($_REQUEST["query_$i"])) : "";
				print "<input type='text' name='query_$i' value='$value' size='30'>\n";
				print "</td>\n";
				
				// Term operator
				print "<td>"._("requiring")."</td>\n";
				print "<td>\n";
				$select = (isset($_REQUEST["term_operator_$i"]) && $_REQUEST["term_operator_$i"] == "ol") ? " selected='selected'" : "";
				print "	<select name='term_operator_$i'>\n
							<option value='al'>"._("All")."</option>\n
							<option value='ol'$select>"._("At Least One")."</option>\n
						</select>\n";
				print "</td>\n";
				print "<td>"._("of the Terms")."</td>\n";
				
				
				// End row
				print "</tr>\n";
			}

		?>

		<tr>
	    	<td>&nbsp;</td>
			<td colspan="6" align="left"><input type='submit' name='search' value='<?php echo _("Search") ?> >>' class='button'></td>
		</tr>
		<tr>
	    	<td colspan=7>&nbsp;</td>
		</tr>

		<tr>
			<td align="right"><?php echo _("Search From") ?>:</td>
            <td align="left" colspan="6">

                <select name='url' onChange="hideOptions()">

				<?php

				foreach ($REMOTE_DATABASE as $name => $url)
				{

					print "<option value='".$url."'";
					if(!isset($_REQUEST['url']) && $url == $config['zebra_host'].":".$config['zebra_port']."/".$config['zebra_db'])
					{
						print " selected";
					}
					elseif(isset($_REQUEST['url']) && $url == $_REQUEST['url'])
					{
						print " selected";
					}
					print ">" . _($name) . "</option>"; //fix gettext?
				}

				?>

				</select>

			</td>
		</tr>
		<tr id="location">
			<td align="right">
				<?php echo _("From Location") ?>:
			</td>
			<td colspan="6" align="left">
				<select name='location'>

				<?php

				$res = sql_cquery("SELECT location_id, location_name FROM locations ORDER BY location_name");
				while ($row = mysql_fetch_array($res))
				{

					print "<option value='".$row['location_id']."'";
					if(!isset($_REQUEST['location']) && $row['location_id'] == USER_LOCATION)
					{
						print " selected";
					}
					elseif(isset($_REQUEST['location']) && $row['location_id'] == $_REQUEST['location'])
					{
						print " selected";
					}
					print ">".$row['location_name']."</option>";
				}

				?>

					<option value="all" <?php echo (isset($_REQUEST['location']) && $_REQUEST['location'] == "all") ? "selected" : "";?>><?php echo _("All") ?></option>
				</select>
			</td>
		</tr>
		<tr id="limits">
            <td>&nbsp;</td>
			<td colspan="6" valign="left">
				<input type="checkbox" name="in_shelf"<?php if(isset($_REQUEST['in_shelf'])) echo " checked"; ?>> <?php echo _("In Shelf") ?><br />
				<input type="checkbox" name="not_reserved"<?php if(isset($_REQUEST['not_reserved'])) echo " checked"; ?>> <?php echo _("Available") ?><br />
			</td>
		</tr>

	</table>
</form>
<br />

<?php

if(isset($_REQUEST['search']))
{
	// Vars
	$conf_directives = array();
	
	// Remember position
	$position = (isset($_REQUEST['position'])) ? $_REQUEST['position'] : 1;
	
	// Generate CCL based on input
 	$ccl = "";
	for ($i=1; array_key_exists("query_$i", $_REQUEST) && $_REQUEST["query_$i"] != ""; $i++) {
		// Check if the target has custom CCL rules
		if (array_key_exists($_REQUEST["url"], $YAZ_attributes) && array_key_exists($_REQUEST["field_$i"], $YAZ_attributes[$_REQUEST["url"]])) {
			$base_rule = $YAZ_attributes[$_REQUEST["url"]][$_REQUEST["field_$i"]];
		} else {
			$base_rule = $YAZ_attributes[$_REQUEST["field_$i"]];
		}
		
		// Geneate a custom CCL conf based on the term_operator requested. The keys of the
		// conf are of the form <field_name>_<nr>, e.g. any_1. Additionally replace
		// s=al with s=ol in the conf if appropriate
		if($_REQUEST["term_operator_$i"] == "ol") {
			// Replace s=al with s=ol if exists, otherwise append
			if(stristr("s=al", $base_rule)) {
 				$conf_directives[$_REQUEST["field_$i"]."_$i"] = preg_replace("/s=al/", "s=ol", $base_rule);
			} else {
				$conf_directives[$_REQUEST["field_$i"]."_$i"] = $base_rule." s=ol";
			}
		} else {
			$conf_directives[$_REQUEST["field_$i"]."_$i"] = $base_rule;
		}
		
		// Check if join_operator exists, and assign if so
		$join_operator = (isset($_REQUEST["join_operator_$i"])) ? $_REQUEST["join_operator_$i"]." " : "";
		
		// Append CCL to already generated ones
		$ccl .= $join_operator.$_REQUEST["field_$i"]."_$i = ( ".stripcslashes($_REQUEST["query_$i"])." ) ";
	}
	
	// Init and perform search
	$search = new Search;
	$search->init($_REQUEST['url']);
	$rpn = $search->ccl2rpn($ccl, $conf_directives);
	$search->perform($rpn, "", $position);

	$criteria_count = 0;

	if ($search->hits())
	{
		$headers = array();
		foreach ($MARC_display_in_list as $value) {
			array_push($headers, _($MARC_config_desc[$value]));
		}

		$my_table =& new Table($headers);

		$narrowed_count = 0;;
		$display_narrowed_count = false;
		$i = 0;

		while ($record = $search->next(10))
		{
			$content = array();
			$row = array();
			$go = true;
			// Increment position
			$i++;

			if($_REQUEST['url'] == $config['zebra_host'].":".$config['zebra_port']."/".$config['zebra_db'])
			{
				$display_narrowed_count = true;

				if($_REQUEST['location'] == 'all') {
					$location_sql = '';
				} else {
					$location_sql = " AND book_location='".$_REQUEST['location']."'";
				}

				if (isset($_REQUEST['in_shelf']) && isset($_REQUEST['not_reserved']))
				{
					$sql = 	"SELECT book_id
							FROM books
							WHERE book_control_number=".book_control_number($record)."
							AND book_borrowed=0 AND book_reserved=0".$location_sql;
				}
				elseif(isset($_REQUEST['in_shelf']))
				{
					$sql = 	"SELECT book_id
							FROM books
							WHERE book_control_number=".book_control_number($record)."
							AND book_borrowed=0".$location_sql;
				}
				elseif (isset($_REQUEST['not_reserved']))
				{
					$sql = 	"SELECT book_id
							FROM books
							WHERE book_control_number=".book_control_number($record)."
							AND book_reserved=0".$location_sql;
				}
				else
				{
					$sql = 	"SELECT book_id
							FROM books
							WHERE book_control_number=".book_control_number($record)."
							".$location_sql;
				}

				$res = sql_query($sql);
				if(mysql_num_rows($res)) {
					$row = createMARCRow($record, return_with_format_syntax($MARC_display_in_list));
					$narrowed_count++;
				} else {
					$go = false;
				}						
			} else {
				$row = createMARCRow($record, return_with_format_syntax($MARC_display_in_list));
				$display_narrowed_count = false;
			}

			if($go)
			{
				if(array_key_exists('book_isbn', $row) && $row['book_isbn'] != "") {
					$row['book_isbn'] = format_isbn($row['book_isbn']);
				}

				foreach($row as $value) {
					array_push($content, $value);
				}

				$my_row =& $my_table->add_row($content);

				$my_row->add_icon('icon_info.png', "book_info.php?id=" . $_REQUEST['id'] . "&auth=" . $_REQUEST['auth'] . "&url=".$_REQUEST['url']."&book_control_number=" . book_control_number($record));

				if($_REQUEST['url'] == $config['zebra_host'].":".$config['zebra_port']."/".$config['zebra_db'])
				{
					if(check_priv('EDIT')) {
						$my_row->add_icon('icon_edit.png', "book_edit.php?id=" . $_REQUEST['id'] . "&auth=" . $_REQUEST['auth'] . "&book_control_number=" . book_control_number($record));
					}
				}
				else
				{
					list($message, $hidden) = book_suggest_message(USER_GIVEN_NAMES." ".USER_SURNAME, $record, $_REQUEST['url']);

					$my_row->add_icon('icon_suggest_book.png', "javascript:document.suggest".book_control_number($record).".submit();");
					$my_row->add_cells(	"<form name='suggest".book_control_number($record)."' action='communication.php?id=".$_REQUEST['id']."&auth=".$_REQUEST['auth']."' method='post'>
											<input type='hidden' name='message_id' value=''>
											<input type='hidden' name='subject' value='" . _("Suggest") . "'>
											<input type='hidden' name='message' value='".$message."'>
											<input type='hidden' name='hidden' value='".$hidden."'>
										</form>");
				}
			}
		}

		$page_index = '';
		if($display_narrowed_count)
		{
			$filtered = guess_filtered_hits($search->hits(), 10, $i, $my_table->row_count());
			$page_index = "<span class='page-index'>" . sprintf(ngettext("%d filtered hit", "%d filtered hits", $filtered), '<b>' . $filtered . '</b>');
			//$page_index .= ($narrowed_count == 1) ? _("") : $langText['FILTERED_HITS'];
		}
		
		$index_url = 'adv_search.php?';
		foreach ($_REQUEST as $key => $value) {
			if ($key != 'position') {
				$index_url .= "$key=" . urlencode($value) . '&';
			}
		}
		$index = create_page_index($search->hits(), $position, 10, $index_url);

		if($index && $display_narrowed_count) {
			$page_index .= " | </span>".$index;
		} elseif($index && !$display_narrowed_count) {
			$page_index = $index;
		} else {
			$page_index .= "</span>";
		}

		$my_table->set_footer($page_index);

		// Show results
		if($my_table->row_count()) {
			layout_page_info(sprintf(ngettext("%d book found", "%d books found", $search->hits()), $search->hits()), 'ok');
		} else {
			layout_page_info(_("No hits for this location"), 'error');
		}

		if ($search->hits() == 0 && $display_narrowed_count)
		{
			$text = sprintf(ngettext("%d filtered hit", "%d filtered hits", $narrowed_count), $narrowed_count);
			layout_page_info($text);
		}
		elseif ($search->hits() > 0) {
			$my_table->render();
		}
	}

	else
	{
        if(isset($error))
			layout_page_info(_("Boolean Error"), 'error');
		else
			layout_page_info(_("No Hits"), 'error');
	}
	// Terminate search
	$search->terminate();
}

layout_footer();

?>
Return current item: Emilda