Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
catalogsectiontabhandler.php
1<?php
2
4
10
11Loc::loadMessages(__FILE__);
12
19{
20 public $name = "Ebay";
21 public $description = "Ebay section mappings";
22
23 protected static $ebayCategoriesVariations = array();
24 protected static $ebayRequiredVariations = array();
25
26 public function Action($arArgs)
27 {
28 if(!isset($_POST["SALE"]["EBAY"]))
29 return true;
30
31 $propsRes = true;
32 $ebayCategoryId = isset($_POST["SALE"]["EBAY"]["EBAY_CATEGORY_ID"]) ? $_POST["SALE"]["EBAY"]["EBAY_CATEGORY_ID"] : "";
33 $params = array();
34
35 if(!empty($_POST["SALE"]["EBAY"]["POLICY"]) && is_array($_POST["SALE"]["EBAY"]["POLICY"]))
36 {
37 $ebay = \Bitrix\Sale\TradingPlatform\Ebay\Ebay::getInstance();
38 $settings = $ebay->getSettings();
39 $siteSettings = $settings[$arArgs["IBLOCK"]["LID"]];
40
41 if($_POST["SALE"]["EBAY"]["POLICY"]["RETURN"] == $siteSettings["POLICY"]["RETURN"]["DEFAULT"])
42 unset($_POST["SALE"]["EBAY"]["POLICY"]["RETURN"]);
43
44 if($_POST["SALE"]["EBAY"]["POLICY"]["PAYMENT"] == $siteSettings["POLICY"]["PAYMENT"]["DEFAULT"])
45 unset($_POST["SALE"]["EBAY"]["POLICY"]["PAYMENT"]);
46
47 if($_POST["SALE"]["EBAY"]["POLICY"]["SHIPPING"] == $siteSettings["POLICY"]["SHIPPING"]["DEFAULT"])
48 unset($_POST["SALE"]["EBAY"]["POLICY"]["SHIPPING"]);
49
50 if(!empty($_POST["SALE"]["EBAY"]["POLICY"]))
51 $params = array("POLICY" => $_POST["SALE"]["EBAY"]["POLICY"]);
52 }
53
54 $catRes = self::saveCategoryMap($arArgs["ID"], $arArgs["IBLOCK"]["ID"], array($ebayCategoryId), $params);
55
56 if(
57 isset($_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"])
58 && is_array($_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"])
59 && isset($_POST["SALE"]["EBAY"]["BITRIX_CATEGORY_PROPS"])
60 && is_array($_POST["SALE"]["EBAY"]["BITRIX_CATEGORY_PROPS"])
61 )
62 {
63 $propsRes = self::saveCategoryPropsMap($arArgs["IBLOCK"]["ID"], $ebayCategoryId, $_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"], $_POST["SALE"]["EBAY"]["BITRIX_CATEGORY_PROPS"]);
64 }
65
66 return $catRes && $propsRes;
67 }
68
69 protected function saveCategoryPropsMap($iblockId, $ebayCategoryId, array $ebayCatVar, array $bitrixCatProps)
70 {
71 $result = true;
72 $mapEntityId = MapHelper::getCategoryVariationEntityId($iblockId, $ebayCategoryId);
74
75 foreach($ebayCatVar as $key => $ebayCategoryVariation)
76 {
77 if(!isset($bitrixCatProps[$key]) || $ebayCategoryVariation == '' )
78 continue;
79
80 $fields = array(
81 "ENTITY_ID" => $mapEntityId,
82 "VALUE_EXTERNAL" => $ebayCategoryVariation,
83 "VALUE_INTERNAL" => $bitrixCatProps[$key]
84 );
85
86 $addRes = MapTable::add($fields);
87 $result = $result && $addRes->isSuccess();
88 }
89
90 return $result;
91 }
92
93 protected function saveCategoryMap($bitrixCategoryId, $iblockId, array $ebayCategoriesIds, $params = array())
94 {
95 $result = true;
96 $catMapEntId = MapHelper::getCategoryEntityId($iblockId);
97
98
99 foreach($ebayCategoriesIds as $ebayCategoryId)
100 {
101 if(trim($ebayCategoryId) == '')
102 continue;
103
104 $fields = array(
105 "ENTITY_ID" => $catMapEntId,
106 "VALUE_INTERNAL" => $bitrixCategoryId
107 );
108
109 $dbRes = MapTable::getList(array(
110 'filter' => $fields
111 ));
112
113 $fields["VALUE_EXTERNAL"] = $ebayCategoryId;
114 if(!empty($params))
115 $fields["PARAMS"] = $params;
116
117 if($map = $dbRes->fetch())
118 $res = MapTable::update($map['ID'], $fields);
119 else
120 $res = MapTable::add($fields);
121
122 $result = $result && $res->isSuccess();
123 }
124
125 return $result;
126 }
127
128 public function Check($arArgs)
129 {
130 if(!isset($_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"]) || !is_array($_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"]))
131 return true;
132
133 $result = true;
134
135 $categoriesVarResult = CategoryVariationTable::getList( array(
136 'select' => array('ID', 'NAME', 'REQUIRED'),
137 'filter' => array("=CATEGORY_ID" => $_POST["SALE"]["EBAY"]["EBAY_CATEGORY_ID"]),
138 ));
139
140 while($var = $categoriesVarResult->fetch())
141 {
142 if($var['REQUIRED'] == 'Y')
143 {
144 if(!in_array($var["ID"], $_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"]))
145 {
146 $result = false;
147 break;
148 }
149
150 $variationIdx = array_search($var["ID"], $_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"]);
151
152 if($variationIdx === false
153 ||!isset($_POST["SALE"]["EBAY"]["BITRIX_CATEGORY_PROPS"][$variationIdx])
154 || $_POST["SALE"]["EBAY"]["BITRIX_CATEGORY_PROPS"][$variationIdx] == '' )
155 {
156 $result = false;
157 break;
158 }
159 }
160 }
161
162 if(!$result)
163 throw new SystemException("All required variations of category must be filled!");
164
165 return $result;
166 }
167
168 public function ShowTabSection($divName, $arArgs, $bVarsFromForm)
169 {
170 $ebay = \Bitrix\Sale\TradingPlatform\Ebay\Ebay::getInstance();
171 $settings = $ebay->getSettings();
172
173 if(empty($settings[$arArgs["IBLOCK"]["LID"]]))
174 return '';
175
176 $siteSettings = $settings[$arArgs["IBLOCK"]["LID"]];
177
178 global $APPLICATION;
179
180 //categories
181 $resultHtml =
182 '<tr>'.
183 '<td width="40%" valign="top">'.Loc::getMessage("SALE_EBAY_CSTH_CATEGORY").':</td>'.
184 '<td width="60%">';
185
186 $catMapRes = \Bitrix\Sale\TradingPlatform\MapTable::getList(array(
187 "filter" => array(
188 "ENTITY_ID" => \Bitrix\Sale\TradingPlatform\Ebay\MapHelper::getCategoryEntityId($arArgs["IBLOCK"]["ID"]),
189 "VALUE_INTERNAL" => $arArgs["ID"]
190 )
191 ));
192
193 $arMapRes = $catMapRes->fetch();
194
195 $params = array(
196 "TOP_CATEGORY_SELECT_NAME" => "SALE[EBAY][TOP_CATEGORY]",
197 "CATEGORY_INPUT_NAME" => "SALE[EBAY][EBAY_CATEGORY_ID]",
198 "EBAY_CATEGORY_VARIATIONS_SN" => "SALE[EBAY][EBAY_CATEGORY_VARIATIONS]",
199 "BITRIX_CATEGORY_PROPS_SN" => "SALE[EBAY][BITRIX_CATEGORY_PROPS]",
200 "BITRIX_CATEGORY_ID"=> $arArgs["ID"],
201 "IBLOCK_ID" => $arArgs["IBLOCK"]["ID"]
202 );
203
204
205 if(isset($_POST["SALE"]["EBAY"]["EBAY_CATEGORY_ID"]))
206 $params["EBAY_CATEGORY_ID"] = $_POST["SALE"]["EBAY"]["EBAY_CATEGORY_ID"];
207 elseif(isset($arMapRes["VALUE_EXTERNAL"]))
208 $params["EBAY_CATEGORY_ID"] = $arMapRes["VALUE_EXTERNAL"];
209
210 if(isset($_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"]) && isset($_POST["SALE"]["EBAY"]["BITRIX_CATEGORY_PROPS"]))
211 {
212 $params["VARIATIONS_VALUES"] = array();
213
214 for($i=0, $l=count($_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"]); $i<$l; $i++)
215 $params["VARIATIONS_VALUES"][$_POST["SALE"]["EBAY"]["EBAY_CATEGORY_VARIATIONS"][$i]] = $_POST["SALE"]["EBAY"]["BITRIX_CATEGORY_PROPS"][$i];
216 }
217
218 ob_start();
219 $APPLICATION->IncludeComponent(
220 "bitrix:sale.ebay.categories",
221 ".default",
222 $params
223 );
224
225 $resultHtml .= ob_get_contents();
226 ob_end_clean();
227
228 $resultHtml .=
229 '</td>'.
230 '</tr>';
231
232 //policies
233 $errorMsg = "";
234 $policy = null;
235 $policyReturn = "";
236 $policyPayment = "";
237 $policyShipping = "";
238
239 if(isset($_POST["SALE"]["EBAY"]["POLICY"]))
240 {
241 if(!empty($_POST["SALE"]["EBAY"]["POLICY"]["RETURN"]))
242 $policyReturn = $_POST["SALE"]["EBAY"]["POLICY"]["RETURN"];
243
244 if(!empty($_POST["SALE"]["EBAY"]["POLICY"]["SHIPPING"]))
245 $policyShipping = $_POST["SALE"]["EBAY"]["POLICY"]["SHIPPING"];
246
247 if(!empty($_POST["SALE"]["EBAY"]["POLICY"]["PAYMENT"]))
248 $policyPayment = $_POST["SALE"]["EBAY"]["POLICY"]["PAYMENT"];
249 }
250 elseif(!empty($arMapRes["PARAMS"]["POLICY"]))
251 {
252 if(!empty($arMapRes["PARAMS"]["POLICY"]["RETURN"]))
253 $policyReturn = $arMapRes["PARAMS"]["POLICY"]["RETURN"];
254
255 if(!empty($arMapRes["PARAMS"]["POLICY"]["SHIPPING"]))
256 $policyShipping = $arMapRes["PARAMS"]["POLICY"]["SHIPPING"];
257
258 if(!empty($arMapRes["PARAMS"]["POLICY"]["PAYMENT"]))
259 $policyPayment = $arMapRes["PARAMS"]["POLICY"]["PAYMENT"];
260 }
261
262 if($policyReturn == '' && !empty($siteSettings["POLICY"]["RETURN"]["DEFAULT"]))
263 $policyReturn = $siteSettings["POLICY"]["RETURN"]["DEFAULT"];
264
265 if($policyShipping == '' && !empty($siteSettings["POLICY"]["SHIPPING"]["DEFAULT"]))
266 $policyShipping = $siteSettings["POLICY"]["SHIPPING"]["DEFAULT"];
267
268 if($policyPayment == '' && !empty($siteSettings["POLICY"]["PAYMENT"]["DEFAULT"]))
269 $policyPayment = $siteSettings["POLICY"]["PAYMENT"]["DEFAULT"];
270
271 if(isset($siteSettings["API"]["AUTH_TOKEN"]) && $siteSettings["API"]["AUTH_TOKEN"] <> '')
272 $policy = new \Bitrix\Sale\TradingPlatform\Ebay\Policy($siteSettings["API"]["AUTH_TOKEN"], $arArgs["IBLOCK"]["LID"]);
273 else
274 $errorMsg .= "You must set API token first!\n";
275
276 $resultHtml .= '
277 <tr></tr><td colspan="2" style="border-top: 2px solid #e0e8ea;">&nbsp;</td></tr>
278 <tr>
279 <td>'.Loc::getMessage("SALE_EBAY_CSTH_POLICY_RETURN").':</span></td>
280 <td>';
281
282 if($policy)
283 $names = $policy->getPoliciesNames(\Bitrix\Sale\TradingPlatform\Ebay\Policy::TYPE_RETURN);
284 else
285 $names = array();
286
287 if($policy && !empty($names))
288 {
289 $resultHtml .= '<select name="SALE[EBAY][POLICY][RETURN]">';
290
291 foreach($names as $policyId => $policyName)
292 $resultHtml .= '<option value="'.htmlspecialcharsbx($policyId).'"'.($policyReturn == $policyId ? " selected" : "").'>'.$policyName.'</option>';
293
294 $resultHtml .= '</select>';
295 }
296 else
297 {
298 $resultHtml .= Loc::getMessage("SALE_EBAY_CSTH_NO_POLICY");
299 }
300
301 $resultHtml .= '
302 </td>
303 </tr>
304 <tr>
305 <td>'.Loc::getMessage("SALE_EBAY_CSTH_POLICY_SHIPMENT").':</td>
306 <td>';
307
308 if($policy)
309 $names = $policy->getPoliciesNames(\Bitrix\Sale\TradingPlatform\Ebay\Policy::TYPE_SHIPPING);
310 else
311 $names = array();
312
313
314 if($policy && !empty($names))
315 {
316 $resultHtml .= '<select name="SALE[EBAY][POLICY][SHIPPING]">';
317
318 foreach($names as $policyId => $policyName)
319 $resultHtml .= '<option value="'.htmlspecialcharsbx($policyId).'"'.($policyShipping == $policyId ? " selected" : "").'>'.$policyName.'</option>';
320
321 $resultHtml .= '</select>';
322 }
323 else
324 {
325 $resultHtml .= Loc::getMessage("SALE_EBAY_CSTH_NO_POLICY");
326 }
327
328 $resultHtml .='
329 </td>
330 </tr>
331 <tr>
332 <td>'.Loc::getMessage("SALE_EBAY_CSTH_POLICY_PAYMENT").':</td>
333 <td>';
334
335 if($policy)
336 $names = $policy->getPoliciesNames(\Bitrix\Sale\TradingPlatform\Ebay\Policy::TYPE_PAYMENT);
337 else
338 $names = array();
339
340 if($policy && !empty($names))
341 {
342 $resultHtml .= '<select name="SALE[EBAY][POLICY][PAYMENT]">';
343
344 foreach($policy->getPoliciesNames(\Bitrix\Sale\TradingPlatform\Ebay\Policy::TYPE_PAYMENT) as $policyId => $policyName)
345 $resultHtml .= '<option value="'.htmlspecialcharsbx($policyId).'"'.($policyPayment == $policyId ? " selected" : "").'>'.$policyName.'</option>';
346
347 $resultHtml .= '</select>';
348 }
349 else
350 {
351 $resultHtml .= Loc::getMessage("SALE_EBAY_CSTH_NO_POLICY");
352 }
353
354 $resultHtml .='
355 </td>
356 </tr>
357 <tr>
358 <td>&nbsp</td><td><a href="http://www.bizpolicy.ebay.ru/businesspolicy/manage?totalPages=1">'.Loc::getMessage('SALE_EBAY_CSTH_EDIT_POLICIES').'</a></td>
359 </tr>';
360
361 return $resultHtml;
362 }
363}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
saveCategoryMap($bitrixCategoryId, $iblockId, array $ebayCategoriesIds, $params=array())
saveCategoryPropsMap($iblockId, $ebayCategoryId, array $ebayCatVar, array $bitrixCatProps)
static getCategoryVariationEntityId($iblockId, $ebayCategoryId)
Definition maphelper.php:71
static deleteByMapEntityId($mapEntityId)
Definition map.php:99