コード例 #1
1
 /**
  * @param $data
  * @param null $dbVersion
  * @throws \Exception
  * @return ApplyResult
  */
 public function applySnapshot($data, $dbVersion = null)
 {
     $data = $this->handleNullValues($data);
     $sec = new \CIBlockSection();
     $res = new ApplyResult();
     $extId = $data['ID'];
     if ($dbVersion) {
         $data['IBLOCK_ID'] = $this->getReferenceController()->getCurrentIdByOtherVersion($data['IBLOCK_ID'], ReferenceController::GROUP_IBLOCK, $dbVersion);
         $data['IBLOCK_SECTION_ID'] && ($data['IBLOCK_SECTION_ID'] = $this->getCurrentVersionId($data['IBLOCK_SECTION_ID'], $dbVersion));
         $id = $this->getCurrentVersionId($extId, $dbVersion);
     } else {
         $id = $extId;
     }
     if (!$dbVersion && !SectionTable::getList(array('filter' => array('=ID' => $id)))->fetch()) {
         $addRes = SectionTable::add(array('ID' => $id, 'IBLOCK_ID' => $data['IBLOCK_ID'], 'TIMESTAMP_X' => new DateTime(), 'NAME' => $data['NAME'], 'DESCRIPTION_TYPE' => $data['DESCRIPTION_TYPE']));
         if (!$addRes->isSuccess()) {
             throw new \Exception('Не удалось возобновить секцию(раздел) текущей версии. ' . implode(', ', $addRes->getErrorMessages()) . "\n" . var_export($data, true));
         }
     }
     unset($data['CREATED_BY'], $data['MODIFIED_BY']);
     if ($id && ($currentData = SectionTable::getById($id)->fetch())) {
         $data['PICTURE'] = $currentData['PICTURE'];
         $data['DETAIL_PICTURE'] = $currentData['DETAIL_PICTURE'];
         $res->setSuccess((bool) $sec->Update($id, $data));
     } else {
         unset($data['PICTURE'], $data['DETAIL_PICTURE']);
         $res->setSuccess((bool) ($id = $sec->Add($data)));
         $this->registerCurrentVersionId($id, $this->getReferenceValue($extId, $dbVersion));
     }
     $res->setId($id);
     $res->setMessage($sec->LAST_ERROR);
     return $res;
 }
コード例 #2
1
ファイル: import.php プロジェクト: phwb/ugraweb.iiko
    private function importSection($arItems, $IBLOCK_ID = 0, $event = 'section')
    {
        if (!$IBLOCK_ID)
        {
            $IBLOCK_ID = $this->CATALOG_ID;
        }
        static $arResult = array();

        $arChild = array();
        foreach ($arItems as $arItem)
        {
            if (!strlen($arItem['XML_ID']))
            {
                throw new \Exception('Empty section XML ID '.$arItem['name']);
            }
            if (!strlen($arItem['CODE']))
            {
                $arItem['CODE'] = \CUtil::translit($arItem['NAME'], 'ru', array(
                    "replace_space" => '-',
                    "replace_other" => '-'
                ));
            }
            $arFields = array(
                'ACTIVE'      => $arItem['ACTIVE'],
                'CODE'        => $arItem['CODE'],
                'NAME'        => $arItem['NAME'],
                'XML_ID'      => $arItem['XML_ID'],
                'TIMESTAMP_X' => new DateTime
            );
            $arSection = $this->getSectionByXML($arItem['XML_ID'], $IBLOCK_ID);
            if ($arSection['ID'] > 0)
            {
                // если раздел существует, сравним хэши
                if (!App::compareHash($arItem) || $arItem['ACTIVE'] !== $arSection['ACTIVE'])
                {
                    // если хэши не совпали, то проапдейтим найденый раздел
                    SectionTable::update($arSection['ID'], $arFields);
                    Report::update($event);
                }
            }
            else
            {
                if (strlen($arItem['SECTION_XML_ID']) > 0)
                {
                    $arParent = $this->getSectionByXML($arItem['SECTION_XML_ID'], $IBLOCK_ID);
                    if (!$arParent['ID'])
                    {
                        // тут хитрая штука, если дочерний раздел идет раньше родителя,
                        // то и создать его нужно позже, чуть ниже вызывается рекурсия
                        $arChild[] = $arItem;
                        continue;
                    }
                    // если он есть, то текущий раздел сделаем потомком
                    $arFields['IBLOCK_SECTION_ID'] = $arParent['ID'];
                }
                $arFields = array_merge($arFields, array(
                    'IBLOCK_ID'        => $IBLOCK_ID,
                    'DESCRIPTION_TYPE' => 'text'
                ));
                $arSection['ID'] = SectionTable::add($arFields)->getId();

                App::compareHash($arItem);
                Report::create($event);
            }

            // если раздел не нашли и не создали, выплюнем эксепшен
            if (!$arSection['ID'])
            {
                throw new \Exception('Cant create sections');
            }
            $arResult[$arItem['XML_ID']] = $arSection['ID'];
        }

        if (!empty($arChild))
        {
            $this->importSection($arChild, $IBLOCK_ID, $event);
        }

        \CIBlockSection::ReSort($IBLOCK_ID);
        return $arResult;
    }