Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
StoreProvider.php
1<?php
2
4
18
20{
21 private const STORE_LIMIT = 10;
22 private const ENTITY_ID = 'store';
23
24 public function __construct(array $options = [])
25 {
26 $this->options['searchDisabledStores'] = $options['searchDisabledStores'] ?? true;
27 $this->options['useAddressAsTitle'] = $options['useAddressAsTitle'] ?? true;
28 $this->options['productId'] = (int)($options['productId'] ?? 0);
29
30 if ($this->options['productId'] > 0)
31 {
32 $product = ProductTable::getRow([
33 'filter' => ['=ID' => $this->options['productId']],
34 'select' => ['MEASURE']
35 ]);
36
37 $this->options['measureSymbol'] = $this->getMeasureSymbol((int)$product['MEASURE']);
38 }
39 else
40 {
41 $this->options['measureSymbol'] = '';
42 }
43
44 parent::__construct();
45 }
46
47 private function getMeasureSymbol(int $measureId = null): string
48 {
49 $measureResult = \CCatalogMeasure::getList(
50 array('CODE' => 'ASC'),
51 array(),
52 false,
53 array(),
54 array('CODE', 'SYMBOL_RUS', 'SYMBOL_INTL', 'IS_DEFAULT', 'ID')
55 );
56
57 $default = '';
58 while ($measureFields = $measureResult->Fetch())
59 {
60 $symbol = $measureFields['SYMBOL_RUS'] ?? $measureFields['SYMBOL_INTL'];
61 if ($measureId === (int)$measureFields['ID'])
62 {
63 return HtmlFilter::encode($symbol);
64 }
65
66 if ($measureFields['IS_DEFAULT'] === 'Y')
67 {
68 $default = $symbol;
69 }
70 }
71
72 return HtmlFilter::encode($default);
73 }
74
75 protected function isSearchDisabledStores(): bool
76 {
77 return $this->getOptions()['searchDisabledStores'];
78 }
79
80 protected function isUseAddressAsTitle(): bool
81 {
82 return $this->getOptions()['useAddressAsTitle'];
83 }
84
85 protected function getProductId(): int
86 {
87 return $this->getOptions()['productId'];
88 }
89
90 public function isAvailable(): bool
91 {
92 return $GLOBALS["USER"]->IsAuthorized();
93 }
94
95 public function getItems(array $ids): array
96 {
97 return $this->getStores(['ID' => $ids]);
98 }
99
100 public function getSelectedItems(array $ids): array
101 {
102 return $this->getStores(['=ID' => $ids]);
103 }
104
105 public function doSearch(SearchQuery $searchQuery, Dialog $dialog): void
106 {
107 $searchQuery->setCacheable(false);
108 $query = $searchQuery->getQuery();
109 $filter = [
110 [
111 '%TITLE' => $query,
112 '%ADDRESS' => $query,
113 'LOGIC' => 'OR',
114 ]
115 ];
116 $items = $this->getStores($filter);
117
118 $dialog->addItems($items);
119 }
120
121 public function fillDialog(Dialog $dialog): void
122 {
123 $dialog->loadPreselectedItems();
124
125 if ($dialog->getItemCollection()->count() > 0)
126 {
127 foreach ($dialog->getItemCollection() as $item)
128 {
129 $dialog->addRecentItem($item);
130 }
131 }
132
133 $recentItemsCount = count($dialog->getRecentItems()->getEntityItems(self::ENTITY_ID));
134
135 if ($recentItemsCount < self::STORE_LIMIT)
136 {
137 foreach ($this->getStores() as $store)
138 {
139 $dialog->addRecentItem($store);
140 }
141 }
142 }
143
144 private function getStores(array $filter = []): array
145 {
146 $allowedStores = AccessController::getCurrent()->getPermissionValue(ActionDictionary::ACTION_STORE_VIEW);
147 if (empty($allowedStores))
148 {
149 return [];
150 }
151
152 if (!in_array(PermissionDictionary::VALUE_VARIATION_ALL, $allowedStores, true))
153 {
154 $filter['=ID'] = $allowedStores;
155 }
156
157 $filter['=ACTIVE'] = 'Y';
158
159 $storeProducts = [];
160 if ($this->getProductId() > 0)
161 {
162 $storeProductRaw = StoreProductTable::getList([
163 'filter' => ['=PRODUCT_ID' => $this->getProductId()],
164 'select' => ['STORE_ID', 'AMOUNT', 'QUANTITY_RESERVED'],
165 ]);
166
167 while ($storeProduct = $storeProductRaw->fetch())
168 {
169 $storeProducts[$storeProduct['STORE_ID']] = [
170 'RESERVED' => $storeProduct['QUANTITY_RESERVED'],
171 'AMOUNT' => $storeProduct['AMOUNT'],
172 ];
173 }
174 }
175
176 $storeRaw = StoreTable::getList([
177 'select' => ['ID', 'TITLE', 'ADDRESS', 'IMAGE_ID'],
178 'filter' => $filter,
179 ]);
180
181 $stores = [];
182 while ($store = $storeRaw->fetch())
183 {
184 $store['PRODUCT_AMOUNT'] = 0;
185 if (isset($storeProducts[$store['ID']]))
186 {
187 $store['PRODUCT_AMOUNT'] = $storeProducts[$store['ID']]['AMOUNT'];
188 $store['PRODUCT_RESERVED'] = $storeProducts[$store['ID']]['RESERVED'];
189 }
190
191 if ($store['IMAGE_ID'] !== null)
192 {
193 $store['IMAGE_ID'] = (int)$store['IMAGE_ID'];
194 if ($store['IMAGE_ID'] <= 0)
195 {
196 $store['IMAGE_ID'] = null;
197 }
198 }
199 $store['IMAGE'] =
200 $store['IMAGE_ID'] !== null
201 ? $this->getImageSource($store['IMAGE_ID'])
202 : null
203 ;
204
205 $stores[] = $store;
206 }
207
208 if ($storeProducts)
209 {
210 usort(
211 $stores,
212 static function ($first, $second)
213 {
214 return ($first['PRODUCT_AMOUNT'] > $second['PRODUCT_AMOUNT']) ? -1 : 1;
215 }
216 );
217 }
218
219 $items = [];
220 foreach ($stores as $key => $store)
221 {
222 $store['SORT'] = 100 * $key;
223 $items[] = $this->makeItem($store);
224 }
225
226 return $items;
227 }
228
229 private function getImageSource(int $id): ?string
230 {
231 if ($id <= 0)
232 {
233 return null;
234 }
235
236 $file = \CFile::GetFileArray($id);
237 if (!$file)
238 {
239 return null;
240 }
241
242 return Tools::getImageSrc($file, false) ?: null;
243 }
244
245 private function makeItem($store): Item
246 {
247 $title = $store['TITLE'];
248 if ($title === '')
249 {
250 $title = ($this->isUseAddressAsTitle())
251 ? $store['ADDRESS']
252 : Loc::getMessage('STORE_SELECTOR_EMPTY_TITLE')
253 ;
254 }
255
256 $item = new Item([
257 'id' => $store['ID'],
258 'sort' => $store['SORT'],
259 'entityId' => self::ENTITY_ID,
260 'title' => $title,
261 'subtitle' => $store['ADDRESS'],
262 'avatar' => $store['IMAGE'],
263 'caption' => [
264 'text' =>
265 $this->getProductId() > 0
266 ? $store['PRODUCT_AMOUNT'] . ' ' . $this->getOptions()['measureSymbol']
267 : ''
268 ,
269 'type' => 'html',
270 ],
271 'customData' => [
272 'amount' => (float)$store['PRODUCT_AMOUNT'],
273 'availableAmount' => (float)$store['PRODUCT_AMOUNT'] - (float)$store['PRODUCT_RESERVED'],
274 ],
275 ]);
276
277 return $item;
278 }
279}
doSearch(SearchQuery $searchQuery, Dialog $dialog)
static getRow(array $parameters)
static getList(array $parameters=array())
static encode($string, $flags=ENT_COMPAT, $doubleEncode=true)
loadPreselectedItems($preselectedMode=true)
Definition dialog.php:389
$GLOBALS['____1444769544']
Definition license.php:1