<?php
/*

This program is a 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 3.0 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 can receive a copy of GNU General Public License at the World Wide Web 
address <http://www.gnu.org/licenses/gpl.html>.

--------------------------------------------------------
 Product Name        QueryCatcher
 Product URL        http://www.jasonclark.info/files.php
 Product Description    A PHP/MySQL search term recorder module - collect queries logged into system, presents terms as browseable links
 
 Latest Version        1.0
 Release Date         2008/06/16

 Author's Name        Jason Clark 
 Author's Email        jaclark@montana.edu
 Author's URL        http://jasonclark.info
 License            GNU's Public License (GPL)
--------------------------------------------------------

The code from this application is also available in the Code4Lib Journal (http://journal.code4lib.org).
The article that explains the "QueryCatcher" application is titled: "Making Patron Data Work Harder: User Search Terms as Access Points?"
It can be retrieved at http://journal.code4lib.org/articles/78.

*/


//purpose: This code searches the database for the user specified search terms and logs the search terms to a database table. Then the results are displayed in groups of 15 to the user.
//note: you will need to create your own database, user login, and password to have data to query - see the querycatcher.sql file in package for an example
//note: all unique functions are stored within this file at the end of PHP script

//pass db name, login data and connection directions
mysql_pconnect("YOUR-DB-HOST","YOUR-DB-USER","YOUR-DB-PASSWORD") or die ("Error: Unable to connect to the database.");    
mysql_select_db("YOUR-DB-NAME");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Search Results View: QueryCatcher</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen, projection">
<!--
@import url("styles.css");
?>
-->
</style>
</head>
<body>
<?php
   
//set POST or GET values to $params variable
   
$params = (count($_POST)) ? $_POST $_GET;
   
$keyword $params['keyword'];

   if (
validation($keyword)) {
      
//creates a string of the search elements for future page reference
      
$fKeyword urlencode($keyword);
      
$searchString 'keyword='.$fKeyword.'';

      
//the number of search items
      
$numitems 0;

      
//boolean values to see which fields have entries
      
$bKeyword false;

      if(
$keyword != "")
      {
        
$numitems++;
        
$bKeyword true;
      }
//end if()
    
    //set up switch statement to display messages and run queries based on form fields used
    
switch($numitems)
      {  
    default:
            {
               echo 
'<h2><strong>There is missing information in your request!</strong></h2>'."\n";
               echo 
'<hr />'."\n";
               echo 
'<h3>Please enter a value(s) to search for.<br />';
               echo 
'<br />Use the browser <strong>BACK</strong> button and enter the missing information.</h3>'."\n";
             }
    break;
        
    case 
1:
             if(
$bKeyword)
             { 
                
$result mysql_query("SELECT * FROM item WHERE item_status='a' AND MATCH (dc_title, dc_creator, dc_description, dc_contributor) AGAINST ('+$keyword*' IN BOOLEAN MODE)");
                
$num_rows mysql_num_rows($result);
                if (
$num_rows == 0) {
                   
noMatches();
                   exit;
                }
                else
                {
                
//log search term query, trim whitespace and store in "log" table of database                 
                
$logQuery = @mysql_query("INSERT INTO log SET query_data='".trim($keyword)."', query_results='".$num_rows."', query_referrer='".$_SERVER['HTTP_REFERER']."'");
                    if (!
$logQuery) {
                        die(
'<h2>Error adding query to log: ' mysql_error() . '</h2>');
                    }
                    echo 
'<h3 class="results">Your search for Keyword <strong>'.stripslashes($keyword).'</strong> resulted in <strong>'.$num_rows.'</strong> match(es).</h3>'."\n";
                }
             }
    break;
    }
//end switch
    
?>
    <p class="nav"><a class="bck" href="./form.php">Back to search page</a></p>
    <?php
      
echo '<hr />'."\n";
      
//this section of code in the for statement displays all the information about the matching item
      
while ($row mysql_fetch_object($result)) {
        
$id $row->item_id;
        
$title stripslashes(html_entity_decode($row->dc_title));
        
$creator stripslashes(html_entity_decode($row->dc_creator));
        
$description stripslashes(html_entity_decode(substr($row->dc_description,0,400)));
        
$dateIssued $row->dc_date;
        
$identifier $row->dc_identifier;
        
$discipline stripslashes(html_entity_decode($row->dc_degreeDiscipline));
        
//code below displays the info to the user
        
echo '<dl>'."\n";
        echo 
'<dt><strong><a href="./item.php?id='.$id.'">'.$title.'</a></strong></dt>'."\n";
        echo 
'<dd><strong>Author:</strong> '.$creator.'</dd>'."\n";
        echo 
'<dd><strong>Date:</strong> '.$dateIssued.'</dd>'."\n";
        echo 
'<dd><strong>Program:</strong> '.$discipline.'</dd>'."\n";
        echo 
'<dd><strong>Abstract:</strong> '.$description.'...</dd>'."\n";         
        echo 
'<dd><a class="download" href="'.$identifier.'">Download File</a> | <a class="expand" title="View full abstract and additional details about Item '.$id.'" href="./item.php?id='.$id.'">View full details</a></dd>'."\n";
        echo 
'</dl>'."\n";         
        echo 
'<hr />'."\n";
      }
//end while()    
   
}//end if()
   
?>
   <p class="nav"><a class="bck" href="./form.php">Back to search page</a></p>
<?php
//function validates the user's search term(s) to make sure that they contain only valid characters. A list of valid characters is given in the validChar string below.
function validation($keyword) {
  
  
$localKeyword $keyword;
  
$localKeyword strtolower($localKeyword);

  
$validChar "abcdefghijklmnopqrstuvwxyz\"',.?&:;-()/\ 1234567890&amp;";
  
  
$length strlen($localKeyword);
  for(
$i 0$i $length$i++)
  {
     if(!
strstr($validChar$localKeyword[$i]))
     {
        echo 
'<h2><strong>Your search contains invalid characters</strong></h2>'."\n";        
        echo 
'<hr />'."\n";
        echo 
'<h3>The Keyword search string '.$localKeyword.' contains invalid character(s)!!<br /><br />
              Please use the browser <strong>BACK</strong> button and fix the error. Then resubmit your request.</h3>'
."\n";

        return 
false;
     }    

  }

  return 
true;
}
//end validation()

//function displays a message to user if search resulted in no matching items.
function noMatches()
{
  echo 
'<h3 class="warn">Your search did not match any documents.</h3>'."\n";
  echo 
'<ul>'."\n";
  echo 
'<li><strong>Suggestions:</strong></li>'."\n";
  echo 
'<li>Make sure all words are spelled correctly.</li>'."\n";
  echo 
'<li>Try different keywords OR more general keywords.</li>'."\n";
  echo 
'</ul>'."\n";
  echo 
'<p><strong><a href="./form.php">Return to Search Form: QueryCatcher</a></strong></p>'."\n"
}
//end noMatches()

?>
</body>
</html>