Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
widget.php
1<?php
2
4
5use Bitrix\Main\Entity\Query;
13
22{
23 protected $gId;
24 protected $weight;
25 protected $boardId;
26 protected $rowId;
27 protected $widgetClass;
31 protected $widgetHandler;
32 protected $viewKey;
33 protected $ownerId = 0;
34 protected $categoryKey = '';
35 protected $isPattern = false;
36 protected $parentWidgetId = 0;
37
39 protected $reports = array();
40
42 protected $row;
43
45 protected $parentWidget;
46
48 protected $childWidgets = array();
49
53 public function __construct()
54 {
55 parent::__construct();
56 $this->weight = 0;
57 }
58
62 public static function getTableClassName()
63 {
65 }
66
73 public static function getMapAttributes()
74 {
75 $attributes = parent::getMapAttributes();
76 $attributes['GID'] = 'gId';
77 $attributes['BOARD_ID'] = 'boardId';
78 $attributes['DASHBOARD_ROW_ID'] = 'rowId';
79 $attributes['PARENT_WIDGET_ID'] = 'parentWidgetId';
80 $attributes['WIDGET_CLASS'] = 'widgetClass';
81 $attributes['CATEGORY_KEY'] = 'categoryKey';
82 $attributes['VIEW_KEY'] = 'viewKey';
83 $attributes['WEIGHT'] = 'weight';
84 $attributes['OWNER_ID'] = 'ownerId';
85 $attributes['IS_PATTERN'] = 'isPattern';
86 return $attributes;
87 }
88
92 public static function getMapReferenceAttributes()
93 {
94 return array(
95 'row' => array(
96 'type' => Common::MANY_TO_ONE,
97 'targetEntity' => DashboardRow::getClassName(),
98 'inversedBy' => 'widgets',
99 'join' => array(
100 'field' => array('rowId', 'id')
101 )
102 ),
103 'configurations' => array(
104 'type' => Common::MANY_TO_MANY,
105 'targetEntity' => Configuration::getClassName(),
106 'join' => array(
107 'tableClassName' => WidgetConfigurationTable::getClassName(),
108 'column' => array('WIDGET' => array('id', 'WIDGET_ID')),
109 'inverseColumn' => array('CONFIGURATION_SETTING' => array('id', 'CONFIGURATION_ID'))
110 ),
111 ),
112 'reports' => array(
113 'type' => Common::ONE_TO_MANY,
114 'targetEntity' => Report::getClassName(),
115 'mappedBy' => 'widget'
116 ),
117 'parentWidget' => array(
118 'type' => Common::MANY_TO_ONE,
119 'targetEntity' => Widget::getClassName(),
120 'inversedBy' => 'childWidgets',
121 'join' => array(
122 'field' => array('parentWidgetId', 'id')
123 ),
124 'options' => array(
125 'deleteSkip' => true
126 )
127 ),
128 'childWidgets' => array(
129 'type' => Common::ONE_TO_MANY,
130 'targetEntity' => Widget::getClassName(),
131 'mappedBy' => 'parentWidget',
132 'options' => array(
133 'deleteSkip' => true
134 )
135 ),
136 );
137 }
138
139
145 public function delete()
146 {
147 if ($this->isPattern())
148 {
149 $this->rowId = 0;
150 $this->save();
151 return true;
152 }
153 else
154 {
155 return parent::delete();
156 }
157 }
158
164 public function deletePatternWidget()
165 {
166 return parent::delete();
167 }
168
175 public function addReportHandler(BaseReport $reportHandler)
176 {
177 $report = $reportHandler->getReport();
178 $this->addReports($report);
179 if ($this->getId())
180 {
181 $report->setWidget($this);
182 $reportHandler->fillReport($report);
183 }
184
185 }
186
192 public function getWidgetHandler($isRuntime = false)
193 {
194 if (!$this->widgetHandler)
195 {
196 $widgetProvider = new WidgetProvider();
197 $widgetProvider->addFilter('widgetClassName', $this->widgetClass);
198 $widgetHandlerFromEvent = $widgetProvider->execute()->getFirstResult();
199 if ($widgetHandlerFromEvent)
200 {
201 $this->widgetHandler = new $widgetHandlerFromEvent;
202 if (!$isRuntime)
203 {
204 $this->loadAttribute('configurations');
205 }
206 $this->widgetHandler->fillWidget($this);
207 }
208 }
209
211 }
212
213
214
223 {
224 $this->setWidgetClass($widgetHandler::getClassName());
225 $this->widgetHandler = $widgetHandler;
226 }
227
231 public function getReports()
232 {
233 return $this->reports;
234 }
235
241 public function getReportsGidKeyed()
242 {
243 $reports = $this->getReports();
244 $result = array();
245 foreach ($reports as $report)
246 {
247 $result[$report->getGId()] = $report;
248 }
249 return $result;
250 }
251
256 public function getReportByGId($reportGId)
257 {
258 foreach ($this->getReports() as $report)
259 {
260 echo $report->getGId() . PHP_EOL;
261 if($report->getGId() === $reportGId)
262 {
263 return $report;
264 }
265 }
266 return null;
267 }
268
272 public function getBoardId()
273 {
274 return $this->boardId;
275 }
276
283 public function setBoardId($boardId)
284 {
285 $this->boardId = $boardId;
286 }
287
291 public function getViewKey()
292 {
293 return $this->viewKey;
294 }
295
302 public function setViewKey($viewKey)
303 {
304 $this->viewKey = $viewKey;
305 }
306
312 public function getFilterId()
313 {
314 return 'report_board_' . $this->getBoardId() . '_filter';
315 }
316
322 public function getCopyForCurrentUser()
323 {
324 global $USER;
325 //TODO create normal copy function for models
326 $coreWidget = clone $this;
327 $copyWidget = new Widget();
328 $copyWidget->setBoardId($coreWidget->getBoardId());
329 $copyWidget->setWidgetClass($coreWidget->getWidgetClass());
330 $copyWidget->setViewKey($coreWidget->getViewKey());
331 $copyWidget->setGId($coreWidget->getGId());
332 $copyWidget->setWeight($coreWidget->getWeight());
333 $copyWidget->setOwnerId($USER->getID());
334 if ($coreWidget->getId())
335 {
336 $copyWidget->setParentWidgetId($coreWidget->getId());
337 }
338 else
339 {
340 $copyWidget->setParentWidgetId($coreWidget->getId());
341 }
342
343 $reports = $coreWidget->getReports();
344 if (is_array($reports))
345 {
346 foreach ($reports as $report)
347 {
348 //HACK: must rewrite
349 $report->loadAttribute('configurations');
350 $reportCopy = $report->getCopy();
351 $reportCopy->setWidget($copyWidget);
352 $reportCopy->setWidgetId(null);
353 $copyWidget->addReports($reportCopy);
354 }
355 }
356
357
358 $configurations = $coreWidget->getConfigurations();
359 if ($configurations)
360 {
361 foreach ($configurations as $configuration)
362 {
363 $configuration->setId(null);
364 $copyWidget->addConfigurations($configuration);
365 }
366 }
367
368
369 return $copyWidget;
370 }
371
379 public static function getWidgetsByBoard($boardId)
380 {
381 $widgets = static::getModelList(array(
382 'select' => array('*'),
383 'filter' => array('=BOARD_ID' => $boardId),
384 'with' => array('reports.configurations', 'reports.widget', 'configurations'),
385 'order' => array('WEIGHT'),
386 ));
387 return $widgets;
388 }
389
393 public static function getCurrentUserPatternedWidgets()
394 {
395 global $USER;
396 $userId = $USER->getId();
397 $filter = Query::filter()
398 ->where('IS_PATTERN', '1')
399 ->where(Query::filter()
400 ->where(Query::filter()
401 ->where('OWNER_ID', $userId)
402 )
403 ->logic('or')
404 ->where(Query::filter()
405 ->where('OWNER_ID', 0)
406 ->whereNot('CATEGORY_KEY', '')
407 )
408 );
409 $widgets = static::getModelList(array(
410 'select' => array('*'),
411 'filter' => $filter,
412 'order' => array('CREATED_DATE' => 'DESC'),
413 ));
414 return $widgets ? $widgets : array();
415 }
416
423 public static function getWidgetConfigurations($widgetId)
424 {
425 $widget = static::getWidgetById($widgetId);
426 $configurations = $widget->getConfigurations();
427 return $configurations;
428 }
429
436 public static function getWidgetById($widgetId)
437 {
438 static $widgets;
439 if (!isset($widgets[$widgetId]))
440 {
441 $widgets[$widgetId] = static::load(array('ID' => $widgetId), array('row', 'reports.configurations', 'configurations'));
442 }
443 return $widgets[$widgetId];
444 }
445
452 public static function getCurrentUserWidgetByGId($widgetGId)
453 {
454 global $USER;
455 if ($USER)
456 {
457 //TODO
458 $filter = Query::filter();
459 $filter->where('GID', $widgetGId);
460 $filter->logic('and');
461 $filter->where('ROW.DASHBOARD.USER_ID', $USER->getId());
462
463 $widget = static::load(
464 $filter,
465 array('row', 'reports.configurations', 'configurations')
466 );
467 if (!$widget)
468 {
469 $filter = Query::filter();
470 $filter->where('GID', $widgetGId);
471 $filter->logic('and');
472 $filter->where('ROW.DASHBOARD.USER_ID', 0);
473 $widget = static::load(
474 $filter,
475 array('row', 'reports.configurations', 'configurations')
476 );
477 }
478
479 return $widget;
480 }
481 return null;
482 }
483
491 public static function buildPseudoWidget($params)
492 {
493 if (mb_strpos($params['widgetGId'], 'pseudo_') === 0)
494 {
495 $widget = new self();
496 $widget->setViewKey($params['viewKey']);
497 $widget->setGId($params['widgetGId']);
498 $widget->setWidgetClass(BaseWidget::getClassName());
499 return $widget;
500 }
501
502 $widget = self::getCurrentUserWidgetByGId($params['widgetGId']);
503 if ($widget)
504 {
505 $widget->setViewKey($params['viewKey']);
506 }
507 return $widget;
508 }
509
516 public static function getWidgetByGId($widgetGId)
517 {
518 return static::load(
519 array(
520 'GID' => $widgetGId
521 ),
522 array('row', 'reports.configurations', 'configurations')
523 );
524 }
525
532 public static function getWidgetByIdWithReports($widgetId)
533 {
534 $widget = static::load(
535 array('ID' => $widgetId),
536 array('row', 'reports', 'reports.configurations', 'configurations')
537 );
538 return $widget;
539 }
540
547 public static function removeCurrentUserWidgetByGId($widgetGId)
548 {
549 global $USER;
550 if ($USER)
551 {
552 $widget = static::load(
553 array(
554 'GID' => $widgetGId,
555 'ROW.DASHBOARD.USER_ID' => $USER->getId()
556 )
557 );
558 $widget->delete();
559 return $widgetGId;
560 }
561 return null;
562
563 }
564
568 public function getWeight()
569 {
570 return $this->weight;
571 }
572
579 public function setWeight($weight)
580 {
581 $this->weight = $weight;
582 }
583
587 public function getWidgetClass()
588 {
589 return $this->widgetClass;
590 }
591
600 {
601 $this->widgetClass = $widgetClass;
602 }
603
607 public function getRow()
608 {
609 return $this->row;
610 }
611
618 public function setRow(DashboardRow $row)
619 {
620 $this->row = $row;
621 }
622
626 public function isPattern()
627 {
628 return $this->isPattern;
629 }
630
637 public function setIsPattern($isPattern)
638 {
639 $this->isPattern = $isPattern;
640 }
641
645 public function getParentWidget()
646 {
647 return $this->parentWidget;
648 }
649
657 {
658 $this->parentWidget = $parentWidget;
659 }
660
664 public function getChildWidgets()
665 {
666 return $this->childWidgets;
667 }
668
672 public function getParentWidgetId()
673 {
675 }
676
684 {
685 $this->parentWidgetId = $parentWidgetId;
686 }
687
691 public function getGId()
692 {
693 return $this->gId;
694 }
695
702 public function setGId($gId)
703 {
704 $this->gId = $gId;
705 }
706
710 public function getCategoryKey()
711 {
712 return $this->categoryKey;
713 }
714
723 {
724 $this->categoryKey = $categoryKey;
725 }
726
730 public function getRowId()
731 {
732 return $this->rowId;
733 }
734
741 public function setRowId($rowId)
742 {
743 $this->rowId = $rowId;
744 }
745
749 public function getOwnerId()
750 {
751 return $this->ownerId;
752 }
753
760 public function setOwnerId($ownerId)
761 {
762 $this->ownerId = $ownerId;
763 }
764
765
766}
addReportHandler(BaseReport $reportHandler)
Definition widget.php:175
setWidgetHandler(BaseWidget $widgetHandler)
Definition widget.php:222