/** * Obtains a list of available widget types. * * @param string $type of the widget * * @return \Symfony\Component\HttpFoundation\Response */ public function getDataAction($type) { $start = microtime(true); $timezone = InputHelper::clean($this->request->get('timezone', null)); $from = InputHelper::clean($this->request->get('dateFrom', null)); $to = InputHelper::clean($this->request->get('dateTo', null)); $dataFormat = InputHelper::clean($this->request->get('dataFormat', null)); $response = ['success' => 0]; if ($timezone) { $fromDate = new \DateTime($from, new \DateTimeZone($timezone)); $toDate = new \DateTime($to, new \DateTimeZone($timezone)); } else { $fromDate = new \DateTime($from); $toDate = new \DateTime($to); } $params = ['timeUnit' => InputHelper::clean($this->request->get('timeUnit', 'Y')), 'dateFormat' => InputHelper::clean($this->request->get('dateFormat', null)), 'dateFrom' => $fromDate, 'dateTo' => $toDate, 'limit' => InputHelper::int($this->request->get('limit', null)), 'filter' => $this->request->get('filter', [])]; $cacheTimeout = InputHelper::int($this->request->get('cacheTimeout', null)); $widgetHeight = InputHelper::int($this->request->get('height', 300)); $widget = new Widget(); $widget->setParams($params); $widget->setType($type); $widget->setHeight($widgetHeight); if ($cacheTimeout === null) { $widget->setCacheTimeout($cacheTimeout); } $this->model->populateWidgetContent($widget); $data = $widget->getTemplateData(); if (!$data) { return $this->notFound(); } if ($dataFormat == 'raw') { if (isset($data['chartData']['labels']) && isset($data['chartData']['datasets'])) { $rawData = []; foreach ($data['chartData']['datasets'] as $dataset) { $rawData[$dataset['label']] = []; foreach ($dataset['data'] as $key => $value) { $rawData[$dataset['label']][$data['chartData']['labels'][$key]] = $value; } } $data = $rawData; } elseif (isset($data['raw'])) { $data = $data['raw']; } } else { if (isset($data['raw'])) { unset($data['raw']); } } $response['cached'] = $widget->isCached(); $response['execution_time'] = microtime(true) - $start; $response['success'] = 1; $response['data'] = $data; $view = $this->view($response, Codes::HTTP_OK); return $this->handleView($view); }
/** * Load widget content from the onWidgetDetailGenerate event * * @param Widget $widget * @param array $filter */ public function populateWidgetContent(Widget &$widget, $filter = array()) { $cacheDir = $this->factory->getParameter('cached_data_dir', $this->factory->getSystemPath('cache', true)); $dispatcher = $this->factory->getDispatcher(); if ($widget->getCacheTimeout() == null || $widget->getCacheTimeout() == -1) { $widget->setCacheTimeout($this->factory->getParameter('cached_data_timeout')); } // Merge global filter with widget params $widgetParams = $widget->getParams(); $resultParams = array_merge($widgetParams, $filter); // Add the user timezone if (empty($resultParams['timezone'])) { $resultParams['timezone'] = $this->factory->getUser()->getTimezone(); } // Clone the objects in param array to avoid reference issues if some subscriber changes them foreach ($resultParams as &$param) { if (is_object($param)) { $param = clone $param; } } $widget->setParams($resultParams); $event = new WidgetDetailEvent($this->translator); $event->setWidget($widget); $event->setCacheDir($cacheDir, $this->factory->getUser()->getId()); $event->setSecurity($this->factory->getSecurity()); $dispatcher->dispatch(DashboardEvents::DASHBOARD_ON_MODULE_DETAIL_GENERATE, $event); }