Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
admingridoption.php
1<?php
3
4use Bitrix\Main\Update\AdminGridOption as BaseAdminGridOption;
7
8
15class AdminGridOption extends BaseAdminGridOption
16{
17 private const COMMON = 'C';
18 private const PERSONAL = 'P';
19
20 protected static $moduleId = "iblock";
21
22 public static function showNewProductNameFieldsAgent(): string
23 {
24 if (
25 !Loader::includeModule('crm')
26 || !Loader::includeModule('catalog')
27 || !Loader::includeModule('bitrix24')
28 )
29 {
30 return '';
31 }
32
33 self::showProductField();
34 self::showNameCodeField('CATALOG_PRODUCT_CARD', self::COMMON);
35 self::showNameCodeField('CATALOG_PRODUCT_CARD', self::PERSONAL);
36 self::showNameCodeField('CATALOG_VARIATION_CARD', self::COMMON);
37 self::showNameCodeField('CATALOG_VARIATION_CARD', self::PERSONAL);
38
39 return '';
40 }
41
42 private static function showProductField(): void
43 {
44 $crmCatalogId = \CCrmCatalog::GetDefaultID();
45
46 if ($crmCatalogId === 0)
47 {
48 return;
49 }
50
51 $tablePrefix = 'tbl_product_list_';
52 $listId = 'CRM_PRODUCT_CATALOG.' . $crmCatalogId;
53
54 if (Catalog\Config\State::isProductCardSliderEnabled())
55 {
56 $listId .= '.NEW';
57 }
58
59 $tableId = $tablePrefix . md5($listId);
60
61 $productGridOptions = new \Bitrix\Main\Grid\Options($tableId);
62 $visibleColumns = $productGridOptions->GetVisibleColumns();
63
64 if (empty($visibleColumns))
65 {
66 return;
67 }
68
69 $hasProductField = in_array('CATALOG_PRODUCT', $visibleColumns, true);
70 $hasMorePhotoField = in_array('MORE_PHOTO', $visibleColumns, true);
71
72 $oldNameFieldIndex = array_search('NAME', $visibleColumns, true);
73
74 if ($hasProductField)
75 {
76 if ($oldNameFieldIndex !== false)
77 {
78 unset($visibleColumns[$oldNameFieldIndex]);
79 }
80 elseif ($hasMorePhotoField)
81 {
82 return;
83 }
84 }
85 else
86 {
87 if ($oldNameFieldIndex !== false)
88 {
89 $visibleColumns[$oldNameFieldIndex] = 'CATALOG_PRODUCT';
90 }
91 else
92 {
93 array_unshift($visibleColumns, 'CATALOG_PRODUCT');
94 }
95 }
96
97 if (!$hasMorePhotoField)
98 {
99 $catalogProductFieldIndex = array_search('CATALOG_PRODUCT', $visibleColumns, true);
100 $visibleColumnsBefore = array_slice($visibleColumns, 0, $catalogProductFieldIndex + 1);
101 $visibleColumnsAfter = array_slice($visibleColumns, $catalogProductFieldIndex + 1);
102 $visibleColumns = array_merge($visibleColumnsBefore, ['MORE_PHOTO'], $visibleColumnsAfter);
103 }
104
105 $productGridOptions->SetVisibleColumns($visibleColumns);
106 }
107
108 private static function showNameCodeField(string $configId, string $scope): void
109 {
110 if (!Loader::includeModule('ui'))
111 {
112 return;
113 }
114
115 $entityEditorConfiguration = new \Bitrix\UI\Form\EntityEditorConfiguration('ui.form.editor');
116 $cardSettings = $entityEditorConfiguration->get($configId, $scope);
117
118 if (!is_array($cardSettings))
119 {
120 return;
121 }
122
123 $hasOldNameField = self::hasField($cardSettings, 'NAME');
124 $hasOldCodeField = self::hasField($cardSettings, 'CODE');
125 $hasNameCodeField = self::hasField($cardSettings, 'NAME-CODE');
126
127 if ($hasNameCodeField)
128 {
129 if (!$hasOldNameField && !$hasOldCodeField)
130 {
131 return;
132 }
133
134 if ($hasOldNameField)
135 {
136 $cardSettings = self::unsetField($cardSettings, 'NAME');
137 }
138 }
139 else
140 {
141 $nameCodeField = [
142 'name' => 'NAME-CODE',
143 'optionFlags' => '1',
144 'options' => [
145 'showCode' => $hasOldCodeField ? 'true' : 'false',
146 ]
147 ];
148
149 if (!$hasOldNameField)
150 {
151 $cardSettings = self::setField($cardSettings, $nameCodeField);
152 }
153 else
154 {
155 $cardSettings = self::replaceField($cardSettings, 'NAME', $nameCodeField);
156 }
157 }
158
159 if ($hasOldCodeField)
160 {
161 $cardSettings = self::unsetField($cardSettings, 'CODE');
162 }
163
164 $entityEditorConfiguration->set($configId, $cardSettings, ['scope' => $scope]);
165 }
166
167 private static function hasField(array $cardSettings, string $fieldName): bool
168 {
169 foreach ($cardSettings as $column)
170 {
171 foreach ($column['elements'] as $columnElement)
172 {
173 if ($columnElement['type'] !== 'section')
174 {
175 continue;
176 }
177
178 foreach ($columnElement['elements'] as $element)
179 {
180 if ($element['name'] === $fieldName)
181 {
182 return true;
183 }
184 }
185 }
186 }
187
188 return false;
189 }
190
191 private static function unsetField(array $cardSettings, string $fieldName): array
192 {
193 foreach ($cardSettings as $columnKey => $column)
194 {
195 foreach ($column['elements'] as $columnElementKey => $columnElement)
196 {
197 if ($columnElement['type'] !== 'section')
198 {
199 continue;
200 }
201
202 foreach ($columnElement['elements'] as $elementKey => $element)
203 {
204 if ($element['name'] === $fieldName)
205 {
206 unset ($cardSettings[$columnKey]['elements'][$columnElementKey]['elements'][$elementKey]);
207 return $cardSettings;
208 }
209 }
210 }
211 }
212
213 return $cardSettings;
214 }
215
216 private static function replaceField(array $cardSettings, string $oldFieldName, array $newField): array
217 {
218 foreach ($cardSettings as $columnKey => $column)
219 {
220 foreach ($column['elements'] as $columnElementKey => $columnElement)
221 {
222 if ($columnElement['type'] !== 'section')
223 {
224 continue;
225 }
226
227 foreach ($columnElement['elements'] as $elementKey => $element)
228 {
229 if ($element['name'] === $oldFieldName)
230 {
231 $cardSettings[$columnKey]['elements'][$columnElementKey]['elements'][$elementKey] = $newField;
232 return $cardSettings;
233 }
234 }
235 }
236 }
237
238 return $cardSettings;
239 }
240
241 private static function setField(array $cardSettings, array $newField): array
242 {
243 foreach ($cardSettings as $columnKey => $column)
244 {
245 foreach ($column['elements'] as $columnElementKey => $columnElement)
246 {
247 if ($columnElement['name'] === 'main')
248 {
249 array_unshift(
250 $cardSettings[$columnKey]['elements'][$columnElementKey]['elements'],
251 $newField
252 );
253 return $cardSettings;
254 }
255 }
256 }
257
258 return $cardSettings;
259 }
260}