CaptivePortalController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace FTTHBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use FTTHBundle\Entity\ONU;
  8. use FTTHBundle\Entity\PreSale;
  9. use WebserviceBundle\Form\Type\RemoteClientType;
  10. use MapBundle\Form\Type\RemoteMapType;
  11. use FOS\RestBundle\Controller\Annotations\RouteResource;
  12. /**
  13. * CaptivePortal controller.
  14. */
  15. class CaptivePortalController extends Controller
  16. {
  17. public function portalAction(Request $request)
  18. {
  19. $client = $request->query->get('client');
  20. $psn = $request->query->get('psn');
  21. $preSale = $request->query->get('preSale');
  22. $preSales = $errors = array();
  23. $result = false;
  24. if(!is_null($client) && !is_null($psn)) {
  25. $em = $this->get("doctrine.orm.entity_manager");
  26. $preSales = $em->getRepository(PreSale::class)->findBy(array('clientId' => $client));
  27. $onu = $em->getRepository(ONU::class)->findOneBy(array('ponSerialNumber' => $psn));
  28. if(empty($preSales)) $errors[] = "We don't register a pending installation to this client.";
  29. if(is_null($onu)) $errors[] = "ONU doesn't exist.";
  30. if(!is_null($preSale) && !is_null($onu)) {
  31. $sale = $em->getRepository(PreSale::class)->findOneById($preSale);
  32. if($sale) {
  33. $this->updateOnu($sale->getClientId(), $sale->getProfile(), $onu, $sale->getAddress());
  34. $onu = $em->getRepository(ONU::class)->findOneBy(array('ponSerialNumber' => $psn));
  35. if($onu->getClientId() == $sale->getClientId() && $onu->getProfile() == $sale->getProfile()) {
  36. $result = true;
  37. $em->remove($sale);
  38. $em->flush();
  39. }
  40. }
  41. }
  42. }
  43. return $this->render('FTTHBundle:Portal:index.html.twig', array(
  44. 'result' => $result,
  45. 'errors' => $errors,
  46. 'preSales' => $preSales,
  47. 'client' => $client,
  48. 'psn' => $psn
  49. ));
  50. }
  51. private function updateOnu($clientId, $profile, $onu, $address) {
  52. $onu->setClientId($clientId);
  53. $onu->setProfile($profile);
  54. $em = $this->get("doctrine.orm.entity_manager");
  55. $em->persist($onu);
  56. $em->flush();
  57. }
  58. }