<?
require_once("include.php");

//This function builds the string for the display fields option
//It uses the morefields array to associate the html value with 
//the refdbc identified value
function setSearchFields($_POST, $morefields)
{
	$searchFields = "";
	while( list($key,$val) = each($morefields))
	{
		if(isset($_POST[$key]) )
		{
			$searchFields .= $val;
		}
	}
	if( $searchFields == "" )
	{
		$searchFields = "ALL";
	}
	return $searchFields;
}
?>
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>RefDB query results <?php print "($database)"; ?></title>
    <link href="refdbqxhtml.css" type="text/css" rel="stylesheet"/>
    </head>
    <body>
        <h1>RefDB query results</h1>
        <div class="navigation">
            <h2>Navigation</h2>
            <ul class="navigationMenu">
                <li>
                    <a href="index.html">Login screen</a>
                </li>
                <li>
                    <a href="refdbquery.html">Search references</a>
                </li>
                <li>
                    <a href="refdbkajquery.html">Search keywords, authors, journals</a>
                </li>
                <li>
                    <a href="refdbadd.html">Add new references</a>
                </li>
                <li>
                    <a href="refdbdbquery.php">Change database</a>
                </li>
                <li>
                    <a href="external.php">Search external database</a>
                </li>
                <li>
                    <a href="refdbadmin.php">Administrative Interface</a>
                </li>
                <li>
                    <a href="refdblogout.php">Logout</a>
                </li>
            </ul>
        </div>
        <div class="content">
<h2>Matching References</h2>
 <form action="refdb-prl-del.php" method="post" enctype="multipart/form-data"> 
	<p><input type="submit" value="Delete selected references" name="deleteref"/>&nbsp;
	<input type="submit" value="Add selected references to Personal List" name="pickref"/>&nbsp;
	<input type="submit" value="Remove selected references from Personal List" name="unpickref"/></p>
<?
$database = $_SESSION["database"];
$name = $_SESSION["name"];
$passwd = $_SESSION["passwd"];
$refdbc = $_SESSION["refdbc"];

$cmd = "$refdbc -c stdout -d $database -u $name -w $passwd -F ";

$searchFields = setSearchFields($_POST, $morefields);

$cmd = $cmd.$searchFields." -C getref ";

//Search just the personal reference list
if($scope == "prl")
{
	$cmd = $cmd."-p ";
}

$terms = "";

//The following if sets the query for the Simple Query
//It builds the refdbc query using the fields and ops  arrays
//from include.php 
if( $sendQuery == "Send Simple query")
{
	$first = true;
	if( $simpquery1 != ""  )
	{
		$field1 = $fields[$simpquery1field];
		$equals1 = $equals[$simpquery1equals];
		$terms = $terms.$field1.$equals1.$simpquery1." ";
		$first = false;
	}
	if( $simpquery2 != ""  )
	{
		$field2 = $fields[$simpquery2field];
		$equals2 = $equals[$simpquery2equals];
		if( $first)
		{
			$terms = $terms.$field2.$equals2.$simpquery2." ";
			$first = false;
		}
		else
		{
			$op1 = $ops[$simpquery1op];
			$terms = $terms.$op1." ".$field2.$equals2.$simpquery2." ";
		}
	}
	if( $simpquery3 != ""  )
	{
		$field3 = $fields[$simpquery3field];
		$equals3 = $equals[$simpquery3equals];
		if( $first)
		{
			$terms = $terms.$field3.$equals3.$simpquery3." ";
			$first = false;
		}
		else
		{
			$op2 = $ops[$simpquery2op];
			$terms = $terms.$op2." ".$field3.$equals3.$simpquery3." ";
		}
	}
	if( $simpquery4 != "" )
	{
		$field4 = $fields[$simpquery4field];
		$equals4 = $equals[$simpquery4equals];
		if( $first)
		{
			$terms = $terms.$field4.$equals4.$simpquery4." ";
			$first = false;
		}
		else
		{
			$op3 = $ops[$simpquery3op];
			$terms = $terms.$op3." ".$field4.$equals4.$simpquery4." ";
		}
	}
}
//Build the advance query
else if( $sendQuery == "Send Advanced query" )
{
	$terms = $advquery;
}
//Query terms if you've returned from a search
else if( $sendQuery == "Return to Search Results" )
{
	$terms = $return;
}

$cmd = $cmd.$terms;

exec($cmd, $lines);

?>
<?
$checkboxNumber = 0;

//If the search generates no results
if( count($lines) <=1 )
{
	print "<p>Your search for <strong>$terms</strong> generated no results.</p>\n";
	print"<p>Back to <a href='refdbquery.html'>Search screen</a>.</p>"; 
}
//Your search generated results display them
else
{
  $lineNumber = 1;
  foreach( $lines as $line )
  {
  	//If this is the first line of a new reference it will contain the ID field
	if( substr($line,0,2) == "ID" )
	{
		$lineNumber = 1;
				
		//strip the ID number out of the reference
		$colon = strpos($line, ':');
		$space = strpos($line, " ");
		$id = substr($line,$colon+1,$space-$colon-1);
		
		++$checkboxNumber;
                print "<hr/>\n";
                print "<p><input type='submit' value='Delete' name='editid$id'/></p>";
		print "<p><input type='checkbox' name='checkbox$checkboxNumber' value='$id'/>&nbsp;$line</p>\n";
		$lineNumber++;
	}
	elseif( substr($line,0,3) == "Key" )
	{
		print "<p class='citekey'>$line</p>\n";
		$lineNumber++;
	}
	elseif($lineNumber == 3)
	{
		print "<p class='authors'>Authors: $line</p>\n";
		$lineNumber++;
	}
	elseif($lineNumber == 4)
	{
		print "<p class='title'>Title: $line</p>\n";
		$lineNumber++;
	}
	elseif($lineNumber == 5)
	{
		print "<p><em class='periodical'>Journal: $line</em></p>\n";
		$lineNumber++;
	}
	elseif( substr($line,0,8) == "ABSTRACT")
	{
		print "<p class='abstract'>Abstract: $line</p>\n";
	}
	elseif( substr($line,0,7) == "REPRINT" )
	{
		print "<p class='reprint'>Reprint: $line</p>\n";
	}
	elseif( substr($line,0,4) == "ISSN")
	{
		print "<p class='issn'>ISSN: $line</p>\n";
	}
	elseif( substr($line,0,4) == "CITY")
	{
		print "<p class='city'>$line</p>\n";
	}
	elseif( substr($line,0,5) == "NOTES" )
	{
		print "<p class='notes'>$line</p>\n";
	}
	elseif( substr($line,0,3) == "URL" )
	{
		$len = strlen($line);
		$url = substr($line, 4,$len);
		print "<p class='url'><a href='$url'>$line</a></p>\n";
	}
	elseif( substr($line,0,9) == "PUBLISHER")
	{
		print "<p class='publisher'>$line</p>\n";
	}
	else
	{
		print "<p>$line</p>\n";
	}
  }
}
print "<p class='hidden'><input type='hidden' name='terms' value='$terms'/>\n";
print "<input type='hidden' name='scope' value='$scope'/>\n";
print "<input type='hidden' name='numberOfCheckBoxes' value='$checkboxNumber'/></p>\n";
?>
</form>
</div>
  	     <div class="footer">
            <p><a href="http://refdb.sourceforge.net">RefDB</a> reference manager and bibliography
                tool V. <!--VERSION-->0.9.8-1<!--/VERSION--></p>
        </div>

</body>

</html>




