Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
product.php
1<?php
2
4
7
8class Product
9 extends DataSource
10 implements \Iterator
11{
12 protected $productFeeds = array();
13 protected $currentFeed;
14
15 protected $ebay;
16 protected $siteId;
17
18 protected $startPos = 0;
19 protected $startProductFeed = 0;
20
21 public function __construct($params)
22 {
23 $this->ebay = \Bitrix\Sale\TradingPlatform\Ebay\Ebay::getInstance();
24
25 if(!$this->ebay->isActive())
26 throw new SystemException("Ebay is not active!".__METHOD__);
27
28 if(!isset($params["SITE_ID"]) || $params["SITE_ID"] == '')
29 throw new ArgumentNullException("SITE_ID");
30
31 $this->siteId = $params["SITE_ID"];
32
33 if(!\Bitrix\Main\Loader::includeModule('catalog'))
34 throw new SystemException("Can't include module \"Catalog\"! ".__METHOD__);
35
36 $iBlockIds = $this->getIblockIds();
37
38 if(empty($iBlockIds))
39 throw new SystemException("Can't find iblocks ids! ".__METHOD__);
40
41
42 foreach($iBlockIds as $iblockId)
43 {
44 $this->productFeeds[] = \Bitrix\Catalog\ExportOfferCreator::getOfferObject(
45 array(
46 "IBLOCK_ID" => $iblockId,
47 "PRODUCT_GROUPS" => $this->getMappedGroups($iblockId),
48 "XML_DATA" => $this->getXmlData(),
49 "SETUP_SERVER_NAME" => $this->getDomainName(),
50 )
51 );
52 }
53 }
54
55 protected function getIblockIds()
56 {
57 $result = array();
58 $settings = $this->ebay->getSettings();
59
60 if(isset($settings[$this->siteId]["IBLOCK_ID"]) && is_array($settings[$this->siteId]["IBLOCK_ID"]))
61 $result = $settings[$this->siteId]["IBLOCK_ID"];
62
63 return $result;
64 }
65
66 protected function getMappedGroups($iblockId)
67 {
68 $result = array();
69 $catMapEntId = \Bitrix\Sale\TradingPlatform\Ebay\MapHelper::getCategoryEntityId($iblockId);
70
71 $catRes = \Bitrix\Sale\TradingPlatform\MapTable::getList(array(
72 'select' => array('VALUE_INTERNAL'),
73 'filter' => array('=ENTITY_ID' => $catMapEntId),
74 'group' => array('VALUE_INTERNAL')
75 ));
76
77 while($category = $catRes->fetch())
78 if(intval($category["VALUE_INTERNAL"]) > 0)
79 $result[] = $category["VALUE_INTERNAL"];
80
81 return $result;
82 }
83
84 protected function getXmlData()
85 {
86 return array();
87 }
88
89 protected function getDomainName()
90 {
91 $result = "";
92 $settings = $this->ebay->getSettings();
93
94 if(isset($settings[$this->siteId]["DOMAIN_NAME"]) && is_array($settings[$this->siteId]["DOMAIN_NAME"]))
95 $result = $settings[$this->siteId]["DOMAIN_NAME"];
96
97 return $result;
98 }
99
100 public function setStartPosition($startPos = "")
101 {
102 if(mb_strlen($startPos) > 3) // format: iBlockId_RecordNumber
103 {
104 $positions = explode("_", $startPos);
105
106 if(isset($positions[0]) && isset($positions[1]))
107 {
108 $this->startProductFeed = $positions[0];
109 $this->startPos = $positions[1];
110 }
111 }
112 }
113
114 //Proxy offers iterator methods
115 public function current()
116 {
117 return $this->productFeeds[$this->currentFeed]->current();
118 }
119
120 public function key()
121 {
122 return $this->currentFeed."_".$this->productFeeds[$this->currentFeed]->key();
123 }
124
125 public function next()
126 {
127 $this->productFeeds[$this->currentFeed]->next();
128
129 if(!$this->valid() && $this->currentFeed < count($this->productFeeds)-1)
130 {
131 $this->currentFeed++;
132 $this->next();
133 }
134 }
135
136 public function rewind()
137 {
138 $this->currentFeed = $this->startProductFeed;
139
140 foreach($this->productFeeds as $feed)
141 $feed->rewind();
142
143 for($i = 0; $i < $this->startPos; $i++)
144 $this->productFeeds[$this->currentFeed]->next();
145 }
146
147 public function valid()
148 {
149 return $this->productFeeds[$this->currentFeed]->valid();
150 }
151}