Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entityselectorfieldadapter.php
1<?php
3
8
10{
11 private $isMultiple;
12 private $isValueContainEntityId;
13 private $dialogOptions = [];
14
15 public function __construct(array $fieldOptions = [])
16 {
17 $this->isValueContainEntityId = (bool)($fieldOptions['ADD_ENTITY_ID_TO_RESULT'] ?? false);
18 $this->isMultiple = (bool)($fieldOptions['MULTIPLE'] ?? false);
19 $this->dialogOptions =
20 isset($fieldOptions['DIALOG_OPTIONS']) && is_array($fieldOptions['DIALOG_OPTIONS'])
21 ? $fieldOptions['DIALOG_OPTIONS']
22 : []
23 ;
24 }
25
32 public function getLabel(string $value): string
33 {
34 if (empty($value))
35 {
36 return '';
37 }
38 $result = $this->getLabels([$value]);
39
40 return ($result[0] ?? '');
41 }
42
50 public function getLabels(array $values): array
51 {
52 $result = [];
53
54 $entities = $this->dialogOptions['entities'] ?? [];
55 $staticItems = $this->dialogOptions['items'] ?? [];
56
57 $hasEntities = (is_array($entities) && !empty($entities));
58 $hasStaticItems = (is_array($staticItems) && !empty($staticItems));
59
60 if (
61 (!$hasEntities && !$hasStaticItems)
62 || empty($values)
63 || !Loader::includeModule('ui')
64 )
65 {
66 return $result;
67 }
68
69 if ($hasEntities)
70 {
71 $result = array_merge($result, $this->getLabelsFromEntities($entities, $values));
72 }
73
74 if ($hasStaticItems)
75 {
76 $result = array_merge($result, $this->getLabelsFromStaticItems($staticItems, $values));
77 }
78
79 return $result;
80 }
81
82 private function getLabelsFromEntities(array $entities, array $values): array
83 {
84 $result = [];
85 foreach ($entities as $entityOptions)
86 {
87 $entity = Entity::create($entityOptions);
88 if ($entity)
89 {
90 $entityValues = $this->getSupposedEntityValues($entity->getId(), $values);
91
92 $itemTitles = [];
94 foreach ($entity->getProvider()->getItems($entityValues) as $item)
95 {
96 $itemTitles[$this->getItemId($item)] = $item->getTitle();
97 }
98 foreach ($values as $valueId)
99 {
100 $result['_labels'][] = $itemTitles[$valueId] ?? ('#' . $valueId);
101 }
102
103 if (!$this->isValueContainEntityId)
104 {
105 break;
106 }
107 }
108 }
109
110 return $result;
111 }
112
113 private function getLabelsFromStaticItems(array $staticItems, $values)
114 {
115 $result = [];
116
117 $itemsByEntity = [];
118 foreach ($staticItems as $itemOptions)
119 {
120 $item = new Item($itemOptions);
121 $itemEntityId = $item->getEntityId();
122 if (!isset($itemsByEntity[$itemEntityId]))
123 {
124 $itemsByEntity[$itemEntityId] = $this->getSupposedEntityValues($item->getEntityId(), $values);
125 }
126 if (in_array((string)$item->getId(), $itemsByEntity[$itemEntityId], true))
127 {
128 $result[] = $item->getTitle();
129 }
130 }
131
132 return $result;
133 }
134
135 private function getSupposedEntityValues(string $entityId, array $values): array
136 {
137 $result = [];
138 foreach ($values as $value)
139 {
140 if ($this->isValueContainEntityId)
141 {
142 $entityValue = $this->extractEntityValue((string)$value, $entityId);
143 if ($entityValue)
144 {
145 $result[] = $entityValue;
146 }
147 }
148 else
149 {
150 $result[] = $value;
151 }
152 }
153
154 return $result;
155 }
156
157 private function extractEntityValue(string $value, string $entityId)
158 {
159 try
160 {
161 $parsedValue = Json::decode($value);
162 if (
163 isset($parsedValue[0])
164 && isset($parsedValue[1])
165 && $parsedValue[0] === $entityId
166 )
167 {
168 return $parsedValue[1];
169 }
170 }
171 catch (\Bitrix\Main\ArgumentException $e)
172 {
173 }
174
175 return null;
176 }
177
178 private function getItemId(Item $item): string
179 {
180 if ($this->isValueContainEntityId)
181 {
182 return Json::encode([(string)$item->getEntityId(), (string)$item->getId()]);
183 }
184 else
185 {
186 return (string)$item->getId();
187 }
188 }
189}
static includeModule($moduleName)
Definition loader.php:69
static create(array $entityOptions)
Definition entity.php:49