1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
catalog.php
См. документацию.
1<?php
7
9{
10 protected static $arCatalogCache = array();
11 protected static $catalogVatCache = array();
12
13 private static $disableCheckIblock = 0;
14
15 public static function CheckFields($ACTION, &$arFields, $ID = 0)
16 {
17 global $APPLICATION;
18
19 $arMsg = array();
20 $boolResult = true;
21
22 if (!is_array($arFields))
23 {
24 return false;
25 }
26 if (array_key_exists('OFFERS', $arFields))
27 {
28 unset($arFields['OFFERS']);
29 }
30
31 $defaultFields = [
32 'YANDEX_EXPORT' => 'N',
33 'SUBSCRIPTION' => 'N',
34 'VAT_ID' => 0,
35 'PRODUCT_IBLOCK_ID' => 0,
36 'SKU_PROPERTY_ID' => 0,
37 ];
38
39 $ID = (int)$ID;
40 $arCatalog = false;
41 if (0 < $ID)
43 if ($boolResult)
44 {
45 if (('UPDATE' == $ACTION) && (false == $arCatalog))
46 {
47 $boolResult = false;
48 $arMsg[] = array('id' => 'ID','text' => Loc::getMessage('BT_MOD_CATALOG_ERR_UPDATE_BAD_ID'));
49 }
50 }
51
52 if ($boolResult)
53 {
54 if ($ACTION === 'ADD')
55 {
56 $arFields = array_merge($defaultFields, $arFields);
57 }
58 if ('ADD' == $ACTION || is_set($arFields,'IBLOCK_ID'))
59 {
60 if (!is_set($arFields,'IBLOCK_ID'))
61 {
62 $arMsg[] = array('id' => 'IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_IBLOCK_ID_FIELD_ABSENT'));
63 $boolResult = false;
64 }
65 elseif((int)$arFields['IBLOCK_ID'] <= 0)
66 {
67 $arMsg[] = array('id' => 'IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_IBLOCK_ID_INVALID'));
68 $boolResult = false;
69 }
70 else
71 {
72 $arFields['IBLOCK_ID'] = (int)$arFields['IBLOCK_ID'];
73 $rsIBlocks = CIBlock::GetByID($arFields['IBLOCK_ID']);
74 if (!($arIBlock = $rsIBlocks->Fetch()))
75 {
76 $arMsg[] = array('id' => 'IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_IBLOCK_ID_ABSENT'));
77 $boolResult = false;
78 }
79 }
80 }
81 if ((is_set($arFields, "SUBSCRIPTION") || $ACTION=="ADD") && $arFields["SUBSCRIPTION"] != "Y")
82 $arFields["SUBSCRIPTION"] = "N";
83 if ((is_set($arFields, "YANDEX_EXPORT") || $ACTION=="ADD") && $arFields["YANDEX_EXPORT"] != "Y")
84 $arFields["YANDEX_EXPORT"] = "N";
85
86 if ((is_set($arFields,'VAT_ID') || ('ADD' == $ACTION)))
87 {
88 $arFields['VAT_ID'] = (int)$arFields['VAT_ID'];
89 if ($arFields['VAT_ID'] <= 0)
90 $arFields['VAT_ID'] = 0;
91 }
92 }
93
94 if ($boolResult)
95 {
96 if ('ADD' == $ACTION)
97 {
98 if (!is_set($arFields, "PRODUCT_IBLOCK_ID"))
99 {
100 $arFields["PRODUCT_IBLOCK_ID"] = 0;
101 }
102 elseif (0 > (int)$arFields["PRODUCT_IBLOCK_ID"])
103 {
104 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_INVALID'));
105 $arFields["PRODUCT_IBLOCK_ID"] = 0;
106 $boolResult = false;
107 }
108 elseif (0 < (int)$arFields["PRODUCT_IBLOCK_ID"])
109 {
110 $arFields["PRODUCT_IBLOCK_ID"] = (int)$arFields["PRODUCT_IBLOCK_ID"];
111 $rsIBlocks = CIBlock::GetByID($arFields['PRODUCT_IBLOCK_ID']);
112 if (!($arIBlock = $rsIBlocks->Fetch()))
113 {
114 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_ABSENT'));
115 $arFields["PRODUCT_IBLOCK_ID"] = 0;
116 $boolResult = false;
117 }
118 else
119 {
120 if ($arFields["PRODUCT_IBLOCK_ID"] == $arFields['IBLOCK_ID'])
121 {
122 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_SELF'));
123 $arFields["PRODUCT_IBLOCK_ID"] = 0;
124 $boolResult = false;
125 }
126 }
127 }
128 else
129 {
130 $arFields["PRODUCT_IBLOCK_ID"] = 0;
131 }
132
133 if (!is_set($arFields, "SKU_PROPERTY_ID"))
134 {
135 $arFields["SKU_PROPERTY_ID"] = 0;
136 }
137 elseif (0 > (int)$arFields["SKU_PROPERTY_ID"])
138 {
139 $arMsg[] = array('id' => 'SKU_PROPERTY_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_SKU_PROP_ID_INVALID'));
140 $arFields["SKU_PROPERTY_ID"] = 0;
141 $boolResult = false;
142 }
143 else
144 {
145 $arFields["SKU_PROPERTY_ID"] = (int)$arFields["SKU_PROPERTY_ID"];
146 }
147
148 if ((0 < $arFields["PRODUCT_IBLOCK_ID"]) && (0 == $arFields['SKU_PROPERTY_ID']))
149 {
150 $arMsg[] = array('id' => 'SKU_PROPERTY_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_WITHOUT_SKU_PROP'));
151 $boolResult = false;
152 }
153 elseif ((0 == $arFields["PRODUCT_IBLOCK_ID"]) && (0 < $arFields['SKU_PROPERTY_ID']))
154 {
155 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_SKU_PROP_WITHOUT_PRODUCT'));
156 $boolResult = false;
157 }
158 elseif ((0 < $arFields["PRODUCT_IBLOCK_ID"]) && (0 < $arFields['SKU_PROPERTY_ID']))
159 {
160 $rsProps = CIBlockProperty::GetList(array(),array('IBLOCK_ID' => $arFields['IBLOCK_ID'],'ID' => $arFields['SKU_PROPERTY_ID'],'ACTIVE' => 'Y'));
161 if ($arProp = $rsProps->Fetch())
162 {
163 if (('E' != $arProp['PROPERTY_TYPE']) || ($arFields["PRODUCT_IBLOCK_ID"] != $arProp['LINK_IBLOCK_ID']))
164 {
165 $arMsg[] = array('id' => 'SKU_PROPERTY_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_SKU_PROP_WITHOUT_PRODUCT'));
166 $boolResult = false;
167 }
168 }
169 else
170 {
171 $arMsg[] = array('id' => 'SKU_PROPERTY_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_SKU_PROP_NOT_FOUND'));
172 $boolResult = false;
173 }
174 }
175 }
176 elseif ('UPDATE' == $ACTION)
177 {
178 $boolLocalFlag = (is_set($arFields,'PRODUCT_IBLOCK_ID') == is_set($arFields,'SKU_PROPERTY_ID'));
179 if (!$boolLocalFlag)
180 {
181 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_AND_SKU_PROPERTY_ID_NEED'));
182 $boolResult = false;
183 }
184 else
185 {
186 if (is_set($arFields, 'PRODUCT_IBLOCK_ID'))
187 {
188 if (0 > (int)$arFields["PRODUCT_IBLOCK_ID"])
189 {
190 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_INVALID'));
191 $arFields["PRODUCT_IBLOCK_ID"] = 0;
192 $boolResult = false;
193 }
194 elseif (0 < (int)$arFields["PRODUCT_IBLOCK_ID"])
195 {
196 $arFields["PRODUCT_IBLOCK_ID"] = (int)$arFields["PRODUCT_IBLOCK_ID"];
197 $rsIBlocks = CIBlock::GetByID($arFields['PRODUCT_IBLOCK_ID']);
198 if (!($arIBlock = $rsIBlocks->Fetch()))
199 {
200 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_ABSENT'));
201 $arFields["PRODUCT_IBLOCK_ID"] = 0;
202 $boolResult = false;
203 }
204 else
205 {
206 if (0 < $ID && $arFields["PRODUCT_IBLOCK_ID"] == $ID)
207 {
208 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_SELF'));
209 $arFields["PRODUCT_IBLOCK_ID"] = 0;
210 $boolResult = false;
211 }
212 else
213 {
214 if (is_set($arFields, 'IBLOCK_ID') && $arFields["PRODUCT_IBLOCK_ID"] == $arFields['IBLOCK_ID'])
215 {
216 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_SELF'));
217 $arFields["PRODUCT_IBLOCK_ID"] = 0;
218 $boolResult = false;
219 }
220 }
221 }
222 }
223 }
224
225 if (is_set($arFields, 'SKU_PROPERTY_ID'))
226 {
227 if (0 > (int)$arFields["SKU_PROPERTY_ID"])
228 {
229 $arMsg[] = array('id' => 'SKU_PROPERTY_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_SKU_PROP_ID_INVALID'));
230 $arFields["SKU_PROPERTY_ID"] = 0;
231 $boolResult = false;
232 }
233 else
234 {
235 $arFields["SKU_PROPERTY_ID"] = (int)$arFields["SKU_PROPERTY_ID"];
236 }
237 }
238 if (is_set($arFields, 'PRODUCT_IBLOCK_ID') && is_set($arFields, 'SKU_PROPERTY_ID'))
239 {
240 if ((0 < $arFields["PRODUCT_IBLOCK_ID"]) && (0 == $arFields['SKU_PROPERTY_ID']))
241 {
242 $arMsg[] = array('id' => 'SKU_PROPERTY_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_WITHOUT_SKU_PROP'));
243 $boolResult = false;
244 }
245 elseif ((0 == $arFields["PRODUCT_IBLOCK_ID"]) && (0 < $arFields['SKU_PROPERTY_ID']))
246 {
247 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_SKU_PROP_WITHOUT_PRODUCT'));
248 $boolResult = false;
249 }
250 elseif ((0 < $arFields["PRODUCT_IBLOCK_ID"]) && (0 < $arFields['SKU_PROPERTY_ID']))
251 {
252 $rsProps = CIBlockProperty::GetList(array(),array('IBLOCK_ID' => $ID, 'ID' => $arFields['SKU_PROPERTY_ID'],'ACTIVE' => 'Y'));
253 if ($arProp = $rsProps->Fetch())
254 {
255 if (('E' != $arProp['PROPERTY_TYPE']) || ($arFields["PRODUCT_IBLOCK_ID"] != $arProp['LINK_IBLOCK_ID']))
256 {
257 $arMsg[] = array('id' => 'SKU_PROPERTY_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_SKU_PROP_WITHOUT_PRODUCT'));
258 $boolResult = false;
259 }
260 }
261 else
262 {
263 $arMsg[] = array('id' => 'SKU_PROPERTY_ID', "text" => Loc::getMessage('BT_MOD_CATALOG_ERR_SKU_PROP_NOT_FOUND'));
264 $boolResult = false;
265 }
266 }
267 }
268 }
269 }
270 }
271
272 if (!$boolResult)
273 {
274 $obError = new CAdminException($arMsg);
275 $APPLICATION->ResetException();
276 $APPLICATION->ThrowException($obError);
277 }
278 return $boolResult;
279 }
280
281 public static function GetByID($ID)
282 {
283 global $DB;
284
285 $ID = (int)$ID;
286 if ($ID <= 0)
287 return false;
288
289 if (isset(self::$arCatalogCache[$ID]))
290 {
291 return self::$arCatalogCache[$ID];
292 }
293 else
294 {
295 $strSql = "SELECT CI.*, I.ID as ID, I.IBLOCK_TYPE_ID, I.LID, I.NAME,
296 OFFERS.IBLOCK_ID OFFERS_IBLOCK_ID, OFFERS.SKU_PROPERTY_ID OFFERS_PROPERTY_ID
297 FROM
298 b_catalog_iblock CI INNER JOIN b_iblock I ON CI.IBLOCK_ID = I.ID
299 LEFT JOIN b_catalog_iblock OFFERS ON CI.IBLOCK_ID = OFFERS.PRODUCT_IBLOCK_ID
300 WHERE
301 CI.IBLOCK_ID = ".$ID;
302 $db_res = $DB->Query($strSql);
303 if ($res = $db_res->Fetch())
304 {
305 $res["OFFERS"] = $res["PRODUCT_IBLOCK_ID"] ? "Y": "N";
306 self::$arCatalogCache[$ID] = $res;
307 if (defined('CATALOG_GLOBAL_VARS') && 'Y' == CATALOG_GLOBAL_VARS)
308 {
309 global $CATALOG_CATALOG_CACHE;
310 $CATALOG_CATALOG_CACHE = self::$arCatalogCache;
311 }
312 return $res;
313 }
314 }
315 return false;
316 }
317
318 public static function GetFilterOperation($key)
319 {
321 'FIELD' => '',
322 'NEGATIVE' => 'N',
323 'OPERATION' => '',
324 'OR_NULL' => 'N'
325 );
326
327 static $arDoubleModify = array(
328 '>=' => '>=',
329 '<=' => '<='
330 );
331
332 static $arOneModify = array(
333 '>' => '>',
334 '<' => '<',
335 '@' => 'IN',
336 '~' => 'LIKE',
337 '%' => 'QUERY',
338 '=' => '='
339 );
340
341 $key = (string)$key;
342 if ($key == '')
343 return false;
344 if (0 == strncmp($key, '!', 1))
345 {
346 $arResult['NEGATIVE'] = 'Y';
347 $key = mb_substr($key, 1);
348 if ($key == '')
349 return false;
350 if (0 == strncmp($key, '+', 1))
351 {
352 $arResult['OR_NULL'] = 'Y';
353 $key = mb_substr($key, 1);
354 }
355 }
356 elseif (0 == strncmp($key, '+', 1))
357 {
358 $arResult['OR_NULL'] = 'Y';
359 $key = mb_substr($key, 1);
360 if ($key == '')
361 return false;
362 if (0 == strncmp($key, '!', 1))
363 {
364 $arResult['NEGATIVE'] = 'Y';
365 $key = mb_substr($key, 1);
366 }
367 }
368 if ($key == '')
369 return false;
370 $strKeyOp = mb_substr($key, 0, 2);
371 if ('' != $strKeyOp && isset($arDoubleModify[$strKeyOp]))
372 {
373 $arResult['OPERATION'] = $arDoubleModify[$strKeyOp];
374 $arResult['FIELD'] = mb_substr($key, 2);
375 return $arResult;
376 }
377 $strKeyOp = mb_substr($key, 0, 1);
378 if ('' != $strKeyOp && isset($arOneModify[$strKeyOp]))
379 {
380 $arResult['OPERATION'] = $arOneModify[$strKeyOp];
381 $arResult['FIELD'] = mb_substr($key, 1);
382 return $arResult;
383 }
384 $arResult['OPERATION'] = '=';
385 $arResult['FIELD'] = $key;
386 return $arResult;
387 }
388
389 public static function PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields)
390 {
391 global $DB;
392
393 $strSqlSelect = '';
394 $strSqlFrom = '';
395 $strSqlWhere = '';
396 $strSqlGroupBy = '';
397 $strSqlOrderBy = '';
398
399 $sqlGroupByList = array();
400 $sqlFrom = array();
401 $sqlSelect = array();
402
403 reset($arFields);
404 $firstField = current($arFields);
405
406 $strDBType = $DB->type;
407 $oracleEdition = ('ORACLE' == $strDBType);
408
409 $arGroupByFunct = array(
410 "COUNT" => true,
411 "AVG" => true,
412 "MIN" => true,
413 "MAX" => true,
414 "SUM" => true
415 );
416
417 // GROUP BY -->
418 if (!empty($arGroupBy) && is_array($arGroupBy))
419 {
420 $arSelectFields = $arGroupBy;
421 foreach ($arGroupBy as $key => $val)
422 {
423 $val = mb_strtoupper($val);
424 $key = mb_strtoupper($key);
425 if (isset($arFields[$val]) && !isset($arGroupByFunct[$key]))
426 {
427 $sqlGroupByList[] = $arFields[$val]["FIELD"];
428 if (isset($arFields[$val]["FROM"]) && !empty($arFields[$val]["FROM"]))
429 {
430 $sqlFrom[$arFields[$val]["FROM"]] = true;
431 }
432 }
433 }
434 }
435 if (!empty($sqlGroupByList))
436 $strSqlGroupBy = implode(', ', $sqlGroupByList);
437 // <-- GROUP BY
438
439 // SELECT -->
440 if (empty($arGroupBy) && is_array($arGroupBy))
441 {
442 $sqlSelect[] = 'COUNT(%%_DISTINCT_%% '.$firstField['FIELD'].') as CNT';
443 }
444 else
445 {
446 if (isset($arSelectFields) && !is_array($arSelectFields))
447 {
448 $arSelectFields = array($arSelectFields);
449 }
450 if (!empty($arSelectFields) && is_array($arSelectFields) && !in_array('*', $arSelectFields))
451 {
452 $arClearFields = array();
453 foreach ($arSelectFields as $key => $val)
454 {
455 if (isset($arFields[$val]))
456 {
457 $arClearFields[$key] = $val;
458 }
459 }
460 $arSelectFields = $arClearFields;
461 }
462
463 if (
464 empty($arSelectFields)
465 || !is_array($arSelectFields)
466 || in_array("*", $arSelectFields)
467 )
468 {
469 foreach ($arFields as $fieldKey => $fieldDescr)
470 {
471 if (isset($fieldDescr['WHERE_ONLY']) && 'Y' == $fieldDescr['WHERE_ONLY'])
472 {
473 continue;
474 }
475 switch ($fieldDescr['TYPE'])
476 {
477 case 'datetime':
478 case 'date':
479 if (isset($arOrder[$fieldKey]))
480 $sqlSelect[] = $fieldDescr['FIELD'].' as '.$fieldKey.'_X1';
481
482 $sqlSelect[] = $DB->DateToCharFunction(
483 $fieldDescr['FIELD'],
484 ('datetime' == $fieldDescr['TYPE'] ? 'FULL' : 'SHORT')
485 ).' as '.$fieldKey;
486 break;
487 default:
488 $sqlSelect[] = $fieldDescr['FIELD'].' as '.$fieldKey;
489 break;
490 }
491 if (isset($fieldDescr['FROM']) && !empty($fieldDescr['FROM']))
492 {
493 $sqlFrom[$fieldDescr['FROM']] = true;
494 }
495 }
496 }
497 else
498 {
499 foreach ($arSelectFields as $key => $val)
500 {
501 $val = mb_strtoupper($val);
502 $key = mb_strtoupper($key);
503 if (isset($arFields[$val]))
504 {
505 if (isset($arGroupByFunct[$key]))
506 {
507 $sqlSelect[] = $key.'('.$arFields[$val]['FIELD'].') as '.$val;
508 }
509 else
510 {
511 switch ($arFields[$val]['TYPE'])
512 {
513 case 'datetime':
514 case 'date':
515 if (isset($arOrder[$val]))
516 $sqlSelect[] = $arFields[$val]['FIELD'].' as '.$val.'_X1';
517
518 $sqlSelect[] = $DB->DateToCharFunction(
519 $arFields[$val]['FIELD'],
520 ('datetime' == $arFields[$val]['TYPE'] ? 'FULL' : 'SHORT')
521 ).' as '.$val;
522 break;
523 default:
524 $sqlSelect[] = $arFields[$val]['FIELD'].' as '.$val;
525 break;
526 }
527 }
528 if (isset($arFields[$val]['FROM']) && !empty($arFields[$val]['FROM']))
529 {
530 $sqlFrom[$arFields[$val]['FROM']] = true;
531 }
532 }
533 }
534 }
535
536 if (!empty($sqlGroupByList))
537 {
538 $sqlSelect[] = 'COUNT(%%_DISTINCT_%% '.$firstField['FIELD'].') as CNT';
539 }
540 else
541 {
542 $sqlSelect[0] = '%%_DISTINCT_%% '.$sqlSelect[0];
543 }
544 }
545 // <-- SELECT
546
547 // WHERE -->
548 $arSqlSearch = array();
549
550 $filter_keys = (!is_array($arFilter) ? array() : array_keys($arFilter));
551
552 for ($i = 0, $intCount = count($filter_keys); $i < $intCount; $i++)
553 {
554 $vals = $arFilter[$filter_keys[$i]];
555 $vals = (!is_array($vals) ? array($vals) : array_values($vals));
556
557 $key = $filter_keys[$i];
559 if (empty($key_res))
560 continue;
561 $key = $key_res["FIELD"];
562 $strNegative = $key_res["NEGATIVE"];
563 $strOperation = $key_res["OPERATION"];
564 $strOrNull = $key_res["OR_NULL"];
565
566 if ('' != $key && isset($arFields[$key]))
567 {
568 $arSqlSearch_tmp = array();
569
570 if (!empty($vals))
571 {
572 if ($strOperation == "IN")
573 {
574 if (isset($arFields[$key]["WHERE"]))
575 {
576 $arSqlSearch_tmp1 = call_user_func_array(
577 $arFields[$key]["WHERE"],
578 array($vals, $key, $strOperation, $strNegative, $arFields[$key]["FIELD"], &$arFields, &$arFilter)
579 );
580 if ($arSqlSearch_tmp1 !== false)
581 $arSqlSearch_tmp[] = $arSqlSearch_tmp1;
582 }
583 else
584 {
585 if ($arFields[$key]['TYPE'] == 'int')
586 {
587 $clearVals = array();
588 foreach ($vals as $item)
589 {
590 $item = (int)$item;
591 $clearVals[$item] = $item;
592 }
593 unset($item);
594 if (empty($clearVals))
595 $arSqlSearch_tmp[] = '(1 = 2)';
596 else
597 $arSqlSearch_tmp[] = ($strNegative == 'Y' ? ' NOT ' : '').'('.$arFields[$key]['FIELD'].' IN ('.implode(',', $clearVals).'))';
598 unset($clearVals);
599 }
600 elseif ($arFields[$key]['TYPE'] == 'double')
601 {
602 $clearVals = array();
603 foreach ($vals as $item)
604 $clearVals[] = (float)$item;
605 unset($item);
606 if (empty($clearVals))
607 {
608 $arSqlSearch_tmp[] = '(1 = 2)';
609 }
610 else
611 {
612 $clearVals = array_unique($clearVals);
613 $arSqlSearch_tmp[] = ($strNegative == 'Y' ? ' NOT ' : '').'('.$arFields[$key]['FIELD'].' IN ('.implode(',', $clearVals).'))';
614 }
615 unset($clearVals);
616 }
617 elseif ($arFields[$key]['TYPE'] == 'string' || $arFields[$key]['TYPE'] == 'char')
618 {
619 $clearVals = array();
620 foreach ($vals as $item)
621 $clearVals[] = "'".$DB->ForSql($item)."'";
622 unset($item);
623 if (empty($clearVals))
624 {
625 $arSqlSearch_tmp[] = '(1 = 2)';
626 }
627 else
628 {
629 $clearVals = array_unique($clearVals);
630 $arSqlSearch_tmp[] = (($strNegative == 'Y') ? ' NOT ' : '').'('.$arFields[$key]['FIELD'].' '.$strOperation.' ('.implode(',', $clearVals).'))';
631 }
632 unset($clearVals);
633 }
634 elseif ($arFields[$key]['TYPE'] == 'datetime' || $arFields[$key]['TYPE'] == 'date')
635 {
636 $valueFormat = ($arFields[$key]['TYPE'] == 'datetime' ? 'FULL' : 'SHORT');
637 $clearVals = array();
638 foreach ($vals as $item)
639 $clearVals[] = $DB->CharToDateFunction($item, $valueFormat);
640 unset($item);
641 if (empty($clearVals))
642 {
643 $arSqlSearch_tmp[] = '(1 = 2)';
644 }
645 else
646 {
647 $clearVals = array_unique($clearVals);
648 $arSqlSearch_tmp[] = ($strNegative == 'Y'? ' NOT ' : '').'('.$arFields[$key]['FIELD'].' '.$strOperation.' ('.implode(',', $clearVals).'))';
649 }
650 unset($clearVals, $valueFormat);
651 }
652 }
653 }
654 else
655 {
656 for ($j = 0, $intCountVals = count($vals); $j < $intCountVals; $j++)
657 {
658 $val = $vals[$j];
659
660 if (isset($arFields[$key]["WHERE"]))
661 {
662 $arSqlSearch_tmp1 = call_user_func_array(
663 $arFields[$key]["WHERE"],
664 array($val, $key, $strOperation, $strNegative, $arFields[$key]["FIELD"], &$arFields, &$arFilter)
665 );
666 if ($arSqlSearch_tmp1 !== false)
667 $arSqlSearch_tmp[] = $arSqlSearch_tmp1;
668 }
669 else
670 {
671 if ($arFields[$key]["TYPE"] == "int")
672 {
673 if ((int)$val == 0 && mb_strpos($strOperation, "=") !== false)
674 $arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND" : "OR")." ".(($strNegative == "Y") ? "NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." 0)";
675 else
676 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".(int)$val." )";
677 }
678 elseif ($arFields[$key]["TYPE"] == "double")
679 {
680 $val = str_replace(",", ".", $val);
681
682 if ((float)$val == 0 && mb_strpos($strOperation, "=") !== false)
683 $arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND" : "OR")." ".(($strNegative == "Y") ? "NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." 0)";
684 else
685 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".(float)$val." )";
686 }
687 elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
688 {
689 if ($strOperation == "QUERY")
690 {
691 $arSqlSearch_tmp[] = GetFilterQuery($arFields[$key]["FIELD"], $val, "Y");
692 }
693 else
694 {
695 if (($val == '') && (mb_strpos($strOperation, "=") !== false))
696 $arSqlSearch_tmp[] = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND NOT" : "OR")." (".$DB->Length($arFields[$key]["FIELD"])." <= 0) ".(($strNegative == "Y") ? "AND NOT" : "OR")." (".$arFields[$key]["FIELD"]." ".$strOperation." '".$DB->ForSql($val)."' )";
697 else
698 $arSqlSearch_tmp[] = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." '".$DB->ForSql($val)."' )";
699 }
700 }
701 elseif ($arFields[$key]["TYPE"] == "datetime")
702 {
703 if ($val == '')
704 $arSqlSearch_tmp[] = ($strNegative=="Y"?"NOT":"")."(".$arFields[$key]["FIELD"]." IS NULL)";
705 else
706 $arSqlSearch_tmp[] = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->CharToDateFunction($val, "FULL").")";
707 }
708 elseif ($arFields[$key]["TYPE"] == "date")
709 {
710 if ($val == '')
711 $arSqlSearch_tmp[] = ($strNegative=="Y"?"NOT":"")."(".$arFields[$key]["FIELD"]." IS NULL)";
712 else
713 $arSqlSearch_tmp[] = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->CharToDateFunction($val, "SHORT").")";
714 }
715 }
716 }
717 }
718 }
719
720 if (isset($arFields[$key]["FROM"]) && !empty($arFields[$key]["FROM"]))
721 {
722 $sqlFrom[$arFields[$key]["FROM"]] = true;
723 }
724
725 $strSqlSearch_tmp = "";
726 for ($j = 0, $intCountSearch = count($arSqlSearch_tmp); $j < $intCountSearch; $j++)
727 {
728 if ($j > 0)
729 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
730 $strSqlSearch_tmp .= "(".$arSqlSearch_tmp[$j].")";
731 }
732 if ($strOrNull == "Y")
733 {
734 if ($strSqlSearch_tmp <> '')
735 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
736 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." IS ".($strNegative=="Y" ? "NOT " : "")."NULL)";
737
738 if ($arFields[$key]["TYPE"] == "int" || $arFields[$key]["TYPE"] == "double")
739 {
740 if ($strSqlSearch_tmp <> '')
741 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
742 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." 0)";
743 }
744 elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
745 {
746 if ($strSqlSearch_tmp <> '')
747 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
748 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." '')";
749 }
750 }
751
752 if ($strSqlSearch_tmp != "")
753 $arSqlSearch[] = "(".$strSqlSearch_tmp.")";
754 }
755 }
756
757 if (!empty($arSqlSearch))
758 $strSqlWhere = '('.implode(') and (', $arSqlSearch).')';
759 // <-- WHERE
760
761 // ORDER BY -->
762 $arSqlOrder = array();
763 $sortExist = array();
764 foreach ($arOrder as $by => $order)
765 {
766 $by = mb_strtoupper($by);
767 $order = mb_strtoupper($order);
768
769 if ($order != 'ASC')
770 $order = 'DESC';
771 if ($oracleEdition)
772 {
773 $order .= ($order == 'ASC' ? ' NULLS FIRST' : ' NULLS LAST');
774 }
775
776 if (isset($arFields[$by]))
777 {
778 if (isset($sortExist[$by]))
779 continue;
780 $sortExist[$by] = true;
781 $arSqlOrder[] = $arFields[$by]["FIELD"].' '.$order;
782 if (isset($arFields[$by]["FROM"]) && !empty($arFields[$by]["FROM"]))
783 {
784 $sqlFrom[$arFields[$by]["FROM"]] = true;
785 }
786 }
787 }
788 if (!empty($arSqlOrder))
789 {
790 $strSqlOrderBy = implode (', ', $arSqlOrder);
791 }
792 // <-- ORDER BY
793
794 $sqlFromTables = array();
795 if (!empty($sqlFrom))
796 {
797 $sqlFromTables = array_keys($sqlFrom);
798 $strSqlFrom = implode(' ', $sqlFromTables);
799 }
800
801 if (!empty($sqlSelect))
802 {
803 $strSqlSelect = implode(', ', $sqlSelect);
804 }
805
806 return array(
807 'SELECT' => $strSqlSelect,
808 'FROM' => $strSqlFrom,
809 'WHERE' => $strSqlWhere,
810 'GROUPBY' => $strSqlGroupBy,
811 'ORDERBY' => $strSqlOrderBy,
812 'SELECT_FIELDS' => $sqlSelect,
813 'FROM_TABLES' => $sqlFromTables,
814 'GROUPBY_FIELDS' => $sqlGroupByList,
815 'ORDERBY_FIELDS' => array_keys($sortExist)
816 );
817 }
818
819 public static function _PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields)
820 {
821 global $DB;
822
823 $strSqlSelect = "";
824 $strSqlFrom = "";
825 $strSqlWhere = "";
826 $strSqlGroupBy = "";
827
828 $sqlGroupByList = array();
829
830 $strDBType = $DB->type;
831 $oracleEdition = ('ORACLE' == $strDBType);
832
833 $arGroupByFunct = array(
834 "COUNT" => true,
835 "AVG" => true,
836 "MIN" => true,
837 "MAX" => true,
838 "SUM" => true
839 );
840
841 $arAlreadyJoined = array();
842
843 // GROUP BY -->
844 if (!empty($arGroupBy) && is_array($arGroupBy))
845 {
846 foreach ($arGroupBy as $key => $val)
847 {
848 $val = mb_strtoupper($val);
849 $key = mb_strtoupper($key);
850 if (isset($arFields[$val]) && !isset($arGroupByFunct[$key]))
851 {
852 $sqlGroupByList[] = $arFields[$val]["FIELD"];
853
854 if (isset($arFields[$val]["FROM"])
855 && $arFields[$val]["FROM"] <> ''
856 && !in_array($arFields[$val]["FROM"], $arAlreadyJoined))
857 {
858 if ($strSqlFrom <> '')
859 $strSqlFrom .= " ";
860 $strSqlFrom .= $arFields[$val]["FROM"];
861 $arAlreadyJoined[] = $arFields[$val]["FROM"];
862 }
863 }
864 }
865 }
866 if (!empty($sqlGroupByList))
867 $strSqlGroupBy = implode(', ', $sqlGroupByList);
868 // <-- GROUP BY
869
870 // SELECT -->
871 $arFieldsKeys = array_keys($arFields);
872
873 if (empty($arGroupBy) && is_array($arGroupBy))
874 {
875 $strSqlSelect = "COUNT(%%_DISTINCT_%% ".$arFields[$arFieldsKeys[0]]["FIELD"].") as CNT ";
876 }
877 else
878 {
879 if (
880 isset($arSelectFields)
881 && is_string($arSelectFields)
882 && '' != $arSelectFields
883 && isset($arFields[$arSelectFields])
884 )
885 {
886 $arSelectFields = array($arSelectFields);
887 }
888
889 if (empty($arSelectFields)
890 || !is_array($arSelectFields)
891 || in_array("*", $arSelectFields))
892 {
893 for ($i = 0, $intCount = count($arFieldsKeys); $i < $intCount; $i++)
894 {
895 if (isset($arFields[$arFieldsKeys[$i]]["WHERE_ONLY"])
896 && $arFields[$arFieldsKeys[$i]]["WHERE_ONLY"] == "Y")
897 {
898 continue;
899 }
900
901 if ('' != $strSqlSelect)
902 $strSqlSelect .= ", ";
903
904 if ($arFields[$arFieldsKeys[$i]]["TYPE"] == "datetime")
905 {
906 if (isset($arOrder[$arFieldsKeys[$i]]))
907 $strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"]." as ".$arFieldsKeys[$i]."_X1, ";
908
909 $strSqlSelect .= $DB->DateToCharFunction($arFields[$arFieldsKeys[$i]]["FIELD"], "FULL")." as ".$arFieldsKeys[$i];
910 }
911 elseif ($arFields[$arFieldsKeys[$i]]["TYPE"] == "date")
912 {
913 if (isset($arOrder[$arFieldsKeys[$i]]))
914 $strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"]." as ".$arFieldsKeys[$i]."_X1, ";
915
916 $strSqlSelect .= $DB->DateToCharFunction($arFields[$arFieldsKeys[$i]]["FIELD"], "SHORT")." as ".$arFieldsKeys[$i];
917 }
918 else
919 $strSqlSelect .= $arFields[$arFieldsKeys[$i]]["FIELD"]." as ".$arFieldsKeys[$i];
920
921 if (isset($arFields[$arFieldsKeys[$i]]["FROM"])
922 && $arFields[$arFieldsKeys[$i]]["FROM"] <> ''
923 && !in_array($arFields[$arFieldsKeys[$i]]["FROM"], $arAlreadyJoined))
924 {
925 if ($strSqlFrom <> '')
926 $strSqlFrom .= " ";
927 $strSqlFrom .= $arFields[$arFieldsKeys[$i]]["FROM"];
928 $arAlreadyJoined[] = $arFields[$arFieldsKeys[$i]]["FROM"];
929 }
930 }
931 }
932 else
933 {
934 foreach ($arSelectFields as $key => $val)
935 {
936 $val = mb_strtoupper($val);
937 $key = mb_strtoupper($key);
938 if (isset($arFields[$val]))
939 {
940 if ('' != $strSqlSelect)
941 $strSqlSelect .= ", ";
942
943 if (isset($arGroupByFunct[$key]))
944 {
945 $strSqlSelect .= $key."(".$arFields[$val]["FIELD"].") as ".$val;
946 }
947 else
948 {
949 if ($arFields[$val]["TYPE"] == "datetime")
950 {
951 if (isset($arOrder[$val]))
952 $strSqlSelect .= $arFields[$val]["FIELD"]." as ".$val."_X1, ";
953
954 $strSqlSelect .= $DB->DateToCharFunction($arFields[$val]["FIELD"], "FULL")." as ".$val;
955 }
956 elseif ($arFields[$val]["TYPE"] == "date")
957 {
958 if (isset($arOrder[$val]))
959 $strSqlSelect .= $arFields[$val]["FIELD"]." as ".$val."_X1, ";
960
961 $strSqlSelect .= $DB->DateToCharFunction($arFields[$val]["FIELD"], "SHORT")." as ".$val;
962 }
963 else
964 $strSqlSelect .= $arFields[$val]["FIELD"]." as ".$val;
965 }
966
967 if (isset($arFields[$val]["FROM"])
968 && $arFields[$val]["FROM"] <> ''
969 && !in_array($arFields[$val]["FROM"], $arAlreadyJoined))
970 {
971 if ('' != $strSqlFrom)
972 $strSqlFrom .= " ";
973 $strSqlFrom .= $arFields[$val]["FROM"];
974 $arAlreadyJoined[] = $arFields[$val]["FROM"];
975 }
976 }
977 }
978 }
979
980 if ('' != $strSqlGroupBy)
981 {
982 if ('' != $strSqlSelect)
983 $strSqlSelect .= ", ";
984 $strSqlSelect .= "COUNT(%%_DISTINCT_%% ".$arFields[$arFieldsKeys[0]]["FIELD"].") as CNT";
985 }
986 else
987 {
988 $strSqlSelect = "%%_DISTINCT_%% ".$strSqlSelect;
989 }
990 }
991 // <-- SELECT
992
993 // WHERE -->
994 $arSqlSearch = Array();
995 $arSqlHaving = Array();
996
997 $filter_keys = (!is_array($arFilter) ? array() : array_keys($arFilter));
998
999 for ($i = 0, $intCount = count($filter_keys); $i < $intCount; $i++)
1000 {
1001 $vals = $arFilter[$filter_keys[$i]];
1002 $vals = (!is_array($vals) ? array($vals) : array_values($vals));
1003
1004 $key = $filter_keys[$i];
1006 if (empty($key_res))
1007 continue;
1008 $key = $key_res["FIELD"];
1009 $strNegative = $key_res["NEGATIVE"];
1010 $strOperation = $key_res["OPERATION"];
1011 $strOrNull = $key_res["OR_NULL"];
1012
1013 if ('' != $key && isset($arFields[$key]))
1014 {
1015 $arSqlSearch_tmp = array();
1016 $arSqlHaving_tmp = array();
1017 for ($j = 0, $intCountVals = count($vals); $j < $intCountVals; $j++)
1018 {
1019 $val = $vals[$j];
1020
1021 if (isset($arFields[$key]["WHERE"]))
1022 {
1023 $arSqlSearch_tmp1 = call_user_func_array(
1024 $arFields[$key]["WHERE"],
1025 array($val, $key, $strOperation, $strNegative, $arFields[$key]["FIELD"], &$arFields, &$arFilter)
1026 );
1027 if ($arSqlSearch_tmp1 !== false)
1028 {
1029 if (isset($arFields[$key]["GROUPED"]) && $arFields[$key]["GROUPED"] == "Y")
1030 $arSqlHaving_tmp[] = $arSqlSearch_tmp1;
1031 else
1032 $arSqlSearch_tmp[] = $arSqlSearch_tmp1;
1033 }
1034 }
1035 else
1036 {
1037 $arSqlSearch_tmp1 = "";
1038
1039 if ($arFields[$key]["TYPE"] == "int")
1040 {
1041 if ((int)$val == 0 && mb_strpos($strOperation, "=") !== false)
1042 $arSqlSearch_tmp1 = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND" : "OR")." ".(($strNegative == "Y") ? "NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." 0)";
1043 else
1044 $arSqlSearch_tmp1 = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".(int)$val." )";
1045 }
1046 elseif ($arFields[$key]["TYPE"] == "double")
1047 {
1048 $val = str_replace(",", ".", $val);
1049
1050 if ((float)$val == 0 && mb_strpos($strOperation, "=") !== false)
1051 $arSqlSearch_tmp1 = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND" : "OR")." ".(($strNegative == "Y") ? "NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." 0)";
1052 else
1053 $arSqlSearch_tmp1 = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".(float)$val." )";
1054 }
1055 elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
1056 {
1057 if ($strOperation == "QUERY")
1058 {
1059 $arSqlSearch_tmp1 = GetFilterQuery($arFields[$key]["FIELD"], $val, "Y");
1060 }
1061 else
1062 {
1063 if (($val == '') && (mb_strpos($strOperation, "=") !== false))
1064 $arSqlSearch_tmp1 = "(".$arFields[$key]["FIELD"]." IS ".(($strNegative == "Y") ? "NOT " : "")."NULL) ".(($strNegative == "Y") ? "AND NOT" : "OR")." (".$DB->Length($arFields[$key]["FIELD"])." <= 0) ".(($strNegative == "Y") ? "AND NOT" : "OR")." (".$arFields[$key]["FIELD"]." ".$strOperation." '".$DB->ForSql($val)."' )";
1065 else
1066 $arSqlSearch_tmp1 = (($strNegative == "Y") ? " ".$arFields[$key]["FIELD"]." IS NULL OR NOT " : "")."(".$arFields[$key]["FIELD"]." ".$strOperation." '".$DB->ForSql($val)."' )";
1067 }
1068 }
1069 elseif ($arFields[$key]["TYPE"] == "datetime")
1070 {
1071 if ($val == '')
1072 $arSqlSearch_tmp1 = ($strNegative=="Y"?"NOT":"")."(".$arFields[$key]["FIELD"]." IS NULL)";
1073 else
1074 $arSqlSearch_tmp1 = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->CharToDateFunction($val, "FULL").")";
1075 }
1076 elseif ($arFields[$key]["TYPE"] == "date")
1077 {
1078 if ($val == '')
1079 $arSqlSearch_tmp1 = ($strNegative=="Y"?"NOT":"")."(".$arFields[$key]["FIELD"]." IS NULL)";
1080 else
1081 $arSqlSearch_tmp1 = ($strNegative=="Y"?" ".$arFields[$key]["FIELD"]." IS NULL OR NOT ":"")."(".$arFields[$key]["FIELD"]." ".$strOperation." ".$DB->CharToDateFunction($val, "SHORT").")";
1082 }
1083
1084 if (isset($arFields[$key]["GROUPED"]) && $arFields[$key]["GROUPED"] == "Y")
1085 $arSqlHaving_tmp[] = $arSqlSearch_tmp1;
1086 else
1087 $arSqlSearch_tmp[] = $arSqlSearch_tmp1;
1088 }
1089 }
1090
1091 if (isset($arFields[$key]["FROM"])
1092 && $arFields[$key]["FROM"] <> ''
1093 && !in_array($arFields[$key]["FROM"], $arAlreadyJoined))
1094 {
1095 if ($strSqlFrom <> '')
1096 $strSqlFrom .= " ";
1097 $strSqlFrom .= $arFields[$key]["FROM"];
1098 $arAlreadyJoined[] = $arFields[$key]["FROM"];
1099 }
1100
1101 $strSqlSearch_tmp = "";
1102 for ($j = 0, $intCountSearchTmp = count($arSqlSearch_tmp); $j < $intCountSearchTmp; $j++)
1103 {
1104 if ($j > 0)
1105 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
1106 $strSqlSearch_tmp .= "(".$arSqlSearch_tmp[$j].")";
1107 }
1108 if ($strOrNull == "Y")
1109 {
1110 if ($strSqlSearch_tmp <> '')
1111 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
1112 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." IS ".($strNegative=="Y" ? "NOT " : "")."NULL)";
1113
1114 if ($arFields[$key]["TYPE"] == "int" || $arFields[$key]["TYPE"] == "double")
1115 {
1116 if ($strSqlSearch_tmp <> '')
1117 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
1118 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." 0)";
1119 }
1120 elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
1121 {
1122 if ($strSqlSearch_tmp <> '')
1123 $strSqlSearch_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
1124 $strSqlSearch_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." '')";
1125 }
1126 }
1127
1128 if ($strSqlSearch_tmp != "")
1129 $arSqlSearch[] = "(".$strSqlSearch_tmp.")";
1130
1131 $strSqlHaving_tmp = "";
1132 for ($j = 0, $intCountHavingTmp = count($arSqlHaving_tmp); $j < $intCountHavingTmp; $j++)
1133 {
1134 if ($j > 0)
1135 $strSqlHaving_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
1136 $strSqlHaving_tmp .= "(".$arSqlHaving_tmp[$j].")";
1137 }
1138 if ($strOrNull == "Y")
1139 {
1140 if ($strSqlHaving_tmp <> '')
1141 $strSqlHaving_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
1142 $strSqlHaving_tmp .= "(".$arFields[$key]["FIELD"]." IS ".($strNegative=="Y" ? "NOT " : "")."NULL)";
1143
1144 if ($strSqlHaving_tmp <> '')
1145 $strSqlHaving_tmp .= ($strNegative=="Y" ? " AND " : " OR ");
1146 if ($arFields[$key]["TYPE"] == "int" || $arFields[$key]["TYPE"] == "double")
1147 $strSqlHaving_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." 0)";
1148 elseif ($arFields[$key]["TYPE"] == "string" || $arFields[$key]["TYPE"] == "char")
1149 $strSqlHaving_tmp .= "(".$arFields[$key]["FIELD"]." ".($strNegative=="Y" ? "<>" : "=")." '')";
1150 }
1151
1152 if ($strSqlHaving_tmp != "")
1153 $arSqlHaving[] = "(".$strSqlHaving_tmp.")";
1154 }
1155 }
1156
1157 if (!empty($arSqlSearch))
1158 $strSqlWhere = '('.implode(') and (', $arSqlSearch).')';
1159
1160 $strSqlHaving = '';
1161 if (!empty($arSqlHaving))
1162 $strSqlHaving = '('.implode(') and (', $arSqlHaving).')';
1163 // <-- WHERE
1164
1165 // ORDER BY -->
1166 $arSqlOrder = array();
1167 foreach ($arOrder as $by => $order)
1168 {
1169 $by = mb_strtoupper($by);
1170 $order = mb_strtoupper($order);
1171
1172 if ($order != "ASC")
1173 $order = "DESC".($oracleEdition ? " NULLS LAST" : "");
1174 else
1175 $order = "ASC".($oracleEdition ? " NULLS FIRST" : "");
1176
1177 if (isset($arFields[$by]))
1178 {
1179 $arSqlOrder[] = " ".$arFields[$by]["FIELD"]." ".$order." ";
1180
1181 if (isset($arFields[$by]["FROM"])
1182 && $arFields[$by]["FROM"] <> ''
1183 && !in_array($arFields[$by]["FROM"], $arAlreadyJoined))
1184 {
1185 if ($strSqlFrom <> '')
1186 $strSqlFrom .= " ";
1187 $strSqlFrom .= $arFields[$by]["FROM"];
1188 $arAlreadyJoined[] = $arFields[$by]["FROM"];
1189 }
1190 }
1191 }
1192
1193 $strSqlOrder = '';
1194 DelDuplicateSort($arSqlOrder);
1195 if (!empty($arSqlOrder))
1196 $strSqlOrder = implode(', ', $arSqlOrder);
1197 // <-- ORDER BY
1198
1199 return array(
1200 "SELECT" => $strSqlSelect,
1201 "FROM" => $strSqlFrom,
1202 "WHERE" => $strSqlWhere,
1203 "GROUPBY" => $strSqlGroupBy,
1204 "ORDERBY" => $strSqlOrder,
1205 "HAVING" => $strSqlHaving
1206 );
1207 }
1208
1209 public static function Add($arFields)
1210 {
1211 global $DB;
1212
1213 if (!CCatalog::CheckFields("ADD", $arFields, 0))
1214 return false;
1215
1216 $arInsert = $DB->PrepareInsert("b_catalog_iblock", $arFields);
1217
1218 $strSql = "INSERT INTO b_catalog_iblock(".$arInsert[0].") VALUES(".$arInsert[1].")";
1219 $DB->Query($strSql);
1220
1221 CCatalogSku::ClearCache();
1223
1224 return true;
1225 }
1226
1227 public static function Update($ID, $arFields)
1228 {
1229 global $DB;
1230 $ID = (int)$ID;
1231
1232 if (!CCatalog::CheckFields("UPDATE", $arFields, $ID))
1233 return false;
1234
1235 $strUpdate = $DB->PrepareUpdate("b_catalog_iblock", $arFields);
1236 if (!empty($strUpdate))
1237 {
1238 $strSql = "UPDATE b_catalog_iblock SET ".$strUpdate." WHERE IBLOCK_ID = ".$ID;
1239 $DB->Query($strSql);
1240
1241 if (isset(self::$arCatalogCache[$ID]))
1242 {
1243 unset(self::$arCatalogCache[$ID]);
1244 if (defined('CATALOG_GLOBAL_VARS') && 'Y' == CATALOG_GLOBAL_VARS)
1245 {
1246 global $CATALOG_CATALOG_CACHE;
1247 $CATALOG_CATALOG_CACHE = self::$arCatalogCache;
1248 }
1249 }
1250 if (isset(self::$catalogVatCache[$ID]))
1251 unset(self::$catalogVatCache[$ID]);
1252 }
1253 CCatalogSku::ClearCache();
1255
1256 return true;
1257 }
1258
1259 public static function Delete($ID)
1260 {
1261 global $DB;
1262 $ID = (int)$ID;
1263
1264 foreach(GetModuleEvents("catalog", "OnBeforeCatalogDelete", true) as $arEvent)
1265 {
1266 if (ExecuteModuleEventEx($arEvent, array($ID))===false)
1267 return false;
1268 }
1269
1270 foreach(GetModuleEvents("catalog", "OnCatalogDelete", true) as $arEvent)
1271 ExecuteModuleEventEx($arEvent, array($ID));
1272
1273 if (isset(self::$arCatalogCache[$ID]))
1274 {
1275 unset(self::$arCatalogCache[$ID]);
1276 if (defined('CATALOG_GLOBAL_VARS') && CATALOG_GLOBAL_VARS == 'Y')
1277 {
1278 global $CATALOG_CATALOG_CACHE;
1279 $CATALOG_CATALOG_CACHE = self::$arCatalogCache;
1280 }
1281 }
1282 if (isset(self::$catalogVatCache[$ID]))
1283 unset(self::$catalogVatCache[$ID]);
1284
1285 CCatalogSku::ClearCache();
1288
1289 return $DB->Query("DELETE FROM b_catalog_iblock WHERE IBLOCK_ID = ".$ID, true);
1290 }
1291
1292 public static function OnIBlockDelete($ID)
1293 {
1294 return CCatalog::Delete($ID);
1295 }
1296
1297 public static function PreGenerateXML($xml_type = 'yandex'): string
1298 {
1299 if ($xml_type == 'yandex')
1300 {
1301 $strYandexAgent = (string)Main\Config\Option::get('catalog','yandex_agent_file');
1302 if ($strYandexAgent != '')
1303 {
1304 if (file_exists($_SERVER['DOCUMENT_ROOT'].$strYandexAgent) && is_file($_SERVER['DOCUMENT_ROOT'].$strYandexAgent))
1305 {
1306 include_once($_SERVER['DOCUMENT_ROOT'].$strYandexAgent);
1307 }
1308 else
1309 {
1310 CEventLog::Log('WARNING','CAT_YAND_FILE','catalog','YandexAgent',$strYandexAgent);
1311 include_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/catalog/load/yandex.php");
1312 }
1313 }
1314 else
1315 {
1316 include_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/catalog/load/yandex.php");
1317 }
1318 }
1319
1320 global $pPERIOD;
1321 $pPERIOD = (int)Main\Config\Option::get('catalog', 'yandex_xml_period')*3600;
1322 return 'CCatalog::PreGenerateXML("'.$xml_type.'");';
1323 }
1324
1332 public static function GetSkuInfoByProductID($ID)
1333 {
1334 return CCatalogSku::GetInfoByProductIBlock($ID);
1335 }
1336
1344 public static function GetSkuInfoByPropID($ID)
1345 {
1346 return CCatalogSku::GetInfoByLinkProperty($ID);
1347 }
1348
1349 public static function OnBeforeIBlockElementDelete($ID): bool
1350 {
1351 global $APPLICATION;
1352
1353 $ID = (int)$ID;
1354 if (0 < $ID)
1355 {
1356 $intIBlockID = (int)CIBlockElement::GetIBlockByID($ID);
1357 if (0 < $intIBlockID)
1358 {
1359 $arCatalog = CCatalogSku::GetInfoByProductIBlock($intIBlockID);
1360 if (!empty($arCatalog))
1361 {
1362 $arFilter = array('IBLOCK_ID' => $arCatalog['IBLOCK_ID'],'=PROPERTY_'.$arCatalog['SKU_PROPERTY_ID'] => $ID);
1363 $rsOffers = CIBlockElement::GetList(array(), $arFilter, false, false, array('ID', 'IBLOCK_ID'));
1364 while($arOffer = $rsOffers->Fetch())
1365 {
1366 foreach(GetModuleEvents("iblock", "OnBeforeIBlockElementDelete", true) as $arEvent)
1367 {
1368 if (ExecuteModuleEventEx($arEvent, array($arOffer['ID']))===false)
1369 {
1370 $err = "";
1371 $err_id = false;
1372 if ($ex = $APPLICATION->GetException())
1373 {
1374 $err = $ex->GetString();
1375 $err_id = $ex->GetID();
1376 }
1377 $APPLICATION->ThrowException($err, $err_id);
1378 return false;
1379 }
1380 }
1381 if (!CIBlockElement::Delete($arOffer['ID']))
1382 {
1383 $APPLICATION->ThrowException(Loc::getMessage('BT_MOD_CATALOG_ERR_CANNOT_DELETE_OFFERS'));
1384 return false;
1385 }
1386 }
1387 }
1388 }
1389 }
1390 return true;
1391 }
1392
1393 public static function OnBeforeCatalogDelete($ID): bool
1394 {
1395 global $APPLICATION;
1396
1397 $arMsg = array();
1398
1399 $ID = (int)$ID;
1400 if (0 >= $ID)
1401 return true;
1402 $arCatalog = CCatalogSku::GetInfoByIBlock($ID);
1403 if (empty($arCatalog))
1404 return true;
1405 if (CCatalogSku::TYPE_CATALOG != $arCatalog['CATALOG_TYPE'])
1406 {
1407 if (CCatalogSku::TYPE_OFFERS == $arCatalog['CATALOG_TYPE'])
1408 {
1409 $arMsg[] = array('id' => 'IBLOCK_ID', 'text' => Loc::getMessage('BT_MOD_CATALOG_ERR_CANNOT_DELETE_SKU_IBLOCK'));
1410 $obError = new CAdminException($arMsg);
1411 $APPLICATION->ThrowException($obError);
1412 return false;
1413 }
1414 else
1415 {
1416 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', 'text' => Loc::getMessage('BT_MOD_CATALOG_ERR_CANNOT_DELETE_PRODUCT_IBLOCK'));
1417 $obError = new CAdminException($arMsg);
1418 $APPLICATION->ThrowException($obError);
1419 return false;
1420 }
1421 }
1422 foreach(GetModuleEvents("catalog", "OnBeforeCatalogDelete", true) as $arEvent)
1423 {
1424 if (false === ExecuteModuleEventEx($arEvent, array($ID)))
1425 {
1426 $strError = Loc::getMessage("BT_MOD_CATALOG_ERR_BEFORE_DEL_TITLE").' '.$arEvent['TO_NAME'];
1427 if ($ex = $APPLICATION->GetException())
1428 {
1429 $strError .= ': '.$ex->GetString();
1430 }
1431 $APPLICATION->ThrowException($strError);
1432 return false;
1433 }
1434 }
1435
1436 return true;
1437 }
1438
1443 public static function OnBeforeIBlockPropertyUpdate(array &$fields): bool
1444 {
1445 global $APPLICATION;
1446
1447 $messages = [];
1448 $id = (int)$fields['ID'];
1449 if ($id > 0)
1450 {
1451 $changeActive = isset($fields['ACTIVE']) && $fields['ACTIVE'] !== 'Y';
1452 $changeMultiple = isset($fields['MULTIPLE']) && $fields['MULTIPLE'] !== 'N';
1453 $changeType = isset($fields['TYPE']) || array_key_exists('USER_TYPE', $fields);
1454 if (
1455 $changeActive
1456 || $changeMultiple
1457 || $changeType
1458 )
1459 {
1461 'select' => [
1462 'IBLOCK_ID',
1463 'PRODUCT_IBLOCK_ID',
1464 'SKU_PROPERTY_ID',
1465 ],
1466 'filter' => ['=SKU_PROPERTY_ID' => $id],
1467 ]);
1468 $row = $iterator->fetch();
1469 unset($iterator);
1470 if (!empty($row))
1471 {
1472 if ($changeActive)
1473 {
1475 'BT_MOD_CATALOG_ERR_CANNOT_DEACTIVE_SKU_PROPERTY',
1476 [
1477 '#SKU_PROPERTY_ID#' => $row['SKU_PROPERTY_ID'],
1478 '#PRODUCT_IBLOCK_ID#' => $row['PRODUCT_IBLOCK_ID'],
1479 '#IBLOCK_ID#' => $row['IBLOCK_ID'],
1480 ]
1481 );
1482 }
1483 if ($changeMultiple)
1484 {
1486 'BT_MOD_CATALOG_ERR_CANNOT_SET_MULTIPLE_SKU_PROPERTY',
1487 [
1488 '#SKU_PROPERTY_ID#' => $row['SKU_PROPERTY_ID'],
1489 '#PRODUCT_IBLOCK_ID#' => $row['PRODUCT_IBLOCK_ID'],
1490 '#IBLOCK_ID#' => $row['IBLOCK_ID'],
1491 ]
1492 );
1493 }
1494 if ($changeType)
1495 {
1496 if (
1497 (isset($fields['TYPE']) && $fields['TYPE'] !== Iblock\PropertyTable::TYPE_ELEMENT)
1498 || (array_key_exists('USER_TYPE', $fields) && $fields['USER_TYPE'] !== \CIBlockPropertySKU::USER_TYPE)
1499 )
1500 {
1502 'BT_MOD_CATALOG_ERR_CANNOT_CHANGE_TYPE_SKU_PROPERTY',
1503 [
1504 '#SKU_PROPERTY_ID#' => $row['SKU_PROPERTY_ID'],
1505 '#PRODUCT_IBLOCK_ID#' => $row['PRODUCT_IBLOCK_ID'],
1506 '#IBLOCK_ID#' => $row['IBLOCK_ID'],
1507 ]
1508 );
1509 }
1510 }
1511 }
1512 unset($row);
1513 }
1514 if (self::isCrmCatalogBrandProperty($id))
1515 {
1516 $property = \CIBlockProperty::GetByID($id)->Fetch();
1517
1518 if (isset($fields['NAME']) && $fields['NAME'] !== $property['NAME'])
1519 {
1520 $messages[] = Loc::getMessage('BT_MOD_CATALOG_ERR_CANNOT_CHANGE_BRAND_PROPERTY_NAME');
1521 }
1522 elseif (isset($fields['CODE']) && $fields['CODE'] !== 'BRAND_FOR_FACEBOOK')
1523 {
1524 $messages[] = Loc::getMessage('BT_MOD_CATALOG_ERR_CANNOT_CHANGE_BRAND_PROPERTY_CODE');
1525 }
1526 elseif (isset($fields['MULTIPLE']) && $fields['MULTIPLE'] !== 'Y')
1527 {
1528 $messages[] = Loc::getMessage('BT_MOD_CATALOG_ERR_CANNOT_CHANGE_BRAND_PROPERTY_MULTIPLE');
1529 }
1530 }
1531 unset($id);
1532 }
1533
1534 if (!empty($messages))
1535 {
1536 $APPLICATION->ThrowException(implode('. ', $messages));
1537 return false;
1538 }
1539
1540 return true;
1541 }
1542
1547 public static function OnBeforeIBlockPropertyDelete($intPropertyID): bool
1548 {
1549 global $APPLICATION;
1550
1551 $result = true;
1552 $intPropertyID = (int)$intPropertyID;
1553 if ($intPropertyID <= 0)
1554 return $result;
1555 $propertyIterator = Catalog\CatalogIblockTable::getList(array(
1556 'select' => array('IBLOCK_ID', 'PRODUCT_IBLOCK_ID', 'SKU_PROPERTY_ID'),
1557 'filter' => array('=SKU_PROPERTY_ID' => $intPropertyID)
1558 ));
1559 $property = $propertyIterator->fetch();
1560 unset($propertyIterator);
1561 if (!empty($property))
1562 {
1563 $APPLICATION->ThrowException(Loc::getMessage(
1564 'BT_MOD_CATALOG_ERR_CANNOT_DELETE_SKU_PROPERTY',
1565 array(
1566 '#SKU_PROPERTY_ID#' => $property['SKU_PROPERTY_ID'],
1567 '#PRODUCT_IBLOCK_ID#' => $property['PRODUCT_IBLOCK_ID'],
1568 '#IBLOCK_ID#' => $property['IBLOCK_ID'],
1569 )
1570 ));
1571 $result = false;
1572 }
1573 elseif (self::isCrmCatalogBrandProperty($intPropertyID))
1574 {
1575 $APPLICATION->throwException(GetMessage("BT_MOD_CATALOG_ERR_CANNOT_DELETE_BRAND_PROPERTY"));
1576 $result = false;
1577 }
1578 unset($property);
1579
1580 return $result;
1581 }
1582
1583 private static function isCrmCatalogBrandProperty($propertyId): bool
1584 {
1585 if (
1586 !Loader::includeModule('crm')
1587 || !Loader::includeModule('bitrix24')
1588 )
1589 {
1590 return false;
1591 }
1592
1593 $crmCatalogId = \CCrmCatalog::GetDefaultID();
1594 $property = \CIBlockProperty::GetByID($propertyId)->Fetch();
1595
1596 return $property['CODE'] === 'BRAND_FOR_FACEBOOK' && (int)$property['IBLOCK_ID'] === $crmCatalogId;
1597 }
1598
1599 public static function OnIBlockModuleUnInstall(): bool
1600 {
1601 global $APPLICATION;
1602
1603 $APPLICATION->ThrowException(Loc::getMessage('BT_MOD_CATALOG_ERR_IBLOCK_REQUIRED'));
1604 return false;
1605 }
1606
1607 public static function OnBeforeIBlockUpdate(array &$fields): bool
1608 {
1609 if (!self::isEnabledHandler())
1610 return true;
1611 if (isset($fields['ID']) && isset($fields['ACTIVE']))
1612 {
1613 $catalog = CCatalogSku::GetInfoByOfferIBlock($fields['ID']);
1614 if (!empty($catalog))
1615 {
1616 $parentActive = CIBlock::GetArrayByID($catalog['PRODUCT_IBLOCK_ID'], 'ACTIVE');
1617 if (!empty($parentActive))
1618 $fields['ACTIVE'] = $parentActive;
1619 unset($parentActive);
1620 }
1621 unset($catalog);
1622 }
1623
1624 return true;
1625 }
1626
1627 public static function OnAfterIBlockUpdate(array &$fields): void
1628 {
1629 if (!self::isEnabledHandler())
1630 return;
1631 if (!$fields['RESULT'])
1632 return;
1633 if (!isset($fields['ID']) || !isset($fields['ACTIVE']))
1634 return;
1635
1636 $catalog = CCatalogSku::GetInfoByProductIBlock($fields['ID']);
1637 if (!empty($catalog))
1638 {
1639 self::disableHandler();
1640 $iblock = new CIBlock();
1641 $result = $iblock->Update($catalog['IBLOCK_ID'], array('ACTIVE' => $fields['ACTIVE']));
1642 unset($result);
1643 self::enableHandler();
1644 }
1645 unset($catalog);
1646 }
1647
1655 public static function GetByIDExt($ID)
1656 {
1657 $arResult = CCatalogSku::GetInfoByIBlock($ID);
1658 if (!empty($arResult))
1659 {
1660 $arResult['OFFERS_IBLOCK_ID'] = 0;
1661 $arResult['OFFERS_PROPERTY_ID'] = 0;
1662 $arResult['OFFERS'] = 'N';
1663 if (CCatalogSku::TYPE_PRODUCT == $arResult['CATALOG_TYPE'] || CCatalogSku::TYPE_FULL == $arResult['CATALOG_TYPE'])
1664 {
1665 $arResult['OFFERS_IBLOCK_ID'] = $arResult['IBLOCK_ID'];
1666 $arResult['OFFERS_PROPERTY_ID'] = $arResult['SKU_PROPERTY_ID'];
1667 $arResult['OFFERS'] = 'Y';
1668 }
1669 if (CCatalogSku::TYPE_PRODUCT != $arResult['CATALOG_TYPE'])
1670 {
1671 $arResult['ID'] = $arResult['IBLOCK_ID'];
1672 $arResult['IBLOCK_TYPE_ID'] = '';
1673 $arResult['NAME'] = '';
1674 $arResult['LID'] = '';
1675 $arIBlock = CIBlock::GetArrayByID($arResult['IBLOCK_ID']);
1676 if (is_array($arIBlock))
1677 {
1678 $arResult['IBLOCK_TYPE_ID'] = $arIBlock['IBLOCK_TYPE_ID'];
1679 $arResult['NAME'] = $arIBlock['NAME'];
1680 $arResult['LID'] = $arIBlock['LID'];
1681 }
1682 }
1683 }
1684 return $arResult;
1685 }
1686
1687 public static function UnLinkSKUIBlock($ID): bool
1688 {
1689 global $APPLICATION;
1690
1691 $arMsg = array();
1692 $boolResult = true;
1693
1694 $ID = (int)$ID;
1695 if (0 >= $ID)
1696 {
1697 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID','text' => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_INVALID'));
1698 $boolResult = false;
1699 }
1700
1701 if ($boolResult)
1702 {
1703 $rsCatalog = CCatalog::GetList(
1704 array(),
1705 array('PRODUCT_IBLOCK_ID' => $ID),
1706 false,
1707 false,
1708 array('IBLOCK_ID')
1709 );
1710 if ($arCatalog = $rsCatalog->Fetch())
1711 {
1712 $arCatalog['IBLOCK_ID'] = (int)$arCatalog['IBLOCK_ID'];
1713 $arFields = array(
1714 'PRODUCT_IBLOCK_ID' => 0,
1715 'SKU_PROPERTY_ID' => 0,
1716 );
1717 if (!CCatalog::Update($arCatalog['IBLOCK_ID'], $arFields))
1718 return false;
1719 }
1720 }
1721 if (!$boolResult)
1722 {
1723 $obError = new CAdminException($arMsg);
1724 $APPLICATION->ResetException();
1725 $APPLICATION->ThrowException($obError);
1726 }
1727 else
1728 {
1729 CCatalogSku::ClearCache();
1730 }
1731 return $boolResult;
1732 }
1733
1742 public static function LinkSKUIBlock($ID, $SKUID)
1743 {
1744 global $APPLICATION;
1745
1746 $arMsg = array();
1747 $boolResult = true;
1748
1749 $propertyId = 0;
1750 $ID = (int)$ID;
1751 if (0 >= $ID)
1752 {
1753 $arMsg[] = array('id' => 'PRODUCT_IBLOCK_ID', 'text' => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_INVALID'));
1754 $boolResult = false;
1755 }
1756 $SKUID = (int)$SKUID;
1757 if (0 >= $SKUID)
1758 {
1759 $arMsg[] = array('id' => 'OFFERS_IBLOCK_ID', 'text' => Loc::getMessage('BT_MOD_CATALOG_ERR_OFFERS_ID_INVALID'));
1760 $boolResult = false;
1761 }
1762 if ($ID == $SKUID)
1763 {
1764 $arMsg[] = array('id' => 'OFFERS_IBLOCK_ID', 'text' => Loc::getMessage('BT_MOD_CATALOG_ERR_PRODUCT_ID_SELF'));
1765 $boolResult = false;
1766 }
1767
1768 if ($boolResult)
1769 {
1771 $SKUID,
1773 array('LINK_IBLOCK_ID' => $ID)
1774 );
1775 if (!$propertyId)
1776 {
1778 $boolResult = false;
1779 }
1780 }
1781
1782 if (!$boolResult)
1783 {
1784 $obError = new CAdminException($arMsg);
1785 $APPLICATION->ResetException();
1786 $APPLICATION->ThrowException($obError);
1787 return false;
1788 }
1789 else
1790 {
1791 return $propertyId;
1792 }
1793 }
1794
1801 public static function GetCatalogFieldsList(): array
1802 {
1803 global $DB;
1804 $arFieldsList = $DB->GetTableFieldsList('b_catalog_iblock');
1805 $arFieldsList[] = 'CATALOG';
1806 $arFieldsList[] = 'CATALOG_TYPE';
1807 $arFieldsList[] = 'OFFERS_IBLOCK_ID';
1808 $arFieldsList[] = 'OFFERS_PROPERTY_ID';
1809 return array_unique($arFieldsList);
1810 }
1811
1812 public static function IsUserExists(): bool
1813 {
1814 global $USER;
1815
1816 return (isset($USER) && $USER instanceof CUser);
1817 }
1818
1819 public static function clearCache(): void
1820 {
1821 self::$arCatalogCache = [];
1822 self::$catalogVatCache = [];
1823 }
1824
1825 private static function disableHandler(): void
1826 {
1827 self::$disableCheckIblock--;
1828 }
1829
1830 private static function enableHandler(): void
1831 {
1832 self::$disableCheckIblock++;
1833 }
1834
1838 private static function isEnabledHandler(): bool
1839 {
1840 return (self::$disableCheckIblock >= 0);
1841 }
1842}
$db_res
Определения options_user_settings.php:8
global $APPLICATION
Определения include.php:80
$arResult
Определения generate_coupon.php:16
const TYPE_ELEMENT
Определения propertytable.php:68
static get($moduleId, $name, $default="", $siteId=false)
Определения option.php:30
Определения loader.php:13
static getMessage($code, $replace=null, $language=null)
Определения loc.php:30
static cleanCache()
Определения datamanager.php:1983
static getList(array $parameters=array())
Определения datamanager.php:431
Определения catalog.php:9
static OnBeforeIBlockUpdate(array &$fields)
Определения catalog.php:1607
static GetSkuInfoByPropID($ID)
Определения catalog.php:1344
static PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields)
Определения catalog.php:389
static GetFilterOperation($key)
Определения catalog.php:318
static LinkSKUIBlock($ID, $SKUID)
Определения catalog.php:1742
static Delete($ID)
Определения catalog.php:1259
static OnBeforeIBlockElementDelete($ID)
Определения catalog.php:1349
static _PrepareSql(&$arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields)
Определения catalog.php:819
static OnIBlockModuleUnInstall()
Определения catalog.php:1599
static Add($arFields)
Определения catalog.php:1209
static OnBeforeIBlockPropertyUpdate(array &$fields)
Определения catalog.php:1443
static OnIBlockDelete($ID)
Определения catalog.php:1292
static GetByID($ID)
Определения catalog.php:281
static UnLinkSKUIBlock($ID)
Определения catalog.php:1687
static OnAfterIBlockUpdate(array &$fields)
Определения catalog.php:1627
static OnBeforeIBlockPropertyDelete($intPropertyID)
Определения catalog.php:1547
static CheckFields($ACTION, &$arFields, $ID=0)
Определения catalog.php:15
static GetByIDExt($ID)
Определения catalog.php:1655
static IsUserExists()
Определения catalog.php:1812
static GetSkuInfoByProductID($ID)
Определения catalog.php:1332
static PreGenerateXML($xml_type='yandex')
Определения catalog.php:1297
static OnBeforeCatalogDelete($ID)
Определения catalog.php:1393
static GetCatalogFieldsList()
Определения catalog.php:1801
static $arCatalogCache
Определения catalog.php:10
static Update($ID, $arFields)
Определения catalog.php:1227
static $catalogVatCache
Определения catalog.php:11
static clearCache()
Определения catalog.php:1819
static ClearCache()
Определения product.php:171
static GetList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения catalog.php:15
static Log($SEVERITY, $AUDIT_TYPE_ID, $MODULE_ID, $ITEM_ID, $DESCRIPTION=false, $SITE_ID=false)
Определения event_log.php:32
Определения iblock.php:9
const USER_TYPE
Определения prop_element_sku.php:9
static getErrors()
Определения iblockproptools.php:45
static createProperty(int $iblockId, string $propertyCode, array $propertyParams=[])
Определения iblockproptools.php:68
const CODE_SKU_LINK
Определения iblockproptools.php:12
Определения user.php:6037
$arFields
Определения dblapprove.php:5
</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
GetFilterQuery($field, $val, $procent="Y", $ex_sep=array(), $clob="N", $div_fields="Y", $clob_upper="N")
Определения filter_tools.php:383
$result
Определения get_property_values.php:14
if($ajaxMode) $ID
Определения get_user.php:27
$catalog
Определения iblock_catalog_edit.php:135
if(! $catalogEdit->isSuccess()) $iblock
Определения iblock_catalog_edit.php:38
$strError
Определения options_user_settings.php:4
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
global $DB
Определения cron_frame.php:29
global $USER
Определения csv_new_run.php:40
$ACTION
Определения csv_new_setup.php:27
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
DelDuplicateSort(&$arSort)
Определения tools.php:2055
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
is_set($a, $k=false)
Определения tools.php:2133
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$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
$messages
Определения template.php:8
$val
Определения options.php:1793
$arFilter
Определения user_search.php:106
$rsIBlocks
Определения yandex_detail.php:58
if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_CATALOG_EXPORT_EDIT)) if((!isset($_REQUEST['IBLOCK_ID']))||($_REQUEST['IBLOCK_ID']=='')) $intIBlockID
Определения yandex_detail.php:44
$arIBlock['PROPERTY']
Определения yandex_detail.php:172
$rsProps
Определения yandex_run.php:584
$iterator
Определения yandex_run.php:610
$arCatalog
Определения yandex_run.php:674
$fields
Определения yandex_run.php:501