Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
customfieldscontroller.php
1<?php
2
4
6
7Main\Localization\Loc::loadMessages(__FILE__);
8
14{
15 private static $instance = null;
16
20 private function __construct() {}
21
25 public static function getInstance() : CustomFieldsController
26 {
27 if (self::$instance === null)
28 {
29 self::$instance = new self();
30 }
31
32 return self::$instance;
33 }
34
43 public function save(Entity $entity)
44 {
45 if ($entity->getId() <= 0)
46 {
47 throw new Main\SystemException(
48 Main\Localization\Loc::getMessage('CUSTOM_FIELDS_CONTROLLER_ERROR_INCORRECT_ENTITY_ID')
49 );
50 }
51
52 $dbRes = CustomFieldsTable::getList([
53 'select' => ['ID', 'FIELD'],
54 'filter' => [
55 '=ENTITY_ID' => $entity->getId(),
56 '=ENTITY_TYPE' => $entity::getRegistryEntity(),
57 '=ENTITY_REGISTRY_TYPE' => $entity::getRegistryType()
58 ]
59 ]);
60
61 $customFieldArray = [];
62 while ($data = $dbRes->fetch())
63 {
64 $customFieldArray[$data['FIELD']] = $data;
65 }
66
67 foreach ($entity::getCustomizableFields() as $field)
68 {
69 if ($entity->isMarkedFieldCustom($field))
70 {
71 if (!isset($customFieldArray[$field]))
72 {
73 CustomFieldsTable::add([
74 'ENTITY_ID' => $entity->getId(),
75 'ENTITY_TYPE' => $entity::getRegistryEntity(),
76 'ENTITY_REGISTRY_TYPE' => $entity::getRegistryType(),
77 'FIELD' => $field
78 ]);
79 }
80 }
81 else
82 {
83 if (isset($customFieldArray[$field]))
84 {
85 CustomFieldsTable::delete($customFieldArray[$field]['ID']);
86 }
87 }
88 }
89
90 return $entity;
91 }
92
100 public function initialize(Entity $entity) : Entity
101 {
102 if ($entity->getId() <= 0)
103 {
104 return $entity;
105 }
106
107 $dbRes = CustomFieldsTable::getList([
108 'select' => ['ID', 'FIELD'],
109 'filter' => [
110 '=ENTITY_ID' => $entity->getId(),
111 '=ENTITY_TYPE' => $entity::getRegistryEntity(),
112 '=ENTITY_REGISTRY_TYPE' => $entity::getRegistryType()
113 ]
114 ]);
115
116 while ($data = $dbRes->fetch())
117 {
118 $entity->markFieldCustom($data['FIELD']);
119 }
120
121 return $entity;
122 }
123
132 {
133 $filter = $this->buildFilter($collection);
134 if ($filter)
135 {
136 $dbRes = CustomFieldsTable::getList([
137 'select' => ['FIELD', 'ENTITY_ID'],
138 'filter' => $filter
139 ]);
140
141 while ($data = $dbRes->fetch())
142 {
143 $entity = $collection->getItemById($data['ENTITY_ID']);
144 $entity->markFieldCustom($data['FIELD']);
145 }
146 }
147
148 return $collection;
149 }
150
156 private function buildFilter(EntityCollection $collection) : array
157 {
158 $entityIdList = [];
159 $entityTypeList = [];
160 $entityRegistryTypeList = [];
161
163 foreach ($collection as $entity)
164 {
165 if ((int)$entity->getId() === 0)
166 {
167 continue;
168 }
169
170 if (!in_array($entity->getId(), $entityIdList))
171 {
172 $entityIdList[] = $entity->getId();
173 }
174
175 if (!in_array($entity::getRegistryEntity(), $entityTypeList))
176 {
177 $entityTypeList[] = $entity::getRegistryEntity();
178 }
179
180 if (!in_array($entity::getRegistryType(), $entityRegistryTypeList))
181 {
182 $entityRegistryTypeList[] = $entity::getRegistryType();
183 }
184 }
185
186 if (
187 empty($entityIdList)
188 || empty($entityTypeList)
189 || empty($entityRegistryTypeList)
190 )
191 {
192 return [];
193 }
194
195 $filter = $this->buildFilterForField('ENTITY_ID', $entityIdList);
196 $filter += $this->buildFilterForField('ENTITY_TYPE', $entityTypeList);
197 $filter += $this->buildFilterForField('ENTITY_REGISTRY_TYPE', $entityRegistryTypeList);
198
199 return $filter;
200 }
201
202 private function buildFilterForField(string $field, array $data) : array
203 {
204 if (count($data) === 1)
205 {
206 return ['='.$field => array_shift($data)];
207 }
208
209 return ['@'.$field => $data];
210 }
211}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29