Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
IBlockElementToAdd.php
1<?php
2
4
10
11final class IBlockElementToAdd extends Data
12{
13 private int $iBlockId;
14 private int $sectionId;
15 private array $values;
16 private int $createdBy;
17
24 private function __construct(
25 int $iBlockId,
26 int $sectionId,
27 array $values,
28 int $createdBy,
29 )
30 {
31 $this->iBlockId = $iBlockId;
32 $this->sectionId = $sectionId;
33 $this->values = $values;
34 $this->createdBy = $createdBy;
35 }
36
42 public static function createFromRequest($request): self
43 {
44 $iBlockId = self::validateId($request->iBlockId);
45 if ($iBlockId === null || $iBlockId === 0)
46 {
47 throw new ArgumentOutOfRangeException('iBlockId', 1, null);
48 }
49
50 $sectionId = self::validateId($request->sectionId);
51 if ($sectionId === null)
52 {
53 throw new ArgumentOutOfRangeException('sectionId', 0, null);
54 }
55
56 $createdBy = self::validateId($request->createdByUserId);
57 if ($createdBy === null || $createdBy === 0)
58 {
59 throw new ArgumentOutOfRangeException('createdBy', 1, null);
60 }
61
62 $values = self::validateValues($request->values, $iBlockId, $sectionId);
63
64 return new self($iBlockId, $sectionId, $values, $createdBy);
65 }
66
73 private static function validateValues(array $values, int $iBlockId, int $sectionId): array
74 {
75 unset($values['ID']);
76 $values['IBLOCK_ID'] = $iBlockId;
77 $values['IBLOCK_SECTION_ID'] = $sectionId;
78
79 return $values;
80 }
81
85 public function getIBlockId(): int
86 {
87 return $this->iBlockId;
88 }
89
93 public function getSectionId(): int
94 {
95 return $this->sectionId;
96 }
97
101 public function getOriginalValues(): array
102 {
103 return $this->values;
104 }
105
109 public function getCreatedBy(): int
110 {
111 return $this->createdBy;
112 }
113
118 public function getFieldValueById(string $fieldsId): mixed
119 {
120 return $this->values[$fieldsId] ?? null;
121 }
122
123 public function getElementValues(array $fields, array $props): Result
124 {
125 $result = new Result();
126
127 $element = [
128 'IBLOCK_ID' => $this->getIBlockId(),
129 'IBLOCK_SECTION_ID' => $this->getSectionId(),
130 'MODIFIED_BY' => $this->getCreatedBy(),
131 ];
132
133 $preparedFields = $this->prepareFields($fields, $result);
134 $element = array_merge($element, $preparedFields);
135 unset($element['TIMESTAMP_X']);
136
137 $preparedProps = $this->prepareProps($props, $result);
138 if ($preparedProps)
139 {
140 $element['PROPERTY_VALUES'] = $preparedProps;
141 }
142
143 return $result->setData(['element' => $element]);
144 }
145
146 private function prepareFields(array $fields, Result $result): array
147 {
148 $prepared = [];
149 foreach ($fields as $fieldId => $property)
150 {
151 $showAddForm = !in_array($fieldId, ['DATE_CREATE', 'TIMESTAMP_X', 'CREATED_BY', 'MODIFIED_BY']);
152 if (isset($property['SETTINGS']['SHOW_ADD_FORM']))
153 {
154 $showAddForm = $property['SETTINGS']['SHOW_ADD_FORM'] === 'Y';
155 }
156
157 if (in_array($fieldId, ['PREVIEW_TEXT', 'DETAIL_TEXT'], true))
158 {
159 $useEditor = isset($property['SETTINGS']['USE_EDITOR']) && $property['SETTINGS']['USE_EDITOR'] === 'Y';
160 $prepared[$fieldId . '_TYPE'] = $useEditor ? 'html' : 'text';
161 }
162
163 $prepared[$fieldId] =
164 $showAddForm ? $this->getFieldValueById($fieldId) : ($property['DEFAULT_VALUE'] ?? null)
165 ;
166 }
167
168 return $prepared;
169 }
170
171 private function prepareProps(array $props, Result $result): array
172 {
173 $prepared = [];
174 foreach ($props as $propId => $property)
175 {
176 $baseType = $property['TYPE'];
177 $type = $property['PROPERTY_TYPE'];
178 $isMultiple = $property['MULTIPLE'] === 'Y';
179
180 $showAddForm =
181 isset($property['SETTINGS']['SHOW_ADD_FORM']) && $property['SETTINGS']['SHOW_ADD_FORM'] === 'Y'
182 ;
183 $defaultValue = ($property['DEFAULT_VALUE'] ?? null);
184 $requestValues = $this->getFieldValueById($propId);
185
186 if ($showAddForm && is_array($requestValues))
187 {
188 $values = [];
189 foreach ($requestValues as $key => $value)
190 {
191 if (in_array($type, ['L', 'E', 'G'], true))
192 {
193 if ($isMultiple)
194 {
195 $values[$key] = $value;
196 continue;
197 }
198
199 $values = $value;
200 break;
201 }
202
203 if ($type === 'N' && !empty($value))
204 {
205 if (is_numeric($value))
206 {
207 $value = (double)$value;
208 }
209 else
210 {
211 // todo: Loc
212 $result->addError(new Error('incorrect number value'));
213 break 2;
214 }
215 }
216
217 if ($type === 'S' && $baseType === 'S:HTML')
218 {
219 $value =
220 is_array($value) && isset($value['TYPE'], $value['TEXT'])
221 ? $value
222 : ['TYPE' => 'html', 'TEXT' => (string)$value]
223 ;
224 }
225
226 $values[$key] = ['VALUE' => $value];
227
228 if (!$isMultiple)
229 {
230 break;
231 }
232 }
233
234 $prepared[$property['ID']] = $values;
235 }
236 elseif ($defaultValue)
237 {
238 $prepared[$property['ID']] = [
239 'n0' => ['VALUE' => $defaultValue],
240 ];
241 }
242 }
243
244 return $prepared;
245 }
246}
static validateId(int $id)
Definition Data.php:9