![]() |
|---|
| [ Index ] |
Source Code Reference for V1.00 |
[Summary view] [Print] [Text view]
1 <?php /* $Id: smartsearch.class.php 140 2008-04-05 15:34:35Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/modules/smartsearch/smartsearch.class.php $ */ 2 if (!defined('W2P_BASE_DIR')) { 3 die('You should not access this file directly.'); 4 } 5 6 class smartsearch { 7 8 var $table = null; 9 var $table_alias = null; 10 var $table_module = null; 11 var $table_key = null; // primary key in searched table 12 var $table_key2 = null; // primary key in parent table 13 var $table_link = null; // first part of link 14 var $table_link2 = null; // second part of link 15 var $table_title = null; 16 var $table_orderby = null; 17 var $table_extra = null; 18 var $search_fields = array(); 19 var $display_fields = array(); 20 var $table_joins = array(); 21 var $keyword = null; 22 var $keywords = null; 23 var $tmppattern = ''; 24 var $display_val = ''; 25 var $search_options = null; 26 // $search_options['display_all_flds']=='on' display all fields 27 // $search_options['display_all_flds']=='' display only first 2 fields from display_fields array 28 // $search_opts['keywords']==array() array of searched keywords 29 // $search_options['ignore_specchar']=='on' enable ignoring special eastern national characters / czech and slovak / 30 // $search_options['ignore_specchar']=='' disable ignoring special eastern national characters / czech and slovak / 31 // $search_options['ignore_case']=='' match case 32 // $search_options['ignore_case']=='on' ignore case /default/ 33 // $search_options['show_empty']=='' hide modules with empty results /default/ 34 // $search_options['show_empty']=='on' show modules with empty results 35 // $search_options['all_words']=='' any of the words /default/ 36 // $search_options['all_words']=='on' match all words 37 38 function smartsearch() { 39 return null; 40 } 41 42 function fetchResults(&$permissions, &$record_count) { 43 global $AppUI; 44 $q = $this->_buildQuery(); 45 $results = null; 46 if ($q) { 47 $results = $q->loadList(); 48 } 49 if ($results) { 50 $outstring = ''; 51 $subrecord_count = 0; 52 foreach ($results as $records) { 53 if ($permissions->checkModuleItem($this->table_module, 'view', $records[preg_replace('/^.*\.([^\.]+)$/', '$1', $this->table_key)])) { 54 //Don't count records for which the user does not have permission 55 $record_count += 1; 56 $subrecord_count += 1; 57 // --MSy- 58 $ii = 0; 59 $display_val = ''; 60 foreach ($this->display_fields as $fld) { 61 $ii++; 62 if (!($this->search_options['display_all_flds'] == 'on') && ($ii > 2)) { 63 break; 64 } 65 $display_val = $display_val . ' ' . $records[preg_replace('/^.*\.([^\.]+)$/', '$1', $fld)]; 66 } 67 //--MSy- 68 $tmplink = ''; 69 if (isset($this->table_link) && isset($this->table_key)) { 70 $tmplink = $this->table_link . $records[preg_replace('/^.*\.([^\.]+)$/', '$1', $this->table_key)]; 71 } 72 if (isset($this->table_link2) && isset($this->table_key2)) { 73 $tmplink = $this->table_link . $records[preg_replace('/^.*\.([^\.]+)$/', '$1', $this->table_key)] . $this->table_link2 . $records[preg_replace('/^.*\.([^\.]+)$/', '$1', $this->table_key2)]; 74 } 75 //--MSy-- 76 $outstring .= '<tr><td><a href = "' . $tmplink . '">' . highlight($display_val, $this->keywords) . '</a></td></tr>'; 77 } 78 } 79 $outstring = '<tr><th><b>' . $AppUI->_($this->table_title) . ' (' . $subrecord_count . ')' . '</b></th></tr> ' . "\n" . $outstring; 80 } else { 81 if ($this->search_options['show_empty'] == 'on') { 82 $outstring = '<tr><th><b>' . $AppUI->_($this->table_title) . ' (0)' . '</b></th></tr><tr><td>' . $AppUI->_('Empty') . '</td></tr>'; 83 } 84 } 85 return $outstring; 86 } 87 88 function setKeyword($keyw) { 89 $this->keyword = $keyw; 90 } 91 function setAdvanced($search_opts) { 92 $this->search_options = $search_opts; 93 $this->keywords = $search_opts['keywords']; 94 } 95 96 function _buildQuery() { 97 $q = new DBQuery; 98 99 if ($this->table_alias) { 100 $q->addTable($this->table, $this->table_alias); 101 } else { 102 $q->addTable($this->table); 103 } 104 $q->addQuery($this->table_key); 105 if (isset($this->table_key2)) { 106 $q->addQuery($this->table_key2); 107 } 108 //--MSy-- 109 foreach ($this->table_joins as $join) { 110 $q->addJoin($join['table'], $join['alias'], $join['join']); 111 } 112 113 foreach ($this->display_fields as $fld) { 114 $q->addQuery($fld); 115 } 116 117 $q->addOrder($this->table_orderby); 118 119 if ($this->table_extra) { 120 $q->addWhere($this->table_extra); 121 } 122 123 $sql = ''; 124 foreach (array_keys($this->keywords) as $keyword) { 125 $sql .= '('; 126 127 foreach ($this->search_fields as $field) { 128 //OR treatment to each keyword 129 // Search for semi-colons, commas or spaces and allow any to be separators 130 $or_keywords = preg_split('/[\s,;]+/', $keyword); 131 foreach ($or_keywords as $or_keyword) { 132 if ($this->search_options['ignore_specchar'] == 'on') { 133 $tmppattern = recode2regexp_utf8($or_keyword); 134 if ($this->search_options['ignore_case'] == 'on') { 135 $sql .= ' ' . $field . ' REGEXP \'' . $tmppattern . '\' or '; 136 } else { 137 $sql .= ' ' . $field . ' REGEXP BINARY \'' . $tmppattern . '\' or '; 138 } 139 } else 140 if ($this->search_options['ignore_case'] == 'on') { 141 $sql .= ' ' . $field . ' LIKE "%' . $or_keyword . '%" or '; 142 } else { 143 $sql .= ' ' . $field . ' LIKE BINARY "%' . $or_keyword . '%" or '; 144 } 145 } 146 } // foreach $field 147 $sql = substr($sql, 0, -4); 148 149 if ($this->search_options['all_words'] == 'on') { 150 $sql .= ') and '; 151 } else { 152 $sql .= ') or '; 153 } 154 155 } // foreach $keyword 156 //--MSy-- 157 $sql = substr($sql, 0, -4); 158 if ($sql) { 159 $q->addWhere($sql); 160 return $q; 161 } else { 162 return null; 163 } 164 } 165 } 166 167 function highlight($text, $keyval) { 168 global $ssearch; 169 170 $txt = $text; 171 $hicolor = array('#FFFF66', '#ADD8E6', '#90EE8A', '#FF99FF'); 172 $keys = array(); 173 if (!is_array($keyval)) 174 $keys = array($keyval); 175 else 176 $keys = $keyval; 177 178 foreach ($keys as $key) { 179 if (strlen($key[0]) > 0) { 180 $key[0] = stripslashes($key[0]); 181 $metacharacters = array('\\', '(', ')', '$', '[', '*', '+', '|', '.', '^', '?'); 182 $metareplacement = array('\\\\', '\(', '\)', '\$', '\[', '\*', '\+', '\|', '\.', '\^', '\?'); 183 $key[0] = str_replace($metacharacters, $metareplacement, $key[0]); 184 if (isset($ssearch['ignore_specchar']) && ($ssearch['ignore_specchar'] == 'on')) { 185 if ($ssearch['ignore_case'] == 'on') { 186 $txt = eregi_replace((recode2regexp_utf8($key[0])), '<span style="background:' . $hicolor[$key[1]] . '" >\\0</span>', $txt); 187 } else { 188 $txt = ereg_replace((recode2regexp_utf8($key[0])), '<span style="background:' . $hicolor[$key[1]] . '" >\\0</span>', $txt); 189 } 190 } elseif (!isset($ssearch['ignore_specchar']) || ($ssearch['ignore_specchar'] == '')) { 191 if ($ssearch['ignore_case'] == 'on') { 192 $txt = eregi_replace($key[0], '<span style="background:' . $hicolor[$key[1]] . '" >\\0</span>', $txt); 193 } else { 194 $txt = ereg_replace($key[0], '<span style="background:' . $hicolor[$key[1]] . '" >\\0</span>', $txt); 195 } 196 } else { 197 $txt = eregi_replace((sql_regcase($key[0])), '<span style="background:' . $hicolor[$key[1]] . '" >\\0</span>', $txt); 198 } 199 } 200 } 201 return $txt; 202 } 203 204 function recode2regexp_utf8($input) { 205 $result = ''; 206 for ($i = 0, $i_cmp = strlen($input); $i < $i_cmp; ++$i) 207 switch ($input[$i]) { 208 case 'A': 209 case 'a': 210 $result .= '(a|A!|A¤|A?|A„)'; 211 break; 212 case 'C': 213 case 'c': 214 $result .= '(c|Ä?|ÄO)'; 215 break; 216 case 'D': 217 case 'd': 218 $result .= '(d|Ä?|ÄŽ)'; 219 break; 220 case 'E': 221 case 'e': 222 $result .= '(e|A©|Ä›|A‰|Äš)'; 223 break; 224 case 'I': 225 case 'i': 226 $result .= '(i|A|A?)'; 227 break; 228 case 'L': 229 case 'l': 230 $result .= '(l|Äo|Ä3|Ä1|Ä1)'; 231 break; 232 case 'N': 233 case 'n': 234 $result .= '(n|A^|A‡)'; 235 break; 236 case 'O': 237 case 'o': 238 $result .= '(o|A3|A´|A“|A”)'; 239 break; 240 case 'R': 241 case 'r': 242 $result .= '(r|A•|A™|A”|A~)'; 243 break; 244 case 'S': 245 case 's': 246 $result .= '(s|A!|A )'; 247 break; 248 case 'T': 249 case 't': 250 $result .= '(t|AY|A¤)'; 251 break; 252 case 'U': 253 case 'u': 254 $result .= '(u|Ao|A—|Aš|A®)'; 255 break; 256 case 'Y': 257 case 'y': 258 $result .= '(y|A1|A?)'; 259 break; 260 case 'Z': 261 case 'z': 262 $result .= '(z|A3|A1)'; 263 break; 264 default: 265 $result .= $input[$i]; 266 } 267 return $result; 268 } 269 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jan 8 03:00:03 2009 | Cross-referenced by PHPXref 0.7 |