Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
factorybased.php
1<?php
2
4
5use Bitrix\Crm\Field;
6use Bitrix\Crm\Service\Container;
7use Bitrix\Crm\Service\Factory;
9
11{
12 protected static function getFactory(string $entityType): ?Factory
13 {
14 if (!Loader::includeModule('crm'))
15 {
16 return null;
17 }
18
19 $entityTypeId = \CCrmOwnerType::ResolveID($entityType);
20 return Container::getInstance()->getFactory($entityTypeId);
21 }
22
23 public static function getEntityFields($entityType): array
24 {
25 $factory = static::getFactory($entityType);
26 if (!$factory)
27 {
28 return [];
29 }
30
31 $entityFields = [];
32 $fieldsCollection = $factory->getFieldsCollection();
33 foreach ($fieldsCollection as $field)
34 {
35 $type = static::getFieldType($field->getType());
36 if (
37 !$type
38 || !$field->isDisplayed()
39 || $field->isHidden()
40 )
41 {
42 continue;
43 }
44 $fieldName = $field->getName();
45
46 $entityFields[$fieldName] = [
47 'Name' => $field->getTitle(),
48 'Type' => $type,
49 'Filterable' => !$field->isUserField(),
50 'Editable' => \CCrmFieldInfoAttr::isFieldHasAttribute($field->getSettings(), \CCrmFieldInfoAttr::ReadOnly),
51 'Required' => $field->isRequired(),
52 // 'personalizeCode' => $field->getName(),
53 ];
54 }
55
56 return $entityFields + static::getAssignedByFields();
57 }
58
59 protected static function getFieldType(string $type): ?string
60 {
61 $map = [
62 Field::TYPE_STRING => Field::TYPE_STRING,
63 Field::TYPE_BOOLEAN => Field::TYPE_BOOLEAN,
64 Field::TYPE_CRM_STATUS => 'select',
65 Field::TYPE_DATE => Field::TYPE_DATETIME,
66 Field::TYPE_DATETIME => Field::TYPE_DATETIME,
67 Field::TYPE_TEXT => Field::TYPE_TEXT,
68 Field::TYPE_INTEGER => 'int',
69 ];
70
71 return $map[$type] ?? null;
72 }
73
74 public static function getData(
75 string $entityType,
76 array $entityIds,
77 array $usedFields = ['*'],
78 string $sortBy = 'id',
79 string $sortOrder = 'asc'
80 ): array
81 {
82 $hasIncorrectFields = false;
83
84 if (empty($usedFields))
85 {
86 return [];
87 }
88 $factory = static::getFactory($entityType);
89 if (!$factory)
90 {
91 return [];
92 }
93
94 $fields = array_values($usedFields);
95
96 $oldIncorrectFields = [
97 'ASSIGNED_BY_EMAIL' => 'ASSIGNED_BY.EMAIL',
98 'ASSIGNED_BY_WORK_PHONE' => 'ASSIGNED_BY.WORK_PHONE',
99 'ASSIGNED_BY_PERSONAL_MOBILE' => 'ASSIGNED_BY.PERSONAL_MOBILE',
100 ];
101 foreach ($fields as &$field)
102 {
103 if (array_key_exists($field, $oldIncorrectFields))
104 {
105 $field = $oldIncorrectFields[$field];
106 if (!$hasIncorrectFields)
107 {
108 $hasIncorrectFields = true;
109 }
110 }
111 }
112
113 $result = [];
114 $items = $factory->getItems([
115 'select' => array_merge(
116 $fields,
117 ['UF_*', 'ASSIGNED_BY_ID']
118 ),
119 'filter' => [
120 '@ID' => $entityIds,
121 ],
122 ]);
123 foreach ($items as $item)
124 {
125 $data = $item->getCompatibleData();
126
127 if ($item->getAssignedById() > 0)
128 {
129 self::addAssignedByFieldsValue($item->getAssignedById(), $data);
130 }
131 static::processUserFieldValues($factory->getUserFields(), $data);
132 $result[$data['ID']] = $data + static::getCommunicationFieldsValues($factory->getEntityTypeId(), $data['ID']);
133 if ($hasIncorrectFields)
134 {
135 foreach ($oldIncorrectFields as $incorrectField => $correctField)
136 {
137 if (isset($data[$correctField]) && !empty($data[$correctField]))
138 {
139 $result[$data['ID']] += [
140 $incorrectField => $data[$correctField]
141 ];
142 }
143 }
144 }
145 }
146
147 return $result; }
148}
static getData(string $entityType, array $entityIds, array $usedFields=[' *'], string $sortBy='id', string $sortOrder='asc')