Location: PHPKode > projects > KORA > kora-2.0.0/advancedSearch.php
<?php 
/**
Copyright (2008) Matrix: Michigan State University

This file is part of KORA.

KORA 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.

KORA 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, see <http://www.gnu.org/licenses/>.
 */

/**
 * searchProject
 * 
 * GET/POST arguments:
 *   schemeid: scheme id                (REQUIRED)
 * 
 * if scheme not specified, will be redirected to projectIndex.php   
 */

// Initial Version: Meghan McNeil, 2009

require_once('includes/utilities.php');
include_once('includes/ingestionClass.php');
requireLogin();

// a project an scheme ID must be passed; if it is not, 
// something is wrong so fall back to the project index page
//if (!isset($_REQUEST['schemeid'])) header('Location: projectIndex.php');
include_once("includes/controlDataFunctions.php");
include_once("includes/koraSearch.php");
include_once('includes/header.php');

function parseSearchString($queryString)
{
	$parts = array();
	
	while($queryString != "")
	{
		$pos = strpos($queryString, "\"");
		
		if(!is_integer($pos)) // just parse the whole thing
		{
			$moreparts = explode(" ", ereg_replace("%", " ", trim($queryString)));
			$parts = array_merge($parts, $moreparts);
			$queryString = "";
		}
		else //sprit the string up into the quoted part, and everything else
		{
			// parse out the left string
			$left = substr($queryString, 0, $pos);
			$moreparts = explode(" ", ereg_replace("%", " ", trim($left)));
			$parts = array_merge($parts, $moreparts);
			
			$right = substr($queryString, $pos+1);
			
			// try to find the right ", and if not, use the whole right string 
			$pos2 = strpos($right, "\"");
			if(!is_integer($pos2)) // use the whole thing
			{
				$moreparts = array(ereg_replace("%", " ", trim($right)));
				$parts = array_merge($parts, $moreparts);
				$queryString = "";
			}
			else
			{
				$left2 = substr($right, 0, $pos2);
				$moreparts = array(ereg_replace("%", " ", trim($left2)));
				$parts = array_merge($parts, $moreparts);
				
				$queryString = substr($right, $pos2+1);
			}
			
		}
	}
	
	return $parts;
}

?>

<h2>Advanced Search</h2>

<?php
//control name that do NOT have advanced search support
$unsupportedAdvSearch = array('File','Image','Record Associator','Geolocator');

//get controls that allow advanced search
$cTable = 'p'.$_SESSION['currentProject'].'Control'; 
$query = "SELECT $cTable.type AS class, $cTable.cid AS cid, $cTable.name AS name, control.file AS file, 
		  control.name AS type FROM $cTable LEFT JOIN control ON ($cTable.type = control.class) WHERE 
		  $cTable.advSearchable=1 AND $cTable.schemeid=".$_SESSION['currentScheme'].' AND control.name 
		  NOT IN ("'.implode('","',$unsupportedAdvSearch).'") ORDER BY collid,sequence';
$query = $db->query($query);
$controls = array();
while ($results = $query->fetch_assoc()) {
	$controls[] = $results;
} 

//initalize variables
$koraClauses = array();	
$boolean = 'AND';
$getQuery = array();

//if keyword search
if(isset($_GET['submit_keywords']) && !empty($_GET['submit_keywords'])) {
	$keywords = parseSearchString($_GET['keywords']);

	// apply each keyword to each advance searchable control
	foreach($controls as $c) {
		$controlClause = array();
		foreach($keywords as $word) {
			$controlClause[] = "value LIKE '%$word%'";		
		}
		// add keyword search to kora clauses 
		$koraClauses[] = "cid=".$c['cid']." AND ".implode(' '.$_GET['andOr'].' ',$controlClause);
	} 
	
	$boolean = 'OR';
} 

// if advanced search
else if(isset($_GET['submit_advanced']) && !empty($_GET['submit_advanced'])) {

	foreach($controls as $c) {
		include_once(basePath.CONTROL_DIR.$c['file']);
		$controlClass = $c['class'];
		$control = new $controlClass($_SESSION['currentProject'],$c['cid']);
		// get correct op and formatted value to search 
		$searchValue = $control->getSearchString($_GET);
		if($searchValue && is_array($searchValue)) {
			$values = array(); 
			foreach($searchValue as $sv) {
				if (is_array($sv[1])) {
					$sv[1] = "('".implode("','",$sv[1])."')";
				}
				$values[] = "value ".$sv[0]." ".$sv[1];
			}
			
			// add search value to kora clauses
			if(!empty($values)) {
				$koraClauses[] = "cid = ".$c['cid']." AND ".implode(' AND ',$values);
			}
		}
	}
}

//if there is something to search
if(!empty($koraClauses)) {
	if(isset($_GET['page'])) {
		$page = $_GET['page'];
	}
	//first time search
	else {
		$page = 1;
	}
	
	// get query string from the $_GET variable without page  
	$query = array();
	foreach($_GET as $key=>$value) {
		if ($key != 'page' && !empty($_GET[$key])){
			// if $value is an array, make the array 
			// query string friendly
			if(is_array($value)) {
				$val = array(); 
				foreach($value as $v) {
					$val[] = $key."[]=$v";
				}
				$query[] = implode('&',$val);
			} else {
				$query[] = "$key=$value";	
			}
		}
	}
	
	//create Search link
	$searchLink = 'href="advancedSearch.php?'.implode('&',$query).'&page=%d"'; 
	
	//search and print results
	print internalSearchResults($_SESSION['currentProject'],$_SESSION['currentScheme'],
								$koraClauses,$boolean,$page,RESULTS_IN_PAGE,$searchLink);
}
else { ?>
	<form id="keywordSearch" name="keywordSearch" action="" method="get">
	<table class="table"><tr><td>
	Keywords: <input type="text" name="keywords" id="keywords" /> <input type="submit" value="Search" name="submit_keywords" />
	</td></tr>
	<tr><td>
	Include objects that match 
	<select name="andOr">
		<option value="AND">all</option>
		<option value="OR">any</option>
	</select>
	 keywords.
	</td></tr></table>
	</form>
	
	<?php
	
	echo '<form id="advancedSearch" name="advancedSearch" action="" method="get">
		  <table class="table">';
	
	foreach($controls as $c) {
		// This check should not be needed but used as an extra
		// security check to ensure that only supported fields
		// can be used in advanced search
		if (!in_array($c['type'],$unsupportedAdvSearch)) {
			include_once(basePath.CONTROL_DIR.$c['file']);
			$controlClass = $c['class'];
			$control = new $controlClass($_SESSION['currentProject'],$c['cid']);
			
			echo '<tr><td>'.$c['name'].': </td><td>';
			$control->display();
			echo '</td></tr>'; 
		}
	}
	
	echo '<tr><td colspan="2"><input type="submit" value="Search" name="submit_advanced" /></td></tr>
		  </table></form>';
}
?>
Return current item: KORA