Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
3
4
10
15abstract class Base
16{
17 protected $configurations = array();
18 protected $view;
19
23 protected $formElementsList = array();
24
28 public static function getClassName()
29 {
30 return get_called_class();
31 }
32
36 public function getCollectedFormElements()
37 {
38 $this->collectFormElements();
39 return $this->getFormElements();
40 }
41
46 public function getFormElementFromCollected($key)
47 {
48 static $collectedFormElements;
49 if (!$collectedFormElements)
50 {
51 $collectedFormElements = $this->getCollectedFormElements();
52 }
53 return $this->getFormElement($key);
54 }
55
61 abstract protected function collectFormElements();
62
63
67 private function addToConfiguration(BaseValuable $element)
68 {
69 $configuration = $this->getConfiguration($element->getKey());
70
71 if (!$configuration)
72 {
73 $newConfiguration = new Configuration();
74 $newConfiguration->setGId(Util::generateUserUniqueId());
75 $newConfiguration->setFieldClassName($element::getClassName());
76 $newConfiguration->setKey($element->getKey());
77 $newConfiguration->setValue($element->getValue());
78 $newConfiguration->setWeight($element->getWeight());
79 $this->configurations[] = $newConfiguration;
80 }
81 }
82
89 public function addFormElement(BaseFormElement $element)
90 {
91 if ($element->getKey())
92 {
93 $this->formElementsList[$element->getKey()] = $element;
94 }
95 else
96 {
97 $this->formElementsList[] = $element;
98 }
99
100 if ($element instanceof BaseValuable)
101 {
102 $this->addToConfiguration($element);
103 }
104
105 }
106
114 public function addFormElementBefore(BaseFormElement $newElement, BaseFormElement $targetElement)
115 {
116 $newFormElementsList = array();
117 foreach ($this->formElementsList as $key => $element)
118 {
119
120 //add new element
121 if ($element === $targetElement)
122 {
123 if ($newElement->getKey())
124 {
125 $newFormElementsList[$newElement->getKey()] = $newElement;
126 }
127 else
128 {
129 $newFormElementsList[] = $newElement;
130 }
131
132 if ($newElement instanceof BaseValuable)
133 {
134 $this->addToConfiguration($newElement);
135 }
136 }
137
138 //rewrite old elements to new collection
139 if ($element->getKey())
140 {
141 $newFormElementsList[$key] = $element;
142 }
143 else
144 {
145 $newFormElementsList[] = $element;
146 }
147 }
148 $this->formElementsList = $newFormElementsList;
149 }
150
158 public function addFormElementAfter(BaseFormElement $newElement, BaseFormElement $targetElement)
159 {
160 $newFormElementsList = [];
161 foreach ($this->formElementsList as $key => $element)
162 {
163 //rewrite old elements to new collection
164 if ($element->getKey())
165 {
166 $newFormElementsList[$key] = $element;
167 }
168 else
169 {
170 $newFormElementsList[] = $element;
171 }
172
173 //add new element
174 if ($element === $targetElement)
175 {
176 if ($newElement->getKey())
177 {
178 $newFormElementsList[$newElement->getKey()] = $newElement;
179 }
180 else
181 {
182 $newFormElementsList[] = $newElement;
183 }
184
185 if ($newElement instanceof BaseValuable)
186 {
187 $this->addToConfiguration($newElement);
188
189 }
190 }
191 }
192 $this->formElementsList = $newFormElementsList;
193 }
194
201 public function addFormElementToStart(BaseFormElement $newElement)
202 {
203 $firstFormElement = reset($this->formElementsList);
204 if ($firstFormElement)
205 {
206 $this->addFormElementBefore($newElement, $firstFormElement);
207 }
208 }
209
216 public function addFormElementToEnd(BaseFormElement $newElement)
217 {
218 $this->addFormElement($newElement);
219 }
220
227 public function setFormElements($formElementList)
228 {
229 $this->formElementsList = [];
230 $this->configurations = [];
231 foreach ($formElementList as $element)
232 {
233 $this->addFormElement($element);
234 }
235 }
236
242 public function updateFormElementValue($formElement, $value)
243 {
244 if (is_string($formElement))
245 {
246 $formElement = $this->getFormElement($formElement);
247 }
248
249 if (!$formElement || !($formElement instanceof BaseValuable))
250 {
251 return false;
252 }
253
254 $formElement->setValue($value);
255 $configuration = $this->getConfiguration($formElement->getKey());
256 if ($configuration)
257 {
258 $configuration->setValue($formElement->getValue());
259 }
260 else
261 {
262 $this->addToConfiguration($formElement);
263 }
264 return true;
265 }
266
270 public function getFormElements()
271 {
272 return $this->formElementsList;
273 }
274
281 public function getFormElement($fieldKey)
282 {
283 $formElements = $this->getFormElements();
284 if (isset($formElements[$fieldKey]))
285 {
286 return $formElements[$fieldKey];
287 }
288 else
289 {
290 return null;
291 }
292 }
293
300 public function getFormElementValue($fieldKey)
301 {
302 $formElements = $this->getFormElements();
303 if (isset($formElements[$fieldKey]))
304 {
305 return $formElements[$fieldKey]->getValue();
306 }
307
308 return null;
309 }
310
318 public function getFormElementByDataAttribute($attributeKey, $value)
319 {
320 $reportHandlerFormElements = $this->getFormElements();
321 if ($reportHandlerFormElements)
322 {
323 foreach ($reportHandlerFormElements as $element)
324 {
325 if ($element->getDataAttribute($attributeKey) === $value)
326 {
327 return $element;
328 }
329 }
330 }
331 return null;
332 }
333
340 public function removeFormElement(BaseFormElement $element)
341 {
342 if ($element instanceof BaseValuable)
343 {
344 if (!empty($this->formElementsList[$element->getKey()]))
345 {
346 $configuration = $this->getConfiguration($element->getKey());
347 foreach ($this->configurations as $i => $configurationFromList)
348 {
349 if ($configurationFromList === $configuration)
350 {
351 unset($this->configurations[$i]);
352 }
353 }
354 }
355 }
356
357 foreach ($this->formElementsList as $i => $elementFromList)
358 {
359 if ($element === $elementFromList)
360 {
361 unset($this->formElementsList[$i]);
362 return true;
363 }
364 }
365 return false;
366 }
367
372 public function setConfigurations($configurations)
373 {
374 $this->configurations = $configurations;
375 }
376
380 public function getConfigurations()
381 {
382 return $this->configurations;
383 }
384
389 {
390 $reports = $this->getConfigurations();
391 $result = array();
392 foreach ($reports as $configuration)
393 {
394 $result[$configuration->getGId()] = $configuration;
395 }
396 return $result;
397}
398
399
405 public function getConfiguration($key)
406 {
408 if (!empty($configurations))
409 {
410 foreach ($configurations as $configuration)
411 {
412 if ($configuration->getKey() == $key)
413 return $configuration;
414 }
415 }
416
417 return null;
418 }
419
426 protected function getNameForFormElement(BaseValuable $element)
427 {
428 $name = '';
429
430 $configuration = $this->getConfiguration($element->getKey());
431 $id = '[new]';
432 if ($configuration && $configuration->getId())
433 {
434 $id = '[old][' . $configuration->getGId() . ']';
435 }
436
437 $name .= '[configurations]' . $id . '[' . $element->getKey() . ']';
438 return $name;
439 }
440
444 public function getView()
445 {
446 return $this->view;
447 }
448
453 public function setView(View $view)
454 {
455 $this->view = $view;
456 }
457}
updateFormElementValue($formElement, $value)
Definition base.php:242
addFormElementAfter(BaseFormElement $newElement, BaseFormElement $targetElement)
Definition base.php:158
getFormElementByDataAttribute($attributeKey, $value)
Definition base.php:318
addFormElementToStart(BaseFormElement $newElement)
Definition base.php:201
removeFormElement(BaseFormElement $element)
Definition base.php:340
getNameForFormElement(BaseValuable $element)
Definition base.php:426
addFormElementToEnd(BaseFormElement $newElement)
Definition base.php:216
addFormElement(BaseFormElement $element)
Definition base.php:89
addFormElementBefore(BaseFormElement $newElement, BaseFormElement $targetElement)
Definition base.php:114