function onIBlockElementDelete($id) { $modifier_id = intval(Option::get('modifier_id')); $params = array( 'select' => array('IBLOCK_ID'), 'filter' => array( '=ID' => $id ) ); $arElement = ElementTable::getRow($params); if (intval($arElement['IBLOCK_ID']) !== $modifier_id) { return ElementModifiers::delete($id); } return Modifiers::delete($id); }
private function __construct($IBLOCK_ID, $params = array()) { if (!isset($params['groups']) || !isset($params['products'])) { throw new \Exception("Groups or Products not exist"); } $rsCatalog = \CCatalog::GetList(array(), array('IBLOCK_ID' => intval($IBLOCK_ID))); if (!$arCatalog = $rsCatalog->Fetch()) { throw new \Exception('Cant find catalog with ID = '.$IBLOCK_ID); } $this->CATALOG_ID = intval($arCatalog['IBLOCK_ID']); $this->OFFERS_ID = intval(Option::get('modifier_id')); if (!$this->OFFERS_ID) { // этот эксепшн можно заменить на создание инфоблока модификаторов throw new \Exception('Need set modifiers iblock'); } $this->defaultSectionID = Option::getDefaultSectionID(); $this->arModifiers[$this->defaultSectionID] = array( 'XML_ID' => $this->defaultSectionID, 'NAME' => 'Другие модификаторы', 'CODE' => $this->defaultSectionID, 'ACTIVE' => 'Y' ); foreach ($params['groups'] as $arItem) { $arInfo = json_decode($arItem['additionalInfo'], true); $arGroup = array( "XML_ID" => $arItem['id'], "SECTION_XML_ID" => $arItem['parentGroup'], "NAME" => $arItem['name'], "CODE" => $arInfo['CODE'], "ACTIVE" => $arInfo['ACTIVE'] === 'N' ? 'N' : 'Y' ); // группа модификаторов if (!$arItem['isIncludedInMenu']) { $this->arModifiers[$arItem['id']] = $arGroup; continue; } // обычная группа $this->arGroups[] = $arGroup; } foreach ($params['products'] as $arItem) { $arElement = array( 'ACTIVE' => 'Y', 'XML_ID' => $arItem['id'], 'NAME' => $arItem['name'], 'CODE' => $arItem['code'], 'TYPE' => strtoupper($arItem['type']), 'DESCRIPTION' => $arItem['description'], 'SECTION_XML_ID' => $arItem['parentGroup'], 'PRICE' => doubleval($arItem['price']), 'WEIGHT' => $arItem['weight'] * 1000 ); if ($arItem['type'] === 'modifier') { $arElement['SECTION_XML_ID'] = $key = strlen($arItem['groupId']) > 0 ? $arItem['groupId'] : $this->defaultSectionID; $this->arModifiers[$key]['ITEMS'][] = $arElement; continue; } $arElement = array_merge($arElement, array( 'MODIFIERS' => array(), 'GROUP_MODIFIERS' => array() )); if (!empty($arItem['modifiers'])) // обычные модификаторы { foreach ($arItem['modifiers'] as $arModifier) { $arElement['MODIFIERS'][] = array( 'XML_ID' => $arModifier['modifierId'], 'REQUIRED' => intval($arModifier['required']) > 0 ? 'Y' : 'N' ); } } if (!empty($arItem['groupModifiers'])) // групповые модификаторы { foreach ($arItem['groupModifiers'] as $arModifier) { $arElement['GROUP_MODIFIERS'][] = array( 'XML_ID' => $arModifier['modifierId'], 'REQUIRED' => intval($arModifier['required']) > 0 ? 'Y' : 'N' ); } } $this->arProducts[] = $arElement; } $iblockFields = \CIBlock::GetFields($this->CATALOG_ID); $iblockFields["XML_IMPORT_START_TIME"] = array( "NAME" => "XML_IMPORT_START_TIME", "IS_REQUIRED" => "N", "DEFAULT_VALUE" => date("Y-m-d H:i:s"), ); \CIBlock::SetFields($this->CATALOG_ID, $iblockFields); }