/**
  * @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;
 }
 /**
  * @param $data
  * @param null $dbVersion
  * @throws \Exception
  * @return ApplyResult
  */
 public function applySnapshot($data, $dbVersion = null)
 {
     $data = $this->handleNullValues($data);
     $prop = new \CIBlockProperty();
     $res = new ApplyResult();
     $extId = $data['ID'];
     if ($dbVersion) {
         $data['IBLOCK_ID'] = $this->getReferenceController()->getCurrentIdByOtherVersion($data['IBLOCK_ID'], ReferenceController::GROUP_IBLOCK, $dbVersion);
         $id = $this->getCurrentVersionId($extId, $dbVersion);
     } else {
         $id = $extId;
     }
     if (!$dbVersion && !PropertyTable::getById($id)->fetch()) {
         unset($data['VERSION']);
         $addRes = PropertyTable::add(array('ID' => $id, 'NAME' => $data['NAME'], 'IBLOCK_ID' => $data['IBLOCK_ID']));
         if (!$addRes->isSuccess()) {
             throw new \Exception('Ќе удалось возобновить свойство текущей версии. ' . implode(', ', $addRes->getErrorMessages()) . "\n" . var_export($data, true));
         }
     }
     if ($id && PropertyTable::getById($id)->fetch()) {
         $res->setSuccess((bool) $prop->Update($id, $data));
     } else {
         $res->setSuccess((bool) ($id = $prop->Add($data)));
         $this->registerCurrentVersionId($id, $this->getReferenceValue($extId, $dbVersion));
     }
     $res->setId($id);
     if ($data['PROPERTY_TYPE'] == self::LIST_TYPE_SIGN && !empty($data['~property_list_values'])) {
         $this->_applyPropertyListTypeValues($id, $data['~property_list_values']);
     }
     return $res->setMessage($prop->LAST_ERROR);
 }
 public function applySnapshot($data, $dbVersion = null)
 {
     $iblockData = $this->handleNullValues($data['iblock']);
     $typeData = $this->handleNullValues($data['type']);
     $res = new ApplyResult();
     $type = new \CIBlockType();
     if (!\CIBlockType::GetByID($typeData['ID'])->Fetch()) {
         $res->setSuccess($type->Add($typeData));
     } else {
         $res->setSuccess($type->Update($typeData['ID'], $typeData));
     }
     if (!$res->isSuccess()) {
         return $res->setMessage($type->LAST_ERROR);
     }
     $extId = $iblockData['ID'];
     if ($dbVersion) {
         $id = $this->getCurrentVersionId($extId, $dbVersion);
     } else {
         $id = $extId;
     }
     if (!$dbVersion && !IblockTable::getById($id)->fetch()) {
         $addRes = IblockTable::add(array('ID' => $id, 'IBLOCK_TYPE_ID' => $typeData['ID'], 'NAME' => 'add'));
         if (!$addRes->isSuccess()) {
             throw new \Exception('add iblock error ' . implode(', ', $addRes->getErrorMessages()));
         }
     }
     $iblock = new \CIBlock();
     if ($id && ($currentData = IblockTable::getById($id)->fetch())) {
         $iblockData['PICTURE'] = $currentData['PICTURE'];
         $res->setSuccess((bool) $iblock->Update($id, $iblockData));
     } else {
         unset($iblockData['PICTURE']);
         $res->setSuccess((bool) ($id = $iblock->Add($iblockData)));
         $this->registerCurrentVersionId($id, $this->getReferenceValue($extId, $dbVersion));
     }
     $res->setId($id);
     return $res->setMessage($iblock->LAST_ERROR);
 }