Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
categories.php
1<?php
2
4
9
10class Categories extends Entity
11{
12 protected function getItems(array $params = array())
13 {
14 $data = '<?xml version="1.0" encoding="utf-8"?>
15 <GetCategoriesRequest xmlns="urn:ebay:apis:eBLBaseComponents">
16 <RequesterCredentials>
17 <eBayAuthToken>'.$this->authToken.'</eBayAuthToken>
18 </RequesterCredentials>
19 <CategorySiteID>'.$this->ebaySiteId.'</CategorySiteID>
20 <WarningLevel>'.$this->warningLevel.'</WarningLevel>'."\n";
21
22 $data .= $this->array2Tags($params);
23 $data .= '</GetCategoriesRequest>?';
24
25 $categoriesXml = $this->apiCaller->sendRequest("GetCategories", $data);
26
27 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
28 $categoriesXml = Encoding::convertEncoding($categoriesXml, 'UTF-8', SITE_CHARSET);
29
30 $result = Xml2Array::convert($categoriesXml);
31 return $result;
32 }
33
34 protected function getTopItems()
35 {
36 return $this->getItems(array(
37 "LevelLimit" => 1,
38 "DetailLevel" => "ReturnAll"
39 ));
40 }
41
42 public function refreshTableData()
43 {
44 $refreshedCount = 0;
45 $catInfo = $this->getItems(array("DetailLevel" => "ReturnAll"));
46 $existCategoriesList = array();
47
48 $res = CategoryTable::getList(array(
49 "select" => array("ID", "CATEGORY_ID")
50 ));
51
52 while($category = $res->fetch())
53 $existCategoriesList[$category["CATEGORY_ID"]] = $category["ID"];
54
55 if(isset($catInfo["CategoryArray"]["Category"]))
56 {
57 $categories = Xml2Array::normalize($catInfo["CategoryArray"]["Category"]);
58
59 foreach($categories as $category)
60 {
61 $fields = array(
62 "CATEGORY_ID" => $category["CategoryID"],
63 "LEVEL" => $category["CategoryLevel"],
64 "NAME" => $category["CategoryName"],
65 "PARENT_ID" => $category["CategoryParentID"]
66 );
67
68 if(array_key_exists($category["CategoryID"], $existCategoriesList))
69 $result = CategoryTable::update($existCategoriesList[$category["CategoryID"]], $fields);
70 else
71 $result = CategoryTable::add($fields);
72
73 if($result->isSuccess())
74 {
75 $refreshedCount++;
76 }
77 }
78 }
79
80 return $refreshedCount;
81 }
82
83 public function getItemSpecifics(array $params)
84 {
85 $data = '<?xml version="1.0" encoding="utf-8"?>
86 <GetCategorySpecificsRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
87 $data.= $this->array2Tags($params);
88 $data.= '<RequesterCredentials>
89 <eBayAuthToken>'.$this->authToken.'</eBayAuthToken>
90 </RequesterCredentials>
91 <WarningLevel>'.$this->warningLevel.'</WarningLevel>
92 </GetCategorySpecificsRequest>?';
93
94 return $this->apiCaller->sendRequest("GetCategorySpecifics", $data);
95 }
96
97 protected function getMappedCategories()
98 {
99 $ebay = \Bitrix\Sale\TradingPlatform\Ebay\Ebay::getInstance();
100 $settings = $ebay->getSettings();
101 $iblocksIds = array();
102 $result = array();
103
104 foreach($settings[$this->siteId]["IBLOCK_ID"] as $iblockId)
105 $iblocksIds[] = \Bitrix\Sale\TradingPlatform\Ebay\MapHelper::getCategoryEntityId($iblockId);
106
107 $catMapRes = \Bitrix\Sale\TradingPlatform\MapTable::getList(array(
108 "filter" => array(
109 "ENTITY_ID" => $iblocksIds
110 )
111 ));
112
113 while($arMapRes = $catMapRes->fetch())
114 $result = $arMapRes["VALUE_EXTERNAL"];
115
116 return $result;
117 }
118
119 public function refreshVariationsTableData(array $ebayCategoriesIds = array())
120 {
121 $refreshedCount = 0;
122
123 $specXml = $this->getItemSpecifics(array(
124 "CategoryID" => empty($ebayCategoriesIds) ? $this->getMappedCategories() : $ebayCategoriesIds
125 ));
126
127 $specifics = new \SimpleXMLElement($specXml, LIBXML_NOCDATA);
128
129 foreach($specifics->Recommendations as $categoryRecommendation)
130 {
131 foreach($categoryRecommendation->NameRecommendation as $nameRecommendation)
132 {
133 $fields = array(
134 "CATEGORY_ID" => $categoryRecommendation->CategoryID->__toString(),
135 "NAME" => $nameRecommendation->Name->__toString()
136 );
137
138 if(isset($nameRecommendation->ValidationRules))
139 {
140
141 if($nameRecommendation->ValidationRules->MinValues)
142 $fields["MIN_VALUES"] = $nameRecommendation->ValidationRules->MinValues->__toString();
143 else
144 $fields["MIN_VALUES"] = 0;
145
146 if($nameRecommendation->ValidationRules->MinValues)
147 $fields["MAX_VALUES"] = $nameRecommendation->ValidationRules->MaxValues->__toString();
148 else
149 $fields["MAX_VALUES"] = 0;
150
151 $fields["REQUIRED"] = intval($fields["MIN_VALUES"]) > 0 ? "Y" : "N";
152 $fields["SELECTION_MODE"] = $nameRecommendation->ValidationRules->SelectionMode->__toString();
153 $fields["ALLOWED_AS_VARIATION"] = $nameRecommendation->ValidationRules->VariationSpecifics->__toString() == "Enabled" ? "Y" : "N";
154 $fields["HELP_URL"] = $nameRecommendation->ValidationRules->HelpURL->__toString();
155 }
156
157 if(isset($nameRecommendation->ValueRecommendation))
158 {
159 $values = array();
160
161 foreach($nameRecommendation->ValueRecommendation as $valueRecommendation)
162 $values[] = $valueRecommendation->Value->__toString();
163
164 $fields["VALUE"] = $values;
165 }
166
167 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
168 $fields = \Bitrix\Main\Text\Encoding::convertEncodingArray($fields, 'UTF-8', SITE_CHARSET);
169
170 $res = CategoryVariationTable::getList(array(
171 "filter" => array(
172 "CATEGORY_ID" => $fields["CATEGORY_ID"],
173 "NAME" => $fields["NAME"]
174 ),
175 "select" => array("ID")
176 ));
177
178 if($savedVar = $res->fetch())
179 $result = CategoryVariationTable::update($savedVar["ID"], $fields);
180 else
181 $result = CategoryVariationTable::add($fields);
182
183 if($result->isSuccess())
184 {
185 $refreshedCount++;
186 }
187 }
188 }
189
190 return $refreshedCount;
191 }
192}
refreshVariationsTableData(array $ebayCategoriesIds=array())
static update($primary, array $data)
Definition category.php:133
static convert($xmlData, $convertCharset=true)
Definition xml2array.php:15
static normalize(array $branch)
Definition xml2array.php:75