<?php
/********************

   Engsoc PHP Registry

 Copyright 2000 Philip Steinke and Kevin Everets
 Licensed under the GNU General Public License ver 2 (or later)

 Notes:
   This has to be fixed up for when a search returns a huge number 
   of results. Do a standard prev-page, next-page kind of a thing.

********************/ 
include ('includes/header.inc.php');
disp_header("Search the Registry");
if (!
check_auth()) {
    
disp_not_logged_in();
}
disp_navbar();
include (
'includes/fielddef.inc.php');

$adminClass =  $webuser->is_admin();

// Only create this the first time we hit the page.
/*
if (!isset($searchSQL)) {
  $searchSQL = new DB_Sql;
  $searchSQL->Database = $db_name;
  $searchSQL->User = $db_user;
  $searchSQL->Password = $db_pwd;
}
*/

function showresult($searchresult) {
  global 
$regdb;
  print 
"<h3>Search Results:</h3>\n";
  
printf("<table border=\"1\">");
  
$numRows $regdb->numRows($searchresult->result);
  if (empty(
$numRows)) $numRows "No";
  print 
"<caption>$numRows match";
  if (
$numRows 1)
      print 
"es";
  print 
" found.</caption>\n";
  
printf("<tr><th>Name</th><th>Year</th><th>City</th><th>Prov<br /></th></tr>\n");
  while(
$row $searchresult->fetchRow(DB_FETCHMODE_ASSOC)) {
    
printf("<tr><td><a href=\"details.php?shandle=%s\">%s %s</a></td><td>%s</td><td>%s</td><td>%s<br /></td></tr>\n",
       
$row['handle'],
       
$row['first_name'],
       
$row['last_name'],
       
$row['class'],
       
$row['address_city'],
       
$row['address_prov']);
  }
  
printf("</table>");
  print 
"<hr />\n";

}

