Example #1
0
 public static function Add($arFields)
 {
     if (!CModule::IncludeModule('catalog')) {
         return false;
     }
     global $DB;
     $element = new CIBlockElement();
     $ID = isset($arFields['ID']) ? $arFields['ID'] : null;
     if ($ID === null) {
         //Try to create a CIBlockElement
         $arElement = array();
         if (isset($arFields['NAME'])) {
             $arElement['NAME'] = $arFields['NAME'];
         }
         if (isset($arFields['SORT'])) {
             $arElement['SORT'] = $arFields['SORT'];
         }
         if (isset($arFields['ACTIVE'])) {
             $arElement['ACTIVE'] = $arFields['ACTIVE'];
         }
         if (isset($arFields['DETAIL_PICTURE'])) {
             $arElement['DETAIL_PICTURE'] = $arFields['DETAIL_PICTURE'];
         }
         if (isset($arFields['DESCRIPTION'])) {
             $arElement['DETAIL_TEXT'] = $arFields['DESCRIPTION'];
             $arElement['DETAIL_TEXT_TYPE'] = 'text';
         }
         if (isset($arFields['DESCRIPTION_TYPE'])) {
             $arElement['DETAIL_TEXT_TYPE'] = $arFields['DESCRIPTION_TYPE'];
         }
         if (isset($arFields['PREVIEW_PICTURE'])) {
             $arElement['PREVIEW_PICTURE'] = $arFields['PREVIEW_PICTURE'];
         }
         if (isset($arFields['PREVIEW_TEXT'])) {
             $arElement['PREVIEW_TEXT'] = $arFields['PREVIEW_TEXT'];
             $arElement['PREVIEW_TEXT_TYPE'] = 'text';
         }
         if (isset($arFields['PREVIEW_TEXT_TYPE'])) {
             $arElement['PREVIEW_TEXT_TYPE'] = $arFields['PREVIEW_TEXT_TYPE'];
         }
         if (isset($arFields['CATALOG_ID'])) {
             $arElement['IBLOCK_ID'] = intval($arFields['CATALOG_ID']);
         } else {
             $arElement['IBLOCK_ID'] = $arFields['CATALOG_ID'] = CCrmCatalog::EnsureDefaultExists();
         }
         if (isset($arFields['SECTION_ID'])) {
             $arElement['IBLOCK_SECTION_ID'] = $arFields['SECTION_ID'];
             $arElement['IBLOCK_SECTION'] = array($arElement['IBLOCK_SECTION_ID']);
         }
         if (isset($arFields['XML_ID'])) {
             $arElement['XML_ID'] = $arFields['XML_ID'];
         } else {
             if (isset($arFields['ORIGINATOR_ID']) || isset($arFields['ORIGIN_ID'])) {
                 if (isset($arFields['ORIGINATOR_ID']) && isset($arFields['ORIGIN_ID'])) {
                     $arElement['XML_ID'] = $arFields['ORIGINATOR_ID'] . '#' . $arFields['ORIGIN_ID'];
                 } else {
                     if (isset($arFields['ORIGINATOR_ID'])) {
                         $arElement['XML_ID'] = $arFields['ORIGINATOR_ID'] . '#';
                     } else {
                         $arElement['XML_ID'] = '#' . $arFields['ORIGIN_ID'];
                     }
                 }
             } else {
                 if ($arElement['IBLOCK_ID'] != self::getDefaultCatalogId()) {
                     $arElement['XML_ID'] = '#';
                 }
             }
         }
         if (!$element->CheckFields($arElement)) {
             self::RegisterError($element->LAST_ERROR);
             return false;
         }
         if (isset($arFields['PROPERTY_VALUES'])) {
             $arElement['PROPERTY_VALUES'] = $arFields['PROPERTY_VALUES'];
         }
         $ID = intval($element->Add($arElement));
         if ($ID <= 0) {
             self::$LAST_ERROR = $element->LAST_ERROR;
             return false;
         }
         $arFields['ID'] = $ID;
     }
     if (!self::CheckFields('ADD', $arFields, 0)) {
         $element->Delete($ID);
         return false;
     }
     $CCatalogProduct = new CCatalogProduct();
     $arCatalogProductFields = array('ID' => $ID, 'QUANTITY' => 0);
     if (isset($arFields['VAT_INCLUDED'])) {
         $arCatalogProductFields['VAT_INCLUDED'] = $arFields['VAT_INCLUDED'];
     }
     if (isset($arFields['VAT_ID']) && !empty($arFields['VAT_ID'])) {
         $arCatalogProductFields['VAT_ID'] = $arFields['VAT_ID'];
     }
     if (isset($arFields['MEASURE']) && !empty($arFields['MEASURE'])) {
         $arCatalogProductFields['MEASURE'] = $arFields['MEASURE'];
     }
     if ($CCatalogProduct->Add($arCatalogProductFields)) {
         if (isset($arFields['PRICE']) && isset($arFields['CURRENCY_ID'])) {
             self::setPrice($ID, $arFields['PRICE'], $arFields['CURRENCY_ID']);
         }
     } else {
         $element->Delete($ID);
         return false;
     }
     //		$arInsert = $DB->PrepareInsert(CCrmProduct::TABLE_NAME, $arFields);
     //		$sQuery =
     //			'INSERT INTO '.CCrmProduct::TABLE_NAME.'('.$arInsert[0].') VALUES('.$arInsert[1].')';
     //		$DB->Query($sQuery, false, 'File: '.__FILE__.'<br/>Line: '.__LINE__);
     return $ID;
 }