Bitrix-D7  20.5.0
customfieldscontroller.php
См. документацию.
1 <?php
2 
3 namespace Bitrix\Sale\Internals;
4 
5 use Bitrix\Main;
6 
8 
9 /**
10  * Class CustomFieldsController
11  * @package Bitrix\Sale\Internals
12  */
14 {
15  private static $instance = null;
16 
17  /**
18  * CustomFieldsController constructor.
19  */
20  private function __construct() {}
21 
22  /**
23  * @return CustomFieldsController
24  */
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 
35  /**
36  * @param Entity $entity
37  * @return Entity
38  * @throws Main\ArgumentException
39  * @throws Main\NotImplementedException
40  * @throws Main\ObjectPropertyException
41  * @throws Main\SystemException
42  */
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 
93  /**
94  * @param Entity $entity
95  * @return Entity
96  * @throws Main\ArgumentException
97  * @throws Main\ObjectPropertyException
98  * @throws Main\SystemException
99  */
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 
124  /**
125  * @param EntityCollection $collection
126  * @return EntityCollection
127  * @throws Main\ArgumentException
128  * @throws Main\ObjectPropertyException
129  * @throws Main\SystemException
130  */
132  {
133  $dbRes = CustomFieldsTable::getList([
134  'select' => ['FIELD', 'ENTITY_ID'],
135  'filter' => $this->buildFilter($collection)
136  ]);
137 
138  while ($data = $dbRes->fetch())
139  {
140  $entity = $collection->getItemById($data['ENTITY_ID']);
141  $entity->markFieldCustom($data['FIELD']);
142  }
143 
144  return $collection;
145  }
146 
147  /**
148  * @param EntityCollection $collection
149  * @return array
150  * @throws Main\NotImplementedException
151  */
152  private function buildFilter(EntityCollection $collection)
153  {
154  $filter = [
155  'ENTITY_ID' => [],
156  'ENTITY_TYPE' => [],
157  'ENTITY_REGISTRY_TYPE' => [],
158  ];
159 
160  /** @var CollectableEntity $entity */
161  foreach ($collection as $entity)
162  {
163  if ((int)$entity->getId() === 0)
164  {
165  continue;
166  }
167 
168  if (!in_array($entity->getId(), $filter['ENTITY_ID']))
169  {
170  $filter['ENTITY_ID'][] = $entity->getId();
171  }
172 
173  if (!in_array($entity::getRegistryEntity(), $filter['ENTITY_TYPE']))
174  {
175  $filter['ENTITY_TYPE'][] = $entity::getRegistryEntity();
176  }
177 
178  if (!in_array($entity::getRegistryType(), $filter['ENTITY_REGISTRY_TYPE']))
179  {
180  $filter['ENTITY_REGISTRY_TYPE'][] = $entity::getRegistryType();
181  }
182  }
183 
184  return $filter;
185  }
186 }
static loadMessages($file)
Loads language messages for specified file in a lazy way.
Definition: loc.php:67
static getMessage($code, $replace=null, $language=null)
Returns translation by message code.
Definition: loc.php:29
Base class for fatal exceptions.