Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basereport.php
1<?php
2
12use Bitrix\Report\VisualConstructor\Fields\Valuable\IValuable;
18
20
21
28abstract class BaseReport extends BaseHandler implements IReportData
29{
30 private $title;
31 private $categoryKey;
32 private $unitKey;
33 private $weight = 0;
34
35 private $report;
36 private $calculatedData;
37 private $widgetHandler;
38
42 public function __construct()
43 {
44 $report = new Report();
45 $report->setReportHandler($this);
46 $this->setReport($report);
47 $this->setTitle(Loc::getMessage('WITHOUT_DATA'));
48 }
49
53 public function getCollectedFormElements()
54 {
55 parent::getCollectedFormElements();
56 $this->getView()->collectReportHandlerFormElements($this);
57 return $this->getFormElements();
58 }
59
65 protected function collectFormElements()
66 {
67 $mainContainer = new Div();
68 $mainContainer->setKey('main_container');
69 if ($this->getConfiguration('color'))
70 {
71 $mainContainer->addInlineStyle('background-color', $this->getConfiguration('color')->getValue() . '5f');
72 }
73 else
74 {
75 $reportHandlersCount = count($this->getWidgetHandler()->getReportHandlers());
76 $mainContainer->addInlineStyle('background-color', $this->getView()->getReportDefaultColor($reportHandlersCount) . '5f');
77 }
78 $mainContainer->addClass('report-configuration-main');
79 $mainContainer->addAssets(array(
80 'css' => array('/bitrix/js/report/css/visualconstructor/configmain.css')
81 ));
82 $this->addFormElement($mainContainer->start());
83 $fieldListContainer = new Div();
84 $fieldListContainer->setKey('field_list_container');
85 $fieldListContainer->addClass('report-configuration-field-list');
86 $this->addFormElement($fieldListContainer->start());
87
88 $reportHandlerSelectContainer = new Div();
89 $reportHandlerSelectContainer->setKey('report_handler_select_container');
90 $reportHandlerSelectContainer->addClass('report-configuration-row');
91 $reportHandlerSelectContainer->addClass('report-configuration-no-padding-bottom');
92 $categorySelectField = $this->getReportHandlerCategoryField();
93 $reportHandlerSelectField = $this->getReportHandlerSelectField($categorySelectField->getValue());
94
95 $reportHandlerSelectField->addJsEventListener($categorySelectField, $categorySelectField::JS_EVENT_ON_CHANGE, array(
96 'class' => 'BX.Report.VisualConstructor.FieldEventHandlers.ReportHandlerSelect',
97 'action' => 'categorySelected'
98 ));
99 $reportHandlerSelectField->addAssets(array(
100 'js' => array('/bitrix/js/report/js/visualconstructor/fields/reporthandlerselect.js')
101 ));
102
103 $this->addFormElement($reportHandlerSelectContainer->start());
104 $this->addFormElement($categorySelectField);
105 $this->addFormElement($reportHandlerSelectField);
106
107 $whatWillCalculateField = $this->getCalculateField();
108
109 $viewCompatibleDataType = $this->getView()->getCompatibleDataType();
110
111 if (in_array($viewCompatibleDataType, array(
112 Common::MULTIPLE_REPORT_TYPE,
113 Common::MULTIPLE_GROUPED_REPORT_TYPE,
114 Common::MULTIPLE_BI_GROUPED_REPORT_TYPE,
115 )))
116 {
117 $groupingField = $this->getGroupingField($whatWillCalculateField);
118 $this->addFormElement($groupingField);
119 $widgetHandler = $this->getWidgetHandler();
120 $previewBlock = $widgetHandler->getFormElement('view_type');
121 $previewBlock->addJsEventListener($groupingField, $groupingField::JS_EVENT_ON_CHANGE, array(
122 'class' => 'BX.Report.VisualConstructor.FieldEventHandlers.PreviewBlock',
123 'action' => 'reloadWidgetPreview'
124 ));
125 }
126
127 $this->addFormElement($reportHandlerSelectContainer->end());
128
129 $selectListContainer = new Div();
130 $selectListContainer->setKey('report_configuration_select_list');
131 $selectListContainer->addClass('report-configuration-row');
132 $selectListContainer->addClass('report-configuration-row-list');
133
134 $this->addFormElement($selectListContainer->start());
135 $this->addFormElement($whatWillCalculateField);
136 $this->addFormElement($selectListContainer->end());
137 $this->addFormElement($fieldListContainer->end());
138 $this->addFormElement($mainContainer->end());
139 }
140
144 public function getCalculateField()
145 {
146 $whatWillCalculate = new DropDown('calculate');
147 $whatWillCalculate->setLabel(Loc::getMessage('BASE_REPORT_HANDLER_WHAT_WILL_CALCULATE'));
148 $whatWillCalculate->addOptions($this->getWhatWillCalculateOptions());
149
150 return $whatWillCalculate;
151 }
152
157 public function getGroupingField(DropDown $whatWillCalculateField)
158 {
159 $groupingField = new CustomDropDown('groupingBy');
160 $groupingField->setLabel(Loc::getMessage('BASE_REPORT_HANDLER_GROUPING'));
161 $groupingField->addOptions($this->getGroupByOptions());
162 $whatWillCalculateField->addJsEventListener($groupingField, $groupingField::JS_EVENT_ON_CHANGE, array(
163 'class' => 'BX.Report.VisualConstructor.FieldEventHandlers.WhatWillCalculate',
164 'action' => 'reloadCompatibleCalculatedTypes',
165 ));
166 $whatWillCalculateField->addAssets(array(
167 'js' => array('/bitrix/js/report/js/visualconstructor/fields/whatwillcalculate.js')
168 ));
169
170 return $groupingField;
171 }
172
176 protected function getGroupByOptions()
177 {
178 return array();
179 }
180
184 public function getFormElements()
185 {
186 $pseudoReportId = '_pseudo' . randString(4);
187 $result = array();
188 foreach ($this->formElementsList as $key => $element)
189 {
190 $viewModesWhereFieldAvailable = $element->getCompatibleViewTypes();
191 if ($viewModesWhereFieldAvailable != null)
192 {
193 $viewKey = $this->getView()->getKey();
194 $viewProvider = new ViewProvider();
195 $viewProvider->addFilter('primary', $viewKey);
196 $viewProvider->addFilter('dataType', $viewModesWhereFieldAvailable);
197 $views = $viewProvider->execute()->getResults();
198 if (!empty($views))
199 {
200 $result[$key] = $element;
201 }
202 }
203 else
204 {
205 $result[$key] = $element;
206 }
207 if ($element instanceof BaseValuable)
208 {
209 $element->setName($this->getNameForFormElement($element, $pseudoReportId));
210 }
211 }
212
213 return $result;
214 }
215
216
222 protected function getNameForFormElement(BaseValuable $element, $pseudoReportId = '')
223 {
224 $name = '';
225 if ($this->getWidgetHandler() && $this->getReport())
226 {
227 $name = 'widget[' .
228 $this->getWidgetHandler()->getWidget()->getGId() .
229 '][reports][' .
230 $this->getReport()->getGId() .
231 ']';
232 }
233 elseif(!$this->getReport())
234 {
235 $name = 'widget[' .
236 $this->getWidgetHandler()->getWidget()->getGId() .
237 '][reports][' .
238 $pseudoReportId .
239 ']';
240 }
241
242 $name .= parent::getNameForFormElement($element);
243 return $name;
244 }
245
249 public function getTitle()
250 {
251 return $this->title;
252 }
253
258 public function setTitle($title)
259 {
260 $this->title = $title;
261 }
262
266 public function getWeight()
267 {
268 return $this->weight;
269 }
270
275 public function setWeight($weight)
276 {
277 $this->weight = $weight;
278 }
279
283 public function getReport()
284 {
285 return $this->report;
286 }
287
292 public function setReport($report)
293 {
294
295 $this->report = $report;
296 }
297
301 public function getCalculatedData()
302 {
303 return $this->calculatedData;
304 }
305
310 public function setCalculatedData($calculatedData)
311 {
312 $this->calculatedData = $calculatedData;
313 }
314
321 public function fillReport(Report $report)
322 {
323 $viewHandler = $this->getView();
324 if ($viewHandler)
325 {
326 $this->setView($viewHandler);
327 }
328 $this->setReport($report);
329 $this->setConfigurations($report->getConfigurations());
331 $this->fillFormElementsValues();
332 }
333
334 private function fillFormElementsValues()
335 {
336 $formElements = $this->getFormElements();
338 if (!empty($configurations))
339 {
340 foreach ($configurations as $configuration)
341 {
342 if (isset($formElements[$configuration->getKey()]) && ($formElements[$configuration->getKey()] instanceof BaseValuable))
343 {
345 $formElements[$configuration->getKey()]->setValue($configuration->getValue());
346 }
347 }
348 }
349 }
350
354 public function getCategoryKey()
355 {
356 return $this->categoryKey;
357 }
358
365 protected function setCategoryKey($categoryKey)
366 {
367 $this->categoryKey = $categoryKey;
368 }
369
373 public function getUnitKey()
374 {
375 return $this->unitKey;
376 }
377
382 protected function setUnitKey($unitKey)
383 {
384 $this->unitKey = $unitKey;
385 }
386
392 private function getReportHandlerCategoryField()
393 {
394 $selectField = new CustomDropDown('reportCategory');
395 $selectField->setLabel(Loc::getMessage('SELECT_REPORT_HANDLER_CATEGORY'));
396 $categories = new CategoryProvider();
397 $categories->addFilter('parent_keys', 'main');
398 $categories->addRelation('children');
399 $categories->addRelation('parent');
400 $categories = $categories->execute()->getResults();
401 $options = Category::getOptionsTree($categories);
402 $selectField->addOptions($options);
403
404 $selectField->setValue($this->getCategoryKey());
405 return $selectField;
406 }
407
415 private function getReportHandlerSelectField($categoryKey = '__')
416 {
417 $selectField = new CustomDropDown('reportHandler');
418 $selectField->addDataAttribute('field-type', 'report-handler-class');
419 $selectField->setLabel(Loc::getMessage('SELECT_DATA_PROVIDER'));
420 $selectField->addJsEventListener($selectField, $selectField::JS_EVENT_ON_CHANGE, array(
421 'class' => 'BX.Report.VisualConstructor.FieldEventHandlers.ReportHandlerSelect',
422 'action' => 'reportHandlerSelected',
423 ));
424 $selectField->addAssets(array(
425 'js' => array('/bitrix/js/report/js/visualconstructor/fields/reporthandlerselect.js')
426 ));
427
428 $reports = new ReportProvider();
429 $reports->addFilter('dataType', $this->getView()->getCompatibleDataType());
430
431 if (!empty($categoryKey) && $categoryKey !== '__')
432 {
433 $reports->addFilter('categories', array($categoryKey));
434 }
435
436 $reports->execute();
437
439 $reportHandlers = $reports->getResults();
440 foreach ($reportHandlers as $report)
441 {
442 $selectField->addOption($report::getClassName(), $report->getTitle());
443 }
444
445 $selectField->setDefaultValue($this::getClassName());
446
447 return $selectField;
448 }
449
454 {
455 $dataTypeMap = Common::$reportImplementationTypesMap;
456 $class = get_class($this);
457 $implementedInterfaceList = class_implements($class);
458 $implementedInterfaceList = array_values($implementedInterfaceList);
459 $result = array();
460 foreach ($dataTypeMap as $reportTypeKey => $settings)
461 {
462 if (in_array($settings['interface'], $implementedInterfaceList))
463 {
464 $result[] = $reportTypeKey;
465 }
466 }
467 return $result;
468 }
469
473 public function getWidgetHandler()
474 {
475 if ($this->getReport()->getWidget())
476 {
477 return $this->getReport()->getWidget()->getWidgetHandler();
478 }
479 elseif ($this->widgetHandler)
480 {
481 return $this->widgetHandler;
482 }
483 else
484 {
485 return null;
486 }
487 }
488
495 public function setWidgetHandler(BaseWidget $widgetHandler)
496 {
497 $this->widgetHandler = $widgetHandler;
498 }
499
504 public function __clone()
505 {
506 foreach ($this->configurations as $key => $configuration)
507 {
508 $this->configurations[$key] = clone $configuration;
509 }
510
511 foreach ($this->formElementsList as $key => $formElement)
512 {
513 $this->formElementsList[$key] = clone $formElement;
514 }
515 }
516
522 public function getWhatWillCalculateOptions($groupingValue = null)
523 {
524 return array();
525 }
526
531 public function isEnabled()
532 {
533 return true;
534 }
535}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
addJsEventListener(Base $field=null, $eventKey, $jsParams)
Definition base.php:215
addFormElement(BaseFormElement $element)
Definition base.php:89
getGroupingField(DropDown $whatWillCalculateField)
getNameForFormElement(BaseValuable $element, $pseudoReportId='')
static getOptionsTree($categories, $options=array(), $depth=0)
Definition category.php:18