Writing New Predefined Searches
Writing New Predefined Searches
This tutorial will walk you through the process of adding a predefined
search to the "Predefined Searches" menu. Predefined searches may offer
unique searches, unavailable through other interfaces, or be
simplifications of more complicated searches. They can also make use of
the free form search functions to provide a static interface for an
otherwise complex set of free form search entries. For this tutorial we
will write a simple search that finds all UARS Days with a given pid
number.
Written by Ziba R. Scott
and Adam D. Gorski
Table of Contents
The Three Searching Components
Every search in HRDIODB has its code split into three components and each
component is in a separate file.
- search_forms.php - Display elements for both data entry and results
- search.php - Flow control logic governing the execution of a search
- search_class.php - All logic which composes/executes a SQL query and returns the values
search_forms.php - Draw the form
In search_forms.php we need to add code in two places: One to add
your search to the menu and one to draw the search entry form.
Add Your Search to the Menu
The predefined_search_forms() function governs the
display of predefined searches in the navigation menu. Add your new search
to the array of search names and add a call to a function to draw your form
in the switch statement below. If you need to populate any variables to
pass to your drawing function, do that now.
$searches[0]="Pid, Date, Latitude Search";
// My new search name
$searches[1]="Pid Search";
.
.
.
// Variables needed by other search functions
$pids = $hdb->get_table("HRDI_Pids");
$types = $hdb->get_table("HRDI_FileTypes");
.
.
.
// Place all search form printing functions here
switch($search_num){
case 0:
break;
case 1:
// My new search form drawing function
break;
}
Write Your Search Drawing Function
Write a function that will draw your search form. Check out
common_class.php for convenience functions you may wish
to use. Also note that you can pass the $got variable
to you function which is a copy of the $_GET variable
that has been checked for security risks. You may also include the
file_filter_form() function which will include a table
of check boxes and file types. If used, it will allow users to find the
intersection of your search results and the selected file sets. This must
be included within your form tags.
function pid_form($got, $types){
?>
<!-- Tables class are used for a consistent look -->
<table class="hrdi">
<tr class="c_label">
<td>
Instructions
</td>
</tr>
<tr>
<td>
Enter a pid and press search.
</td>
</tr>
</table>
<!-- Using the "get" method allows users to bookmark
searches, but also invites tinkering.
Please use the $got variable to access submitted
data for safety. -->
<form name="search" method="get">
<table class="hrdi">
<tr class="c_label">
<td>
Pid Search
</td>
</tr>
<tr>
<td>
<!-- This tells search.php to run a search and
which search to run -->
<input type="hidden" name="submitted" value="pid">
<!-- This tells search_form.php to return to this
view after it is submitted -->
<input type=hidden name=predefined_search value=1>
<!-- This is the data we are submitting. The
previous value (if any) is repopulated.
The @ sign suppresses the warning generated
by php if "pid" is not a member of array "got". -->
<input type=text name=pid value=<?php print(@$got['pid']); ?>>
<input type=submit name="" value="Search">
</td>
</tr>
</table>
</form>
<?php
}
search.php - Call the desired search function
To connect your search form to piece of search logic you will need to add
code to the flow control contained in search.php. For this
tutorial we will be using the flexible search::free_form() function
because it meets our needs. Near the end of search.php, not within
a function call are the search trigger statements which check the
"submitted" variable. Add one that matches the value of your "submitted"
variable which will execute the code you desire.
// Process a predefined search entry (Sample of an existing search trigger section)
if($got['submitted']=="pid_day_coverage"){
$status = $search->pid_day_coverage($got['pid'],
@$got['date_type'],
$got['date_value_low'],
$got['date_value_high'],
$got['lat_day_min'],
$got['lat_day_max'],
$got['lat_night_min'],
$got['lat_night_max']);
}
//New code which will be activated on form submit
if($got['submitted']=="pid"){
// Compose conditions for the free_form() search based on submitted information
// in the $_GET variable
$conditions[0]["Table"]="HRDI_Modes";
$conditions[0]["Field"]="Process_ID";
$conditions[0]["Operator"]="=";
$conditions[0]["Value"]=@$got['pid'];
// Call the free_form() method of the search class
$status = $search->free_form($conditions);
}
}
And you're done! The search::free_form() function is very flexible
and highly reusable. However, if you wish to implement custom search logic,
read on to the next section.
search_class.php - Search Logic
Adding new search methods to the search class requires that the results be
stored as an array of UARS Days in $this->results. This
is where the result display and file type filtering functions expect to
find them. It is also highly recommended that you make use of the
hrdi_db class contained in the search object for
convenience and security. If possible use
$this->hdb->get_column() to get a simple array of
values. The method should return FALSE if the query has
failed and an empty array if there are no results (which
get_column() will do).
Here is a simple example of a search method that would implement all the
logic necessary to operate the search implemented previously in this
tutorial. Note that it is an unnecesary method because the
search::free_form() function can be made to perform the same
query.
function pid_search($pid){
$query = "SELECT UARS_Day from HRDI_Modes WHERE Process_ID='$pid'";
return $this->results =
$this->hdb->get_column($query);
}