chill-bundles/src/Bundle/ChillMainBundle/Controller/DashboardHomepageController.php
2023-05-09 12:58:02 +02:00

121 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Widget\WidgetHandlerManager;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/{_locale}/dashboard")
*/
class DashboardHomepageController extends AbstractController{
// private WidgetHandlerManager $widgetHandlerManager;
//
// public function __construct(
// //AccompanyingPeriodRepository $accompanyingPeriodRepository,
// WidgetHandlerManager $widgetHandlerManager
// )
// {
// //$this->accompanyingPeriodRepository = $accompanyingPeriodRepository;
// $this->widgetHandlerManager = $widgetHandlerManager;
// }
private WidgetHandlerManager $widgetHandlerManager;
/**
* @Route("/raw_data", name="chill_main_widget_raw_data")
*/
public function index(): JsonResponse
{
// Retrieve data and prepare it for the chart
$chartData = [
'data'=> [
'labels' => [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
],
'datasets' =>[
[
'label' => 'Dataset 1',
'data' => [40, 20, 12, 39, 10, 40, 39, 80, 40, 20, 12, 11],
'backgroundColor' => ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)'],
'borderColor' => ['rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)'],
'borderWidth' => 1,
],
]
],
];
$chartOptions = [
'options' => [
'responsive'=> 'true',
'maintainAspectRatio'=> 'false'
]
];
$chartConfig =$chartData + $chartOptions;
//Return the chart data as JSON response
return $this->json($chartConfig, $status = 200,$headers = [], $context= []);
}
/**
* @Route("/widget/{context}", name="chill_main_widget_get_data")
*/
public function getDataForWidget(Request $request, $context): JsonResponse
{
//Retrieve data from request in vue component
$requestData = json_decode($request->getContent(),true);
// Process the widget data using the WidgetHandlerManager
$handler = $this->widgetHandlerManager->getHandler($requestData, $context);
// Return the widget data in JSON response
return new JsonResponse($this->getData($requestData, $context));
}
private function getData( array $config,array $context)
{
$widgetData = [];
foreach ($config as $widget) {
$widgetData[] = [
'data' => $this->widgetHandlerManager->getDataForWidget($config,$context),
'options'=> 'responsive: true'
];
}
return $widgetData;
}
}