<?php
namespace App\Controller;
use App\Entity\EquipmentSpecific;
use App\Entity\InterfaceGeneric;
use App\Entity\InterfaceSpecific;
use App\Entity\Rack;
use App\Entity\Room;
use App\Entity\Trunk;
use App\Entity\TrunkCableFiber;
use App\Entity\TrunkExtension;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Pagerfanta\Pagerfanta;
// use Pagerfanta\Adapter\DoctrineORMAdapter;
use Symfony\Contracts\Translation\TranslatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use App\Form\RackForm;
class RackController extends AbstractController
{
/**
* @Route("/settings/location/rack", name="rack")
*/
public function indexAction(Request $request, TranslatorInterface $translator)
{
$user = $this->getUser();
$rack = new Rack();
$errors = [];
$form = $this->createForm(RackForm::class, $rack);
$form->handleRequest($request);
if ($form->isSubmitted() && $user->hasRole("ROLE_SUPER_ADMIN")) {
if($form->isValid()){
$rack = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($rack);
$em->flush();
return $this->redirectToRoute('rack');
}
$validator = $this->get('validator');
$errors = $validator->validate($rack);
}
$list = $this->getDoctrine()->getRepository(Rack::class)->findAll();
return $this->render('location/rack.html.twig', [
'action' => 'insert',
'page_title' => $translator->trans('Racks'),
'box_title' => '<i class="fa fa-plus-circle fa-fw"></i> '.$translator->trans('Add new'),
'path_default' => 'rack',
'path_update' => 'rack_update',
'path_delete' => 'rack_delete',
'form' => $form->createView(),
'list' => $list,
'errors' => $errors
]);
}
/**
* @Route("/settings/location/rack/edit/{id}", name="rack_update")
*/
public function updateAction(Request $request, Rack $rack, TranslatorInterface $translator)
{
$user = $this->getUser();
$errors = [];
$form = $this->createForm(RackForm::class, $rack);
$form->handleRequest($request);
if ($form->isSubmitted() && $user->hasRole("ROLE_SUPER_ADMIN")) {
if($form->isValid()){
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('rack');
}
$validator = $this->get('validator');
$errors = $validator->validate($rack);
$this->getDoctrine()->getManager()->detach($rack);
}
$equipmentSpecific = $this->getDoctrine()->getRepository(EquipmentSpecific::class)->findBy(["rack"=>$rack, "isModule"=>false]);
//MODIFIED ON 14/02/2020
$racks = $this->getDoctrine()->getRepository(Rack::class)->findBy(["room"=>$rack->getRoom()], ["title"=>"ASC"]);
usort($racks, function ($a, $b) {
if($a && $b){
return strnatcmp($a->getTitle(), $b->getTitle());
}
else return 1;
});
return $this->render('location/rack_view.html.twig', [
'action' => 'update',
'page_title' => $translator->trans('Racks'),
'box_title' => '<i class="fa fa-edit fa-fw"></i> '.$translator->trans('Edit'),
'path_default' => 'rack',
'path_update' => 'rack_update',
'path_delete' => 'rack_delete',
'form' => $form->createView(),
'rack' => $rack,
'equipmentSpecific' => $equipmentSpecific,
"racks" => $racks,
'errors' => $errors
]);
}
/**
* @IsGranted("ROLE_SUPER_ADMIN")
* @Route("/settings/location/rack/delete/{id}", name="rack_delete")
*/
public function deleteAction($id, TranslatorInterface $translator)
{
$em = $this->getDoctrine()->getManager();
$rack = $em->getRepository(Rack::class)->find($id);
$msgError = null;
// find racks in usage
$eq = $em->getRepository(EquipmentSpecific::class)->findByRack($rack);
if (!$eq){
$em->remove($rack);
$em->flush();
}
else {
$msgError = $translator->trans('deleteError');
$this->addFlash(
'error',
$msgError
);
}
return $this->redirectToRoute('rack');
}
/**
* @Route("/rack/usage/item/{id}", name="rack_usage")
*/
public function usageFiberRacksAction($id, TranslatorInterface $translator)
{
$em = $this->getDoctrine()->getManager();
$rack = $em->getRepository(Rack::class)->find($id);
$equipmentSpecificList = $this->getDoctrine()->getRepository(EquipmentSpecific::class)->findBy(["rack"=>$rack, "isModule"=>false]);
// var_dump($equipmentSpecificList);
$eqList = [];
foreach ($equipmentSpecificList as $equipment){
$eq['equipmentSpecificName'] = $equipment->getEquipmentSpecificName();
$eq['equipmentGenericTitle'] = $equipment->getEquipmentGeneric()->getTitle();
$eq["modules"] = [];
$eq["slots"] = [];
if ($equipment->isModulaire()) {
foreach ($equipment->getSlotModulaire() as $slot) {
$module = $slot->getModule();
if ($module) {
$moduleTemp = array("equipmentSpecificName" => $module->getEquipmentSpecificName(),
"equipmentGenericTitle" => $module->getEquipmentGeneric()->getTitle()
);
$moduleTemp["interfaces"] = [];
$interfaces = $module->getInterfaceSpecific();
foreach ($interfaces as $interface){
$trunks = [];
$ports = $interface->getPort();
$nbPortLibreLink = count($ports);
$nbPortLibreTrunk = count($ports);
//$trunk = new Trunk();
foreach ($ports as $port){
if($port->getLink()){
$nbPortLibreLink--;
}
$tcfA = $this->getDoctrine()->getRepository(TrunkCableFiber::class)->findOneByPortA($port);
$tcfB = $this->getDoctrine()->getRepository(TrunkCableFiber::class)->findOneByPortB($port);
$tExt = $this->getDoctrine()->getRepository(TrunkExtension::class)->findOneByPortB($port);
if ($tcfA){
$nbPortLibreTrunk--;
$trunk = $tcfA->getTrunk();
if (!in_array($trunk, $trunks)){
$trunks[] = $trunk;
}
}
if ($tcfB){
$nbPortLibreTrunk--;
$trunk = $tcfB->getTrunk();
if (!in_array($trunk, $trunks)){
$trunks[] = $trunk;
}
}
if ($tExt){
$nbPortLibreTrunk--;
$tcf = $tExt->getTrunkCableFiber();
$trunk = $tcf->getTrunk();
if (!in_array($trunk, $trunks)){
$trunks[] = $trunk;
}
}
}
$moduleTemp["interfaces"][]= [
'interfaceNumber' => $interface->getInterfaceGeneric()->getInterfaceNumber(),
'trunks' => $trunks,
'nbPortLibreLink'=> $nbPortLibreLink,
'nbPortLibreTrunk'=> $nbPortLibreTrunk
];
}
$eq["slots"][] = ["slotNumber"=>$slot->getSlotNumber(), "module"=>$moduleTemp];
}
else {
$eq["slots"][] = ["slotNumber"=>$slot->getSlotNumber(), "module"=>null];
}
}
// foreach ($equipment->getModules() as $module) {
// $moduleTemp = array("equipmentSpecificName" => $module->getEquipmentSpecificName(),
// "equipmentGenericTitle" => $module->getEquipmentGeneric()->getTitle()
// );
// $moduleTemp["interfaces"] = [];
// $interfaces = $module->getInterfaceSpecific();
// foreach ($interfaces as $interface){
//
//
// $trunks = [];
//
// $ports = $interface->getPort();
//
// //$trunk = new Trunk();
//
// foreach ($ports as $port){
//
// $tcfA = $this->getDoctrine()->getRepository(TrunkCableFiber::class)->findOneByPortA($port);
// $tcfB = $this->getDoctrine()->getRepository(TrunkCableFiber::class)->findOneByPortB($port);
// $tExt = $this->getDoctrine()->getRepository(TrunkExtension::class)->findOneByPortB($port);
//
// if ($tcfA){
// $trunk = $tcfA->getTrunk();
// if (!in_array($trunk, $trunks)){
// $trunks[] = $trunk;
// }
// }
//
// if ($tcfB){
// $trunk = $tcfB->getTrunk();
// if (!in_array($trunk, $trunks)){
// $trunks[] = $trunk;
// }
// }
//
// if ($tExt){
// $tcf = $tExt->getTrunkCableFiber();
// $trunk = $tcf->getTrunk();
// if (!in_array($trunk, $trunks)){
// $trunks[] = $trunk;
// }
// }
//
// }
// $moduleTemp["interfaces"][]= [
// 'interfaceNumber' => $interface->getInterfaceGeneric()->getInterfaceNumber(),
// 'trunks' => $trunks
// ];
// }
// $eq["modules"][] = $moduleTemp;
//
// }
}
else {
$interfaces = $equipment->getInterfaceSpecific();
$eq['interfaces'] = [];
foreach ($interfaces as $interface){
$trunks = [];
$ports = $interface->getPort();
$nbPortLibreLink = count($ports);
$nbPortLibreTrunk = count($ports);
//$trunk = new Trunk();
foreach ($ports as $port){
if($port->getLink()){
$nbPortLibreLink--;
}
$tcfA = $this->getDoctrine()->getRepository(TrunkCableFiber::class)->findOneByPortA($port);
$tcfB = $this->getDoctrine()->getRepository(TrunkCableFiber::class)->findOneByPortB($port);
$tExt = $this->getDoctrine()->getRepository(TrunkExtension::class)->findOneByPortB($port);
if ($tcfA){
$nbPortLibreTrunk--;
$trunk = $tcfA->getTrunk();
if (!in_array($trunk, $trunks)){
$trunks[] = $trunk;
}
}
if ($tcfB){
$nbPortLibreTrunk--;
$trunk = $tcfB->getTrunk();
if (!in_array($trunk, $trunks)){
$trunks[] = $trunk;
}
}
if ($tExt){
$nbPortLibreTrunk--;
$tcf = $tExt->getTrunkCableFiber();
$trunk = $tcf->getTrunk();
if (!in_array($trunk, $trunks)){
$trunks[] = $trunk;
}
}
}
$eq['interfaces'][]=[
'interfaceNumber' => $interface->getInterfaceGeneric()->getInterfaceNumber(),
'trunks' => $trunks,
'nbPortLibreLink'=> $nbPortLibreLink,
'nbPortLibreTrunk'=> $nbPortLibreTrunk
];
/*if (isset($trunk)){
$eq['interfaces'][]=[
'interfaceNumber' => $interface->getInterfaceGeneric()->getInterfaceNumber(),
'trunkID' => $trunk->getTrunkID(),
'trunk_id' => $trunk->getId()
];
} else {
$eq['interfaces'][]=[
'interfaceNumber' => $interface->getInterfaceGeneric()->getInterfaceNumber(),
'trunkID' => '',
'trunk_id' => ''
];
}*/
}
}
$eqList[] = $eq;
}
return $this->render('overview/rack_usage.html.twig', [
'action' => 'list',
'page_title' => $translator->trans('Rack Usage'),
'rack' => $rack,
'equipmentSpecific' => $eqList,
]);
}
/**
* @Route("/rack/overview/list", name="rack_overview_list")
*/
public function overviewRacksListAction(Request $request, TranslatorInterface $translator)
{
$list = $this->getDoctrine()->getRepository(Rack::class)->findAll();
// $em = $this->getDoctrine()->getManager();
// $queryBuilder = $em->createQueryBuilder()
// ->select('eqs')
// ->from('App\Entity\Rack', 'eqs')
// ->orderBy("eqs.title", "ASC");
// $adapter = new DoctrineORMAdapter($queryBuilder);
// $pagerfanta = new Pagerfanta($adapter);
// $pageLength = $request->query->get("pageLength", $this->getParameter("rack.maxPerPage"));
// if ($pageLength == "Tout") {
// $qb = $em->createQueryBuilder()
// ->select('COUNT(eqs.id)')
// ->from('App\Entity\Rack', 'eqs')
// ->orderBy("eqs.title", "ASC");
//
// $count = $qb->getQuery()->getSingleScalarResult();
// $pagerfanta->setMaxPerPage($count); // 10 by default
// }
// else {
// $pagerfanta->setMaxPerPage($pageLength); // 10 by default
// }
// $page = $request->query->get("page", 1);
// $pagerfanta->setCurrentPage($page);
// foreach ($list as $item){
//
// $title = $item->getTitle();
// if ( strtolower(substr($title, 0, 1)) == 'f' ){
// $item->isFiber = 1;
// } else {
// $item->isFiber = 0;
// }
//
// }
return $this->render('overview/rack_list.html.twig', [
'action' => 'insert',
'page_title' => $translator->trans('Rack Overview'),
'list' => $list
// 'my_pager' => $pagerfanta,
// 'pageLength' => $pageLength,
]);
}
/**
* @Route("/rack/overview/item/{id}", name="rack_overview")
*/
public function overviewRacksAction($id, TranslatorInterface $translator)
{
$em = $this->getDoctrine()->getManager();
$rack = $em->getRepository(Rack::class)->find($id);
$equipmentSpecific = $this->getDoctrine()->getRepository(EquipmentSpecific::class)->findBy(["rack"=>$rack, "isModule"=>false]);
//MODIFIED ON 14/02/2020
$racks = $this->getDoctrine()->getRepository(Rack::class)->findBy(["room"=>$rack->getRoom()], ["title"=>"ASC"]);
usort($racks, function ($a, $b) {
if($a && $b){
return strnatcmp($a->getTitle(), $b->getTitle());
}
else return 1;
});
return $this->render('overview/rack_view.html.twig', [
'action' => 'list',
'page_title' => $translator->trans('Rack View'),
'rack' => $rack,
'equipmentSpecific' => $equipmentSpecific,
"racks" => $racks
]);
}
/**
* @return string
*/
private function generateUniqueFileName()
{
// md5() reduces the similarity of the file names generated by
// uniqid(), which is based on timestamps
return md5(uniqid());
}
}