Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
catalogedit.php
1<?php
3
8
9Loc::loadMessages(__FILE__);
10
12{
13 const CATALOG_ACTION_ADD = 'add';
14 const CATALOG_ACTION_UPDATE = 'update';
15 const CATALOG_ACTION_DELETE = 'delete';
16
18
19
20 protected $iblockId = 0;
21 protected $iblockData = array();
22 protected $iblockCatalogData = array();
23
24 protected $simpleIblock = true;
25 protected $parentIblock = false;
26 protected $offerIblock = false;
27 protected $catalogIblock = false;
28
29 protected $enableRecurring = null;
30
31 protected $updateData = array();
32 protected $catalogTableActions = array();
33 protected $iblockActions = array();
34
35 protected $errors = array();
36
37 protected static $siteListSeparator = '|';
38
42 public function __construct($iblockId)
43 {
44 $this->iblockId = (int)$iblockId;
45 $this->enableRecurring = \CBXFeatures::isFeatureEnabled('SaleRecurring');
46 $this->loadIblock();
47 $this->loadCatalog();
48 $this->initUpdateData();
49 }
50
56 public function isSuccess()
57 {
58 return empty($this->errors);
59 }
60
66 public function getErrors()
67 {
68 return $this->errors;
69 }
70
76 public function clearErrors()
77 {
78 $this->errors = array();
79 }
80
86 public function isEnableRecurring()
87 {
89 }
90
97 public function loadIblock()
98 {
99 if ($this->iblockId <= 0)
100 {
101 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_IBLOCK_ID_ABSENT');
102 return;
103 }
104 $this->iblockData = self::loadIblockFromDatabase($this->iblockId);
105 if (empty($this->iblockData))
106 {
107 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_IBLOCK_IS_NOT_EXISTS');
108 return;
109 }
110 $siteList = self::loadIblockSitesFromDatabase($this->iblockId);
111 if (empty($siteList))
112 {
113 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_IBLOCK_SITELIST_IS_EMPTY');
114 return;
115 }
116 $this->iblockData['SITES'] = static::getSiteListString($siteList, true);
117 unset($siteList);
118 }
119
125 public function loadCatalog()
126 {
127 if (!$this->isSuccess())
128 return;
130 $this->iblockCatalogData = \CCatalogSku::getInfoByIBlock($this->iblockId);
131 if ($this->iblockCatalogData === false)
132 $this->iblockCatalogData = array();
133 $this->simpleIblock = self::isSimpleIblock($this->iblockCatalogData);
134 if (!$this->simpleIblock)
135 {
136 $this->parentIblock = self::isParentIblock($this->iblockCatalogData);
137 $this->offerIblock = self::isOfferIblock($this->iblockCatalogData);
138 $this->catalogIblock = self::isCatalogIblock($this->iblockCatalogData);
139 $this->iblockCatalogData['USE_SKU'] = ($this->parentIblock ? 'Y' : 'N');
140 }
141 }
142
148 public function getIblock()
149 {
150 return $this->iblockData;
151 }
152
158 public function getCatalog()
159 {
161 }
162
169 public function saveCatalog($catalogData)
170 {
171 if (!$this->isSuccess())
172 return;
173
174 $this->prepareCatalogData($catalogData);
175 if (!$this->isSuccess())
176 return;
177
178 if (empty($this->catalogTableActions))
179 return;
180
181 foreach ($this->catalogTableActions as $iblockId => $action)
182 {
183 switch ($action)
184 {
186 break;
188
189
190 break;
192 $result = \CCatalog::delete($iblockId);
193 if (!$result)
194 {
195
196 }
197 break;
198 }
199 if (!$this->isSuccess())
200 break;
201 }
202 unset($iblockId, $action);
203 }
204
211 public function prepareCatalogData($catalogData)
212 {
213 $checkedData = $this->updateData;
214 if (!$this->isSuccess())
215 return;
216
217 if (empty($catalogData))
218 {
219 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_EMPTY_DATA');
220 return;
221 }
222 elseif (!is_array($catalogData))
223 {
224 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_BAD_DATA');
225 return;
226 }
227
228 if (!isset($catalogData['CATALOG']) || ($catalogData['CATALOG'] != 'Y' && $catalogData['CATALOG'] != 'N'))
229 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_FIELD_CATALOG_IS_ABSENT');
230
231 if (!isset($catalogData['USE_SKU']) || ($catalogData['USE_SKU'] != 'Y' && $catalogData['USE_SKU'] != 'N'))
232 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_FIELD_USE_SKU_IS_ABSENT');
233
234 if ($this->isEnableRecurring())
235 {
236 if (!isset($catalogData['SUBSCRIPTION']) || ($catalogData['SUBSCRIPTION'] != 'Y' && $catalogData['SUBSCRIPTION'] != 'N'))
237 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_FIELD_SUBSCRIPTION_IS_ABSENT');
238 }
239
240 if (!$this->isSuccess())
241 return;
242
243 if (!$this->simpleIblock)
244 {
245 if ($this->offerIblock && $catalogData['CATALOG'] == 'N')
246 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_OFFERS_ONLY_CATALOG');
247 if ($this->isEnableRecurring() && $this->parentIblock && $catalogData['SUBSCRIPTION'] == 'Y')
248 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_PARENT_IBLOCK_WITH_SUBSCRIPTION');
249 if ($this->offerIblock && $catalogData['USE_SKU'] == 'Y')
250 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_WITH_SKU');
251 }
252
253 if (!$this->isSuccess())
254 return;
255
256 $skuIblockId = 0;
257 $skuCatalog = false;
258 if ($catalogData['USE_SKU'] == 'Y')
259 {
260 if (!isset($catalogData['SKU']) || (int)$catalogData['SKU'] <= 0)
261 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_FIELD_SKU_IS_ABSENT');
262
263 if ($this->isSuccess())
264 {
265 $skuIblockId = (int)$catalogData['SKU'];
266 if ($skuIblockId == $this->iblockId)
267 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_SELF');
268
269 if ($this->isSuccess())
270 {
271 $skuIblock = self::loadIblockFromDatabase($skuIblockId);
272 if (empty($skuIblock))
273 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_BAD');
274 unset($skuIblock);
275 }
276
277 if ($this->isSuccess())
278 {
279 $skuSiteList = self::loadIblockSitesFromDatabase($skuIblockId);
280 if (empty($skuSiteList))
281 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_SITES_EMPTY');
282 elseif ($this->iblockData['SITES'] != self::getSiteListString($skuSiteList, true))
283 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_SITES_NOT_EQUAL');
284 unset($skuSiteList);
285 }
286
287 if ($this->isSuccess())
288 {
289 $skuCatalog = \CCatalogSku::getInfoByIBlock($skuIblockId);
290 if (!self::isSimpleIblock($skuCatalog))
291 {
292 if (self::isParentIblock($skuCatalog))
293 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_WITH_SKU');
294 elseif (self::isOfferIblock($skuCatalog) && $skuCatalog['PRODUCT_IBLOCK_ID'] != $this->iblockId)
295 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_FROM_OTHER_IBLOCK');
296 }
297 }
298 }
299 }
300 else
301 {
302 if (!$this->simpleIblock && $this->offerIblock)
303 {
304 if (!isset($catalogData['SKU']) || (int)$catalogData['SKU'] <= 0)
305 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_PARENT_IBLOCK_IS_ABSENT');
306 elseif ($this->iblockCatalogData['PRODUCT_IBLOCK_ID'] != $catalogData['SKU'])
307 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_SKU_PARENT_IBLOCK_OTHER');
308 }
309 }
310
311 if (!$this->isSuccess())
312 return;
313
314 if ($catalogData['CATALOG'] == 'Y')
315 {
316 if (!isset($catalogData['VAT_ID']))
317 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_VAT_ID_IS_ABSENT');
318 elseif ((int)$catalogData['VAT_ID'] < 0)
319 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_BAD_VAT_ID');
320 if (!isset($catalogData['YANDEX_EXPORT']))
321 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_YANDEX_EXPORT_IS_ABSENT');
322 elseif ($catalogData['YANDEX_EXPORT'] != 'Y' && $catalogData['YANDEX_EXPORT'] != 'N')
323 $this->errors[] = Loc::getMessage('BX_CAT_HELPER_ADMIN_CATALOGEDIT_ERR_BAD_YANDEX_EXPORT');
324 }
325
326 if (!$this->isSuccess())
327 return;
328
329 if ($catalogData['CATALOG'] != $this->iblockCatalogData['CATALOG'])
330 {
331 if ($catalogData['CATALOG'] == 'Y')
332 {
333 $this->catalogTableActions[$this->iblockId] = self::CATALOG_ACTION_ADD;
334 if (!isset($checkedData[$this->iblockId]))
335 $checkedData[$this->iblockId] = array();
336 $checkedData[$this->iblockId]['IBLOCK_ID'] = $this->iblockId;
337 }
338 else
339 {
340 $this->catalogTableActions[$this->iblockId] = self::CATALOG_ACTION_DELETE;
341 // TODO: set clear iblock
342 }
343 }
344
345 if ($catalogData['USE_SKU'] != $this->iblockCatalogData['USE_SKU'])
346 {
347 if ($catalogData['USE_SKU'] == 'Y')
348 {
349 $newOffersIBlock = self::isSimpleIblock($skuCatalog);
350 if ($newOffersIBlock || $skuCatalog['PRODUCT_IBLOCK_ID'] == 0)
351 {
352 if (!isset($checkedData[$skuIblockId]))
353 $checkedData[$skuIblockId] = array();
354 $checkedData[$skuIblockId]['PRODUCT_IBLOCK_ID'] = $this->iblockId;
355 $checkedData[$skuIblockId]['SKU_PROPERTY_ID'] = 0;
356 if ($newOffersIBlock)
357 $checkedData[$skuIblockId]['IBLOCK_ID'] = $skuIblockId;
358 }
359 unset($newOffersIBlock);
360 }
361 else
362 {
363
364 }
365 }
366
367 $this->updateData = $checkedData;
368 }
369
377 protected static function getSiteListString($siteList, $sorted = false)
378 {
379 if (empty($siteList) || !is_array($siteList))
380 return '';
381 $sorted = ($sorted === true);
382 if (!$sorted)
383 sort($siteList);
384 return implode(self::$siteListSeparator, $siteList);
385 }
386
394 protected static function loadIblockFromDatabase($iblockId)
395 {
396 $iblockId = (int)$iblockId;
397 if ($iblockId <= 0)
398 return false;
399 return Iblock\IblockTable::getList(array(
400 'select' => array('ID', 'NAME', 'IBLOCK_TYPE_ID', 'ACTIVE', 'PROPERTY_INDEX'),
401 'filter' => array('=ID' => $iblockId)
402 ))->fetch();
403 }
404
412 protected static function loadIblockSitesFromDatabase($iblockId)
413 {
414 $iblockId = (int)$iblockId;
415 if ($iblockId <= 0)
416 return array();
417 $result = array();
418 $sitesIterator = Iblock\IblockSiteTable::getList(array(
419 'select' => array('SITE_ID'),
420 'filter' => array('=IBLOCK_ID' => $iblockId),
421 'order' => array('SITE_ID' => 'ASC')
422 ));
423 while ($site = $sitesIterator->fetch())
424 $result[] = $site['SITE_ID'];
425 unset($site, $sitesIterator);
426 return $result;
427 }
428
435 protected static function isSimpleIblock($iblockCatalog)
436 {
437 return (empty($iblockCatalog) || !is_array($iblockCatalog));
438 }
439
446 protected static function isCatalogIblock($iblockCatalog)
447 {
448 return (
449 is_array($iblockCatalog)
450 && (
451 $iblockCatalog['CATALOG_TYPE'] == \CCatalogSku::TYPE_CATALOG
452 || $iblockCatalog['CATALOG_TYPE'] == \CCatalogSku::TYPE_FULL
453 )
454 );
455 }
456
463 protected static function isParentIblock($iblockCatalog)
464 {
465 return (
466 is_array($iblockCatalog)
467 && (
468 $iblockCatalog['CATALOG_TYPE'] == \CCatalogSku::TYPE_PRODUCT
469 || $iblockCatalog['CATALOG_TYPE'] == \CCatalogSku::TYPE_FULL
470 )
471 );
472 }
473
480 protected static function isOfferIblock($iblockCatalog)
481 {
482 return (is_array($iblockCatalog) && $iblockCatalog['CATALOG_TYPE'] == \CCatalogSku::TYPE_OFFERS);
483 }
484
490 protected function initUpdateData()
491 {
492 if (!$this->isSuccess())
493 return;
494
495 if ($this->simpleIblock)
496 return;
497
498 if ($this->offerIblock || $this->catalogIblock)
499 {
500 $this->updateData[$this->iblockId] = array();
501 $this->catalogTableActions[$this->iblockCatalogData['IBLOCK_ID']] = self::CATALOG_ACTION_UPDATE;
502 }
503 if ($this->parentIblock)
504 {
505 $this->updateData[$this->iblockCatalogData['IBLOCK_ID']] = array();
506 $this->catalogTableActions[$this->iblockCatalogData['IBLOCK_ID']] = self::CATALOG_ACTION_UPDATE;
507 }
508 }
509
517 protected function createSkuProperty($parentiblockId, $offerIblockId)
518 {
519 $parentiblockId = (int)$parentiblockId;
520 $offerIblockId = (int)$offerIblockId;
521 if ($parentiblockId <= 0 || $offerIblockId <= 0)
522 return 0;
523
524 $result = \CIBlockPropertyTools::createProperty(
525 $offerIblockId,
526 \CIBlockPropertyTools::CODE_SKU_LINK,
527 array('LINK_IBLOCK_ID' => $parentiblockId)
528 );
529 if (!$result)
530 $this->errors = array_merge($this->errors, \CIBlockPropertyTools::getErrors());
531
532 return $result;
533 }
534}
createSkuProperty($parentiblockId, $offerIblockId)
static getSiteListString($siteList, $sorted=false)
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29