Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BrandProperty.php
1<?php
2
4
11
12final class BrandProperty
13{
14 public const PROPERTY_CODE = 'BRAND_FOR_FACEBOOK';
15
16 public static function createFacebookBrandProperty(): void
17 {
18 if (!Loader::includeModule('crm'))
19 {
20 return;
21 }
22
23 $crmCatalogIblockId = \Bitrix\Crm\Product\Catalog::getDefaultId();
24
25 if (!Loader::includeModule('iblock') || !self::checkIblock($crmCatalogIblockId))
26 {
27 return;
28 }
29
30 $propertyId = self::loadProperty($crmCatalogIblockId);
31 if (!$propertyId)
32 {
33 self::createProperty($crmCatalogIblockId);
34 }
35 }
36
37 private static function checkIblock(int $iblockId): bool
38 {
39 $iblock = IblockTable::getList([
40 'select' => ['ID'],
41 'filter' => ['=ID' => $iblockId],
42 ])
43 ->fetch()
44 ;
45
46 return !empty($iblock);
47 }
48
49 private static function loadProperty(int $iblockId): ?int
50 {
51 $property = PropertyTable::getList([
52 'select' => ['ID'],
53 'filter' => [
54 '=IBLOCK_ID' => $iblockId,
55 '=CODE' => self::PROPERTY_CODE,
56 '=ACTIVE' => 'Y',
57 ],
58 ])
59 ->fetch()
60 ;
61 if (!empty($property))
62 {
63 return (int)$property['ID'];
64 }
65
66 return null;
67 }
68
69 private static function createProperty(int $iblockId): ?int
70 {
71 $highloadBlockTable = self::createHighloadBlockTable();
72 if (!$highloadBlockTable)
73 {
74 return null;
75 }
76
77 $propertyDescription = self::getPropertyDescription($iblockId, $highloadBlockTable);
78 $propertyId = (new \CIBlockProperty())->Add($propertyDescription);
79
80 return (int)$propertyId ?: null;
81 }
82
83 private static function getPropertyDescription(int $iblockId, string $highloadBlockTable): array
84 {
85 return [
86 'NAME' => Loc::getMessage('CATALOG_BRAND_PROPERTY_NAME'),
87 'CODE' => self::PROPERTY_CODE,
88 'XML_ID' => self::PROPERTY_CODE,
89 'IBLOCK_ID' => $iblockId,
90 'PROPERTY_TYPE' => PropertyTable::TYPE_STRING,
91 'USER_TYPE' => 'directory',
92 'USER_TYPE_SETTINGS' => [
93 'size' => 1,
94 'width' => 0,
95 'group' => 'N',
96 'multiple' => 'N',
97 'TABLE_NAME' => $highloadBlockTable,
98 ],
99 'MULTIPLE' => 'Y',
100 'MULTIPLE_CNT' => 1,
101 'WITH_DESCRIPTION' => 'N',
102 'SORT' => 200,
103 ];
104 }
105
106 private static function createHighloadBlockTable(): ?string
107 {
108 if (!Loader::includeModule('highloadblock'))
109 {
110 return null;
111 }
112
113 $uniqId = uniqid('BRAND_FOR_FACEBOOK_', false);
114 $className = StringHelper::snake2camel($uniqId);
115 $tableName = mb_strtolower(\CIBlockPropertyDirectory::createHighloadTableName($uniqId));
116
117 $addResult = HighloadBlockTable::add([
118 'NAME' => $className,
119 'TABLE_NAME' => $tableName,
120 ]);
121 if (!$addResult->isSuccess())
122 {
123 return null;
124 }
125
126 $userFieldEntity = new \CUserTypeEntity();
127
128 foreach (self::getUserFieldDescriptions((string)$addResult->getId()) as $description)
129 {
130 $userFieldEntity->Add($description);
131 }
132
133 return $tableName;
134 }
135
136 private static function getUserFieldDescriptions(string $highloadBlockId): array
137 {
138 return [
139 [
140 'ENTITY_ID' => 'HLBLOCK_' . $highloadBlockId,
141 'FIELD_NAME' => 'UF_NAME',
142 'USER_TYPE_ID' => 'string',
143 'XML_ID' => 'UF_BRAND_NAME',
144 'SORT' => '100',
145 'MULTIPLE' => 'N',
146 'MANDATORY' => 'N',
147 'SHOW_FILTER' => 'N',
148 'SHOW_IN_LIST' => 'Y',
149 'EDIT_IN_LIST' => 'Y',
150 'IS_SEARCHABLE' => 'Y',
151 ],
152 [
153 'ENTITY_ID' => 'HLBLOCK_' . $highloadBlockId,
154 'FIELD_NAME' => 'UF_FILE',
155 'USER_TYPE_ID' => 'file',
156 'XML_ID' => 'UF_BRAND_FILE',
157 'SORT' => '200',
158 'MULTIPLE' => 'N',
159 'MANDATORY' => 'N',
160 'SHOW_FILTER' => 'N',
161 'SHOW_IN_LIST' => 'Y',
162 'EDIT_IN_LIST' => 'Y',
163 'IS_SEARCHABLE' => 'Y',
164 ],
165 [
166 'ENTITY_ID' => 'HLBLOCK_' . $highloadBlockId,
167 'FIELD_NAME' => 'UF_LINK',
168 'USER_TYPE_ID' => 'string',
169 'XML_ID' => 'UF_BRAND_LINK',
170 'SORT' => '300',
171 'MULTIPLE' => 'N',
172 'MANDATORY' => 'N',
173 'SHOW_FILTER' => 'N',
174 'SHOW_IN_LIST' => 'Y',
175 'EDIT_IN_LIST' => 'Y',
176 'IS_SEARCHABLE' => 'Y',
177 ],
178 [
179 'ENTITY_ID' => 'HLBLOCK_' . $highloadBlockId,
180 'FIELD_NAME' => 'UF_DESCRIPTION',
181 'USER_TYPE_ID' => 'string',
182 'XML_ID' => 'UF_BRAND_DESCR',
183 'SORT' => '400',
184 'MULTIPLE' => 'N',
185 'MANDATORY' => 'N',
186 'SHOW_FILTER' => 'N',
187 'SHOW_IN_LIST' => 'Y',
188 'EDIT_IN_LIST' => 'Y',
189 'IS_SEARCHABLE' => 'Y',
190 ],
191 [
192 'ENTITY_ID' => 'HLBLOCK_' . $highloadBlockId,
193 'FIELD_NAME' => 'UF_FULL_DESCRIPTION',
194 'USER_TYPE_ID' => 'string',
195 'XML_ID' => 'UF_BRAND_FULL_DESCR',
196 'SORT' => '500',
197 'MULTIPLE' => 'N',
198 'MANDATORY' => 'N',
199 'SHOW_FILTER' => 'N',
200 'SHOW_IN_LIST' => 'Y',
201 'EDIT_IN_LIST' => 'Y',
202 'IS_SEARCHABLE' => 'Y',
203 ],
204 [
205 'ENTITY_ID' => 'HLBLOCK_' . $highloadBlockId,
206 'FIELD_NAME' => 'UF_SORT',
207 'USER_TYPE_ID' => 'double',
208 'XML_ID' => 'UF_BRAND_SORT',
209 'SORT' => '600',
210 'MULTIPLE' => 'N',
211 'MANDATORY' => 'N',
212 'SHOW_FILTER' => 'N',
213 'SHOW_IN_LIST' => 'Y',
214 'EDIT_IN_LIST' => 'Y',
215 'IS_SEARCHABLE' => 'N',
216 ],
217 [
218 'ENTITY_ID' => 'HLBLOCK_' . $highloadBlockId,
219 'FIELD_NAME' => 'UF_EXTERNAL_CODE',
220 'USER_TYPE_ID' => 'string',
221 'XML_ID' => 'UF_BRAND_EXTERNAL_CODE',
222 'SORT' => '700',
223 'MULTIPLE' => 'N',
224 'MANDATORY' => 'N',
225 'SHOW_FILTER' => 'N',
226 'SHOW_IN_LIST' => 'Y',
227 'EDIT_IN_LIST' => 'Y',
228 'IS_SEARCHABLE' => 'N',
229 ],
230 [
231 'ENTITY_ID' => 'HLBLOCK_' . $highloadBlockId,
232 'FIELD_NAME' => 'UF_XML_ID',
233 'USER_TYPE_ID' => 'string',
234 'XML_ID' => 'UF_XML_ID',
235 'SORT' => '800',
236 'MULTIPLE' => 'N',
237 'MANDATORY' => 'Y',
238 'SHOW_FILTER' => 'N',
239 'SHOW_IN_LIST' => 'Y',
240 'EDIT_IN_LIST' => 'Y',
241 'IS_SEARCHABLE' => 'N',
242 ],
243 ];
244 }
245}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29