Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entityufdataprovider.php
1<?php
2namespace Bitrix\Main\Filter;
3
5
7{
9 protected $settings = null;
10 protected $ufReserved = [];
11
13 {
14 $this->settings = $settings;
15 }
16
21 public function getSettings()
22 {
23 return $this->settings;
24 }
25
30 public function getUserFieldEntityID()
31 {
32 return $this->settings->getUserFieldEntityID();
33 }
34
39 protected function getUserFields()
40 {
41 global $USER_FIELD_MANAGER;
42
43 static $result = [];
44
45 $entityId = $this->getUserFieldEntityID();
46 if (!isset($result[$entityId]))
47 {
48 $result[$entityId] = $USER_FIELD_MANAGER->getUserFields($entityId, 0, LANGUAGE_ID, false);
49 $result[$entityId] = $this->postFilterFields($result[$entityId]);
50 }
51 return $result[$entityId];
52 }
53
58 public function prepareFields()
59 {
60 $result = array();
61 foreach($this->getUserFields() as $fieldName => $userField)
62 {
63 if (
64 $userField['SHOW_FILTER'] === 'N'
65 || $userField['USER_TYPE']['BASE_TYPE'] === 'file'
66 )
67 {
68 continue;
69 }
70
71 $typeID = $userField['USER_TYPE']['USER_TYPE_ID'];
72 //$isMultiple = isset($userField['MULTIPLE']) && $userField['MULTIPLE'] === 'Y';
73
74 $fieldLabel = $userField['LIST_FILTER_LABEL'] ?? '';
75 if($fieldLabel === '')
76 {
77 if(isset($userField['LIST_COLUMN_LABEL']))
78 {
79 $fieldLabel = $userField['LIST_COLUMN_LABEL'];
80 }
81 elseif(isset($userField['EDIT_FORM_LABEL']))
82 {
83 $fieldLabel = $userField['EDIT_FORM_LABEL'];
84 }
85 }
86 if ($fieldLabel == '')
87 {
88 $fieldLabel = $fieldName;
89 }
90
91 if($typeID === 'employee')
92 {
93 $result[$fieldName] = $this->createField(
94 $fieldName,
95 [
96 'type' => 'entity_selector',
97 'name' => $fieldLabel,
98 'partial' => true,
99 ]
100 );
101 }
102 elseif($typeID === 'string' || $typeID === 'url' || $typeID === 'address' || $typeID === 'money')
103 {
104 $result[$fieldName] = $this->createField(
105 $fieldName,
106 [
107 'type' => 'text',
108 'name' => $fieldLabel,
109 'data' => [
110 'additionalFilter' => [
111 'isEmpty',
112 'hasAnyValue',
113 ],
114 ],
115 ]
116 );
117 continue;
118 }
119 elseif($typeID === 'integer' || $typeID === 'double')
120 {
121 $result[$fieldName] = $this->createField(
122 $fieldName,
123 [
124 'type' => 'number',
125 'name' => $fieldLabel,
126 'data' => [
127 'additionalFilter' => [
128 'isEmpty',
129 'hasAnyValue',
130 ],
131 ],
132 ]
133 );
134 continue;
135 }
136 elseif($typeID === 'boolean')
137 {
138 $result[$fieldName] = $this->createField(
139 $fieldName,
140 array(
141 'type' => 'checkbox',
142 'name' => $fieldLabel,
143 'data' => array('valueType' => 'numeric')
144 )
145 );
146 }
147 elseif($typeID === 'datetime' || $typeID === 'date')
148 {
149 $result[$fieldName] = $this->createField(
150 $fieldName,
151 [
152 'type' => 'date',
153 'name' => $fieldLabel,
154 'data' =>
155 [
156 'time' => $typeID === 'datetime',
157 'additionalFilter' => [
158 'isEmpty',
159 'hasAnyValue',
160 ],
161 ],
162 ]
163 );
164 }
165 elseif(
166 ($typeID === 'enumeration' || $typeID === 'crm_status')
167 && isset($userField['SETTINGS']['DISPLAY'])
168 && $userField['SETTINGS']['DISPLAY'] === EnumType::DISPLAY_DIALOG
169 )
170 {
171 $result[$fieldName] = $this->createField(
172 $fieldName,
173 [
174 'type' => 'entity_selector',
175 'name' => $fieldLabel,
176 'partial' => true,
177 ]
178 );
179 }
180 elseif(
181 $typeID === 'enumeration'
182 || $typeID === 'crm_status'
183 || $typeID === 'iblock_element'
184 || $typeID === 'iblock_section'
185 )
186 {
187 $result[$fieldName] = $this->createField(
188 $fieldName,
189 [
190 'type' => 'list',
191 'name' => $fieldLabel,
192 'partial' => true,
193 ]
194 );
195 }
196 elseif($typeID === 'crm')
197 {
198 $result[$fieldName] = $this->createField(
199 $fieldName,
200 array(
201 'type' => 'dest_selector',
202 'name' => $fieldLabel,
203 'partial' => true
204 )
205 );
206 }
207 else
208 {
209 $result[$fieldName] = $this->createField(
210 $fieldName,
211 array(
212 'type' => 'custom',
213 'name' => $fieldLabel,
214 'data' => array('value' => '')
215 )
216 );
217 }
218 }
219
220 return $result;
221 }
222
228 public function prepareFieldData($fieldID)
229 {
230 $userFields = $this->getUserFields();
231 if(!isset($userFields[$fieldID]))
232 {
233 return null;
234 }
235
236 $userField = $userFields[$fieldID];
237 $typeID = $userField['USER_TYPE']['USER_TYPE_ID'];
238 $isMultiple = isset($userField['MULTIPLE']) && $userField['MULTIPLE'] === 'Y';
239 $ID = $userField['ID'];
240 if ($typeID === 'employee')
241 {
242 return [
243 'params' => [
244 'multiple' => $isMultiple ? 'Y' : 'N',
245 'dialogOptions' => [
246 'height' => 200,
247 'context' => 'CRM_UF_FILTER_' . $fieldID,
248 'entities' => [
249 [
250 'id' => 'user',
251 'options' => [
252 'inviteEmployeeLink' => false,
253 'intranetUsersOnly' => true,
254 ]
255 ],
256 ],
257 'showAvatars' => true,
258 'dropdownMode' => false,
259 ],
260 ],
261 ];
262 }
263 elseif($typeID === 'enumeration')
264 {
265 $entity = new \CUserFieldEnum();
266 $dbResult = $entity->GetList(array('SORT' => 'ASC'), array('USER_FIELD_ID' => $ID));
267
268 $items = array();
269 if(is_object($dbResult))
270 {
271 while($ary = $dbResult->Fetch())
272 {
273 $items[$ary['ID']] = $ary['VALUE'];
274 }
275 }
276
277 if ($userField['SETTINGS']['DISPLAY'] === EnumType::DISPLAY_DIALOG)
278 {
279 $dialogItems = [];
280 foreach ($items as $itemId => $itemTitle)
281 {
282 $dialogItems[] = [
283 'id' => $itemId,
284 'entityId' => $userField['FIELD_NAME'],
285 'title' => $itemTitle,
286 'tabs' => $userField['FIELD_NAME'],
287 ];
288 }
289 return [
290 'params' => [
291 'multiple' => 'Y',
292 'dialogOptions' => [
293 'items' => $dialogItems,
294 'height' => 200,
295 'dropdownMode' => true,
296 'compactView' => true,
297 'tabs' => [
298 [
299 'id' => $userField['FIELD_NAME'],
300 'title' => $userField['EDIT_FORM_LABEL'],
301 ],
302 ],
303 ],
304 ],
305 ];
306 }
307
308 return array(
309 'params' => array('multiple' => 'Y'),
310 'items' => $items
311 );
312 }
313 elseif($typeID === 'iblock_element')
314 {
315 $entity = new \CUserTypeIBlockElement();
316 $dbResult = $entity->GetList($userField);
317
318 $items = array();
319 if(is_object($dbResult))
320 {
321 $count = 0;
322 while($ary = $dbResult->Fetch())
323 {
324 $items[$ary['ID']] = $ary['NAME'];
325
326 if(++$count > 500)
327 {
328 break;
329 }
330 }
331 }
332
333 return array(
334 'params' => array('multiple' => 'Y'),
335 'items' => $items
336 );
337 }
338 elseif($typeID === 'iblock_section')
339 {
340 $entity = new \CUserTypeIBlockSection();
341 $dbResult = $entity->GetList($userField);
342
343 $items = array();
344 if(is_object($dbResult))
345 {
346 $count = 0;
347 while($ary = $dbResult->Fetch())
348 {
349 $items[$ary['ID']] = isset($ary['DEPTH_LEVEL']) && $ary['DEPTH_LEVEL'] > 1
350 ? str_repeat('. ', ($ary['DEPTH_LEVEL'] - 1)).$ary['NAME'] : $ary['NAME'];
351
352 if(++$count > 500)
353 {
354 break;
355 }
356 }
357 }
358
359 return array(
360 'params' => array('multiple' => 'Y'),
361 'items' => $items
362 );
363 }
364 elseif($typeID === 'crm')
365 {
366 $settings = isset($userField['SETTINGS']) && is_array($userField['SETTINGS'])
367 ? $userField['SETTINGS'] : array();
368
369 $entityTypeNames = array();
370 $supportedEntityTypeNames = array(
371 \CCrmOwnerType::LeadName,
372 \CCrmOwnerType::DealName,
373 \CCrmOwnerType::ContactName,
374 \CCrmOwnerType::CompanyName
375 );
376 foreach($supportedEntityTypeNames as $entityTypeName)
377 {
378 if(isset($settings[$entityTypeName]) && $settings[$entityTypeName] === 'Y')
379 {
380 $entityTypeNames[] = $entityTypeName;
381 }
382 }
383
384 $destSelectorParams = array(
385 'apiVersion' => 3,
386 'context' => 'CRM_UF_FILTER_ENTITY',
387 'contextCode' => 'CRM',
388 'useClientDatabase' => 'N',
389 'enableAll' => 'N',
390 'enableDepartments' => 'N',
391 'enableUsers' => 'N',
392 'enableSonetgroups' => 'N',
393 'allowEmailInvitation' => 'N',
394 'allowSearchEmailUsers' => 'N',
395 'departmentSelectDisable' => 'Y',
396 'enableCrm' => 'Y',
397 'multiple' => ($isMultiple ? 'Y' : 'N'),
398 'convertJson' => 'Y'
399 );
400
401 $entityTypeCounter = 0;
402 foreach($entityTypeNames as $entityTypeName)
403 {
404 switch($entityTypeName)
405 {
406 case \CCrmOwnerType::LeadName:
407 $destSelectorParams['enableCrmLeads'] = 'Y';
408 $destSelectorParams['addTabCrmLeads'] = 'Y';
409 $entityTypeCounter++;
410 break;
411 case \CCrmOwnerType::DealName:
412 $destSelectorParams['enableCrmDeals'] = 'Y';
413 $destSelectorParams['addTabCrmDeals'] = 'Y';
414 $entityTypeCounter++;
415 break;
416 case \CCrmOwnerType::ContactName:
417 $destSelectorParams['enableCrmContacts'] = 'Y';
418 $destSelectorParams['addTabCrmContacts'] = 'Y';
419 $entityTypeCounter++;
420 break;
421 case \CCrmOwnerType::CompanyName:
422 $destSelectorParams['enableCrmCompanies'] = 'Y';
423 $destSelectorParams['addTabCrmCompanies'] = 'Y';
424 $entityTypeCounter++;
425 break;
426 default:
427 }
428 }
429 if ($entityTypeCounter <= 1)
430 {
431 $destSelectorParams['addTabCrmLeads'] = 'N';
432 $destSelectorParams['addTabCrmDeals'] = 'N';
433 $destSelectorParams['addTabCrmContacts'] = 'N';
434 $destSelectorParams['addTabCrmCompanies'] = 'N';
435 }
436
437 return array(
438 'params' => $destSelectorParams
439 );
440 }
441 elseif($typeID === 'crm_status')
442 {
443 $items = array();
444 if(isset($userField['SETTINGS'])
445 && is_array($userField['SETTINGS'])
446 && isset($userField['SETTINGS']['ENTITY_TYPE'])
447 )
448 {
449 $entityType = $userField['SETTINGS']['ENTITY_TYPE'];
450 if($entityType !== '')
451 {
452 $items = \CCrmStatus::GetStatusList($entityType);
453 }
454 }
455
456 return array(
457 'params' => array('multiple' => 'Y'),
458 'items' => $items
459 );
460 }
461 return null;
462 }
463
470 protected function createField($fieldID, array $params = null)
471 {
472 return new Field($this, $fieldID, $params);
473 }
474
475 public function getUfReserved()
476 {
477 return $this->ufReserved;
478 }
479
480 protected function postFilterFields(array $fields)
481 {
482 foreach ($this->getUfReserved() as $ufId)
483 {
484 if (isset($fields[$ufId]))
485 {
486 unset($fields[$ufId]);
487 }
488 }
489
490 return $fields;
491 }
492
493}