30 public function run(
int $propertyId,
int $iblockId, array $fields): ?array
32 if (!$this->checkWritePermissions($iblockId))
34 $this->errorCollection->setError(
41 $fields = $this->cleanEntityFields($fields);
44 $this->errorCollection->setError(
54 $oldFields = $this->getPropertyFields($propertyId, $iblockId);
57 $this->errorCollection->setError(
65 $fields[
'IBLOCK_ID'] = $iblockId;
66 $fields = $this->processUserTypeSettingsFields($fields, $oldFields);
67 $fields = $this->processFeatureFields($fields, $oldFields);
68 $fields = $this->processEnumFields($fields, $oldFields);
70 if (\CIBlock::GetArrayByID($iblockId,
'SECTION_PROPERTY') !==
'Y')
72 if (($fields[
'SMART_FILTER'] ??
'N') ===
'Y')
74 $iblock = new \CIBlock;
75 $iblock->Update($iblockId, [
'SECTION_PROPERTY' =>
'Y']);
80 $entity =
new CIBlockProperty();
83 $result = $entity->Update($propertyId, $fields);
87 $result = $entity->Add($fields);
88 if ($result !==
false)
90 $propertyId = (int)$result;
96 if ($entity->LAST_ERROR)
98 $this->errorCollection->setError(
99 new Error($entity->LAST_ERROR)
118 private function checkWritePermissions(
int $iblockId): bool
120 return CIBlockRights::UserHasRightTo($iblockId, $iblockId,
'iblock_edit');
130 private function convertPublicFeature(array $fields): array
137 $features = $fields[
'FEATURES'] ?? [];
138 if (!is_array($features))
144 'MODULE_ID' =>
'iblock',
148 'MODULE_ID' =>
'iblock',
156 $fields[
'FEATURES'] = $features;
169 private function processFeatureFields(array $fields, ?array $oldFields): array
171 $fields = $this->convertPublicFeature($fields);
173 $features = $fields[
'FEATURES'] ??
null;
174 if (!isset($features) || !is_array($features))
181 $currentFeatures = [];
182 if (isset($oldFields[
'FEATURES']))
184 foreach ($oldFields[
'FEATURES'] as $index => $isEnabled)
186 $currentFeatures[$index] = $isEnabled;
190 foreach ($features as $index => $isEnabled)
192 if (isset($currentFeatures[$index]))
194 unset($currentFeatures[$index]);
200 $feature[
'IS_ENABLED'] = $isEnabled ===
'Y' ?
'Y' :
'N';
201 $result[] = $feature;
205 foreach ($currentFeatures as $index => $isEnabled)
210 $feature[
'IS_ENABLED'] = $isEnabled ===
'Y' ?
'Y' :
'N';
211 $result[] = $feature;
215 $fields[
'FEATURES'] = $result;
228 private function processUserTypeSettingsFields(array $fields, ?array $oldFields): array
230 $fields = $this->parseUserType($fields);
232 if (!isset($fields[
'PROPERTY_TYPE']) && isset($oldFields))
234 $fields[
'USER_TYPE'] = $oldFields[
'USER_TYPE'];
235 $fields[
'PROPERTY_TYPE'] = $oldFields[
'PROPERTY_TYPE'];
238 if (empty($fields[
'USER_TYPE']))
240 $fields[
'USER_TYPE_SETTINGS'] =
false;
244 isset($fields[
'USER_TYPE_SETTINGS'], $oldFields[
'USER_TYPE_SETTINGS'])
245 && is_array($fields[
'USER_TYPE_SETTINGS'])
248 $fields[
'USER_TYPE_SETTINGS'] += $oldFields[
'USER_TYPE_SETTINGS'];
261 private function processEnumFields(array $fields): array
266 && isset($fields[
'VALUES'])
267 && is_array($fields[
'VALUES'])
271 foreach ($fields[
'VALUES'] as &$item)
273 if (isset($item[
'DEF']) && $item[
'DEF'] ===
'Y')
298 private function parseUserType(array $fields): array
300 if (isset($fields[
'PROPERTY_TYPE']))
302 $parts = explode(
':', $fields[
'PROPERTY_TYPE']);
303 if (count($parts) === 2)
305 $fields[
'PROPERTY_TYPE'] = $parts[0];
306 $fields[
'USER_TYPE'] = $parts[1];
310 $fields[
'USER_TYPE'] =
false;
324 private function cleanEntityFields(array $fields): array
326 $availableFields = array_fill_keys([
353 'USER_TYPE_SETTINGS',
357 return array_intersect_key($fields, $availableFields);
368 private function getPropertyFields(
int $propertyId,
int $iblockId): ?array
372 '=ID' => $propertyId,
373 '=IBLOCK_ID' => $iblockId,
382 $fields[
'USER_TYPE_SETTINGS'] = $fields[
'USER_TYPE_SETTINGS_LIST'];
383 unset($fields[
'USER_TYPE_SETTINGS_LIST']);
386 $sectionProperty = SectionPropertyTable::getRow([
394 '=IBLOCK_ID' => $iblockId,
395 '=PROPERTY_ID' => $propertyId,
398 if (is_array($sectionProperty))
400 $fields += $sectionProperty;
404 $fields[
'FEATURES'] = [];
413 '=PROPERTY_ID' => $propertyId,
416 foreach ($rows as $row)
419 $fields[
"FEATURES"][$index] = $row[
'IS_ENABLED'];
static getMessage($code, $replace=null, $language=null)
static getRow(array $parameters)
static getList(array $parameters=array())