19 private $fieldList = [];
25 $this->param = $param;
28 $this->fieldList = [
"NAME",
"IS_REQUIRED",
"MULTIPLE",
"TYPE",
"SORT",
"DEFAULT_VALUE",
"LIST",
29 "USER_TYPE_SETTINGS",
"LIST_TEXT_VALUES",
"LIST_DEF",
"CODE",
"SETTINGS",
"ROW_COUNT",
30 "COL_COUNT",
"LINK_IBLOCK_ID"];
39 $this->param->checkRequiredInputParams([
"IBLOCK_CODE",
"IBLOCK_ID", [
"FIELDS" => [
"NAME",
"TYPE"]]]);
40 if ($this->param->hasErrors())
42 $this->errorCollection->add($this->param->getErrors());
46 $fields = $this->getFields();
47 $this->validateFields($fields);
53 $object = new \CList($this->iblockId);
55 if (!$object->is_field($fields[
"TYPE"]))
57 if (!empty($fields[
"CODE"]))
59 $property = $this->getProperty($this->iblockId, $fields[
"CODE"]);
60 if (!empty($property) && is_array($property))
62 $this->errorCollection->setError(
new Error(
"Property already exists", self::ERROR_SAVE_FIELD));
68 $this->errorCollection->setError(
new Error(
"Please fill the code fields", self::ERROR_SAVE_FIELD));
72 $result = $object->addField($fields);
76 global $CACHE_MANAGER;
77 $CACHE_MANAGER->clearByTag(
"lists_list_".$this->iblockId);
82 $this->errorCollection->setError(
new Error(
"Error saving the field", self::ERROR_SAVE_FIELD));
87 public function get(array $navData = [])
89 $this->param->checkRequiredInputParams([
"IBLOCK_CODE",
"IBLOCK_ID"]);
90 if ($this->param->hasErrors())
92 $this->errorCollection->add($this->param->getErrors());
98 if (!empty($this->params[
"FIELD_ID"]))
100 $object = new \CListFieldList($this->iblockId);
101 $fieldsObject = $object->getByID($this->params[
"FIELD_ID"]);
104 $fieldData = $fieldsObject->getArray();
105 $fields = [$fieldData[
"TYPE"] => $fieldData];
110 $object = new \CList($this->iblockId);
111 $fields = $object->getFields();
114 return $this->adjustmentFields($fields);
119 $this->param->checkRequiredInputParams([
"IBLOCK_CODE",
"IBLOCK_ID",
120 "FIELD_ID", [
"FIELDS" => [
"NAME",
"TYPE"]]]);
121 if ($this->param->hasErrors())
123 $this->errorCollection->add($this->param->getErrors());
127 $fields = $this->getFields();
128 $this->validateFields($fields);
134 $object = new \CList($this->iblockId);
135 if (!$this->canChangeField($fields[
"TYPE"]))
140 $result = $object->updateField($this->params[
"FIELD_ID"], $fields);
144 global $CACHE_MANAGER;
145 $CACHE_MANAGER->clearByTag(
"lists_list_".$this->iblockId);
150 $this->errorCollection->setError(
new Error(
"Error update the field", self::ERROR_UPDATE_FIELD));
155 public function delete()
157 $this->param->checkRequiredInputParams([
"IBLOCK_CODE",
"IBLOCK_ID",
"FIELD_ID"]);
158 if ($this->param->hasErrors())
160 $this->errorCollection->add($this->param->getErrors());
164 $object = new \CList($this->iblockId);
165 $object->deleteField($this->params[
"FIELD_ID"]);
168 global $CACHE_MANAGER;
169 $CACHE_MANAGER->clearByTag(
'lists_list_'.$this->iblockId);
176 $this->param->checkRequiredInputParams([
"IBLOCK_CODE",
"IBLOCK_ID"]);
177 if ($this->param->hasErrors())
179 $this->errorCollection->add($this->param->getErrors());
183 $fieldId = (!empty($this->params[
"FIELD_ID"]) ? $this->params[
"FIELD_ID"] :
"");
184 $object = new \CList($this->iblockId);
185 return $object->getAvailableTypes($fieldId);
188 private function getFields()
192 foreach ($this->params[
"FIELDS"] as $fieldId => $fieldValue)
194 if (!in_array($fieldId, $this->fieldList))
199 if (is_array($fieldValue))
201 $fieldValue = array_change_key_case($fieldValue, CASE_UPPER);
204 $fields[$fieldId] = $fieldValue;
207 if (!is_array($fields[
"LIST"]))
209 $fields[
"LIST"] = [];
211 if (!empty($fields[
"LIST_TEXT_VALUES"]))
215 foreach ($fields[
"LIST"] as $key => $enum)
217 if ($enum[
"SORT"] > $maxSort)
219 $maxSort = intval($enum[
"SORT"]);
221 $listMap[trim($enum[
"VALUE"],
" \t\n\r")] = $enum[
"ID"];
223 foreach (explode(
"\n", $fields[
"LIST_TEXT_VALUES"]) as $valueLine)
225 $value = trim($valueLine,
" \t\n\r");
226 if ((
string) $value <>
'' && !isset($listMap[$value]))
229 $listMap[$value] =
"m".$maxSort;
230 $fields[
"LIST"][
"m".$maxSort] = array(
237 if (!empty($fields[
"LIST_DEF"]) && is_array($fields[
"LIST_DEF"]))
239 foreach ($fields[
"LIST"] as $key => $enum)
241 $fields[
"LIST"][$key][
"DEF"] =
"N";
243 foreach ($fields[
"LIST_DEF"] as $def)
246 if ($def > 0 && isset($fields[
"LIST"][$def]))
248 $fields[
"LIST"][$def][
"DEF"] =
"Y";
256 private function canChangeField($type)
258 $objectFieldList = new \CListFieldList($this->iblockId);
259 $fieldObject = $objectFieldList->getByID($this->params[
"FIELD_ID"]);
262 $oldType = $fieldObject->getTypeID();
263 if ($oldType != $type)
265 $this->errorCollection->setError(
new Error(
"Field type can not be changed", self::ERROR_UPDATE_FIELD));
272 private function validateFields($fields)
274 if (isset($fields[
"SETTINGS"][
"ADD_READ_ONLY_FIELD"]) && $fields[
"SETTINGS"][
"ADD_READ_ONLY_FIELD"] ==
"Y")
276 $defaultValueError =
false;
277 switch ($fields[
"TYPE"])
280 $defaultValueError = ($fields[
"DEFAULT_VALUE"] ==
'');
283 if (is_array($fields[
"LIST_DEF"]))
285 $defaultValueError = (empty(current($fields[
"LIST_DEF"])));
289 $defaultValueError = (empty($fields[
"DEFAULT_VALUE"][
"TEXT"]));
292 $defaultValueError = (empty($fields[
"DEFAULT_VALUE"]));
294 if ($defaultValueError)
296 $this->errorCollection->setError(
new Error(
297 "The default value of the field \"".$fields[
"NAME"].
"\" is required", self::ERROR_SAVE_FIELD));
302 if ($fields[
"TYPE"] ==
"PREVIEW_PICTURE")
304 $fields[
"DEFAULT_VALUE"][
"METHOD"] =
"resample";
305 $fields[
"DEFAULT_VALUE"][
"COMPRESSION"] = intval(\COption::getOptionString(
"main",
"image_resize_quality",
"95"));
307 elseif ($fields[
"TYPE"] ==
"S:Date")
309 if (!empty($fields[
"DEFAULT_VALUE"]) && !CheckDateTime($fields[
"DEFAULT_VALUE"], FORMAT_DATE))
311 $formatError =
"The default value of the field \"".$fields[
"NAME"].
"\" is incorrect";
314 elseif($fields[
"TYPE"] ==
"S:DateTime")
316 if(!empty($fields[
"DEFAULT_VALUE"]) && !CheckDateTime($fields[
"DEFAULT_VALUE"]))
318 $formatError =
"The default value of the field \"".$fields[
"NAME"].
"\" is incorrect";
321 if (preg_match(
"/^(G|G:|E|E:)/", $fields[
"TYPE"]))
323 $blocks = \CLists::getIBlocks($this->params[
"IBLOCK_TYPE_ID"],
"Y", $this->params[
"SOCNET_GROUP_ID"]);
324 if (mb_substr($fields[
"TYPE"], 0, 1) ==
"G")
326 unset($blocks[$this->params[
"IBLOCK_ID"]]);
328 if (!array_key_exists($fields[
"LINK_IBLOCK_ID"], $blocks))
330 $formatError =
"Incorrect lists specified for \"".$fields[
"NAME"].
"\" property";
335 $this->errorCollection->setError(
new Error(
336 "The default value of the field \"".$fields[
"NAME"].
"\" is required", self::ERROR_SAVE_FIELD));
340 private function adjustmentFields($fields)
342 foreach ($fields as $fieldId => &$field)
344 if ($field[
"TYPE"] ==
"ACTIVE_FROM")
346 if ($field[
"DEFAULT_VALUE"] ===
"=now")
347 $field[
"DEFAULT_VALUE"] = ConvertTimeStamp(time()+\CTimeZone::getOffset(),
"FULL");
348 elseif ($field[
"DEFAULT_VALUE"] ===
"=today")
349 $field["DEFAULT_VALUE"] = ConvertTimeStamp(time()+\CTimeZone::getOffset(), "SHORT");
351 elseif ($field["TYPE"] == "L")
354 $propertyEnum = \CIBlockProperty::getPropertyEnum($field[
"ID"]);
355 while ($listEnum = $propertyEnum->fetch())
357 if ($listEnum[
"DEF"] ===
"Y")
359 $field[
"DEFAULT_VALUE"] = $listEnum[
"ID"];
361 $option[$listEnum[
"ID"]] = $listEnum[
"VALUE"];
363 $field[
"DISPLAY_VALUES_FORM"] = $option;
365 elseif ($field[
"TYPE"] ==
"G")
368 $sections = \CIBlockSection::getTreeList([
"IBLOCK_ID" => $field[
"LINK_IBLOCK_ID"]]);
369 while ($section = $sections->getNext())
370 $option[$section[
"ID"]] = str_repeat(
" . ", $section[
"DEPTH_LEVEL"]).$section[
"~NAME"];
371 $field[
"DISPLAY_VALUES_FORM"] = $option;
373 elseif (preg_match(
"/^(E|E:)/", $field[
"TYPE"]))
376 $elements = \CIBlockElement::getList([
"NAME"=>
"ASC"],
377 [
"IBLOCK_ID" => $field[
"LINK_IBLOCK_ID"]],
false,
false, [
"ID",
"NAME"]);
378 while ($element = $elements->fetch())
379 $option[$element[
"ID"]] = $element[
"NAME"];
380 $field[
"DISPLAY_VALUES_FORM"] = $option;
382 elseif ($field[
"TYPE"] ==
"N:Sequence")
384 $sequence = new \CIBlockSequence($field[
"IBLOCK_ID"], $field[
"ID"]);
385 $field[
"USER_TYPE_SETTINGS"][
"VALUE"] = $sequence->getNext();
392 private function getProperty($iblockId, $code)
394 $queryObject = \CIBlockProperty::getList(array(), array(
"IBLOCK_ID" => $iblockId,
"CODE" => $code));
395 return $queryObject->fetch();
trait ErrorableImplementation