Bitrix-D7 22.6
 
Загрузка...
Поиск...
Не найдено
exportoffer.php
1<?php
2namespace Bitrix\Catalog\Ebay;
3
7
8class ExportOffer implements \Iterator
9{
10 /*Constructor input vars*/
11 protected $iBlockId;
12 protected $xmlData;
13
14 /*Counted by constructor vars*/
15 protected $bAllSections;
16 protected $arSections = array();
17 protected $arIblock;
18 protected $startPosition = null;
19 protected $includeSubsections = true;
20 protected $intMaxSectionID = 0;
21 protected $arSectionIDs = array();
22
23 /*Iterator vars*/
24 protected $currentKey = 0;
25 protected $currentRecord = array();
26
27 /*other vars*/
28 protected $cnt = 0;
30 protected $dbItems = NULL;
31 protected $catalogType;
32 protected $onlyAvailableElements = false; // export with flag "available"
33
34 public function __construct($catalogType, $params)
35 {
36 if(!isset($params["IBLOCK_ID"]) || intval($params["IBLOCK_ID"]) <= 0)
37 throw new SystemException("Incorrect iBlock ID (".__CLASS__."::".__METHOD__.")");
38
39 $this->catalogType = $catalogType;
40
41 $this->iBlockId = $params["IBLOCK_ID"];
42 $this->xmlData = $params["XML_DATA"];
43
44 $this->arIblock = $this->getIblockProps($params["SETUP_SERVER_NAME"]);
45 $this->arSections = $this->getSections($params["PRODUCT_GROUPS"]);
46 if(isset($params["START_POSITION"]) && $params["START_POSITION"])
47 $this->startPosition = $params["START_POSITION"];
48 if(isset($params["INCLUDE_SUBSECTION"]))
49 $this->includeSubsections = $params["INCLUDE_SUBSECTION"];
50
51 $this->bAllSections = in_array(0, $this->arSections) ? true : false;
52 $availGroups = $this->getAvailGroups();
53 $this->intMaxSectionID = $this->getMaxSectionId($availGroups);
54 $this->arSectionIDs = $this->getSectionIDs($availGroups);
55 }
56
57 /*Iterator methods*/
58
64 #[\ReturnTypeWillChange]
65 public function current()
66 {
68 }
69
75 #[\ReturnTypeWillChange]
76 public function key()
77 {
78 return $this->currentKey;
79 }
80
86 #[\ReturnTypeWillChange]
87 public function next()
88 {
89 $this->currentKey++;
90 $this->currentRecord = $this->nextItem();
91 $this->checkDiscountCache();
92 }
93
99 #[\ReturnTypeWillChange]
100 public function rewind()
101 {
102 $this->currentKey = 0;
103 $this->dbItems = $this->createDbResObject();
104 $this->currentRecord = $this->nextItem();
105 $this->cnt = 100;
106 $this->checkDiscountCache();
107 }
108
114 #[\ReturnTypeWillChange]
115 public function valid ()
116 {
117 return is_array($this->currentRecord);
118 }
119
120 protected function createDbResObject()
121 {
122// order need for limiting by ID
123 $order = array("ID" => "ASC");
124 $arSelect = array("ID", "LID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "NAME", "PREVIEW_PICTURE", "PREVIEW_TEXT",
125 "PREVIEW_TEXT_TYPE", "DETAIL_PICTURE", "LANG_DIR", "DETAIL_PAGE_URL", "DETAIL_TEXT");
126
127 $filter = array("IBLOCK_ID" => $this->iBlockId);
128
129// if set start position - limit result by ID
130 if($this->startPosition)
131 $filter[">=ID"] = $this->startPosition;
132
133 if (!$this->bAllSections && !empty($this->arSections))
134 {
135 $filter["INCLUDE_SUBSECTIONS"] = $this->includeSubsections ? "Y" : "N";
136 $filter["SECTION_ID"] = $this->arSections;
137 }
138
139 if($this->onlyAvailableElements)
140 $filter["CATALOG_AVAILABLE"] = "Y";
141 $filter["ACTIVE"] = "Y";
142 $filter["SECTION_GLOBAL_ACTIVE"] = "Y";
143 $filter["ACTIVE_DATE"] = "Y";
144
145 return \CIBlockElement::GetList($order, $filter, false, false, $arSelect);
146 }
147
148 protected function getMaxSectionId(array $arAvailGroups)
149 {
150 $result = 0;
151
152 foreach($arAvailGroups as $group)
153 if ($result < $group["ID"])
154 $result = $group["ID"];
155
156 $result += 100000000;
157
158 return $result;
159 }
160
161 protected function getAvailGroups()
162 {
163 $arAvailGroups = array();
164
165 if (!$this->bAllSections)
166 {
167 for ($i = 0, $intSectionsCount = count($this->arSections); $i < $intSectionsCount; $i++)
168 {
169 $db_res = \CIBlockSection::GetNavChain($this->iBlockId, $this->arSections[$i]);
170 $curLEFT_MARGIN = 0;
171 $curRIGHT_MARGIN = 0;
172 while ($ar_res = $db_res->Fetch())
173 {
174 $curLEFT_MARGIN = (int)$ar_res["LEFT_MARGIN"];
175 $curRIGHT_MARGIN = (int)$ar_res["RIGHT_MARGIN"];
176 $arAvailGroups[$ar_res["ID"]] = array(
177 "ID" => (int)$ar_res["ID"],
178 "IBLOCK_SECTION_ID" => (int)$ar_res["IBLOCK_SECTION_ID"],
179 "NAME" => $ar_res["NAME"]
180 );
181 }
182
183 $filter = array("IBLOCK_ID"=>$this->iBlockId, ">LEFT_MARGIN"=>$curLEFT_MARGIN, "<RIGHT_MARGIN"=>$curRIGHT_MARGIN, "ACTIVE"=>"Y", "IBLOCK_ACTIVE"=>"Y", "GLOBAL_ACTIVE"=>"Y");
184 $db_res = \CIBlockSection::GetList(array("left_margin"=>"asc"), $filter);
185 while ($ar_res = $db_res->Fetch())
186 {
187 $arAvailGroups[$ar_res["ID"]] = array(
188 "ID" => (int)$ar_res["ID"],
189 "IBLOCK_SECTION_ID" => (int)$ar_res["IBLOCK_SECTION_ID"],
190 "NAME" => $ar_res["NAME"]
191 );
192 }
193 }
194 }
195 else
196 {
197 $filter = array("IBLOCK_ID"=>$this->iBlockId, "ACTIVE"=>"Y", "IBLOCK_ACTIVE"=>"Y", "GLOBAL_ACTIVE"=>"Y");
198 $db_res = \CIBlockSection::GetList(array("left_margin"=>"asc"), $filter);
199 while ($ar_res = $db_res->Fetch())
200 {
201 $arAvailGroups[$ar_res["ID"]] = array(
202 "ID" => (int)$ar_res["ID"],
203 "IBLOCK_SECTION_ID" => (int)$ar_res["IBLOCK_SECTION_ID"],
204 "NAME" => $ar_res["NAME"]
205 );
206 }
207 }
208
209 return $arAvailGroups;
210 }
211
212 protected function getSections($selectedGroups)
213 {
214 $arSections = array();
215
216 if (is_array($selectedGroups))
217 {
218 foreach ($selectedGroups as $value)
219 {
220 $arSections[] = (int)$value;
221
222 if ($value == 0)
223 break;
224 }
225 }
226
227 return $arSections;
228 }
229
230 protected function getIblockProps($serverName)
231 {
232 $dbIblock = \CIBlock::GetByID($this->iBlockId);
233 $arIblock = $dbIblock->Fetch();
234
235 if($arIblock)
236 {
237 if ($serverName == '')
238 {
239 if ($arIblock['SERVER_NAME'] == '')
240 {
241 $rsSite = \CSite::GetList('', '', array("LID" => $arIblock["LID"]));
242 if($arSite = $rsSite->Fetch())
243 $arIblock["SERVER_NAME"] = $arSite["SERVER_NAME"];
244 if($arIblock["SERVER_NAME"] == '' && defined("SITE_SERVER_NAME"))
245 $arIblock["SERVER_NAME"] = SITE_SERVER_NAME;
246 if($arIblock["SERVER_NAME"] == '')
247 $arIblock["SERVER_NAME"] = \COption::GetOptionString("main", "server_name", "");
248 }
249 }
250 else
251 {
252 $arIblock['SERVER_NAME'] = $serverName;
253 }
254
255 $arIblock['PROPERTY'] = array();
256
257 $rsProps = \CIBlockProperty::GetList(
258 array('SORT' => 'ASC', 'NAME' => 'ASC'),
259 array('IBLOCK_ID' => $this->iBlockId, 'ACTIVE' => 'Y', 'CHECK_PERMISSIONS' => 'N')
260 );
261 while ($arProp = $rsProps->Fetch())
262 {
263 $arProp['ID'] = (int)$arProp['ID'];
264 $arProp['USER_TYPE'] = (string)$arProp['USER_TYPE'];
265 $arProp['CODE'] = (string)$arProp['CODE'];
266 $arIblock['PROPERTY'][$arProp['ID']] = $arProp;
267 }
268 }
269
270 return $arIblock;
271 }
272
273 protected function getQuantity($productId)
274 {
275 $result = 0;
276
277 $rsProducts = \CCatalogProduct::GetList(
278 array(),
279 array('ID' => $productId),
280 false,
281 false,
282 array('ID', 'QUANTITY', 'QUANTITY_TRACE', 'CAN_BUY_ZERO')
283 );
284
285 if ($arProduct = $rsProducts->Fetch())
286 {
287 $arProduct['QUANTITY'] = doubleval($arProduct['QUANTITY']);
288
289 if (0 >= $arProduct['QUANTITY'] && ('Y' != $arProduct['QUANTITY_TRACE'] || 'N' != $arProduct['CAN_BUY_ZERO']))
290 $result = 1;
291 else
292 $result = $arProduct['QUANTITY'];
293 }
294
295 return $result;
296 }
297
303 public static function getRub()
304 {
305 $currencyList = Currency\CurrencyManager::getCurrencyList();
306
307 return (isset($currencyList['RUR']) ? 'RUR' : 'RUB');
308 }
309
315 public function setOnlyAvailableFlag($flag)
316 {
317 if($flag)
318 $this->onlyAvailableElements = true;
319 else
320 $this->onlyAvailableElements = false;
321 }
322
323 protected function getPrices($productId, $siteId)
324 {
325 $minPrice = 0;
326 $minPriceRUR = 0;
327 $minPriceGroup = 0;
328 $minPriceCurrency = "";
329
330 $baseCurrency = Currency\CurrencyManager::getBaseCurrency();
331 $RUR = $this->getRub();
332
333 if ($this->xmlData['PRICE'] > 0)
334 {
335 $rsPrices = \CPrice::GetListEx(array(),array(
336 'PRODUCT_ID' => $productId,
337 'CATALOG_GROUP_ID' => $this->xmlData['PRICE'],
338 'CAN_BUY' => 'Y',
339 'GROUP_GROUP_ID' => array(2),
340 '+<=QUANTITY_FROM' => 1,
341 '+>=QUANTITY_TO' => 1,
342 )
343 );
344
345 if ($arPrice = $rsPrices->Fetch())
346 {
347 if ($arOptimalPrice = \CCatalogProduct::GetOptimalPrice(
348 $productId,
349 1,
350 array(2), // anonymous
351 'N',
352 array($arPrice),
353 $siteId
354 ))
355 {
356 $minPrice = $arOptimalPrice['RESULT_PRICE']['DISCOUNT_PRICE'];
357 $minPriceCurrency = $baseCurrency;
358 $minPriceRUR = \CCurrencyRates::ConvertCurrency($minPrice, $baseCurrency, $RUR);
359 $minPriceGroup = $arOptimalPrice['PRICE']['CATALOG_GROUP_ID'];
360 }
361 }
362 }
363 else
364 {
365 if ($arPrice = \CCatalogProduct::GetOptimalPrice(
366 $productId,
367 1,
368 array(2), // anonymous
369 'N',
370 array(),
371 $siteId
372 ))
373 {
374 $minPrice = $arPrice['RESULT_PRICE']['DISCOUNT_PRICE'];
375 $minPriceCurrency = $baseCurrency;
376 $minPriceRUR = \CCurrencyRates::ConvertCurrency($minPrice, $baseCurrency, $RUR);
377 $minPriceGroup = $arPrice['PRICE']['CATALOG_GROUP_ID'];
378 }
379 }
380
381 $result = array(
382 "MIN" => $minPrice,
383 "MIN_RUB" => $minPriceRUR,
384 "MIN_GROUP" => $minPriceGroup,
385 "MIN_CURRENCY" => $minPriceCurrency
386 );
387
388 return $result;
389 }
390
391 protected function getDetailPageUrl($detailPageUrl)
392 {
393 if ($detailPageUrl == '')
394 $detailPageUrl = '/';
395 else
396 $detailPageUrl = str_replace(' ', '%20', $detailPageUrl);
397
398 $result = "http://".$this->arIblock['SERVER_NAME'].Main\Text\HtmlFilter::encode($detailPageUrl);
399
400 return $result;
401 }
402
403 protected function getPictureUrl($pictNo)
404 {
405 $strFile = "";
406
407 if ($file = \CFile::GetFileArray($pictNo))
408 {
409 if(mb_substr($file["SRC"], 0, 1) == "/")
410 $strFile = "http://".$this->arIblock['SERVER_NAME'].implode("/", array_map("rawurlencode", explode("/", $file["SRC"])));
411 elseif(preg_match("/^(http|https):\\/\\/(.*?)\\/(.*)\$/", $file["SRC"], $match))
412 $strFile = "http://".$match[2].'/'.implode("/", array_map("rawurlencode", explode("/", $match[3])));
413 else
414 $strFile = $file["SRC"];
415 }
416
417 return $strFile;
418 }
419
420 protected function getParams($product, $arIblock)
421 {
422 if (isset($arIblock['PROPERTY']))
423 $arProperties = $arIblock['PROPERTY'];
424 else
425 $arProperties = array();
426
427 $arUserTypeFormat = array();
428
429 foreach($arProperties as $key => $arProperty)
430 {
431 $arUserTypeFormat[$arProperty["ID"]] = false;
432 if($arProperty["USER_TYPE"] <> '')
433 {
434 $arUserType = \CIBlockProperty::GetUserType($arProperty["USER_TYPE"]);
435 if(array_key_exists("GetPublicViewHTML", $arUserType))
436 {
437 $arUserTypeFormat[$arProperty["ID"]] = $arUserType["GetPublicViewHTML"];
438 $arProperties[$key]['PROPERTY_TYPE'] = 'USER_TYPE';
439 }
440 }
441 }
442
443 $result = array();
444
445
446 if (is_array($this->xmlData) && is_array($this->xmlData['XML_DATA']) && is_array($this->xmlData['XML_DATA']['PARAMS']))
447 {
448 foreach ($this->xmlData['XML_DATA']['PARAMS'] as $key => $propId)
449 {
450 if ($propId)
451 $result[] = $this->getValue($product, 'PARAM_'.$key, $propId, $arProperties, $arUserTypeFormat);
452 }
453 }
454
455 return $result;
456 }
457
458 protected function getValue($arOffer, $param, $PROPERTY, $arProperties, $arUserTypeFormat)
459 {
460 $result = array();
461
462 $bParam = (strncmp($param, 'PARAM_', 6) == 0);
463
464 if (isset($arProperties[$PROPERTY]) && !empty($arProperties[$PROPERTY]))
465 {
466 $PROPERTY_CODE = $arProperties[$PROPERTY]['CODE'];
467 $arProperty = (
468 isset($arOffer['PROPERTIES'][$PROPERTY_CODE])
469 ? $arOffer['PROPERTIES'][$PROPERTY_CODE]
470 : $arOffer['PROPERTIES'][$PROPERTY]
471 );
472
473 $value = array();
474
475 if(!is_array($arProperty["VALUE"]))
476 $arProperty["VALUE"] = array($arProperty["VALUE"]);
477
478 switch ($arProperties[$PROPERTY]['PROPERTY_TYPE'])
479 {
480 case 'USER_TYPE':
481 foreach($arProperty["VALUE"] as $oneValue)
482 {
483 $value[] = call_user_func_array($arUserTypeFormat[$PROPERTY],
484 array(
485 $arProperty,
486 array("VALUE" => $oneValue),
487 array('MODE' => 'SIMPLE_TEXT'),
488 ));
489 }
490 break;
491
492 case 'E':
493 $arCheckValue = array();
494
495 foreach ($arProperty['VALUE'] as &$intValue)
496 {
497 $intValue = (int)$intValue;
498 if (0 < $intValue)
499 $arCheckValue[] = $intValue;
500 }
501
502 if (isset($intValue))
503 unset($intValue);
504
505 if (!empty($arCheckValue))
506 {
507 $dbRes = \CIBlockElement::GetList(array(), array('IBLOCK_ID' => $arProperties[$PROPERTY]['LINK_IBLOCK_ID'], 'ID' => $arCheckValue), false, false, array('NAME'));
508 while ($arRes = $dbRes->Fetch())
509 {
510 $value[] = $arRes['NAME'];
511 }
512 }
513 break;
514
515 case 'G':
516 $arCheckValue = array();
517
518 foreach ($arProperty['VALUE'] as &$intValue)
519 {
520 $intValue = (int)$intValue;
521 if (0 < $intValue)
522 $arCheckValue[] = $intValue;
523 }
524
525 if (isset($intValue))
526 unset($intValue);
527
528 if (!empty($arCheckValue))
529 {
530 $dbRes = \CIBlockSection::GetList(array(), array('IBLOCK_ID' => $arProperty['LINK_IBLOCK_ID'], 'ID' => $arCheckValue), false, array('NAME'));
531 while ($arRes = $dbRes->Fetch())
532 {
533 $value[] = $arRes['NAME'];
534 }
535 }
536 break;
537
538 case 'L':
539 $value .= $arProperty['VALUE'];
540 break;
541
542 case 'F':
543 foreach ($arProperty['VALUE'] as &$intValue)
544 {
545 $intValue = (int)$intValue;
546 if ($intValue > 0)
547 {
548 if ($ar_file = \CFile::GetFileArray($intValue))
549 {
550 if(mb_substr($ar_file["SRC"], 0, 1) == "/")
551 $strFile = "http://".$this->arIblock["SERVER_NAME"].implode("/", array_map("rawurlencode", explode("/", $ar_file["SRC"])));
552 elseif(preg_match("/^(http|https):\\/\\/(.*?)\\/(.*)\$/", $ar_file["SRC"], $match))
553 $strFile = "http://".$match[2].'/'.implode("/", array_map("rawurlencode", explode("/", $match[3])));
554 else
555 $strFile = $ar_file["SRC"];
556 $value[] = $strFile;
557 }
558 }
559 }
560
561 if (isset($intValue))
562 unset($intValue);
563 break;
564
565 default:
566 $value = $arProperty['VALUE'];
567 }
568
569 if(is_array($value) && count($value) == 1)
570 $value = implode("", $value);
571
572 if ($bParam)
573 {
574 $result[$param] = array(
575 "NAME" => $arProperties[$PROPERTY]['NAME'],
576 "VALUES" => $value,
577 );
578 }
579 else
580 {
581 $result[$param] = $value;
582 }
583 }
584
585 return $result;
586 }
587
588 protected function getCategories($productId)
589 {
590 $boolCurrentSections = false;
591 $result = array();
592
593 $dbElementGroups = \CIBlockElement::GetElementGroups($productId, false, array('ID', 'ADDITIONAL_PROPERTY_ID'));
594
595 while ($arElementGroup = $dbElementGroups->Fetch())
596 {
597 if (0 < (int)$arElementGroup['ADDITIONAL_PROPERTY_ID'])
598 continue;
599
600 $boolCurrentSections = true;
601
602 if (in_array((int)$arElementGroup["ID"], $this->arSectionIDs))
603 $result[] = $arElementGroup["ID"];
604 }
605
606 if (!$boolCurrentSections)
607 $result[] = $this->intMaxSectionID;
608
609 return $result;
610 }
611
612 protected function getSectionIDs(array $availGroups)
613 {
614 if (!empty($availGroups))
615 $arSectionIDs = array_keys($availGroups);
616 else
617 $arSectionIDs = array();
618
619 return $arSectionIDs;
620 }
621
622 protected function checkDiscountCache()
623 {
624 $this->cnt++;
625
626 if (100 <= $this->cnt)
627 {
628 $this->cnt = 0;
629 \CCatalogDiscount::ClearDiscountCache(array(
630 'PRODUCT' => true,
631 'SECTIONS' => true,
632 'PROPERTIES' => true,
633 ));
634 }
635 }
636
637 protected function nextItem()
638 {
639 if(!$obElement = $this->dbItems->GetNextElement())
640 return false;
641
642 $arItem = $obElement->GetFields();
643 $arItem['PROPERTIES'] = $obElement->GetProperties();
644 $arItem["QUANTITY"] = $this->getQuantity($arItem["ID"]);
645 $arItem["PRICES"] = $this->getPrices($arItem["ID"], $this->arIblock['LID']);
646 $arItem["CATEGORIES"] = $this->getCategories($arItem["ID"]);
647 $arItem["DETAIL_PICTURE_URL"] = $this->getPictureUrl((int)$arItem["DETAIL_PICTURE"]);
648 $arItem["PREVIEW_PICTURE_URL"] = $this->getPictureUrl((int)$arItem["PREVIEW_PICTURE"]);
649 $arItem["PARAMS"] = $this->getParams($arItem, $this->arIblock);
650 $arItem["DETAIL_PAGE_URL"] = $this->getDetailPageUrl($arItem["~DETAIL_PAGE_URL"]);
651
652 return $arItem;
653 }
654}
getValue($arOffer, $param, $PROPERTY, $arProperties, $arUserTypeFormat)
getParams($product, $arIblock)
getMaxSectionId(array $arAvailGroups)
getSectionIDs(array $availGroups)
getPrices($productId, $siteId)
__construct($catalogType, $params)
Definition: exportoffer.php:34