1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
StoreProvider.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\v2\Integration\UI\EntitySelector;
4
5use Bitrix\Catalog\Access\AccessController;
6use Bitrix\Catalog\Access\ActionDictionary;
7use Bitrix\Catalog\Access\Permission\PermissionDictionary;
8use Bitrix\Catalog\ProductTable;
9use Bitrix\Catalog\StoreProductTable;
10use Bitrix\Catalog\StoreTable;
11use Bitrix\Iblock\Component\Tools;
12use Bitrix\Main\Localization\Loc;
13use Bitrix\Main\Text\HtmlFilter;
14use Bitrix\UI\EntitySelector\BaseProvider;
15use Bitrix\UI\EntitySelector\Dialog;
16use Bitrix\UI\EntitySelector\Item;
17use Bitrix\UI\EntitySelector\SearchQuery;
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
185 $store['PRODUCT_AMOUNT'] = $storeProducts[$store['ID']]['AMOUNT'] ?? 0;
186 $store['PRODUCT_RESERVED'] = $storeProducts[$store['ID']]['RESERVED'] ?? 0;
187
188 if ($store['IMAGE_ID'] !== null)
189 {
190 $store['IMAGE_ID'] = (int)$store['IMAGE_ID'];
191 if ($store['IMAGE_ID'] <= 0)
192 {
193 $store['IMAGE_ID'] = null;
194 }
195 }
196 $store['IMAGE'] =
197 $store['IMAGE_ID'] !== null
198 ? $this->getImageSource($store['IMAGE_ID'])
199 : null
200 ;
201
202 $stores[] = $store;
203 }
204
205 if ($storeProducts)
206 {
207 usort(
208 $stores,
209 static function ($first, $second)
210 {
211 return ($first['PRODUCT_AMOUNT'] > $second['PRODUCT_AMOUNT']) ? -1 : 1;
212 }
213 );
214 }
215
216 $items = [];
217 foreach ($stores as $key => $store)
218 {
219 $store['SORT'] = 100 * $key;
220 $items[] = $this->makeItem($store);
221 }
222
223 return $items;
224 }
225
226 private function getImageSource(int $id): ?string
227 {
228 if ($id <= 0)
229 {
230 return null;
231 }
232
233 $file = \CFile::GetFileArray($id);
234 if (!$file)
235 {
236 return null;
237 }
238
239 return Tools::getImageSrc($file, false) ?: null;
240 }
241
242 private function makeItem($store): Item
243 {
244 $title = trim((string)$store['TITLE']);
245 if ($title === '')
246 {
247 $title = ($this->isUseAddressAsTitle())
248 ? $store['ADDRESS']
249 : Loc::getMessage('STORE_SELECTOR_EMPTY_TITLE')
250 ;
251 }
252
253 return new Item([
254 'id' => $store['ID'],
255 'sort' => $store['SORT'],
256 'entityId' => self::ENTITY_ID,
257 'title' => $title,
258 'subtitle' => $store['ADDRESS'],
259 'avatar' => $store['IMAGE'],
260 'caption' => [
261 'text' =>
262 $this->getProductId() > 0
263 ? $store['PRODUCT_AMOUNT'] . ' ' . $this->getOptions()['measureSymbol']
264 : ''
265 ,
266 'type' => 'html',
267 ],
268 'customData' => [
269 'amount' => (float)$store['PRODUCT_AMOUNT'],
270 'availableAmount' => (float)$store['PRODUCT_AMOUNT'] - (float)$store['PRODUCT_RESERVED'],
271 ],
272 ]);
273 }
274}
if(isset( $_REQUEST["mode"]) &&$_REQUEST["mode"]=="ajax") if(isset($_REQUEST["mode"]) && $_REQUEST["mode"]=="save_lru" &&check_bitrix_sessid()) $first
Определения access_dialog.php:54
doSearch(SearchQuery $searchQuery, Dialog $dialog)
Определения StoreProvider.php:105
static getRow(array $parameters)
Определения datamanager.php:398
static getList(array $parameters=array())
Определения datamanager.php:431
static encode($string, $flags=ENT_COMPAT, $doubleEncode=true)
Определения htmlfilter.php:12
loadPreselectedItems($preselectedMode=true)
Определения dialog.php:410
addItems(array $items)
Определения dialog.php:135
addRecentItem(Item $item)
Определения dialog.php:143
getItemCollection()
Определения dialog.php:116
setCacheable(bool $flag=true)
Определения searchquery.php:72
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
$filter
Определения iblock_catalog_list.php:54
$GLOBALS['____1690880296']
Определения license.php:1
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$items
Определения template.php:224
$title
Определения pdf.php:123