HRDIODB
[ class tree: HRDIODB ] [ index: HRDIODB ] [ all elements ]

Source for file search_class.php

Documentation is available at search_class.php

  1. <?php
  2. /**
  3.  * Composes search queries and returns results.
  4.  * This file contains the {@link search} class. The class is responsible for
  5.  * composing search queries and returning the search results.
  6.  * @package HRDIODB
  7.  */
  8.  
  9. /**
  10.  * This file is part of HRDIODB.
  11.  *
  12.  * HRDIODB is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * HRDIODB is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with HRDIODB; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25.  */
  26.  
  27. /**
  28.  * Search class.
  29.  * The search class handles all SQL related searching logic. An object of this
  30.  * class populates a results array with key value pairs from the database based
  31.  * on conditions supplied to the class instance.
  32.  * @package HRDIODB
  33.  */
  34. class search {
  35.   /**
  36.    * Constructor.
  37.    * Takes a {@link hrdi_db} class instance. Sets the internal result array to
  38.    * empty.
  39.    * @param hrdi_db $hdb The hrdi_db class instance
  40.    */
  41.   function search (&$hdb{
  42.     $this->hdb $hdb;
  43.     $this->results array();
  44.   }
  45.  
  46.   /**
  47.    * Free form search.
  48.    * Takes an array of conditions and based on those conditions stores a list
  49.    * of UARS_Day numbers in the results array.
  50.    * @param array $conditions An indexed array of associative arrays with four
  51.    *  values: Table, Field, Operator, Value
  52.    * @return mixed An indexed array of UARS days
  53.    */
  54.   function free_form($conditions){
  55.     $regex_year_day "/^\d{4}-\d{1,3}$/";
  56.     $regex_uars_day "/^\d+$/";
  57.     $this->conditions $conditions;
  58.     foreach $conditions as $i=>$condition ){
  59.         if($condition['Field']=='UARS_Day'){
  60.             if(preg_match($regex_year_day,$condition['Value'])){
  61.                 $this->conditions[$i]['Value'common::doy_to_uars_day($condition['Value'])
  62.             }
  63.             elseif(preg_match($regex_uars_day,$condition['Value'])){
  64.             }
  65.             else{
  66.                 $this->conditions[$i]['Value'common::calendar_to_uars_day($condition['Value'])
  67.             }
  68.         }
  69.         if($condition['Field']=='Created'){
  70.             if(preg_match($regex_year_day,$condition['Value'])){
  71.                 $timestamp common::doy_to_timestamp($condition['Value']);
  72.                 $this->conditions[$i]['Value'common::timestamp_to_datetime($timestamp);
  73.             }
  74.             elseif(preg_match($regex_uars_day,$condition['Value'])){
  75.                 $timestamp common::uars_day_to_timestamp($condition['Value']);
  76.                 $this->conditions[$i]['Value'common::timestamp_to_datetime($timestamp);
  77.             }
  78.             else{
  79.                 $timestamp strtotime($condition['Value']);
  80.                 $this->conditions[$i]['Value'common::timestamp_to_datetime($timestamp);
  81.             }
  82.         }
  83.         if($condition['Field']=='Modified'){
  84.             if(preg_match($regex_year_day,$condition['Value'])){
  85.                 $timestamp common::doy_to_timestamp($condition['Value']);
  86.                 $this->conditions[$i]['Value'common::timestamp_to_datetime($timestamp);
  87.             }
  88.             elseif(preg_match($regex_uars_day,$condition['Value'])){
  89.                 $timestamp common::uars_day_to_timestamp($condition['Value']);
  90.                 $this->conditions[$i]['Value'common::timestamp_to_datetime($timestamp);
  91.             }
  92.             else{
  93.                 $timestamp strtotime($condition['Value']);
  94.                 $this->conditions[$i]['Value'common::timestamp_to_datetime($timestamp);
  95.             }
  96.         }
  97.         if($condition['Field']=='year_doy'){
  98.                 if($this->conditions[$i]['Value'common::doy_to_uars_day($condition['Value'])){
  99.                     $this->conditions[$i]['Field']='UARS_Day';
  100.                 }
  101.                 else{
  102.                     return FALSE;
  103.                 }
  104.         }
  105.         if($condition['Field']=='calendar_date'){
  106.             if(    $this->conditions[$i]['Value'common::calendar_to_uars_day($condition['Value'])){
  107.                 $this->conditions[$i]['Field']='UARS_Day';
  108.             
  109.             else{
  110.                 return FALSE;
  111.             }
  112.  
  113.         }
  114.     }
  115.     $query $this->compose_query();
  116.     return $this->results $this->hdb->get_column($query);
  117.   }
  118.  
  119.   /**
  120.    * Filters search results by file type.
  121.    * Optionally called after a search has been made. It will compose and
  122.    * execute a query which retrieves all UARS_Day numbers which have filetypes
  123.    * matching the given array of filetypes. The search instances result set is
  124.    * then overwritten by the intersection of the previous search results and
  125.    * the filetype search results.  This method a query for each file type and
  126.    * discards some fetched data, however it makes up for this by never needing
  127.    * to reference all UARS days in one query. That would potentially by tens
  128.    * of thousands of integers in one query.
  129.    * @param array $filetypes An array of integers which are filetypes stored in
  130.    *  the database.
  131.    * @return mixed An indexed array of UARS days
  132.    */
  133.   function filetype_filter($filetypes{
  134.     if (!isset($this->results)) {
  135.       return inline_error("A search must be made before results can be filtered");
  136.     }
  137.  
  138.     if(!is_array($this->results)){
  139.       return inline_error("No results to filter by file type");
  140.     }
  141.  
  142.     foreach($filetypes as $filetype){
  143.       $sql "SELECT d.UARS_Day
  144.               FROM HRDI_Days dHRDI_Files f
  145.               WHERE f.UARS_Day = d.UARS_Day
  146.               AND f.Type = $filetype
  147.               GROUP BY d.UARS_Day;";
  148.       $files $this->hdb->get_column($sql);
  149.       $this->results array_intersect($this->results$files);
  150.     }
  151.  
  152.     return sort($this->results);
  153.   }
  154.   
  155.   /**
  156.    * Composes a SQL query.
  157.    * Converts an array of conditions into a SQL query. Used by the free form
  158.    * search.
  159.    * @internal
  160.    * @return string SQL query
  161.    */
  162.   function compose_query(){
  163.     $sub_query array();
  164.  
  165.     if (($this->conditions == ""!isset($this->conditions)) {
  166.       return error("No search conditions set");
  167.     }
  168.     
  169.     if(!isset($this->conditions[0]['Table'])){
  170.       return error("The first conditions entry MUST have a table.");
  171.     }
  172.    
  173.     // Only the first Table statement in the condition array is used
  174.     $sql "SELECT UARS_Day
  175.             FROM "$this->conditions[0]['Table'."
  176.             WHERE ";
  177.  
  178.     foreach($this->conditions as $condition{
  179.       if(($condition['Table'== ""($condition['Field'== ""($condition['Value'== "")) {
  180.         return error("Invalid search conditions");
  181.       }
  182.  
  183.       $sub_query[.= "{$condition['Field']} {$condition['Operator']} '{$condition['Value']}'";
  184.     }
  185.  
  186.     $sql .= implode($sub_query" AND ");
  187.     $sql .= " GROUP BY UARS_Day";
  188.     return $sql;
  189.   }
  190.   
  191.   /**
  192.    * Builds Pid day coverage search query.
  193.    * This function takes several variables, builds a query, and returns an
  194.    * array of UARS days. It is a companion function to a custom search form.
  195.    * @param mixed $pid A Process_ID number or the keyword "any"
  196.    * @param mixed $date_type A string which describes the format of the high
  197.    *  and low values
  198.    * @param mixed $date_value_low The low end of a date range
  199.    * @param mixed $date_value_high The high end of a date range
  200.    * @param int $lat_day_min The minimum acceptable day latitude
  201.    * @param int $lat_day_max The maximum acceptable day latitude
  202.    * @param int $lat_night_min The minimum acceptable night latitude
  203.    * @param int $lat_night_max The maximum acceptable night latitude
  204.    * @return mixed An indexed array of UARS days
  205.    */
  206.   function pid_day_coverage($pid="any"$date_type="none"$date_value_low$date_value_high
  207.                             $lat_day_min$lat_day_max$lat_night_min$lat_night_max{
  208.     // Compose pid query
  209.     if ($pid != "any"{
  210.       $sets[]='Daytime';
  211.       $sets[]='Stratosphere';
  212.       $sets[]='Mesosphere';
  213.       $sets[]='Daytime mesosphere';
  214.       $sets[]='Nighttime mesosphere';
  215.  
  216.       if (is_numeric($pid)) {
  217.         $queries[]"  m.UARS_Day = d.UARS_Day AND m.Process_ID = $pid "
  218.       elseif (in_array($pid$sets)) {
  219.         switch($pid){
  220.           case "Daytime":
  221.             $queries[]"m.UARS_Day = d.UARS_Day AND m.Process_ID IN (27, 29, 35, 37, 41) ";
  222.             break;
  223.           case "Mesosphere":
  224.             $queries[]"m.UARS_Day = d.UARS_Day AND 
  225.                          (m.Process_ID in (27, 29, 35, 36, 45, 60 ) OR 
  226.                          (m.Process_ID = 37 AND d.UARS_Day > 2740)) 
  227.                          ";
  228.             break;
  229.           case "Stratosphere":
  230.             $queries[]"m.UARS_Day = d.UARS_Day AND 
  231.                          (m.Process_ID = 35 OR 
  232.                          (m.Process_ID = 37 AND d.UARS_Day < 188) OR
  233.                          (m.Process_ID = 41 AND d.UARS_Day < 173) OR
  234.                          (m.Process_ID = 41 AND d.UARS_Day > 2740)) 
  235.                          ";
  236.             break;
  237.           case "Daytime mesosphere":
  238.             $queries[]"m.UARS_Day = d.UARS_Day AND 
  239.                          (m.Process_ID in (27, 29, 35 ) OR 
  240.                          (m.Process_ID = 37 AND d.UARS_Day > 2740)) 
  241.                          ";
  242.             break;
  243.           case "Nighttime mesosphere":
  244.             $queries[]"m.UARS_Day = d.UARS_Day AND m.Process_ID IN (36, 45, 60) ";
  245.             break;
  246.         }
  247.       else {
  248.         return error("Pid must be \"any\" or a numbernot: \"$pid\"");
  249.       }
  250.     }
  251.     
  252.     // Compose day query
  253.     if ($date_type == ""{
  254.       $date_type "none";
  255.     }
  256.  
  257.     if ($date_type != 'none'{
  258.         if(!empty($date_value_low&& empty($date_value_high)){
  259.             $date_value_high $date_value_low;
  260.             $_GET['date_value_high']=$date_value_high;
  261.         }
  262.     }
  263.     if ($date_type == 'UARS_Day'{
  264.       $low $date_value_low;
  265.       $high $date_value_high;
  266.     }
  267.     // Convert day of year to UARS_Day
  268.     elseif ($date_type == 'year_day'{
  269.       $low common::doy_to_uars_day($date_value_low);
  270.       $high common::doy_to_uars_day($date_value_high);
  271.  
  272.       if(!$low or !$high){
  273.         return FALSE;
  274.       }
  275.     }
  276.     // Convert calendar day to UARS_Day
  277.     elseif ($date_type == 'calendar_day'{
  278.       $low common::calendar_to_uars_day($date_value_low);
  279.       $high common::calendar_to_uars_day($date_value_high);
  280.  
  281.       if(!$low or !$high{
  282.         return FALSE;
  283.       }
  284.     elseif ($date_type != 'none'{
  285.       return inline_error("unable convert \"$date_type\" to UARS_day");
  286.     }
  287.     
  288.     if ($date_type != 'none'{
  289.       if (!is_numeric($low|| !is_numeric($high)) {
  290.         return inline_error("Low and high must be numerals");
  291.       }
  292.  
  293.       $queries["d.UARS_day >= $low and d.UARS_day <= $high";
  294.     }
  295.  
  296.     // Compose day coverage query
  297.     if($lat_day_min != ""){
  298.       if(!is_numeric($lat_day_min|| !is_numeric($lat_day_max)){
  299.         return inline_error("You must enter numbers for both latitude min and max");
  300.       }
  301.  
  302.       $between "BETWEEN $lat_day_min AND $lat_day_max";
  303.  
  304.       $fields array("d.cov_LAT_Min_WDA","d.cov_LAT_Max_WDA","d.cov_LAT_Min_WDD",
  305.                       "d.cov_LAT_Max_WDD","d.cov_LAT_Min_CDA","d.cov_LAT_Max_CDA",
  306.                       "d.cov_LAT_Min_CDD","cov_LAT_Max_CDD")
  307.       $pieces array()
  308.  
  309.       foreach($fields as $field){
  310.         $pieces["($field $between)";
  311.       }
  312.  
  313.       $queries["(".implode(" OR "$pieces).")";
  314.     }
  315.  
  316.     // Compose night coverage query
  317.     if($lat_night_min != ""){
  318.       if(!is_numeric($lat_night_min|| !is_numeric($lat_night_max)){
  319.         return inline_error("You must enter numbers for both latitude min and max");
  320.       }
  321.  
  322.       $between "BETWEEN $lat_night_min AND $lat_night_max";
  323.  
  324.       $fields array("d.cov_LAT_Min_WNA","d.cov_LAT_Max_WNA","d.cov_LAT_Min_WND",
  325.                       "d.cov_LAT_Max_WND","d.cov_LAT_Min_CNA","d.cov_LAT_Max_CNA",
  326.                       "d.cov_LAT_Min_CND","cov_LAT_Max_CND")
  327.       $pieces array()
  328.  
  329.       foreach($fields as $field){
  330.         $pieces["($field $between)";
  331.       }
  332.  
  333.       $queries["(".implode(" OR "$pieces).")";
  334.     }
  335.  
  336.     $sql "SELECT d.UARS_Day
  337.             FROM HRDI_Days d, HRDI_Modes m 
  338.             WHERE d.UARS_Day=m.UARS_Day ";
  339.  
  340.     if (isset($queries)) {
  341.       $sql .= "AND ".implode(" AND "$queries);
  342.     }
  343.  
  344.     $sql .= " GROUP BY d.UARS_Day;";
  345.     return $this->results $this->hdb->get_column($sql);
  346.   }
  347. }
  348. ?>

Documentation generated on Tue, 23 Jan 2007 22:57:51 -0500 by phpDocumentor 1.3.0RC6