Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
widget.php
1<?php
2
4
7use Bitrix\Main\Entity\Query;
21
27{
28 public function configureActions()
29 {
30 return [
31 'load' => [
32 '+prefilters' => [
34 ]
35 ],
36 'loadByBoardId' => [
37 '+prefilters' => [
39 ]
40 ],
41 ];
42 }
43
51 public function showConfigurationFormAction($widgetId, $boardId)
52 {
53 $componentName = 'bitrix:report.visualconstructor.widget.form';
54 $widget = \Bitrix\Report\VisualConstructor\Entity\Widget::getCurrentUserWidgetByGId($widgetId);
55 if ($widget)
56 {
57 $templateName = '';
58 $params = array(
59 'MODE' => 'update',
60 'ORIGINAL_WIDGET_GID' => $widgetId,
61 'WIDGET' => $widget,
62 'BOARD_ID' => $boardId,
63 'PAGE_TITLE' => Loc::getMessage('REPORT_WIDGET_SETTINGS_CONTENT_TITLE'),
64 'SAVE_BUTTON_TITLE' => Loc::getMessage('REPORT_WIDGET_SETTINGS_SAVE_BUTTON_TITLE'),
65 );
66 return new Component($componentName, $templateName, $params);
67 }
68 else
69 {
70 $this->addError(new Error('No widget with this id'));
71 return false;
72 }
73 }
74
81 public function saveConfigurationFormAction($formParams)
82 {
83 if (!empty($formParams['boardId']))
84 {
85 $boardKey = $formParams['boardId'];
86 }
87 else
88 {
89 $this->addError(new Error('Argument boardId not exist'));
90 return false;
91 }
92
93
94 $dashboardForUser = DashboardHelper::getDashboardByKeyForCurrentUser($boardKey);
95
96 if (!$dashboardForUser)
97 {
98 $this->addError(new Error('Can\'t save configuration because current user has not dashboard to edit'));
99 return false;
100 }
101
102 $isPattern = false;
103 if (!empty($formParams['isPattern']))
104 {
105 $isPattern = $formParams['isPattern'] === 'on' ? true : false;
106 }
107
108 $categoryKey = !empty($formParams['categoryKey']) ? $formParams['categoryKey']: '';
109
110 $widgetGId = $formParams['widgetId'];
111 $originalWidgetGId = $formParams['originalWidgetGId'];
112 $widgetConfigurations = $formParams['widget'][$widgetGId]['configurations'];
113 $reportsConfigurationsFromForm = $formParams['widget'][$widgetGId]['reports'];
114 $deletedReportIds = !empty($formParams['deletedReports']) ? $formParams['deletedReports'] : array();
115
116
117 if ($widgetGId !== $originalWidgetGId)
118 {
119 $widget = \Bitrix\Report\VisualConstructor\Entity\Widget::getCurrentUserWidgetByGId($originalWidgetGId);
120 }
121 else
122 {
123 $widget = \Bitrix\Report\VisualConstructor\Entity\Widget::getCurrentUserWidgetByGId($widgetGId);
124 }
125
126 if (!$widget)
127 {
128 $this->addError(new Error('Can\'t save configuration because widget with this id not exist'));
129 return false;
130 }
131 else
132 {
133 if ($widgetGId !== $originalWidgetGId)
134 {
135 $newWidget = $this->createWidgetByParams($formParams);
136 $newWidget->setWeight($widget->getWeight());
137 $newWidget->setGId($widget->getGId());
138 $newWidget->setRowId((int)$widget->getRowId());
139 $newWidget->setCategoryKey($widget->getCategoryKey());
140 $newWidget->save();
141
142 $widget->delete();
143 return array('widgetId' => $newWidget->getGId());
144 }
145 else
146 {
147 $widget->loadAttribute('reports');
148 $widgetHandler = $widget->getWidgetHandler();
149 $this->setConfigurableEntityConfiguration($widget, $widgetHandler, $widgetConfigurations);
150 $widget->setViewKey($widget->getWidgetHandler()->getConfiguration('view_type')->getValue());
151 $widget->setBoardId($boardKey);
152 $widget->setCategoryKey($categoryKey);
153 $widget->save();
154 }
155 }
156
157
158 $widgetReports = $widget->getReportsGidKeyed();
159
160
161 //delete reports, which mark as deleted in form
162 foreach ($deletedReportIds as $deletedReportId)
163 {
164 if (!empty($widgetReports[$deletedReportId]))
165 {
166 $widgetReports[$deletedReportId]->delete();
167 }
168 unset($reportsConfigurationsFromForm[$deletedReportId]);
169 }
170
171 //save report configurations
172 foreach ($reportsConfigurationsFromForm as $reportId => $configurationFromForm)
173 {
174 $configuration = $configurationFromForm['configurations'];
175
176 if ($this->isReportPseudo($reportId))
177 {
178 $this->addReportToWidget($widget, $configuration);
179 }
180 else
181 {
182 $report = $widgetReports[$reportId];
183 $reportHandler = $report->getReportHandler();
184 $this->setConfigurableEntityConfiguration($report, $reportHandler, $configuration);
185 }
186 }
187
188 $widget->save();
189
190 if ($isPattern)
191 {
192 \Bitrix\Report\VisualConstructor\Helper\Widget::saveWidgetAsCurrentUserPattern($widget);
193 }
194
195 return array('widgetId' => $widget->getGId());
196 }
197
204 public function addWidgetFromConfigurationFormAction($formParams)
205 {
206 //@TODO optimize
207 $widget = $this->createWidgetByParams($formParams);
208
209 return array('widgetId' => $widget->getGId());
210 }
211
212
217 private function createWidgetByParams($params)
218 {
219 $widgetGid = $params['widgetId'];
220
221 $viewController = ViewProvider::getViewByViewKey($params['widget'][$widgetGid]['configurations']['new']['view_type']);
222 if (!$viewController)
223 {
224 $this->addError(new Error('No such view controller.'));
225 return false;
226 }
227
228 global $USER;
229 $userId = $USER->getId();
230 if (!$userId)
231 {
232 $this->addError(new Error('Can\'t create widget because current user has not id'));
233 return false;
234 }
235
236 $dashboardForUser = DashboardHelper::getDashboardByKeyForCurrentUser($params['boardId']);
237 if (!$dashboardForUser)
238 {
239 $this->addError(new Error('Can\'t create widget because current user has not board to edit'));
240 return false;
241 }
242
243
244 $isPattern = false;
245 if (!empty($params['isPattern']))
246 {
247 $isPattern = $params['isPattern'] === 'on' ? true : false;
248 }
249
250 $categoryKey = !empty($params['categoryKey']) ? $params['categoryKey']: '';
251 $widgetHandler = $viewController->buildWidgetHandlerForBoard($params['boardId']);
252
253
254 $widgetPositions = array('cell_' . rand(999, 99999));
255
256 try
257 {
258 $row = \Bitrix\Report\VisualConstructor\Helper\Row::getRowDefaultEntity(array(
259 'cellIds' => $widgetPositions
260 ));
261 }
262 catch (ArgumentException $e)
263 {
264 $this->addError(new Error($e->getMessage()));
265 return false;
266 }
267
268 $dashboardForUser->addRows($row);
269
270
272 $widget = $widgetHandler->getWidget();
273 $widget->setCategoryKey($categoryKey);
274 $widget->setWeight($widgetPositions[0]);
275 $widget->setOwnerId($userId);
276 $widget->setGId(Util::generateUserUniqueId());
277 $row->addWidgets($widget);
278 $dashboardForUser->save();
279
280
281 $widgetGId = $params['widgetId'];
282 $widgetConfigurations = $params['widget'][$widgetGId]['configurations'];
283 $reportsConfigurationsFromForm = $params['widget'][$widgetGId]['reports'];
284
285 $widgetHandler = $widget->getWidgetHandler();
286 $this->setConfigurableEntityConfiguration($widget, $widgetHandler, $widgetConfigurations);
287 //save report configurations
288 foreach ($reportsConfigurationsFromForm as $reportId => $configurationFromForm)
289 {
290 $configuration = $configurationFromForm['configurations'];
291
292 if (is_array($params['deletedReports']) && in_array($reportId, $params['deletedReports']))
293 {
294 continue;
295 }
296
297 if ($this->isReportPseudo($reportId))
298 {
299 $this->addReportToWidget($widget, $configuration);
300 }
301 }
302
303 if ($isPattern)
304 {
305 \Bitrix\Report\VisualConstructor\Helper\Widget::saveWidgetAsCurrentUserPattern($widget, $widget->getCategoryKey());
306 }
307
308 return $widget;
309 }
310
317 public function buildFormAction($params)
318 {
319 $componentName = 'bitrix:report.visualconstructor.widget.form';
320
321 $boardId = $params['boardId'];
322 $mode = $params['mode'];
323 try
324 {
325 $widget = \Bitrix\Report\VisualConstructor\Helper\Widget::constructNewPseudoWidgetByParams($params);
326 }
327 catch (ArgumentException $e)
328 {
329 $this->addError(new Error($e->getMessage()));
330 return false;
331 }
332
333 $widget->setGId('pseudo_widget_for_add');
334 if ($widget)
335 {
336 $templateName = '';
337 $params = array(
338 'MODE' => $mode,
339 'ORIGINAL_WIDGET_GID' => $params['widgetId'],
340 'WIDGET' => $widget,
341 'BOARD_ID' => $boardId,
342 'PAGE_TITLE' => $mode === 'create' ? Loc::getMessage('REPORT_CREATE_WIDGET_SETTINGS_CONTENT_TITLE') : Loc::getMessage('REPORT_WIDGET_SETTINGS_CONTENT_TITLE'),
343 'SAVE_BUTTON_TITLE' => $mode === 'create' ? Loc::getMessage('REPORT_CREATE_WIDGET_SETTINGS_SAVE_BUTTON_TITLE') : Loc::getMessage('REPORT_WIDGET_SETTINGS_SAVE_BUTTON_TITLE'),
344 );
345 return new Component($componentName, $templateName, $params);
346 }
347 else
348 {
349 $this->addError(new Error('No widget with this id'));
350 return false;
351 }
352 }
353
362 {
363 if (!isset($params['newViewKey']))
364 {
365 $this->addError(new Error('new view key not exist'));
366 return false;
367 }
368
369 if (!isset($params['oldViewKey']))
370 {
371 $this->addError(new Error('old view key not exist'));
372 return false;
373 }
374 $newView = ViewProvider::getViewByViewKey($params['newViewKey']);
375 $oldView = ViewProvider::getViewByViewKey($params['oldViewKey']);
376
377 if (!$newView)
378 {
379 $this->addError(new Error('view not found with key: ' . $params['newViewKey']));
380 return false;
381 }
382
383 if (!$oldView)
384 {
385 $this->addError(new Error('view not found with key: ' . $params['oldViewKey']));
386 return false;
387 }
388
389
390 $result = $oldView->isCompatibleWithView($newView);
391 return array('isCompatible' => $result);
392 }
393
400 public function constructPseudoWidgetAction($params)
401 {
402 try
403 {
404 $widget = \Bitrix\Report\VisualConstructor\Helper\Widget::constructPseudoWidgetByParams($params);
405 }
406 catch (ArgumentException $e)
407 {
408 $this->addError(new Error($e->getMessage()));
409 return false;
410 }
411
412 $pseudoWidgetPreparedData = \Bitrix\Report\VisualConstructor\Helper\Widget::prepareWidgetContent($widget, true);
413 $widgetConfigurationFields = $widget->getWidgetHandler()->getFormElements();
414 $reports = $widget->getReports();
415 $reportsResult = array();
416 if ($reports)
417 {
418 foreach ($reports as $report)
419 {
420 $configurationFields = $report->getReportHandler()->getFormElements();
421 $reportsResult[] = array(
422 'configurationFields' => $configurationFields
423 );
424 }
425 }
426
427 return array(
428 'widget' => array(
429 'pseudoWidget' => $pseudoWidgetPreparedData,
430 'configurationFields' => $widgetConfigurationFields,
431 ),
432 'reports' => $reportsResult
433 );
434 }
435
441 private function setConfigurableEntityConfiguration(ConfigurableModel $model, Base $handler, $formConfigurations)
442 {
443 if (!empty($formConfigurations['old']))
444 {
445 $keys = array_keys($formConfigurations['old']);
446 $configurations = $handler->getConfigurationsGidKeyed();
447 foreach ($configurations as $id => $configuration)
448 {
449 if (in_array($id, $keys))
450 {
451 $field = $handler->getFormElement($configuration->getKey());
452 if ($field instanceof BaseValuable)
453 {
454 $newValue = $formConfigurations['old'][$id][$configuration->getKey()];
455 $field->setValue($newValue);
456 $configuration->setValue($field->getValue());
457 }
458 }
459 }
460 }
461
462
463 if (!empty($formConfigurations['new']))
464 {
465 foreach ($formConfigurations['new'] as $key => $newConfiguration)
466 {
467 $field = $handler->getFormElement($key);
468 if ($field instanceof BaseValuable)
469 {
470 $field->setDefaultValue($newConfiguration);
471 $model->addConfigurationField($field);
472 }
473 }
474 }
475 }
476
477
484 private function isReportPseudo($reportId)
485 {
486 return (mb_strpos($reportId, '_pseudo') === 0);
487 }
488
489
494 private function addReportToWidget(\Bitrix\Report\VisualConstructor\Entity\Widget $widget, $configuration)
495 {
496 if (!empty($configuration['new']['reportHandler']))
497 {
498 $reportHandler = Report::buildReportHandlerForWidget($configuration['new']['reportHandler'], $widget);
499 if ($reportHandler instanceof BaseReport)
500 {
501 foreach ($configuration['new'] as $key => $configurationValue)
502 {
503 $formElement = $reportHandler->getFormElementFromCollected($key);
504 if ($formElement instanceof BaseValuable)
505 {
506 $formElement->setValue($configurationValue);
507 $reportConfiguration = $reportHandler->getConfiguration($key);
508 if ($reportConfiguration)
509 {
510 $reportConfiguration->setValue($formElement->getValue());
511 }
512 }
513 }
514 $reportHandler->getReport()->setConfigurations($reportHandler->getConfigurations());
515 $widget->addReportHandler($reportHandler);
516 $widget->save();
517 }
518
519
520 }
521 }
522
529 public function loadAction($widgetId)
530 {
531 $widget = \Bitrix\Report\VisualConstructor\Entity\Widget::getCurrentUserWidgetByGId($widgetId);
532 if (!$widget->getId())
533 {
534 $this->addError(new Error('Widget no exist'));
535 return false;
536 }
537 $preparedWidget = \Bitrix\Report\VisualConstructor\Helper\Widget::prepareWidgetContent($widget, true);
538 $preparedWidget['row'] = array(
539 'id' => $widget->getRow()->getGId(),
540 'layoutMap' => $widget->getRow()->getLayoutMap(),
541 );
542 return $preparedWidget;
543 }
544
551 public function loadByBoardIdAction($boardId)
552 {
553 $preparedObjectForDashboard = \Bitrix\Report\VisualConstructor\Helper\Widget::prepareBoardWithEntitiesByBoardId($boardId);
554 return $preparedObjectForDashboard;
555 }
556
566 public function updateAction($boardKey, $widgetId, $params)
567 {
568 $dashboardForUser = DashboardHelper::getDashboardByKeyForCurrentUser($boardKey);
569
570 if (!$dashboardForUser)
571 {
572 $this->addError(new Error('Can\'t update widget because current user has not dashboard to edit'));
573 return false;
574 }
575
576 $widget = \Bitrix\Report\VisualConstructor\Entity\Widget::getCurrentUserWidgetByGId($widgetId);
577 if ($widget)
578 {
579
580 if (!empty($params['rowId']) && $params['rowId'] != $widget->getRow()->getGId())
581 {
582 $row = DashboardRow::getCurrentUserRowByGId($params['rowId']);
583
584 if ($row)
585 {
586 $widget->setRow($row);
587 }
588 else
589 {
590 $this->addError(new Error("No row with id: " . $params['rowId']));
591 }
592 }
593
594 $widget->getRow()->setLayoutMap($params['rowLayoutMap']);
595
596
597 if (!empty($params['cellId']))
598 {
599 $widget->setWeight($params['cellId']);
600 }
601
602 $widget->save();
603 return $widget->getGId();
604 }
605 else
606 {
607 $this->addError(new Error("No widget with id: " . $widgetId));
608 return null;
609 }
610
611 }
612
619 public function removeAction($params)
620 {
621 $boardKey = $params['boardId'];
622 $widgetId = $params['widgetId'];
623 $dashboardForUser = DashboardHelper::getDashboardByKeyForCurrentUser($boardKey);
624
625 if ($dashboardForUser)
626 {
627 $deleteWidgetId = \Bitrix\Report\VisualConstructor\Entity\Widget::removeCurrentUserWidgetByGId($widgetId);
628 return $deleteWidgetId;
629 }
630 else
631 {
632 $this->addError(new Error('Cant delete row because current user has not own dashboard'));
633 return false;
634 }
635
636 }
637
645 public function removePatternAction($widgetId)
646 {
647 global $USER;
648 $filter = Query::filter();
649 $filter->where('GID', $widgetId);
650 $filter->where('OWNER_ID', $USER->getId());
651 $filter->where('IS_PATTERN', true);
652 $widget = \Bitrix\Report\VisualConstructor\Entity\Widget::load($filter);
653
654 if ($widget->getOwnerId() === $USER->getId())
655 {
656 $widget->deletePatternWidget();
657 }
658 }
659
660
661}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
updateAction($boardKey, $widgetId, $params)
Definition widget.php:566