示例#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;
     }
 }
示例#2
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);
     $propertyDescription = static::getPropertyDescription($propertyCode, $propertyParams);
     if ($propertyDescription === false) {
         return false;
     }
     $propertyDescription['IBLOCK_ID'] = $iblockID;
     if (!static::validatePropertyDescription($propertyDescription)) {
         return false;
     }
     $propertyId = 0;
     $getListParams = array('select' => array('ID'), 'filter' => array('=IBLOCK_ID' => $iblockID, '=CODE' => $propertyCode, '=ACTIVE' => 'Y'));
     static::modifyGetListParams($getListParams, $propertyCode, $propertyDescription);
     $property = Iblock\PropertyTable::getList($getListParams)->fetch();
     if (!empty($property)) {
         if (static::validateExistProperty($propertyCode, $property)) {
             $propertyId = (int) $property['ID'];
         }
     }
     unset($property);
     if (!empty(self::$errors)) {
         return false;
     }
     if ($propertyId > 0) {
         return $propertyId;
     }
     unset($propertyId);
     $propertyResult = Iblock\PropertyTable::add($propertyDescription);
     if ($propertyResult->isSuccess()) {
         return $propertyResult->getId();
     } else {
         self::$errors = $propertyResult->getErrorMessages();
         return false;
     }
 }
 /**
  * @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);
 }