src/Controller/ChartController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Vendor;
  4. use App\Entity\VendorCategory;
  5. use App\Enumerations\VendorCategoryEnumeration;
  6. use App\Enumerations\VendorStatusEnumeration;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class ChartController extends AbstractController
  14. {
  15.     #[Route('/chart'name'app_chart')]
  16.     public function index(): Response
  17.     {
  18.         return $this->render('chart/index.html.twig', [
  19.             'controller_name' => 'ChartController',
  20.         ]);
  21.     }
  22.     #[Route('/chart/vendors/all'name'app_chartallvendors')]
  23.     public function allvendors(Request $requestEntityManagerInterface $entityManager): Response
  24.     {
  25.         $vendors $entityManager->getRepository(Vendor::class)->findAll();
  26.         $pieData = [];
  27.         /**
  28.          * @var Vendor $vendor
  29.          */
  30.         foreach ($vendors as $vendor) {
  31.             $categories $vendor->getVendorCategories();
  32.             /**
  33.              * @var VendorCategory $category
  34.              */
  35.             foreach ($categories as $category) {
  36.                 if (!$category->isIsPrimary()) {
  37.                     continue;
  38.                 }
  39.                 if (!isset($pieData[$category->getCategory()])) {
  40.                     $pieData[$category->getCategory()] = 0;
  41.                 }
  42.                 $pieData[$category->getCategory()]++;
  43.             }
  44.         }
  45.         $colors = [];
  46.         $labels array_keys($pieData);
  47.         foreach ($labels as $label) {
  48.             $colors[] = VendorCategoryEnumeration::getColor($label);
  49.         }
  50.         $data = [
  51.             'labels' => array_keys($pieData),
  52.             'datasets' => [[
  53.                 'label' => 'Categories',
  54.                 'data' => array_values($pieData),
  55.                 'backgroundColor' => $colors,
  56.                 'hoverOffset' => 4,
  57.                 'options' => [
  58.                     'legend' => [
  59.                         'display' => false
  60.                     ]
  61.                 ]
  62.             ]],
  63.         ];
  64.         return new JsonResponse($data);
  65.     }
  66.     #[Route('/chart/vendors/approved'name'app_chartapprovedvendors')]
  67.     public function approvedvendors(Request $requestEntityManagerInterface $entityManager): Response
  68.     {
  69.         $vendors $entityManager->getRepository(Vendor::class)->findBy(['status' => VendorStatusEnumeration::STATUS_APPROVED]);
  70.         $pieData = [];
  71.         /**
  72.          * @var Vendor $vendor
  73.          */
  74.         foreach ($vendors as $vendor) {
  75.             $categories $vendor->getVendorCategories();
  76.             /**
  77.              * @var VendorCategory $category
  78.              */
  79.             foreach ($categories as $category) {
  80.                 if (!$category->isIsPrimary()) {
  81.                     continue;
  82.                 }
  83.                 if (!isset($pieData[$category->getCategory()])) {
  84.                     $pieData[$category->getCategory()] = 0;
  85.                 }
  86.                 $pieData[$category->getCategory()]++;
  87.             }
  88.         }
  89.         $colors = [];
  90.         $labels array_keys($pieData);
  91.         foreach ($labels as $label) {
  92.             $colors[] = VendorCategoryEnumeration::getColor($label);
  93.         }
  94.         $data = [
  95.             'labels' => array_keys($pieData),
  96.             'datasets' => [[
  97.                 'label' => 'Categories',
  98.                 'data' => array_values($pieData),
  99.                 'backgroundColor' => $colors,
  100.                 'hoverOffset' => 4,
  101.                 'options' => [
  102.                     'legend' => [
  103.                         'display' => false
  104.                     ]
  105.                 ]
  106.             ]],
  107.         ];
  108.         return new JsonResponse($data);
  109.     }
  110. }