Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usertypepropertyfieldassembler.php
1<?php
2
4
9use CIBlockProperty;
10use Closure;
11
13{
14 private int $iblockId;
15 private array $customEditableColumnIds;
16 private array $properties;
17
18 public function __construct(int $iblockId, array $customEditableColumnIds)
19 {
20 $this->iblockId = $iblockId;
21 $this->customEditableColumnIds = $customEditableColumnIds;
22
23 parent::__construct(
24 $this->getPropertyColumnsIdsWithUserType()
25 );
26
27 $this->preloadResources();
28 }
29
37 private function preloadResources(): void
38 {
39 global $APPLICATION;
40
41 $APPLICATION->AddHeadScript('/bitrix/js/main/utils.js');
42 }
43
44 private function getPropertyColumnsIdsWithUserType(): array
45 {
46 $result = [];
47
49 'select' => [
50 'ID',
51 ],
52 'filter' => [
53 '=IBLOCK_ID' => $this->iblockId,
54 '!USER_TYPE' => null,
55 ],
56 ]);
57 foreach ($rows as $row)
58 {
59 $result[] = ElementPropertyProvider::getColumnIdByPropertyId($row['ID']);
60 }
61
62 return $result;
63 }
64
65 private function getPropertiesWithUserType(): array
66 {
67 if (!isset($this->properties))
68 {
69 $this->properties = [];
70
71 $usedPropertyIds = ElementPropertyProvider::getPropertyIdsFromColumnsIds($this->getColumnIds());
72 if (!empty($usedPropertyIds))
73 {
75 'filter' => [
76 '=IBLOCK_ID' => $this->iblockId,
77 '!USER_TYPE' => null,
78 '@ID' => $usedPropertyIds,
79 ],
80 ]);
81 foreach ($rows as $row)
82 {
83 $id = $row['ID'];
84
85 $this->properties[$id] = $row;
86 $this->properties[$id]['USER_TYPE_SETTINGS'] = $row['USER_TYPE_SETTINGS_LIST'];
87 $this->properties[$id]['PROPERTY_USER_TYPE'] = CIBlockProperty::GetUserType($row['USER_TYPE']);
88 }
89 }
90 }
91
92 return $this->properties;
93 }
94
95 protected function prepareRow(array $row): array
96 {
97 $row['columns'] ??= [];
98
99 if (($row['data']['ROW_TYPE'] ?? '') !== RowType::ELEMENT)
100 {
101 return $row;
102 }
103
104 foreach ($this->getPropertiesWithUserType() as $propertyId => $property)
105 {
106 $columnId = ElementPropertyProvider::getColumnIdByPropertyId($propertyId);
107 $value = $row['data'][$columnId];
108
109 // view
110 $viewValue = $this->getColumnValue($columnId, $property, $value);
111 if (isset($viewValue))
112 {
113 $row['columns'][$columnId] = is_array($viewValue) ? implode(' / ', $viewValue) : $viewValue;
114 }
115
116 // edit custom
117 $isCustom = in_array($columnId, $this->customEditableColumnIds, false);
118 if ($isCustom)
119 {
120 $row['data']['~' . $columnId] = $this->getEditValue($columnId, $property, $value);
121 }
122 }
123
124 return $row;
125 }
126
127 private function renderUserTypeFunction(Closure $callback, string $name, $value, array $property)
128 {
129 return call_user_func_array(
130 $callback,
131 [
132 $property,
133 $value,
134 [
135 'GRID' => 'PUBLIC',
136 'VALUE' => $name . '[VALUE]',
137 'DESCRIPTION' => $name . '[DESCRIPTION]',
138 ],
139 ]
140 );
141 }
142
143 private function getColumnValue(string $columnId, array $property, $value)
144 {
145 if ($value === null)
146 {
147 return null;
148 }
149 if (isset($property['PROPERTY_USER_TYPE']['GetPublicViewHTML']))
150 {
151 if ($property['MULTIPLE'] === 'Y')
152 {
153 $tmp = [];
154 foreach ($value as $i => $valueItem)
155 {
156 $tmp[] = $this->renderUserTypeFunction(
157 Closure::fromCallable($property['PROPERTY_USER_TYPE']['GetPublicViewHTML']),
158 $columnId . "[n{$i}]",
159 $valueItem,
160 $property
161 );
162 }
163
164 $separator = '';
165
166 $isCommaSeparator = in_array($property['USER_TYPE'], [
169 ]);
170 if ($isCommaSeparator)
171 {
172 $separator = ', ';
173 }
174
175 return join($separator, $tmp);
176 }
177 else
178 {
179 return $this->renderUserTypeFunction(
180 Closure::fromCallable($property['PROPERTY_USER_TYPE']['GetPublicViewHTML']),
181 $columnId,
182 $value,
183 $property
184 );
185 }
186 }
187
188 return null;
189 }
190
191 private function getEditValue(string $columnId, array $property, $value)
192 {
193 if ($property['MULTIPLE'] === 'Y')
194 {
195 if (isset($property['PROPERTY_USER_TYPE']['GetPublicEditHTMLMulty']))
196 {
197 return $this->renderUserTypeFunction(
198 Closure::fromCallable($property['PROPERTY_USER_TYPE']['GetPublicEditHTMLMulty']),
199 $columnId,
200 $value,
201 $property
202 );
203 }
204 elseif (isset($property['PROPERTY_USER_TYPE']['GetPublicEditHTML']))
205 {
206 $tmp = [];
207 foreach ($value as $i => $valueItem)
208 {
209 $tmp[] = $this->renderUserTypeFunction(
210 Closure::fromCallable($property['PROPERTY_USER_TYPE']['GetPublicEditHTML']),
211 $columnId . "[n{$i}]",
212 $valueItem,
213 $property
214 );
215 }
216
217 return join('', $tmp);
218 }
219 }
220 elseif (isset($property['PROPERTY_USER_TYPE']['GetPublicEditHTML']))
221 {
222 return $this->renderUserTypeFunction(
223 Closure::fromCallable($property['PROPERTY_USER_TYPE']['GetPublicEditHTML']),
224 $columnId,
225 $value,
226 $property
227 );
228 }
229
230 return null;
231 }
232}
static getList(array $parameters=array())