Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
widget.php
1<?php
3
5use Bitrix\Report\VisualConstructor\BaseReportHandler;
12
17class Widget
18{
19 const LAZY_LOAD_MODE = true;
20
28 public static function prepareWidgetContent(WidgetEntity $widget, $withCalculatedData = false)
29 {
30 $viewKey = $widget->getViewKey();
31 $view = ViewProvider::getViewByViewKey($viewKey);
32 $resultWidget = $view->prepareWidgetContent($widget, $withCalculatedData);
33
34 return $resultWidget;
35 }
36
42 public static function getCalculatedPerformedData($view, $widget)
43 {
44 $result = [];
45 $widget->loadAttribute('reports');
46
47 $reports = $widget->getReports();
48 $reportsCount = count($widget->getReports());
49 if ($reportsCount > $view::MAX_RENDER_REPORT_COUNT)
50 {
51 $result['errors'][] = 'View with key:' . $view->getKey() . 'can\'t render this count(' . $reportsCount . ') of reports';
52 return $result;
53 }
54
55 $handledReportData = array();
56 foreach ($reports as $reportId => $report)
57 {
58 $reportDispatcher = new ReportDispatcher();
59 $reportDispatcher->setReport($report);
60 $reportDispatcher->setView($view);
61 $data = $reportDispatcher->getReportCompatibleData();
62 if ($data === null)
63 {
64 $errors = $reportDispatcher->getErrors();
65 if(count($errors) > 0)
66 {
67 foreach ($errors as $error)
68 {
69 $result['errors'][] = $error->getMessage();
70 }
71 return $result;
72 }
73 continue;
74 }
75 if (!$reportDispatcher->getErrors())
76 {
77 if ($view::MAX_RENDER_REPORT_COUNT == 1)
78 {
79 $handledReportData = $data;
80 }
81 elseif ($view::MAX_RENDER_REPORT_COUNT > 1)
82 {
83 $handledReportData[] = $data;
84 }
85 }
86 else
87 {
88 foreach ($reportDispatcher->getErrors() as $error)
89 {
90 $result['errors'][] = $error->getMessage();
91 }
92 }
93 }
94 return $handledReportData;
95 }
96
104 public static function prepareBoardWithEntitiesByBoardId($boardKey)
105 {
106 $dashboard = self::getDashboard($boardKey);
107 if ($dashboard)
108 {
109 $rows = $dashboard->getRows();
110 $resultRows = array();
111 $i = 0;
112 if ($rows)
113 {
114 foreach ($rows as $row)
115 {
116 $resultRow = array(
117 'id' => $row->getGId(),
118 'layoutMap' => $row->getLayoutMap(),
119 'weight' => $row->getWeight(),
120 );
122 foreach ($row->getWidgets() as $widget)
123 {
124 $resultRow['widgets'][] = self::prepareWidgetContent($widget, !self::LAZY_LOAD_MODE);
125 }
126 $i++;
127 $resultRows[] = $resultRow;
128 }
129 }
130
131 return array(
132 'boardId' => $dashboard->getBoardKey(),
133 'boardKey' => $dashboard->getBoardKey(),
134 'userId' => $dashboard->getUserId(),
135 'rows' => $resultRows
136 );
137 }
138 else
139 {
140 return array();
141 }
142 }
143
150 private static function getDashboard($boardKey)
151 {
153 global $USER;
154 $dashboard = null;
155 $dashboardForUser = DashboardEntity::loadByBoardKeyAndUserId($boardKey, $USER->getid());
156 if ($dashboardForUser)
157 {
158 $dashboard = DashboardEntity::getBoardWithRowsAndWidgetsByBoardKeyUserId($boardKey, $USER->getId());
159 }
160 else
161 {
163 }
164
165 return $dashboard;
166 }
167
175 public static function constructPseudoWidgetByParams($params)
176 {
177 if (!isset($params['viewType']))
178 {
179 throw new ArgumentException('viewType argument not exist');
180 }
181
182 if (!isset($params['widgetId']))
183 {
184 throw new ArgumentException('widgetId argument not exist');
185 }
186
187 if (!isset($params['boardId']))
188 {
189 throw new ArgumentException('boardId argument not exist');
190 }
191
192 if (!isset($params['categoryKey']) || $params['categoryKey'] === 'myWidgets')
193 {
194 $categoryKey = '';
195 }
196 else
197 {
198 $categoryKey = $params['categoryKey'];
199 }
200
201 $viewKey = $params['viewType'];
202 $widgetGId = $params['widgetId'];
203 $boardId = $params['boardId'];
204 $widgetConfigurations = !empty($params['widget'][$widgetGId]['configurations']) ? $params['widget'][$widgetGId]['configurations'] : array();
205 $reportsConfigurationsFromForm = !empty($params['widget'][$widgetGId]['reports']) ? $params['widget'][$widgetGId]['reports'] : array();
206
207 $viewController = ViewProvider::getViewByViewKey($viewKey);
208 $widgetHandler = $viewController->buildWidgetHandlerForBoard($boardId);
209 $widget = $widgetHandler->getWidget();
210 $widget->setCategoryKey($categoryKey);
211
212 if (!empty($widgetConfigurations['old']))
213 {
214 foreach ($widgetConfigurations['old'] as $configurationGid => $configuration)
215 {
216 foreach ($configuration as $key => $value)
217 {
218 $widgetHandler->getFormElement($key)->setValue($value);
219 }
220 }
221 }
222
223 if (!empty($widgetConfigurations['new']))
224 {
225 foreach ($widgetConfigurations['new'] as $key => $value)
226 {
227 $widgetHandler->getFormElement($key)->setValue($value);
228 }
229 }
230
231 foreach ($reportsConfigurationsFromForm as $reportId=> $report)
232 {
233 if (!empty($params['deletedReports']) && in_array($reportId, $params['deletedReports']))
234 {
235 continue;
236 }
237
238 if (!empty($report['configurations']['new']['reportHandler']))
239 {
241 $reportHandler = $viewController->getReportHandler($report['configurations']['new']['reportHandler'], $widgetHandler);
242 }
243 elseif (!empty($report['configurations']['old']))
244 {
245 foreach ($report['configurations']['old'] as $configuration)
246 {
247 foreach ($configuration as $key => $value)
248 {
249 if ($key === 'reportHandler')
250 {
252 $reportHandler = $viewController->getReportHandler($value, $widgetHandler);
253 break 2;
254 }
255 }
256 }
257 }
258 else
259 {
260 continue;
261 }
262
263 if (isset($reportHandler) && $reportHandler instanceof BaseReport)
264 {
265 if (!empty($report['configurations']['old']))
266 {
267 foreach ($report['configurations']['old'] as $configuration)
268 {
269 foreach ($configuration as $key => $value)
270 {
271 $reportHandler->getFormElement($key)->setValue($value);
272 }
273 }
274 }
275
276 if (!empty($report['configurations']['new']))
277 {
278 foreach ($report['configurations']['new'] as $key => $value)
279 {
280 $reportHandler->getFormElement($key)->setValue($value);
281 }
282 }
283
284 $widgetHandler->addReportHandler($reportHandler);
285 $reportHandler->getReport()->setConfigurations($reportHandler->getConfigurations());
286 $reportHandler->getReport()->setWidget($widget);
287 }
288 }
289 $widget->setConfigurations($widgetHandler->getConfigurations());
290 $widget->setGId('pseudo_' . randString(4));
291 return $widget;
292 }
293
301 public static function constructNewPseudoWidgetByParams($params)
302 {
303 if (!isset($params['viewType']))
304 {
305 throw new ArgumentException('viewType argument not exist');
306 }
307
308 if (!isset($params['widgetId']))
309 {
310 throw new ArgumentException('widgetId argument not exist');
311 }
312
313 if (!isset($params['boardId']))
314 {
315 throw new ArgumentException('boardId argument not exist');
316 }
317
318 if (!isset($params['categoryKey']) || $params['categoryKey'] === 'myWidgets')
319 {
320 $categoryKey = '';
321 }
322 else
323 {
324 $categoryKey = $params['categoryKey'];
325 }
326
327 $viewKey = $params['viewType'];
328 $boardId = $params['boardId'];
329 $viewController = ViewProvider::getViewByViewKey($viewKey);
330 $widgetHandler = $viewController->buildWidgetHandlerForBoard($boardId);
331 $widgetHandler = $viewController->addDefaultReportHandlersToWidgetHandler($widgetHandler);
332 $widget = $widgetHandler->getWidget();
333 $widget->setCategoryKey($categoryKey);
334 $widget->setConfigurations($widgetHandler->getConfigurations());
335 $widget->setGId('pseudo_' . randString(4));
336
337 return $widget;
338 }
339
348 public static function saveWidgetAsCurrentUserPattern(WidgetEntity $widget, $categoryKey = '')
349 {
350 $patternWidget = $widget->getCopyForCurrentUser();
351 $patternWidget->setIsPattern(true);
352 $patternWidget->setGId(Util::generateUserUniqueId());
353 $patternWidget->setRowId(0);
354 $patternWidget->setCategoryKey($categoryKey);
355 $patternWidget->save();
356 return $patternWidget;
357 }
358}
static loadByBoardKeyAndUserId($boardKey, $userId)
static getBoardWithRowsAndWidgetsByBoardKeyUserId($boardKey, $userId)
Definition dashboard.php:81
static generateUserUniqueId($prefix='')
Definition util.php:16
static saveWidgetAsCurrentUserPattern(WidgetEntity $widget, $categoryKey='')
Definition widget.php:348
static prepareWidgetContent(WidgetEntity $widget, $withCalculatedData=false)
Definition widget.php:28
static getCalculatedPerformedData($view, $widget)
Definition widget.php:42