mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-22 00:27:43 +00:00
Adding the file for visitor pattern + function that need to be implemented.
This commit is contained in:
parent
ef9e872394
commit
59cd8466be
@ -0,0 +1,64 @@
|
|||||||
|
<?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\WidgetManager;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
|
||||||
|
class DashboardHomepageController extends AbstractController{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/dashboard-chart-data", name="dashboard_chart_data")
|
||||||
|
*/
|
||||||
|
public function index(): JsonResponse
|
||||||
|
{
|
||||||
|
// Retrieve data and prepare it for the chart
|
||||||
|
$chartData = [
|
||||||
|
'labels' => ['Label 1', 'Label 2', 'Label 3'],
|
||||||
|
'datasets' => [
|
||||||
|
[
|
||||||
|
'label' => 'Dataset 1',
|
||||||
|
'data' => [10, 20, 30],
|
||||||
|
'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,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
//Return the chart data as JSON response
|
||||||
|
return $this->json($chartData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/widget/{context}, name="widget_data")
|
||||||
|
*/
|
||||||
|
public function getWidgetData(Request $request, $context, WidgetManager $widgetManager): JsonResponse
|
||||||
|
{
|
||||||
|
|
||||||
|
//Retrieve data from request in vue component
|
||||||
|
$requestData = json_decode($request->getContent(),true);
|
||||||
|
|
||||||
|
// Process the widget data using the WidgetManager
|
||||||
|
$widgetData = $widgetManager->getDataForWidget($requestData, $context);
|
||||||
|
|
||||||
|
// Return the widget data in JSON response
|
||||||
|
return new JsonResponse($widgetData);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
37
src/Bundle/ChillMainBundle/Widget/WidgetManager.php
Normal file
37
src/Bundle/ChillMainBundle/Widget/WidgetManager.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?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\Widget;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
|
||||||
|
|
||||||
|
final class WidgetManager
|
||||||
|
{
|
||||||
|
private WidgetProviderInterface $widgetProvider;
|
||||||
|
|
||||||
|
public function __construct(WidgetProviderInterface $widgetProvider)
|
||||||
|
{
|
||||||
|
$this->widgetProvider = $widgetProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDataForWidget($data,$context)
|
||||||
|
{
|
||||||
|
//Check if the WidgetProvider supports the given context
|
||||||
|
if ($this->widgetProvider->supports($data,$context)) {
|
||||||
|
// Get the widget data using specific Widget implementation
|
||||||
|
$widgetData = $this->widgetProvider->getDataForWidget($data);
|
||||||
|
return $widgetData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle unsupported context
|
||||||
|
throw new \InvalidArgumentException('Unsupported context.');
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
<?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\Widget;
|
||||||
|
|
||||||
|
interface WidgetProviderInterface{
|
||||||
|
|
||||||
|
public function supports(array $data, array $context);
|
||||||
|
|
||||||
|
public function getDataForWidget($data);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
36
src/Bundle/ChillMainBundle/Widget/Widgets/WidgetBar.php
Normal file
36
src/Bundle/ChillMainBundle/Widget/Widgets/WidgetBar.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?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\Widget\Widgets;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Widget\WidgetProviderInterface;
|
||||||
|
|
||||||
|
class WidgetBar implements WidgetProviderInterface
|
||||||
|
{
|
||||||
|
public function supports($data, $context): bool
|
||||||
|
{
|
||||||
|
// Check if the context is "bar"
|
||||||
|
return $context === 'bar';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDataForWidget($data): array
|
||||||
|
{
|
||||||
|
// Process the data for the bar widget
|
||||||
|
// In this example, we assume the data is an array of numbers
|
||||||
|
$barData = $data['bar'];
|
||||||
|
|
||||||
|
// Return the widget data as an associative array
|
||||||
|
return [
|
||||||
|
'type' => 'bar',
|
||||||
|
'data' => $barData,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
35
src/Bundle/ChillMainBundle/Widget/Widgets/WidgetNumber.php
Normal file
35
src/Bundle/ChillMainBundle/Widget/Widgets/WidgetNumber.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?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\Widget\Widgets;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Widget\WidgetProviderInterface;
|
||||||
|
|
||||||
|
class WidgetNumber implements WidgetProviderInterface{
|
||||||
|
public function supports($data, $context): bool
|
||||||
|
{
|
||||||
|
// Check if the context is "number"
|
||||||
|
return $context === 'number';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDataForWidget($data): array
|
||||||
|
{
|
||||||
|
// Process the data for the number widget
|
||||||
|
// We suppose that its only a single number
|
||||||
|
$numberData = $data['number'];
|
||||||
|
|
||||||
|
// Return the widget data as an associative array
|
||||||
|
return [
|
||||||
|
'type' => 'number',
|
||||||
|
'data' => $numberData,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user