Exemplo n.º 1
0
 /**
  * Create property.
  *
  * @param int $iblockID					Iblock id.
  * @param string $propertyCode			Property code.
  * @param array $propertyParams			Property params.
  * @return bool|int
  */
 public static function createProperty($iblockID, $propertyCode, $propertyParams = array())
 {
     self::$errors = array();
     $iblockID = (int) $iblockID;
     $propertyCode = (string) $propertyCode;
     if ($iblockID <= 0 || $propertyCode === '') {
         return false;
     }
     $iblockIterator = Iblock\IblockTable::getList(array('select' => array('ID'), 'filter' => array('ID' => $iblockID)));
     if (!($iblock = $iblockIterator->fetch())) {
         return false;
     }
     unset($iblock, $iblockIterator);
     $propertyIterator = Iblock\PropertyTable::getList(array('select' => array('ID'), 'filter' => array('IBLOCK_ID' => $iblockID, '=CODE' => $propertyCode)));
     if ($property = $propertyIterator->fetch()) {
         return (int) $property['ID'];
     }
     unset($propertyIterator);
     $propertyDescription = self::getPropertyDescription($propertyCode, $propertyParams);
     if ($propertyDescription === false) {
         return false;
     }
     $propertyDescription['IBLOCK_ID'] = $iblockID;
     if (!self::validatePropertyDescription($propertyDescription)) {
         return false;
     }
     $propertyResult = Iblock\PropertyTable::add($propertyDescription);
     if ($propertyResult->isSuccess()) {
         return $propertyResult->getId();
     } else {
         self::$errors = $propertyResult->getErrorMessages();
         return false;
     }
 }
Exemplo n.º 2
0
 /**
  * Validate and modify exist property.
  *
  * @param string $propertyCode			Property code.
  * @param array $property				Current property data.
  * @return bool
  * @throws Exception
  */
 protected static function validateExistProperty($propertyCode, $property)
 {
     $result = true;
     switch ($propertyCode) {
         case self::CODE_SKU_LINK:
             $fields = array();
             if ($property['USER_TYPE'] != self::USER_TYPE_SKU_LINK) {
                 $fields['USER_TYPE'] = self::USER_TYPE_SKU_LINK;
             }
             if ($property['XML_ID'] != self::XML_SKU_LINK) {
                 $fields['XML_ID'] = self::XML_SKU_LINK;
             }
             if (!empty($fields)) {
                 $propertyResult = Iblock\PropertyTable::update($property['ID'], $fields);
                 if (!$propertyResult->isSuccess()) {
                     self::$errors = $propertyResult->getErrorMessages();
                     $result = false;
                 }
                 unset($propertyResult);
             }
             unset($fields);
             break;
     }
     return $result;
 }