// NOTE TO SELF: May want to make sure that people never ever do this unless
// they're uber powerful.
if (isset($searchstring) && !empty($searchstring) && $adminClass) {
  
$searchstring stripslashes($searchstring);
  if (
$adminClass == 'master') {
      
$searchstring "SELECT " $searchstring;
  } elseif (
$adminClass) {
      
$searchstring "SELECT * FROM people WHERE class='$adminClass' AND "
                    
$searchstring;
  }
  echo 
"<p>Current User: ".$webuser->handle."</p><p>SearchString: ".$searchstring."</p>\n";

  
$searchresult $regdb->query($searchstring);
  
showresult($searchresult);
  
$searchresult->free();

/*
  Search by various fields. Even does partial matches (might get unwieldy with 70,000 users).
  NOTE TO SELF: Come up with a better way of doing this. Does OOHForms have better functionality?
*/
## elseif ($searchFirstName != "" || $searchLastName != "" || $searchCity != "" || $searchYear != "") {
  /*****
   * Set up query as per input from Phil's phrase-based search
   *
   * This now checks the publish fields when necessary.  It uses the global
   * "always_publish" array (from fielddef.inc.php) to see when it should
   * check the publish table.  This array is now also used for the building
   * of the add/update info form.
   *****/
  
elseif (!empty($searchField1) && !empty($searchString1)) {

  if (
preg_match("/^address/"$searchField1))
      
$publishField1 'address';
  else
      
$publishField1 $searchField1;
  
$searchString1 strtoupper($regdb->quoteString($searchString1));
  
$searchstring "SELECT p.handle, p.first_name, p.last_name, p.address_city,
              p.address_prov, p.class
                   FROM people p, publish pub
           WHERE (upper(text(p.$searchField1)) LIKE '%$searchString1%'
             AND p.handle = pub.handle"
;
  if (!
in_array($searchField1$always_publish)) {
      
$searchstring .= "\nAND pub.$publishField1 = 't'";
  }
  
$searchstring .= ")";
  if (!empty(
$searchStyle) && !empty($searchString2)) {
      if (
ereg("^address"$searchField2))
          
$publishField2 'address';
      else
          
$publishField2 $searchField2;
      
$searchString2 strtoupper($regdb->quoteString($searchString2));
    
$searchStyle strtoupper($searchStyle);
    
$searchstring .= "\n$searchStyle\n(upper(text(p.$searchField2)) LIKE '%$searchString2%'
                AND p.handle = pub.handle"
;
      if (!
in_array($searchField2$always_publish)) {
          
$searchstring .= "\nAND pub.$publishField2 = 't'";
     }
      
$searchstring .= ")";
  }
  
$searchstring .= "\nORDER BY last_name";
  
/***** 
   * Query-building from Kevin's original search
   *
   ********************************************
  $searchFirstName = strtoupper($searchFirstName);
  $searchLastName = strtoupper($searchLastName);
  $searchCity = strtoupper($searchCity);
  $searchstring = sprintf("SELECT handle, first_name, last_name, address_city, address_prov, class FROM people WHERE UPPER(first_name) LIKE '%%%s%%' AND UPPER(last_name) LIKE '%%%s%%' AND UPPER(address_city) LIKE '%%%s%%' AND class LIKE '%%%s%%' ORDER BY last_name;",
              $searchFirstName,
              $searchLastName,
              $searchCity,
              $searchYear);
  *************************************************************/
  # print "<pre>Executing query: $searchstring</pre>\n";
  
$searchresult $regdb->query($searchstring);


  
/*  
  printf("I've found %s %s that matched your query. Hope you're happy.\n",
     $searchresult->numRows(),
     ($searchresult->numRows()==1)?"person":"people");

  $t->show_result($searchSQL, "results");
  */

  
showresult($searchresult);
  
$searchresult->free();
}

echo 
"<form action=\"$PHP_SELF\" method=\"get\">\n";

if (
$adminClass == 'master') {
  echo 
"<p>Custom Super-Duper-Don't-Let-Random-Users-Do-This Query:</p>";
  echo 
"SELECT <input type=\"text\" size=\"80\" name=\"searchstring\" /></p>";
} elseif (
$adminClass) {
  echo 
"<p>Admin Direct-SQL Query:</p>\n";
  echo 
"SELECT * FROM people WHERE class='$adminClass' AND ";
  print 
"<input type=\"text\" size=\"80\" name=\"searchstring\" /></p>";
}

// Let anyone do these:
/*****
 * Here begins the prompt for Phil's phrase-style search (idea stolen from CMC).
 *****/
?>
<h3>Search the Registry</h3>
Find people where <select name="searchField1">
<?php
    
foreach ($form_field as $field => $info) {
        if (
$info[2] != 'hidden') {
            print 
"<option value=\"$field\">$info[0]</option>\n";
        }
    }
?>
</select> contains <input type="text" size="30" name="searchString1" />, <br />
<select name="searchStyle">
    <option></option>
    <option>and</option>
    <option>or</option>
</select>
<select name="searchField2">
<?php
    
foreach ($form_field as $field => $info) {
        if (
$info[2] != 'hidden') {
            print 
"<option value=\"$field\">$info[0]</option>\n";
        }
    }
?>
</select> contains <input type="text" size="30" name="searchString2" />
<p>
    Leaving the second phrase blank will just search on the first.
</p>
<?php
/*****
 * Kevin's old search.  I've commented this out for now in favour of one
 * that mimics the search structure at CMC, as I think it's a bit more
 * intuitive and flexible for people.

*****************************************************
<p>
    Enter full or partial information in any of the fields below.  The more
    details you supply, the narrower the search will be.
</p>
<table>
<tr><td>Last Name:</td>
<td><input type="text" size="30" name="searchLastName" /><br /></td></tr>
<tr><td> First Name:</td>
<td><input type="text" size="30" name="searchFirstName" /><br /></td></tr>
<tr><td>Year:</td>
<td><tt>Sci'</tt><input type="text" size="2" name="searchYear" /><br /></td></tr>
<tr><td>City/Town:</td>
<td><input type="text" size="30" name="searchCity" /><br /></td></tr>
</table>
*****************************************************/ 
?>
<p><input type="image" name="Search" alt="Search" value="Search" 
          src="images/buttons/search.png" 
      <?php if ($using_ns4) print 'border="0" '?>/></p>
</form>
<?php include ('includes/footer.inc.php'); ?>