Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ContractorProvider.php
1<?php
2
4
10
12{
13 private const MAX_ITEMS_IN_RECENT = 10;
14 private const ENTITY_ID = 'contractor';
15
16 public function __construct(array $options = [])
17 {
18 parent::__construct();
19 }
20
21 public function isAvailable(): bool
22 {
23 return $GLOBALS["USER"]->IsAuthorized();
24 }
25
26 public function getItems(array $ids): array
27 {
28 return $this->getContractorItemsByFilter(['ID' => $ids]);
29 }
30
31 public function getSelectedItems(array $ids): array
32 {
33 return $this->getContractorItemsByFilter(['ID' => $ids]);
34 }
35
36 public function doSearch(SearchQuery $searchQuery, Dialog $dialog): void
37 {
38 $searchQuery->setCacheable(false);
39 $query = $searchQuery->getQuery();
40
41 $filter = [
42 'LOGIC' => 'OR',
43 ['%PERSON_NAME' => $query],
44 ['%COMPANY' => $query],
45 ];
46 $items = $this->getContractorItemsByFilter($filter);
47
48 $dialog->addItems($items);
49 }
50
51 public function fillDialog(Dialog $dialog): void
52 {
53 $dialog->loadPreselectedItems();
54
55 if ($dialog->getItemCollection()->count() > 0)
56 {
57 foreach ($dialog->getItemCollection() as $item)
58 {
59 $dialog->addRecentItem($item);
60 }
61 }
62
63 $recentItemsCount = count($dialog->getRecentItems()->getEntityItems(self::ENTITY_ID));
64
65 if ($recentItemsCount < self::MAX_ITEMS_IN_RECENT)
66 {
67 $amountToSelect = self::MAX_ITEMS_IN_RECENT - $recentItemsCount;
68 $excludedIds = array_keys($dialog->getRecentItems()->getEntityItems(self::ENTITY_ID));
69 $contractors = ContractorTable::getList([
70 'select' => ['ID', 'PERSON_TYPE', 'PERSON_NAME', 'COMPANY'],
71 'order' => ['ID' => 'ASC'],
72 'filter' => ['!ID' => $excludedIds],
73 'limit' => $amountToSelect,
74 ])->fetchAll();
75 foreach ($contractors as $contractor)
76 {
77 $dialog->addRecentItem($this->makeItem($contractor));
78 }
79 }
80 }
81
82 private function getContractorItemsByFilter($filter)
83 {
84 $contractors = ContractorTable::getList([
85 'select' => ['ID', 'PERSON_TYPE', 'PERSON_NAME', 'COMPANY'],
86 'filter' => $filter,
87 ])->fetchAll();
88
89 $items = [];
90 foreach ($contractors as $contractor)
91 {
92 $items[] = $this->makeItem($contractor);
93 }
94
95 return $items;
96 }
97
98 private function makeItem($contractor)
99 {
100 if ((int)$contractor['PERSON_TYPE'] === (int)\Bitrix\Catalog\ContractorTable::TYPE_INDIVIDUAL)
101 {
102 $title = $contractor['PERSON_NAME'];
103 }
104 else
105 {
106 $title = $contractor['COMPANY'];
107 }
108
109 return new Item([
110 'id' => $contractor['ID'],
111 'entityId' => self::ENTITY_ID,
112 'title' => $title,
113 ]);
114 }
115}
static getList(array $parameters=array())
loadPreselectedItems($preselectedMode=true)
Definition dialog.php:389
$GLOBALS['____1444769544']
Definition license.php:1