Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
IBlockElementsToGet.php
1<?php
2
4
6{
7 private const LIMIT = 5;
8
9 private IBlockElementFilter $filter;
10 private array $order = [];
11 private array $navigation = [];
12 private array $selectFields = ['ID', 'NAME'];
13 private bool $isCheckPermissionsEnabled = true;
14 private bool $isNeedLoadWorkflowState = false;
15
16 public function __construct(
17 IBlockElementFilter $filter,
18 array $order = [],
19 int $offset = 0,
20 int $limit = self::LIMIT,
21 array $additionalSelectFields = [],
22 )
23 {
24 $this->filter = $filter;
25 $this->setOrder($order);
26 $this->setNavigation($offset, $limit);
27 $this->setAdditionalSelectFields($additionalSelectFields);
28 }
29
30 private function setOrder(array $order): void
31 {
32 $allowedFields = ['ID'];
33
34 $newOrder = [];
35 foreach ($order as $field => $value)
36 {
37 if (
38 in_array($field, $allowedFields, true)
39 && in_array(mb_strtolower((string)$value), ['asc', 'desc'], true)
40 )
41 {
42 $newOrder[$field] = $value;
43 }
44 }
45
46 $this->order = $newOrder;
47 }
48
49 private function setNavigation(int $offset, int $limit): void
50 {
51 $this->navigation = [
52 'nPageSize' => $limit >= 0 ? $limit : self::LIMIT,
53 'iNumPage' =>
54 ($limit > 0 && $offset > 0)
55 ? (int)($offset / $limit) + 1
56 : 1
57 ,
58 ];
59 }
60
61 private function setAdditionalSelectFields(array $additionalSelectFields): void
62 {
63 $additionalAllowedFields = ['IBLOCK_ID', 'IBLOCK_SECTION_ID', 'IBLOCK_TYPE_ID', 'FIELDS', 'PROPS'];
64
65 $fields = [];
66 foreach ($additionalSelectFields as $fieldId)
67 {
68 if (in_array($fieldId, $additionalAllowedFields, true))
69 {
70 $fields[] = $fieldId;
71 }
72 }
73
74 $this->selectFields = array_merge($this->selectFields, $fields);
75 }
76
77 public function getFilter(): IBlockElementFilter
78 {
79 return $this->filter;
80 }
81
82 public function getOrmFilter(): array
83 {
84 return $this->filter->getOrmFilter();
85 }
86
87 public function getOrder(): array
88 {
89 return $this->order;
90 }
91
92 public function getNavigation(): array
93 {
94 return $this->navigation;
95 }
96
97 public function getSelect(): array
98 {
99 $shouldLoadFields = $this->isNeedLoadFields();
100 $shouldLoadProps = $this->isNeedLoadProps();
101
102 if (!$shouldLoadFields && !$shouldLoadProps)
103 {
104 return $this->selectFields;
105 }
106
107 $select = array_filter(
108 $this->selectFields,
109 static fn($fieldId) => !in_array($fieldId, ['FIELDS', 'PROPS'], true)
110 );
111
112 if ($shouldLoadFields && $this->getFilter()->hasField('IBLOCK_ID'))
113 {
114 $list = new \CList($this->filter->getFieldValue('IBLOCK_ID'));
115 foreach ($list->GetFields() as $fieldId => $property)
116 {
117 if ($list->is_field($fieldId))
118 {
119 $select[] = $fieldId;
120 }
121 }
122
123 $select = array_unique($select);
124 }
125
126 return array_values($select);
127 }
128
129 public function isNeedLoadProps(): bool
130 {
131 return in_array('PROPS', $this->selectFields, true);
132 }
133
134 public function isNeedLoadFields(): bool
135 {
136 return in_array('FIELDS', $this->selectFields, true);
137 }
138
139 public function enableCheckPermissions(): static
140 {
141 $this->isCheckPermissionsEnabled = true;
142
143 return $this;
144 }
145
146 public function disableCheckPermissions(): static
147 {
148 $this->isCheckPermissionsEnabled = false;
149
150 return $this;
151 }
152
153 public function isCheckPermissionsEnabled(): bool
154 {
155 return $this->isCheckPermissionsEnabled;
156 }
157
158 public function setIsNeedLoadWorkflowStateInfo(bool $flag = true): static
159 {
160 $this->isNeedLoadWorkflowState = $flag;
161
162 return $this;
163 }
164
165 public function isNeedLoadWorkflowState(): bool
166 {
167 return $this->isNeedLoadWorkflowState;
168 }
169}
__construct(IBlockElementFilter $filter, array $order=[], int $offset=0, int $limit=self::LIMIT, array $additionalSelectFields=[],)