Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BrandProvider.php
1<?php
2
4
12
14{
15 private const BRAND_LIMIT = 20;
16 private const BRAND_ENTITY_ID = 'brand';
17 private const BRAND_CODE = 'BRAND_FOR_FACEBOOK';
18
19 public function __construct(array $options = [])
20 {
21 parent::__construct();
22
23 $this->options['iblockId'] = (int)($options['iblockId'] ?? 0);
24 }
25
26 public function isAvailable(): bool
27 {
28 return $GLOBALS['USER']->isAuthorized();
29 }
30
31 public function getItems(array $ids): array
32 {
33 $items = [];
34
35 $filter = !empty($ids) ? ['=UF_XML_ID' => $ids] : [];
36
37 foreach ($this->getBrands($filter) as $section)
38 {
39 $items[] = $this->makeItem($section);
40 }
41
42 return $items;
43 }
44
45 public function getSelectedItems(array $ids): array
46 {
47 return $this->getItems($ids);
48 }
49
50 public function fillDialog(Dialog $dialog): void
51 {
52 $dialog->loadPreselectedItems();
53
54 if ($dialog->getItemCollection()->count() > 0)
55 {
56 foreach ($dialog->getItemCollection() as $item)
57 {
58 $dialog->addRecentItem($item);
59 }
60 }
61
62 $recentItemsCount = count($dialog->getRecentItems()->getEntityItems(self::BRAND_ENTITY_ID));
63
64 if ($recentItemsCount < self::BRAND_LIMIT)
65 {
66 foreach ($this->getBrands() as $brand)
67 {
68 $dialog->addRecentItem(
69 $this->makeItem($brand)
70 );
71 }
72 }
73 }
74
75 public function doSearch(SearchQuery $searchQuery, Dialog $dialog): void
76 {
77 $filter = [];
78
79 $query = $searchQuery->getQuery();
80 if ($query !== '')
81 {
82 $filter['%UF_NAME'] = $query;
83 }
84
85 foreach ($this->getBrands($filter) as $brand)
86 {
87 $dialog->addItem(
88 $this->makeItem($brand)
89 );
90 }
91
92 if ($dialog->getItemCollection()->count() >= self::BRAND_LIMIT)
93 {
94 $searchQuery->setCacheable(false);
95 }
96 }
97
98 protected function getBrands(array $filter = []): array
99 {
100 if (!Loader::includeModule('highloadblock') || !Loader::includeModule('iblock'))
101 {
102 return [];
103 }
104
105 \Bitrix\Catalog\v2\Integration\Iblock\BrandProperty::createFacebookBrandProperty();
106
107 $catalogId = $this->options['iblockId'];
108 if ($catalogId <= 0)
109 {
110 return [];
111 }
112
113 $propertySettings = PropertyTable::getList([
114 'select' => ['ID', 'USER_TYPE_SETTINGS'],
115 'filter' => [
116 '=IBLOCK_ID' => $catalogId,
117 '=ACTIVE' => 'Y',
118 '=CODE' => self::BRAND_CODE,
119 ],
120 'limit' => 1,
121 ])
122 ->fetch()
123 ;
124
125 if (!$propertySettings)
126 {
127 return [];
128 }
129
130 $propertySettings['USER_TYPE_SETTINGS'] = (
131 $userTypeSettings = CheckSerializedData($propertySettings['USER_TYPE_SETTINGS'])
132 ? unserialize($propertySettings['USER_TYPE_SETTINGS'], ['allowed_classes' => false])
133 : []
134 );
135
136 if (empty($userTypeSettings['TABLE_NAME']))
137 {
138 return [];
139 }
140
141 $table = HL\HighloadBlockTable::getList([
142 'select' => ['TABLE_NAME', 'NAME', 'ID'],
143 'filter' => ['=TABLE_NAME' => $userTypeSettings['TABLE_NAME']],
144 ])
145 ->fetch()
146 ;
147
148 $brandEntity = HL\HighloadBlockTable::compileEntity($table);
149 $brandEntityClass = $brandEntity->getDataClass();
150
151 $parameters = [
152 'select' => ['UF_XML_ID', 'UF_FILE', 'UF_NAME'],
153 ];
154
155 if (!empty($filter))
156 {
157 $parameters['filter'] = $filter;
158 }
159
160 $brands = [];
161 $brandsRaw = $brandEntityClass::getList($parameters);
162 while ($brand = $brandsRaw->fetch())
163 {
164 if (!empty($brand['UF_FILE']))
165 {
166 $brand['LOGO'] = \CFile::resizeImageGet(
167 $brand['UF_FILE'],
168 [
169 'width' => 100,
170 'height' => 100,
171 ],
172 BX_RESIZE_IMAGE_EXACT,
173 false
174 )['src'];
175 }
176
177 $brands[] = $brand;
178 }
179
180 return $brands;
181 }
182
183 protected function makeItem(array $brand): Item
184 {
185 return new Item([
186 'id' => $brand['UF_XML_ID'],
187 'entityId' => self::BRAND_ENTITY_ID,
188 'title' => $brand['UF_NAME'],
189 'avatar' => $brand['LOGO'],
190 ]);
191 }
192}
doSearch(SearchQuery $searchQuery, Dialog $dialog)
loadPreselectedItems($preselectedMode=true)
Definition dialog.php:389
$GLOBALS['____1444769544']
Definition license.php:1