1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
filterquery.php
См. документацию.
1<?php
2
4
8abstract class CAllFilterQuery
9{
10 var $cnt = 0;
14 var $m_kav;
17 var $error;
20 var $clob;
24
25 /*
26 $default_query_type - logic for spaces
27 $rus_bool_lang - use russian logic words
28 $ex_sep - array with exceptions for delimiters
29 */
30 public function __construct($default_query_type = "and", $rus_bool_lang = "yes", $procent="Y", $ex_sep = array(), $clob="N", $div_fields="Y", $clob_upper="N")
31 {
32 $this->m_query = "";
33 $this->m_fields = "";
34 $this->default_query_type = $default_query_type;
35 $this->rus_bool_lang = $rus_bool_lang;
36 $this->m_kav = array();
37 $this->error = "";
38 $this->procent = $procent;
39 $this->ex_sep = $ex_sep;
40 $this->clob = $clob;
41 $this->clob_upper = $clob_upper;
42 $this->div_fields = $div_fields;
43 }
44
45 abstract public function BuildWhereClause($word);
46
47 public function GetQueryString($fields, $query)
48 {
49 $this->m_words = array();
50 if($this->div_fields=="Y")
51 $this->m_fields = explode(",", $fields);
52 else
53 $this->m_fields = $fields;
54 if(!is_array($this->m_fields))
55 $this->m_fields=array($this->m_fields);
56
57 $query = $this->CutKav($query);
58 $query = $this->ParseQ($query);
59 if($query == "( )" || $query == '')
60 {
61 $this->error=GetMessage("FILTER_ERROR3");
62 $this->errorno=3;
63 return false;
64 }
65 $query = $this->PrepareQuery($query);
66
67 return $query;
68 }
69
70 public function CutKav($query)
71 {
72 $bdcnt = 0;
73 while (preg_match("/\"([^\"]*)\"/",$query,$pt))
74 {
75 $res = $pt[1];
76 if(trim($pt[1]) <> '')
77 {
78 $trimpt = $bdcnt."cut5";
79 $this->m_kav[$trimpt] = $res;
80 $query = str_replace("\"".$pt[1]."\"", " ".$trimpt." ", $query);
81 }
82 else
83 {
84 $query = str_replace("\"".$pt[1]."\"", " ", $query);
85 }
86 $bdcnt++;
87 if($bdcnt>100) break;
88 }
89
90 $bdcnt = 0;
91 while (preg_match("/'([^']*)'/",$query,$pt))
92 {
93 $res = $pt[1];
94 if(trim($pt[1]) <> '')
95 {
96 $trimpt = $bdcnt."cut6";
97 $this->m_kav[$trimpt] = $res;
98 $query = str_replace("'".$pt[1]."'", " ".$trimpt." ", $query);
99 }
100 else
101 {
102 $query = str_replace("'".$pt[1]."'", " ", $query);
103 }
104 $bdcnt++;
105 if($bdcnt>100) break;
106 }
107 return $query;
108 }
109
110 public function ParseQ($q)
111 {
112 $q = trim($q);
113 if($q == '')
114 return '';
115
116 $q=$this->ParseStr($q);
117
118 $q = str_replace(
119 array("&" , "|" , "~" , "(" , ")"),
120 array(" && ", " || ", " ! ", " ( ", " ) "),
121 $q
122 );
123 $q="( $q )";
124 $q = preg_replace("/\\s+/u", " ", $q);
125
126 return $q;
127 }
128
129 public function ParseStr($qwe)
130 {
131 $qwe=trim($qwe);
132
133 $qwe=preg_replace("/ {0,}\\+ {0,}/", "&", $qwe);
134
135 $qwe=preg_replace("/ {0,}([()|~]) {0,}/", "\\1", $qwe);
136
137 // default query type is and
138 if(mb_strtolower($this->default_query_type) == 'or')
139 $default_op = "|";
140 else
141 $default_op = "&";
142
143 $qwe=preg_replace("/( {1,}|\\&\\|{1,}|\\|\\&{1,})/", $default_op, $qwe);
144
145 // remove unnesessary boolean operators
146 $qwe=preg_replace("/\\|+/", "|", $qwe);
147 $qwe=preg_replace("/\\&+/", "&", $qwe);
148 $qwe=preg_replace("/\\~+/", "~", $qwe);
149 $qwe=preg_replace("/\\|\\&\\|/", "&", $qwe);
150 $qwe=preg_replace("/[|&~]+$/", "", $qwe);
151 $qwe=preg_replace("/^[|&]+/", "", $qwe);
152
153 // transform "w1 ~w2" -> "w1 default_op ~ w2"
154 // ") ~w" -> ") default_op ~w"
155 // "w ~ (" -> "w default_op ~("
156 // ") w" -> ") default_op w"
157 // "w (" -> "w default_op ("
158 // ")(" -> ") default_op ("
159
160 $qwe=preg_replace("/([^&~|()]+)~([^&~|()]+)/", "\\1".$default_op."~\\2", $qwe);
161 $qwe=preg_replace("/\\)~{1,}/", ")".$default_op."~", $qwe);
162 $qwe=preg_replace("/~{1,}\\(/", ($default_op=="|"? "~|(": "&~("), $qwe);
163 $qwe=preg_replace("/\\)([^&~|()]+)/", ")".$default_op."\\1", $qwe);
164 $qwe=preg_replace("/([^&~|()]+)\\(/", "\\1".$default_op."(", $qwe);
165 $qwe=preg_replace("/\\) *\\(/", ")".$default_op."(", $qwe);
166
167 // remove unnesessary boolean operators
168 $qwe=preg_replace("/\\|+/", "|", $qwe);
169 $qwe=preg_replace("/\\&+/", "&", $qwe);
170
171 // remove errornous format of query - ie: '(&', '&)', '(|', '|)', '~&', '~|', '~)'
172 $qwe=preg_replace("/\\(\\&{1,}/", "(", $qwe);
173 $qwe=preg_replace("/\\&{1,}\\)/", ")", $qwe);
174 $qwe=preg_replace("/\\~{1,}\\)/", ")", $qwe);
175 $qwe=preg_replace("/\\(\\|{1,}/", "(", $qwe);
176 $qwe=preg_replace("/\\|{1,}\\)/", ")", $qwe);
177 $qwe=preg_replace("/\\~{1,}\\&{1,}/", "&", $qwe);
178 $qwe=preg_replace("/\\~{1,}\\|{1,}/", "|", $qwe);
179
180 $qwe=preg_replace("/\\(\\)/", "", $qwe);
181 $qwe=preg_replace("/^[|&]{1,}/", "", $qwe);
182 $qwe=preg_replace("/[|&~]{1,}\$/", "", $qwe);
183 $qwe=preg_replace("/\\|\\&/", "&", $qwe);
184 $qwe=preg_replace("/\\&\\|/", "|", $qwe);
185
186 // remove unnesessary boolean operators
187 $qwe=preg_replace("/\\|+/", "|", $qwe);
188 $qwe=preg_replace("/\\&+/", "&", $qwe);
189
190 return($qwe);
191 }
192
193 public function PrepareQuery($q)
194 {
195 $state = 0;
196 $qu = '';
197 $n = 0;
198 $this->error = '';
199
200 foreach (preg_split('/ +/', $q) as $t)
201 {
202 if ($state)
203 {
204 if (($t === '||') || ($t === '&&'))
205 {
206 $state = 0;
207 $qu .= $t === '||' ? ' OR ' : ' AND ';
208 }
209 elseif ($t === ')')
210 {
211 $n--;
212 $qu .= ')';
213 }
214 else
215 {
216 $this->error = GetMessage('FILTER_ERROR2') . ' ' . $t;
217 $this->errorno = 2;
218 break;
219 }
220 }
221 else
222 {
223 if (($t === '||') || ($t === '&&') || ($t === ')'))
224 {
225 $this->error = GetMessage('FILTER_ERROR2') . ' ' . $t;
226 $this->errorno = 2;
227 }
228 elseif ($t === '!')
229 {
230 $qu .= ' NOT ';
231 }
232 elseif ($t === '(')
233 {
234 $n++;
235 $qu .= '(';
236 }
237 else
238 {
239 $state = 1;
240 $qu .= ' ' . $this->BuildWhereClause($t) . ' ';
241 }
242 }
243 }
244
245 if (($this->error === '') && ($n !== 0))
246 {
247 $this->error = GetMessage('FILTER_ERROR1');
248 $this->errorno = 1;
249 }
250
251 if ($this->error !== '')
252 {
253 return 0;
254 }
255
256 return $qu;
257 }
258}
259
261{
262 public function BuildWhereClause($word)
263 {
264 $this->cnt++;
265 //if($this->cnt>10) return "1=1";
266
267 global $DB;
268 if (isset($this->m_kav[$word]))
269 $word = $this->m_kav[$word];
270
271 $this->m_words[] = $word;
272
273 $n = count($this->m_fields);
274 $ret = "";
275 if ($n>1) $ret = "(";
276 for ($i=0; $i<$n; $i++)
277 {
278 $field = $this->m_fields[$i];
279 if ($this->procent=="Y")
280 {
281 $ret.= "
282 (upper($field) like upper('%".$DB->ForSqlLike($word, 2000)."%') and $field is not null)
283 ";
284 }
285 elseif (str_contains($word, "%") || str_contains($word, "_"))
286 {
287 $ret.= "
288 (upper($field) like upper('".$DB->ForSqlLike($word, 2000)."') and $field is not null)
289 ";
290 }
291 else
292 {
293 $ret.= "
294 ($field='".$DB->ForSql($word, 2000)."' and $field is not null)
295 ";
296
297 }
298 if ($i<>$n-1) $ret.= " OR ";
299 }
300 if ($n>1) $ret.= ")";
301 return $ret;
302 }
303}
Определения filterquery.php:9
$m_query
Определения filterquery.php:11
$ex_sep
Определения filterquery.php:19
BuildWhereClause($word)
GetQueryString($fields, $query)
Определения filterquery.php:47
$m_fields
Определения filterquery.php:13
CutKav($query)
Определения filterquery.php:70
__construct($default_query_type="and", $rus_bool_lang="yes", $procent="Y", $ex_sep=array(), $clob="N", $div_fields="Y", $clob_upper="N")
Определения filterquery.php:30
$rus_bool_lang
Определения filterquery.php:16
$procent
Определения filterquery.php:18
$cnt
Определения filterquery.php:10
$div_fields
Определения filterquery.php:21
ParseQ($q)
Определения filterquery.php:110
$errorno
Определения filterquery.php:23
$m_kav
Определения filterquery.php:14
$clob_upper
Определения filterquery.php:22
PrepareQuery($q)
Определения filterquery.php:193
ParseStr($qwe)
Определения filterquery.php:129
$default_query_type
Определения filterquery.php:15
$error
Определения filterquery.php:17
$clob
Определения filterquery.php:20
$m_words
Определения filterquery.php:12
Определения filterquery.php:261
BuildWhereClause($word)
Определения filterquery.php:262
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$query
Определения get_search.php:11
global $DB
Определения cron_frame.php:29
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$n
Определения update_log.php:107
$fields
Определения yandex_run.php:501