system/classes/searchcriteria.class.php
author Dirk Haun <dirk@haun-online.de>
Thu, 29 Oct 2009 13:01:33 +0100
branchHEAD
changeset 7398 e1700815379b
parent 7387 39932e68c099
child 7660 3f3700aee0c2
permissions -rw-r--r--
Removed executable flag from file permissions, no change in content
     1 <?php
     2 
     3 /* Reminder: always indent with 4 spaces (no tabs). */
     4 // +---------------------------------------------------------------------------+
     5 // | Geeklog 1.6.1                                                             |
     6 // +---------------------------------------------------------------------------+
     7 // | searchcriteria.class.php                                                  |
     8 // |                                                                           |
     9 // | This class acts as a container which allows data to be passed between the |
    10 // | search engine and plugins.                                                |
    11 // +---------------------------------------------------------------------------+
    12 // | Copyright (C) 2000-2009 by the following authors:                         |
    13 // |                                                                           |
    14 // | Authors: Sami Barakat, s.m.barakat AT gmail DOT com                       |
    15 // +---------------------------------------------------------------------------+
    16 // |                                                                           |
    17 // | This program is free software; you can redistribute it and/or             |
    18 // | modify it under the terms of the GNU General Public License               |
    19 // | as published by the Free Software Foundation; either version 2            |
    20 // | of the License, or (at your option) any later version.                    |
    21 // |                                                                           |
    22 // | This program is distributed in the hope that it will be useful,           |
    23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
    24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
    25 // | GNU General Public License for more details.                              |
    26 // |                                                                           |
    27 // | You should have received a copy of the GNU General Public License         |
    28 // | along with this program; if not, write to the Free Software Foundation,   |
    29 // | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
    30 // |                                                                           |
    31 // +---------------------------------------------------------------------------+
    32 
    33 class SearchCriteria {
    34 
    35     // PRIVATE PROPERTIES
    36     var $_sql = '';
    37     var $_ftsql = '';
    38     var $_pluginLabel;
    39     var $_pluginName;
    40     var $_rank;
    41     var $_url_rewrite;
    42     var $_append_query;
    43     var $_results = array();
    44     var $_callback_func = '';
    45     var $_total_results = 0;
    46 
    47     // CONSTRUCTOR
    48     function SearchCriteria( $pluginName, $pluginLabel )
    49     {
    50         $this->_pluginName = $pluginName;
    51         $this->_pluginLabel = $pluginLabel;
    52         $this->_rank = 3;
    53         $this->_url_rewrite = false;
    54         $this->_append_query = true;
    55         $this->_total_results = 0;
    56     }
    57 
    58     // GENERAL METHODS
    59     function getName()
    60     {
    61         return $this->_pluginName;
    62     }
    63 
    64     function getLabel()
    65     {
    66         return $this->_pluginLabel;
    67     }
    68 
    69     function setRank( $rank )
    70     {
    71         $this->_rank = $rank;
    72     }
    73 
    74     function setURLRewrite( $url_rewrite )
    75     {
    76         $this->_url_rewrite = $url_rewrite;
    77     }
    78 
    79     function setAppendQuery( $append_query )
    80     {
    81         $this->_append_query = $append_query;
    82     }
    83 
    84     function setResults( $result_arr )
    85     {
    86         $this->_results = $result_arr;
    87     }
    88 
    89     function getRank()
    90     {
    91         return $this->_rank;
    92     }
    93 
    94     function UrlRewriteEnable()
    95     {
    96         return $this->_url_rewrite;
    97     }
    98 
    99     function AppendQueryEnable()
   100     {
   101         return $this->_append_query;
   102     }
   103 
   104     function getResults()
   105     {
   106         return $this->_results;
   107     }
   108 
   109     // CALLBACK METHODS
   110     function setCallback( $func )
   111     {
   112         $this->_callback_func = $func;
   113     }
   114 
   115     function getCallback()
   116     {
   117         return $this->_callback_func;
   118     }
   119 
   120     function setTotal( $total_results )
   121     {
   122         $this->_total_results = $total_results;
   123     }
   124 
   125     function getTotal()
   126     {
   127         return $this->_total_results;
   128     }
   129 
   130     // SQL METHODS
   131     function setSQL( $sql )
   132     {
   133         $this->_sql = $sql;
   134     }
   135 
   136     function setFTSQL( $ftsql )
   137     {
   138         $this->_ftsql = $ftsql;
   139     }
   140 
   141     function getSQL()
   142     {
   143         return $this->_sql;
   144     }
   145 
   146     function getFTSQL()
   147     {
   148         global $_DB_dbms;
   149 
   150         // When only one SQL statment is set we assume it is for MySQL
   151         if ($this->_ftsql != '' && (is_string($this->_ftsql) && $_DB_dbms == 'mysql') || is_array($this->_ftsql)) {
   152             return $this->_ftsql;
   153         } else {
   154             return '';
   155         }
   156     }
   157 
   158     function buildSearchSQL( $keyType, $query, $columns, $sql = '' )
   159     {
   160         if ($keyType == 'all')
   161         {
   162             // must contain ALL of the keywords
   163             $words = explode(' ', $query);
   164             $sep = 'AND';
   165 
   166             $ftwords['mysql'] = '+' . str_replace(' ', ' +', $query);
   167             $ftwords['mssql'] = '"' . str_replace(' ', '" AND "', $query) . '"';
   168         }
   169         else if ($keyType == 'any')
   170         {
   171             // must contain ANY of the keywords
   172             $words = explode(' ', $query);
   173             $sep = 'OR ';
   174             $ftwords['mysql'] = $query;
   175             $ftwords['mssql'] = '"' . str_replace(' ', '" OR "', $query) . '"';
   176         }
   177         else
   178         {
   179             // do an exact phrase search (default)
   180             $words = array($query);
   181             $sep = '   ';
   182 
   183             // Puttings quotes around a single word in mysql really slows things down
   184             if (strpos($query, ' ') !== false) {
   185                 $ftwords['mysql'] = '"' . $query . '"';
   186             } else {
   187                 $ftwords['mysql'] = $query;
   188             }
   189             $ftwords['mssql'] = '"' . $query . '"';
   190         }
   191 
   192         $titles = (isset($_GET['title']) && isset($columns['title'])) ? true : false;
   193 
   194         if ($titles) {
   195             $strcol = $columns['title'];
   196         } else {
   197             $strcol = implode(',', $columns);
   198         }
   199 
   200         $ftsql['mysql'] = $sql . "AND MATCH($strcol) AGAINST ('{$ftwords['mysql']}' IN BOOLEAN MODE)";
   201         $ftsql['mssql'] = $sql . "AND CONTAINS(($strcol), '{$ftwords['mssql']}')";
   202 
   203         $tmp = 'AND (';
   204         foreach ($words AS $word)
   205         {
   206             $word = trim($word);
   207             $tmp .= '(';
   208 
   209             if ($titles) {
   210                 $tmp .= $columns['title'] . " LIKE '%$word%' OR ";
   211             } else {
   212                 foreach ($columns AS $col) {
   213                     $tmp .= "$col LIKE '%$word%' OR ";
   214                 }
   215             }
   216             $tmp = substr($tmp, 0, -4) . ") $sep ";
   217         }
   218         $sql .= substr($tmp, 0, -5) . ') ';
   219 
   220         return array($sql,$ftsql);
   221     }
   222 
   223     function getDateRangeSQL( $type = 'WHERE', $column, $datestart, $dateend )
   224     {
   225         if (!empty($datestart) && !empty($dateend))
   226         {
   227             $delim = substr($datestart, 4, 1);
   228             if (!empty($delim))
   229             {
   230                 $DS = explode($delim, $datestart);
   231                 $DE = explode($delim, $dateend);
   232                 $startdate = mktime(0,0,0,$DS[1],$DS[2],$DS[0]);
   233                 $enddate = mktime(23,59,59,$DE[1],$DE[2],$DE[0]);
   234                 return " $type (UNIX_TIMESTAMP($column) BETWEEN '$startdate' AND '$enddate') ";
   235             }
   236         }
   237 
   238         return '';
   239     }
   240 }
   241 
   242 ?>