Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
dataloader.php
1<?php
3
5
6abstract class DataLoader
7{
8 protected $config = [
9 'select' => [],
10 'filter' => [],
11 'order' => [],
12 'limit' => 0,
13 'internal_filter' => []
14 ];
15
16 protected $options = [];
17
19 protected $seo = null;
20
21 public function __construct()
22 {
23 $this->seo = new Seo();
24 }
25
30 public function setConfig(array $config)
31 {
32 if (empty($config))
33 {
34 return;
35 }
36 $this->config = array_merge($this->config, $config);
37 }
38
43 public function setOptions(array $options)
44 {
45 if (empty($options))
46 {
47 return;
48 }
49 $this->options = array_merge($this->options, $options);
50 }
51
55 abstract public function getElementListData();
56
61 abstract public function getElementData($element);
62
67 public function normalizeFilter($filter)
68 {
69 if (!is_array($filter))
70 {
71 return [];
72 }
73 if (empty($filter))
74 {
75 return $filter;
76 }
77
78 $result = [];
79 foreach ($filter as $row)
80 {
81 if (empty($row) || !is_array($row))
82 {
83 continue;
84 }
85 if (empty($row['key']) || empty($row['value']) || !is_array($row['value']))
86 {
87 continue;
88 }
89 $result[] = $row;
90 }
91 unset($row);
92
93 if (!empty($result))
94 {
95 Main\Type\Collection::sortByColumn($result, ['key' => SORT_ASC]);
96 }
97 return $result;
98 }
99
104 public function calculateFilterHash($filter)
105 {
106 if (!is_array($filter))
107 {
108 $filter = [];
109 }
110 return md5(serialize($filter));
111 }
112
117 public function getFilterHash($filter)
118 {
119 return $this->calculateFilterHash($this->normalizeFilter($filter));
120 }
121
127 protected function getSelectFields()
128 {
129 return $this->getSettingsValue('select');
130 }
131
137 protected function getPreparedSelectFields()
138 {
139 $result = [];
140 $fields = $this->getSelectFields();
141 if (!empty($fields) && is_array($fields))
142 {
143 foreach ($fields as $row)
144 {
145 if (empty($row) || !is_array($row) || empty($row['id']))
146 {
147 continue;
148 }
149 $result[] = $row['id'];
150 }
151 unset($row);
152 }
153 return $result;
154 }
155
161 protected function getFilter()
162 {
163 return $this->getSettingsValue('filter');
164 }
165
172 protected function getPreparedFilter(array $fields)
173 {
174 $result = [];
175 $filter = $this->getFilter();
176 if (!empty($filter) && is_array($filter))
177 {
178 $dataFilter = new DataFilter();
179 $dataFilter->setFields($fields);
180 $result = $dataFilter->create($filter);
181 unset($dataFilter);
182 }
183 return $result;
184 }
185
191 protected function getOrder()
192 {
193 return $this->getSettingsValue('order');
194 }
195
201 protected function getLimit()
202 {
203 return (int)$this->getSettingsValue('limit');
204 }
205
211 protected function getInternalFilter()
212 {
213 return $this->getSettingsValue('internal_filter');
214 }
215
222 protected function getSettingsValue($index)
223 {
224 $index = trim((string)$index);
225 if ($index === '')
226 {
227 return null;
228 }
229 if (!empty($this->config[$index]))
230 {
231 return $this->config[$index];
232 }
233 return null;
234 }
235
242 protected function getOptionsValue($index)
243 {
244 $index = trim((string)$index);
245 if ($index === '')
246 {
247 return null;
248 }
249 if (!empty($this->options[$index]))
250 {
251 return $this->options[$index];
252 }
253 return null;
254 }
255
259 public function getSeo()
260 {
261 return $this->seo;
262 }
263
268 public function getSeoProperty($name)
269 {
270 return $this->seo->getProperty($name);
271 }
272
276 public function getSeoTitle()
277 {
278 return $this->getSeoProperty(Seo::TITLE);
279 }
280}