<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Supply;
use App\Form\BoxStatsType;
use App\Form\ACControlType;
use App\Entity\Container;
use App\Entity\ACState;
use App\Entity\ACSetting;
use Doctrine\Common\Collections\ArrayCollection;
class DefaultController extends BaseController
{
/**
* @Route("/", name="root")
*/
public function index()
{
// !!! na poradi IFu zalezi (protoze hierarchie...)
if($this->isGranted('ROLE_ADMIN'))
{
return $this->render(
'pages/dashboard_admin.html.twig',
[
'containers' => $this->containerModel->getAllContainersWithData(),
]
);
}
if($this->isGranted('ROLE_SUPPLIER'))
{
$supplies = array();
foreach(['future', 'today', 'past'] as $when)
{
$sups = $this->supplyModel->getSuppliesOfSupplier($this->getUser()->getSupplier(), $when);
if(count($sups) > 0) $supplies[$when] = $sups;
}
return $this->render(
'pages/dashboard_supplier.html.twig',
[
'supplies' => $supplies,
]
);
}
if($this->isGranted('ROLE_PROVIDER'))
{
return $this->render(
'pages/dashboard_provider.html.twig',
[
]
);
}
// return $this->render(
// 'index.html.twig'
// );
}
/**
* @Route("/dashboard", name="dashboard")
*/
public function dashboard()
{
return $this->redirectToRoute('root');
}
/**
* @Route("/log/{day}/{month}/{year}", name="view_log", defaults={"year"=null,"month"=null,"day"=null}, requirements={"year"="\d+","month"="\d+","day"="\d+"})
*/
public function viewLog(Request $request, $year, $month, $day)
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
// osetreni parametru
$now = new \DateTime();
if(!is_null($year)) $year = intval($year); else $year = intval($now->format('Y'));
if(!is_null($month)) $month = intval($month); else $month = intval($now->format('n'));
if(!is_null($day)) $day = intval($day); else $day = intval($now->format('j'));
if(!checkdate($month, $day, $year)) {$year = intval($now->format('Y')); $month = intval($now->format('n')); $day = intval($now->format('j')); }
$date = new \DateTime("$year-$month-$day");
// minuly a nasledujici mesic
$prev = array();
$next = array();
$prev['month'] = $month - 1;
if($prev['month'] < 1)
{
$prev['month'] = 12;
$prev['year'] = $year - 1;
}
else
{
$prev['year'] = $year;
}
$prev['day'] = cal_days_in_month(CAL_GREGORIAN, $prev['month'], $prev['year']);
$next['day'] = 1;
$next['month'] = $month + 1;
if($next['month'] > 12)
{
$next['month'] = 1;
$next['year'] = $year + 1;
}
else
{
$next['year'] = $year;
}
// zjistit pocet logovanych operaci pro jednotlive dny
$operationCounts = $this->boxModel->getLogCountsByDaysOfMonth($year, $month);
$opCnts = array();
foreach($operationCounts as $opcnt)
{
$opCnts[$opcnt['day']] = $opcnt['ops'];
}
// vyrobit kalendar do sablony (po radkach)
$firstDayOfMonth = mktime(0,0,0,$month,1,$year); // prvni den mesice
$numberDays = date('t',$firstDayOfMonth); // kolik dni je v mesici
$dayOfWeek = (getdate($firstDayOfMonth)['wday'] + 6) % 7; // jakym dnem mesic zacina? (su-sa, my chceme mo-su)
$calendar = array(); // pole radku (tydny mo-su)
$currentDay = 1; // zaciname prvniho
$row = array(); // tyden
for($i = 0; $i < $dayOfWeek; $i++) // prazdne bunky za prvni vynechane dny (minuly mesic)
{
$row[] = null;
}
while($currentDay <= $numberDays) // pres kazdy den v mesici
{
if ($dayOfWeek == 7) // mame hotovo 0 - 6, prepiname na dalsi row
{
$dayOfWeek = 0;
$calendar[] = $row;
$row = array();
}
$cell = array();
$cell['displayDate'] = $currentDay;
if(!empty($opCnts[$currentDay])) $cell['opCount'] = $opCnts[$currentDay];
if ("$year-$month-$currentDay" == $date->format("Y-n-j"))
{
// aktualne zobrazeny datum
$cell['displayed'] = true;
}
else
{
// aktualne nezobrazeny datum (bude tam link)
$cell['href'] = $this->generateUrl('view_log', ['year' => $year, 'month' => $month, 'day' => $currentDay]);
}
// posunujeme datum a take day of week
$row[] = $cell;
$currentDay++;
$dayOfWeek++;
}
// prazdne bunky do konce tydne
for($i = 0; $i < 7 - $dayOfWeek; $i++)
{
$row[] = null;
}
$calendar[] = $row; // posledni radek do kalendare
return $this->render(
'pages/log.html.twig',
[
'date' => $date,
'calendar' => $calendar,
'prevHref' => $this->generateUrl('view_log', $prev),
'nextHref' => $this->generateUrl('view_log', $next),
'logItems' => $this->boxModel->getLogItemsWithDescriptionByDate($date),
]
);
}
/**
* @Route("/supply-log/{id_supply}", name="view_supply_log", requirements={"id_supply"="\d+"})
*/
public function viewSupplyLog(Request $request, $id_supply)
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$supply = $this->supplyModel->getSupply($id_supply);
if(!$supply instanceof Supply) throw $this->createNotFoundException('Dodávka nenalezena');
return $this->render(
'pages/supply-log.html.twig',
[
'supply' => $supply,
'logItems' => $this->boxModel->getLogItemsWithDescriptionBySupply($supply),
]
);
}
/**
* @Route("/box-stats", name="box_stats")
*/
public function boxStats(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$form = $this->createForm(BoxStatsType::class);
$form->get('container')->setData($this->containerModel->getFirstContainer());
$form->get('startDate')->setData(new \DateTime("first day of previous month"));
$form->get('endDate')->setData(new \DateTime("last day of previous month"));
$form->get('suppliersMode')->setData(1);
$form->get('suppliers')->setData(new ArrayCollection);
$form->handleRequest($request); // postara se o prepsani dat formu z requestu pokud byl submit
// tohle je mi vlastne jedno...
if($form->isSubmitted() && $form->isValid())
{
}
// zde si proste vezmu data z formu a je to... bud byla prepsana submitem, nebo jsou default
// vyhledam si operace:
$stats_by_date = $this->boxModel->getBoxStats2(
$form->get('container')->getData(),
$form->get('startDate')->getData(),
$form->get('endDate')->getData(),
$form->get('suppliersMode')->getData(),
$form->get('suppliers')->getData(),
'by_date'
);
$stats_by_supplier = $this->boxModel->getBoxStats2(
$form->get('container')->getData(),
$form->get('startDate')->getData(),
$form->get('endDate')->getData(),
$form->get('suppliersMode')->getData(),
$form->get('suppliers')->getData(),
'by_supplier'
);
/**/
//$this->supplyModel->getSuppliesForStats($form->get('startDate')->getData(), $endDate = $form->get('endDate')->getData(), $form->get('suppliersMode')->getData(), $suppliers = $form->get('suppliers')->getData());
/*
foreach($supplies as &$supply)
{
$supply->setData('warmInserted', $this->boxModel->getLogCountBy(['supply' => $supply, 'sourceState' => 100, 'destinationState' => 400, 'zone' => 'W']));
$supply->setData('coldInserted', $this->boxModel->getLogCountBy(['supply' => $supply, 'sourceState' => 100, 'destinationState' => 400, 'zone' => 'C']));
$supply->setData('warmInContainer', count($supply->getBoxesInSlots('W')));
$supply->setData('coldInContainer', count($supply->getBoxesInSlots('C')));
$supply->setData('warmExtracted', $this->boxModel->getLogCountBy(['supply' => $supply, 'sourceState' => 400, 'destinationState' => 200, 'zone' => 'W']));
$supply->setData('coldExtracted', $this->boxModel->getLogCountBy(['supply' => $supply, 'sourceState' => 400, 'destinationState' => 200, 'zone' => 'C']));
}
*/
/**/
return $this->render(
'pages/box-stats.html.twig',
[
'form' => $form->createView(),
'stats_by_date' => $stats_by_date,
'stats_by_supplier' => $stats_by_supplier
]
);
}
/**
* @Route("/ac-control/{container_id}", name="ac_control", requirements={"container_id"="\d+"})
*/
public function acControl(Request $request, $container_id)
{
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
$container = $this->containerModel->getContainer($container_id);
if(!$container instanceof Container) $this->createNotFoundException('Kontejner nenalezen');
$form = $this->createForm(ACControlType::class);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$this->containerModel->saveSettedAC($container, $form->getData());
}
$acState = $this->containerModel->getLastReportedACState($container);
if(is_array($acState))
{
$container->setData('reportedACWhen', $acState['reportedACWhen']);
$container->setData('reportedAC', $acState['reportedAC']);
}
$acSetting = $this->containerModel->getLastACSetting($container);
if(is_array($acSetting))
{
$container->setData('settedACWhen', $acSetting['settedACWhen']);
$container->setData('settedAC', $acSetting['settedAC']);
$container->setData('downloadedAC', $acSetting['downloaded']);
if(!$form->isSubmitted())
{
$form->get('temp_1')->setData($acSetting['settedAC']['temp_1'] / 10);
$form->get('temp_2')->setData($acSetting['settedAC']['temp_2'] / 10);
$form->get('power_1')->setData($acSetting['settedAC']['power_1']);
$form->get('power_2')->setData($acSetting['settedAC']['power_2']);
}
}
return $this->render(
'pages/ac-control.html.twig',
[
'c' => $container,
'form' => $form->createView(),
]
);
}
/**
* @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+"})
*/
public function viewACLog(Request $request, $container_id, $year, $month, $day)
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
// osetreni parametru
$now = new \DateTime();
if(!is_null($year)) $year = intval($year); else $year = intval($now->format('Y'));
if(!is_null($month)) $month = intval($month); else $month = intval($now->format('n'));
if(!is_null($day)) $day = intval($day); else $day = intval($now->format('j'));
if(!checkdate($month, $day, $year)) {$year = intval($now->format('Y')); $month = intval($now->format('n')); $day = intval($now->format('j')); }
$date = new \DateTime("$year-$month-$day");
if(is_null($container_id))
{
$container = $this->containerModel->getFirstContainer();
}
else
{
$container = $this->containerModel->getContainer($container_id);
}
if(!$container instanceof Container) $this->createNotFoundException('Kontejner nenalezen');
// minuly a nasledujici mesic
$prev = array();
$next = array();
$prev['month'] = $month - 1;
if($prev['month'] < 1)
{
$prev['month'] = 12;
$prev['year'] = $year - 1;
}
else
{
$prev['year'] = $year;
}
$prev['day'] = cal_days_in_month(CAL_GREGORIAN, $prev['month'], $prev['year']);
$next['day'] = 1;
$next['month'] = $month + 1;
if($next['month'] > 12)
{
$next['month'] = 1;
$next['year'] = $year + 1;
}
else
{
$next['year'] = $year;
}
// zjistit pocet logovanych operaci pro jednotlive dny
$acsCounts = $this->boxModel->getACStatesByDaysOfMonth($container, $year, $month);
$acsCnts = array();
foreach($acsCounts as $acscnt)
{
$acsCnts[$acscnt['day']] = $acscnt['reps'];
}
// vyrobit kalendar do sablony (po radkach)
$firstDayOfMonth = mktime(0,0,0,$month,1,$year); // prvni den mesice
$numberDays = date('t',$firstDayOfMonth); // kolik dni je v mesici
$dayOfWeek = (getdate($firstDayOfMonth)['wday'] + 6) % 7; // jakym dnem mesic zacina? (su-sa, my chceme mo-su)
$calendar = array(); // pole radku (tydny mo-su)
$currentDay = 1; // zaciname prvniho
$row = array(); // tyden
for($i = 0; $i < $dayOfWeek; $i++) // prazdne bunky za prvni vynechane dny (minuly mesic)
{
$row[] = null;
}
while($currentDay <= $numberDays) // pres kazdy den v mesici
{
if ($dayOfWeek == 7) // mame hotovo 0 - 6, prepiname na dalsi row
{
$dayOfWeek = 0;
$calendar[] = $row;
$row = array();
}
$cell = array();
$cell['displayDate'] = $currentDay;
if(!empty($acsCnts[$currentDay])) $cell['opCount'] = $acsCnts[$currentDay];
if ("$year-$month-$currentDay" == $date->format("Y-n-j"))
{
// aktualne zobrazeny datum
$cell['displayed'] = true;
}
else
{
// aktualne nezobrazeny datum (bude tam link)
$cell['href'] = $this->generateUrl('view_ac_log', ['year' => $year, 'month' => $month, 'day' => $currentDay, 'container_id' => $container->getId()]);
}
// posunujeme datum a take day of week
$row[] = $cell;
$currentDay++;
$dayOfWeek++;
}
// prazdne bunky do konce tydne
for($i = 0; $i < 7 - $dayOfWeek; $i++)
{
$row[] = null;
}
$calendar[] = $row; // posledni radek do kalendare
$data = $this->boxModel->getACSItemsByDate($container, $date);
//dump(json_encode($data));
return $this->render(
'pages/ac-log.html.twig',
[
'date' => $date,
'container' => $container,
'allContainers' => $this->containerModel->getAllUsableContainers(),
'calendar' => $calendar,
'prevHref' => $this->generateUrl('view_ac_log', array_merge($prev, ['container_id' => $container->getId()])),
'nextHref' => $this->generateUrl('view_ac_log', array_merge($next, ['container_id' => $container->getId()])),
'acsItems' => json_encode($data),
]
);
}
/**
* @Route("/cams/{container_id}", name="view_cams", requirements={"container_id"="\d+"})
*/
public function viewCams(Request $request, $container_id)
{
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
$container = $this->containerModel->getContainer($container_id);
if(!$container instanceof Container) $this->createNotFoundException('Kontejner nenalezen');
return $this->render(
'pages/cams.html.twig',
[
'c' => $container,
//'imgs' => $this->containerModel->getCamImages($container),
]
);
}
/**
* @Route("/get-cam-images/{container_id}", name="get_cam_images", requirements={"container_id"="\d+"}, methods={"GET"}, condition="request.isXmlHttpRequest()")
*/
public function getCamImages($container_id, Request $request)
{
$container = $this->containerModel->getContainer($container_id);
if(!$container instanceof Container) $this->createNotFoundException('Kontejner nenalezen');
$imgs = $this->containerModel->getCamImages($container);
//sleep(3);
return $this->json($imgs);
}
/**
* @Route("/live/{container_id}/{cam_id}", name="live_cam", requirements={"container_id"="\d+","cam_id"="\d+"})
*/
public function viewLiveCam(Request $request, $container_id, $cam_id=0)
{
$this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
$container = $this->containerModel->getContainer($container_id);
if(!$container instanceof Container) $this->createNotFoundException('Kontejner nenalezen');
if($cam_id == 0) return $this->redirectToRoute('live_cam', ['container_id' => $container_id, 'cam_id' => 1]);
$playlist_file = "stream/$container_id/$cam_id/index.m3u8";
return $this->render(
'pages/live.html.twig',
[
'c' => $container,
'cam' => $cam_id,
'hls_playlist_url' => "/$playlist_file?nocache=" . filemtime($playlist_file)
]
);
}
}