src/Controller/DefaultController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use App\Entity\Supply;
  6. use App\Form\BoxStatsType;
  7. use App\Form\ACControlType;
  8. use App\Entity\Container;
  9. use App\Entity\ACState;
  10. use App\Entity\ACSetting;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. class DefaultController extends BaseController
  13. {
  14.     /**
  15.      * @Route("/", name="root")
  16.      */
  17.     public function index()
  18.     {
  19.         // !!! na poradi IFu zalezi (protoze hierarchie...)
  20.         if($this->isGranted('ROLE_ADMIN'))
  21.         {
  22.             return $this->render(
  23.                 'pages/dashboard_admin.html.twig',
  24.                 [
  25.                     'containers' => $this->containerModel->getAllContainersWithData(),
  26.                 ]
  27.             );
  28.         }
  29.         if($this->isGranted('ROLE_SUPPLIER'))
  30.         {
  31.             $supplies = array();
  32.             foreach(['future''today''past'] as $when)
  33.             {
  34.                 $sups $this->supplyModel->getSuppliesOfSupplier($this->getUser()->getSupplier(), $when);
  35.                 if(count($sups) > 0$supplies[$when] = $sups;
  36.             }
  37.             return $this->render(
  38.                 'pages/dashboard_supplier.html.twig',
  39.                 [
  40.                     'supplies' => $supplies,
  41.                 ]
  42.             );
  43.         }
  44.         if($this->isGranted('ROLE_PROVIDER'))
  45.         {
  46.             return $this->render(
  47.                 'pages/dashboard_provider.html.twig',
  48.                 [
  49.                 ]
  50.             );
  51.         }
  52. //        return $this->render(
  53. //            'index.html.twig'
  54. //        );
  55.     }
  56.     /**
  57.      * @Route("/dashboard", name="dashboard")
  58.      */
  59.     public function dashboard()
  60.     {
  61.         return $this->redirectToRoute('root');
  62.     }
  63.     /**
  64.      * @Route("/log/{day}/{month}/{year}", name="view_log", defaults={"year"=null,"month"=null,"day"=null}, requirements={"year"="\d+","month"="\d+","day"="\d+"})
  65.      */
  66.     public function viewLog(Request $request$year$month$day)
  67.     {
  68.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  69.         // osetreni parametru
  70.         $now = new \DateTime();
  71.         if(!is_null($year)) $year intval($year); else $year intval($now->format('Y'));
  72.         if(!is_null($month)) $month intval($month); else $month intval($now->format('n'));
  73.         if(!is_null($day)) $day intval($day); else $day intval($now->format('j'));
  74.         if(!checkdate($month$day$year)) {$year intval($now->format('Y')); $month intval($now->format('n')); $day intval($now->format('j')); }
  75.         $date = new \DateTime("$year-$month-$day");
  76.         // minuly a nasledujici mesic
  77.         $prev = array();
  78.         $next = array();
  79.         $prev['month'] = $month 1;
  80.         if($prev['month'] < 1)
  81.         {
  82.             $prev['month'] = 12;
  83.             $prev['year'] = $year 1;
  84.         }
  85.         else
  86.         {
  87.             $prev['year'] = $year;
  88.         }
  89.         $prev['day'] = cal_days_in_month(CAL_GREGORIAN$prev['month'], $prev['year']);
  90.         $next['day'] = 1;
  91.         $next['month'] = $month 1;
  92.         if($next['month'] > 12)
  93.         {
  94.             $next['month'] = 1;
  95.             $next['year'] = $year 1;
  96.         }
  97.         else
  98.         {
  99.             $next['year'] = $year;
  100.         }
  101.         // zjistit pocet logovanych operaci pro jednotlive dny
  102.         $operationCounts $this->boxModel->getLogCountsByDaysOfMonth($year$month);
  103.         $opCnts = array();
  104.         foreach($operationCounts as $opcnt)
  105.         {
  106.             $opCnts[$opcnt['day']] = $opcnt['ops'];
  107.         }
  108.         // vyrobit kalendar do sablony (po radkach)
  109.         $firstDayOfMonth mktime(0,0,0,$month,1,$year); // prvni den mesice
  110.         $numberDays date('t',$firstDayOfMonth); // kolik dni je v mesici
  111.         $dayOfWeek = (getdate($firstDayOfMonth)['wday'] + 6) % 7// jakym dnem mesic zacina? (su-sa, my chceme mo-su)
  112.         $calendar = array(); // pole radku (tydny mo-su)
  113.         $currentDay 1// zaciname prvniho
  114.         $row = array(); // tyden
  115.         for($i 0$i $dayOfWeek$i++) // prazdne bunky za prvni vynechane dny (minuly mesic)
  116.         {
  117.             $row[] = null;
  118.         }
  119.         while($currentDay <= $numberDays// pres kazdy den v mesici
  120.         {
  121.             if ($dayOfWeek == 7// mame hotovo 0 - 6, prepiname na dalsi row
  122.             {
  123.                  $dayOfWeek 0;
  124.                  $calendar[] = $row;
  125.                  $row = array();
  126.             }
  127.             $cell = array();
  128.             $cell['displayDate'] = $currentDay;
  129.             if(!empty($opCnts[$currentDay])) $cell['opCount'] = $opCnts[$currentDay];
  130.             if ("$year-$month-$currentDay== $date->format("Y-n-j"))
  131.             {
  132.                 // aktualne zobrazeny datum
  133.                 $cell['displayed'] = true;
  134.             }
  135.             else
  136.             {
  137.                 // aktualne nezobrazeny datum (bude tam link)
  138.                 $cell['href'] = $this->generateUrl('view_log', ['year' => $year'month' => $month'day' => $currentDay]);
  139.             }
  140.             // posunujeme datum a take day of week
  141.             $row[] = $cell;
  142.             $currentDay++;
  143.             $dayOfWeek++;
  144.         }
  145.         // prazdne bunky do konce tydne
  146.         for($i 0$i $dayOfWeek$i++)
  147.         {
  148.             $row[] = null;
  149.         }
  150.         $calendar[] = $row// posledni radek do kalendare
  151.         return $this->render(
  152.             'pages/log.html.twig',
  153.             [
  154.                 'date'     => $date,
  155.                 'calendar' => $calendar,
  156.                 'prevHref'  => $this->generateUrl('view_log'$prev),
  157.                 'nextHref'  => $this->generateUrl('view_log'$next),
  158.                 'logItems'  => $this->boxModel->getLogItemsWithDescriptionByDate($date),
  159.             ]
  160.         );
  161.     }
  162.     /**
  163.      * @Route("/supply-log/{id_supply}", name="view_supply_log", requirements={"id_supply"="\d+"})
  164.      */
  165.     public function viewSupplyLog(Request $request$id_supply)
  166.     {
  167.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  168.         $supply $this->supplyModel->getSupply($id_supply);
  169.         if(!$supply instanceof Supply) throw $this->createNotFoundException('Dodávka nenalezena');
  170.         return $this->render(
  171.             'pages/supply-log.html.twig',
  172.             [
  173.                 'supply'    => $supply,
  174.                 'logItems'  => $this->boxModel->getLogItemsWithDescriptionBySupply($supply),
  175.             ]
  176.         );
  177.     }
  178.     /**
  179.      * @Route("/box-stats", name="box_stats")
  180.      */
  181.     public function boxStats(Request $request)
  182.     {
  183.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  184.         $form $this->createForm(BoxStatsType::class);
  185.         $form->get('container')->setData($this->containerModel->getFirstContainer());
  186.         $form->get('startDate')->setData(new \DateTime("first day of previous month"));
  187.         $form->get('endDate')->setData(new \DateTime("last day of previous month"));
  188.         $form->get('suppliersMode')->setData(1);
  189.         $form->get('suppliers')->setData(new ArrayCollection);
  190.         $form->handleRequest($request); // postara se o prepsani dat formu z requestu pokud byl submit
  191.         // tohle je mi vlastne jedno...
  192.         if($form->isSubmitted() && $form->isValid())
  193.         {
  194.         }
  195.         // zde si proste vezmu data z formu a je to... bud byla prepsana submitem, nebo jsou default
  196.         // vyhledam si operace:
  197.         $stats_by_date $this->boxModel->getBoxStats2(
  198.             $form->get('container')->getData(),
  199.             $form->get('startDate')->getData(),
  200.             $form->get('endDate')->getData(),
  201.             $form->get('suppliersMode')->getData(),
  202.             $form->get('suppliers')->getData(),
  203.             'by_date'
  204.         );
  205.         $stats_by_supplier $this->boxModel->getBoxStats2(
  206.             $form->get('container')->getData(),
  207.             $form->get('startDate')->getData(),
  208.             $form->get('endDate')->getData(),
  209.             $form->get('suppliersMode')->getData(),
  210.             $form->get('suppliers')->getData(),
  211.             'by_supplier'
  212.         );
  213.         /**/
  214.         //$this->supplyModel->getSuppliesForStats($form->get('startDate')->getData(), $endDate = $form->get('endDate')->getData(), $form->get('suppliersMode')->getData(), $suppliers = $form->get('suppliers')->getData());
  215. /*
  216.         foreach($supplies as &$supply)
  217.         {
  218.             $supply->setData('warmInserted', $this->boxModel->getLogCountBy(['supply' => $supply, 'sourceState' => 100, 'destinationState' => 400, 'zone' => 'W']));
  219.             $supply->setData('coldInserted', $this->boxModel->getLogCountBy(['supply' => $supply, 'sourceState' => 100, 'destinationState' => 400, 'zone' => 'C']));
  220.             $supply->setData('warmInContainer', count($supply->getBoxesInSlots('W')));
  221.             $supply->setData('coldInContainer', count($supply->getBoxesInSlots('C')));
  222.             $supply->setData('warmExtracted', $this->boxModel->getLogCountBy(['supply' => $supply, 'sourceState' => 400, 'destinationState' => 200, 'zone' => 'W']));
  223.             $supply->setData('coldExtracted', $this->boxModel->getLogCountBy(['supply' => $supply, 'sourceState' => 400, 'destinationState' => 200, 'zone' => 'C']));
  224.         }
  225. */
  226.         /**/
  227.         return $this->render(
  228.                 'pages/box-stats.html.twig',
  229.                 [
  230.                     'form'      => $form->createView(),
  231.                     'stats_by_date'     => $stats_by_date,
  232.                     'stats_by_supplier'    => $stats_by_supplier
  233.                 ]
  234.         );
  235.     }
  236.     /**
  237.      * @Route("/ac-control/{container_id}", name="ac_control", requirements={"container_id"="\d+"})
  238.      */
  239.     public function acControl(Request $request$container_id)
  240.     {
  241.         $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
  242.         $container $this->containerModel->getContainer($container_id);
  243.         if(!$container instanceof Container$this->createNotFoundException('Kontejner nenalezen');
  244.         $form $this->createForm(ACControlType::class);
  245.         $form->handleRequest($request);
  246.         if($form->isSubmitted() && $form->isValid())
  247.         {
  248.             $this->containerModel->saveSettedAC($container$form->getData());
  249.         }
  250.         $acState $this->containerModel->getLastReportedACState($container);
  251.         if(is_array($acState))
  252.         {
  253.             $container->setData('reportedACWhen'$acState['reportedACWhen']);
  254.             $container->setData('reportedAC'$acState['reportedAC']);
  255.         }
  256.         $acSetting $this->containerModel->getLastACSetting($container);
  257.         if(is_array($acSetting))
  258.         {
  259.             $container->setData('settedACWhen'$acSetting['settedACWhen']);
  260.             $container->setData('settedAC'$acSetting['settedAC']);
  261.             $container->setData('downloadedAC'$acSetting['downloaded']);
  262.             if(!$form->isSubmitted())
  263.             {
  264.                 $form->get('temp_1')->setData($acSetting['settedAC']['temp_1'] / 10);
  265.                 $form->get('temp_2')->setData($acSetting['settedAC']['temp_2'] / 10);
  266.                 $form->get('power_1')->setData($acSetting['settedAC']['power_1']);
  267.                 $form->get('power_2')->setData($acSetting['settedAC']['power_2']);
  268.             }
  269.         }
  270.         return $this->render(
  271.                 'pages/ac-control.html.twig',
  272.                 [
  273.                     'c' => $container,
  274.                     'form'  => $form->createView(),
  275.                 ]
  276.         );
  277.     }
  278.     /**
  279.      * @Route("/ac-log/{container_id}/{day}/{month}/{year}", name="view_ac_log", defaults={"container_id"=null,"year"=null,"month"=null,"day"=null}, requirements={"container_id"="\d+","year"="\d+","month"="\d+","day"="\d+"})
  280.      */
  281.     public function viewACLog(Request $request$container_id$year$month$day)
  282.     {
  283.         $this->denyAccessUnlessGranted('ROLE_ADMIN');
  284.         // osetreni parametru
  285.         $now = new \DateTime();
  286.         if(!is_null($year)) $year intval($year); else $year intval($now->format('Y'));
  287.         if(!is_null($month)) $month intval($month); else $month intval($now->format('n'));
  288.         if(!is_null($day)) $day intval($day); else $day intval($now->format('j'));
  289.         if(!checkdate($month$day$year)) {$year intval($now->format('Y')); $month intval($now->format('n')); $day intval($now->format('j')); }
  290.         $date = new \DateTime("$year-$month-$day");
  291.         if(is_null($container_id))
  292.         {
  293.             $container $this->containerModel->getFirstContainer();
  294.         }
  295.         else
  296.         {
  297.             $container $this->containerModel->getContainer($container_id);
  298.         }
  299.         if(!$container instanceof Container$this->createNotFoundException('Kontejner nenalezen');
  300.         // minuly a nasledujici mesic
  301.         $prev = array();
  302.         $next = array();
  303.         $prev['month'] = $month 1;
  304.         if($prev['month'] < 1)
  305.         {
  306.             $prev['month'] = 12;
  307.             $prev['year'] = $year 1;
  308.         }
  309.         else
  310.         {
  311.             $prev['year'] = $year;
  312.         }
  313.         $prev['day'] = cal_days_in_month(CAL_GREGORIAN$prev['month'], $prev['year']);
  314.         $next['day'] = 1;
  315.         $next['month'] = $month 1;
  316.         if($next['month'] > 12)
  317.         {
  318.             $next['month'] = 1;
  319.             $next['year'] = $year 1;
  320.         }
  321.         else
  322.         {
  323.             $next['year'] = $year;
  324.         }
  325.         // zjistit pocet logovanych operaci pro jednotlive dny
  326.         $acsCounts $this->boxModel->getACStatesByDaysOfMonth($container$year$month);
  327.         $acsCnts = array();
  328.         foreach($acsCounts as $acscnt)
  329.         {
  330.             $acsCnts[$acscnt['day']] = $acscnt['reps'];
  331.         }
  332.         // vyrobit kalendar do sablony (po radkach)
  333.         $firstDayOfMonth mktime(0,0,0,$month,1,$year); // prvni den mesice
  334.         $numberDays date('t',$firstDayOfMonth); // kolik dni je v mesici
  335.         $dayOfWeek = (getdate($firstDayOfMonth)['wday'] + 6) % 7// jakym dnem mesic zacina? (su-sa, my chceme mo-su)
  336.         $calendar = array(); // pole radku (tydny mo-su)
  337.         $currentDay 1// zaciname prvniho
  338.         $row = array(); // tyden
  339.         for($i 0$i $dayOfWeek$i++) // prazdne bunky za prvni vynechane dny (minuly mesic)
  340.         {
  341.             $row[] = null;
  342.         }
  343.         while($currentDay <= $numberDays// pres kazdy den v mesici
  344.         {
  345.             if ($dayOfWeek == 7// mame hotovo 0 - 6, prepiname na dalsi row
  346.             {
  347.                  $dayOfWeek 0;
  348.                  $calendar[] = $row;
  349.                  $row = array();
  350.             }
  351.             $cell = array();
  352.             $cell['displayDate'] = $currentDay;
  353.             if(!empty($acsCnts[$currentDay])) $cell['opCount'] = $acsCnts[$currentDay];
  354.             if ("$year-$month-$currentDay== $date->format("Y-n-j"))
  355.             {
  356.                 // aktualne zobrazeny datum
  357.                 $cell['displayed'] = true;
  358.             }
  359.             else
  360.             {
  361.                 // aktualne nezobrazeny datum (bude tam link)
  362.                 $cell['href'] = $this->generateUrl('view_ac_log', ['year' => $year'month' => $month'day' => $currentDay'container_id' => $container->getId()]);
  363.             }
  364.             // posunujeme datum a take day of week
  365.             $row[] = $cell;
  366.             $currentDay++;
  367.             $dayOfWeek++;
  368.         }
  369.         // prazdne bunky do konce tydne
  370.         for($i 0$i $dayOfWeek$i++)
  371.         {
  372.             $row[] = null;
  373.         }
  374.         $calendar[] = $row// posledni radek do kalendare
  375.         $data $this->boxModel->getACSItemsByDate($container$date);
  376.         //dump(json_encode($data));
  377.         return $this->render(
  378.             'pages/ac-log.html.twig',
  379.             [
  380.                 'date'     => $date,
  381.                 'container' => $container,
  382.                 'allContainers' => $this->containerModel->getAllUsableContainers(),
  383.                 'calendar' => $calendar,
  384.                 'prevHref'  => $this->generateUrl('view_ac_log'array_merge($prev, ['container_id' => $container->getId()])),
  385.                 'nextHref'  => $this->generateUrl('view_ac_log'array_merge($next, ['container_id' => $container->getId()])),
  386.                 'acsItems'  => json_encode($data),
  387.             ]
  388.         );
  389.     }
  390.     /**
  391.      * @Route("/cams/{container_id}", name="view_cams", requirements={"container_id"="\d+"})
  392.      */
  393.     public function viewCams(Request $request$container_id)
  394.     {
  395.         $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
  396.         $container $this->containerModel->getContainer($container_id);
  397.         if(!$container instanceof Container$this->createNotFoundException('Kontejner nenalezen');
  398.         return $this->render(
  399.                 'pages/cams.html.twig',
  400.                 [
  401.                     'c' => $container,
  402.                     //'imgs'  => $this->containerModel->getCamImages($container),
  403.                 ]
  404.         );
  405.     }
  406.     /**
  407.      * @Route("/get-cam-images/{container_id}", name="get_cam_images", requirements={"container_id"="\d+"}, methods={"GET"}, condition="request.isXmlHttpRequest()")
  408.      */
  409.     public function getCamImages($container_idRequest $request)
  410.     {
  411.         $container $this->containerModel->getContainer($container_id);
  412.         if(!$container instanceof Container$this->createNotFoundException('Kontejner nenalezen');
  413.         $imgs $this->containerModel->getCamImages($container);
  414.         //sleep(3);
  415.         return $this->json($imgs);
  416.     }
  417.     /**
  418.      * @Route("/live/{container_id}/{cam_id}", name="live_cam", requirements={"container_id"="\d+","cam_id"="\d+"})
  419.      */
  420.     public function viewLiveCam(Request $request$container_id$cam_id=0)
  421.     {
  422.         $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
  423.         $container $this->containerModel->getContainer($container_id);
  424.         if(!$container instanceof Container$this->createNotFoundException('Kontejner nenalezen');
  425.         if($cam_id == 0) return $this->redirectToRoute('live_cam', ['container_id' => $container_id'cam_id' => 1]);
  426.         $playlist_file "stream/$container_id/$cam_id/index.m3u8";
  427.         return $this->render(
  428.                 'pages/live.html.twig',
  429.                 [
  430.                     'c' => $container,
  431.                     'cam'   => $cam_id,
  432.                     'hls_playlist_url' => "/$playlist_file?nocache=" filemtime($playlist_file)
  433.                 ]
  434.         );
  435.     }
  436. }