src/Aqarmap/Bundle/NeighborhoodBundle/Controller/DefaultController.php line 268

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\NeighborhoodBundle\Controller;
  3. use Aqarmap\Bundle\DiscussionBundle\Entity\Discussion;
  4. use Aqarmap\Bundle\DiscussionBundle\Form\DiscussionType;
  5. use Aqarmap\Bundle\DiscussionBundle\Service\DiscussionManager;
  6. use Aqarmap\Bundle\ListingBundle\Entity\Listing;
  7. use Aqarmap\Bundle\ListingBundle\Entity\Location;
  8. use Aqarmap\Bundle\ListingBundle\Entity\Section;
  9. use Aqarmap\Bundle\ListingBundle\Form\ContactSellerFormType;
  10. use Aqarmap\Bundle\ListingBundle\Form\QuickLeadType;
  11. use Aqarmap\Bundle\ListingBundle\Service\LocationManager;
  12. use Aqarmap\Bundle\MainBundle\Form\ConfirmFormType;
  13. use Aqarmap\Bundle\NeighborhoodBundle\Form\CompoundSearchType;
  14. use Aqarmap\Bundle\NeighborhoodBundle\Service\NeighborhoodManager;
  15. use Aqarmap\Bundle\UserBundle\Constant\UserInterestStatus;
  16. use Aqarmap\Bundle\UserBundle\Entity\User;
  17. use Aqarmap\Bundle\UserBundle\Entity\UserInterest;
  18. use Aqarmap\Bundle\UserBundle\Form\QuickRegistrationFormType;
  19. use Predis\ClientInterface;
  20. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\Form\FormInterface;
  23. use Symfony\Component\HttpFoundation\RedirectResponse;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. use Symfony\Contracts\Translation\TranslatorInterface;
  29. /**
  30.  * @Route("/neighborhood")
  31.  */
  32. class DefaultController extends AbstractController
  33. {
  34.     public const NEIGHBORHOOD_LOCATION_LEVEL 0;
  35.     private $discussionManager;
  36.     /**
  37.      * @var NeighborhoodManager
  38.      */
  39.     private $neighborhoodManager;
  40.     /**
  41.      * @var TranslatorInterface
  42.      */
  43.     private $translator;
  44.     /**
  45.      * @var SessionInterface
  46.      */
  47.     private $session;
  48.     public function __construct(
  49.         DiscussionManager $discussionManager,
  50.         CompoundSearchType $compoundSearchType,
  51.         NeighborhoodManager $neighborhoodManager,
  52.         ClientInterface $redisClient,
  53.         TranslatorInterface $translator,
  54.         SessionInterface $session
  55.     ) {
  56.         $this->discussionManager $discussionManager;
  57.         $this->neighborhoodManager $neighborhoodManager;
  58.         $this->translator $translator;
  59.         $this->session $session;
  60.     }
  61.     /**
  62.      * Neighborhood Compounds View.
  63.      *
  64.      * @Route("/compounds", name="neighborhood_compounds")
  65.      *
  66.      * @return array
  67.      */
  68.     public function compounds(): RedirectResponse
  69.     {
  70.         // Redirect For now as 301 to neighborhoods page
  71.         return $this->redirect($this->generateUrl('neighborhood_all_locations'), Response::HTTP_MOVED_PERMANENTLY);
  72.     }
  73.     /**
  74.      * Add Neighbourhood Subscribers.
  75.      *
  76.      * @Route("/{location}/subscription", name="neighbourhood_add_subscriber")
  77.      */
  78.     public function addNeighbourhoodSubscriber(Location $locationRequest $request): RedirectResponse
  79.     {
  80.         $this->neighborhoodManager->addSubscriber($this->getUser(), $location);
  81.         $this->session->getFlashBag()->add(
  82.             'success',
  83.             $this->translator->trans('neighborhoods.discussion.subscriber_success_message')
  84.         );
  85.         return $this->redirect($request->headers->get('referer') ?: $this->generateUrl('homepage'));
  86.     }
  87.     /**
  88.      * Remove  Neighbourhood Subscribers.
  89.      *
  90.      * @Route("/{location}/subscription/remove", name="neighbourhood_remove_subscriber")
  91.      */
  92.     public function unsetNeighbourhoodSubscriber(Location $locationRequest $request): RedirectResponse
  93.     {
  94.         $this->neighborhoodManager->removeLocationSubscriber($this->getUser(), $location);
  95.         $this->session->getFlashBag()->add(
  96.             'success',
  97.             $this->translator->trans('neighborhoods.discussion.subscriber_unset_message')
  98.         );
  99.         return $this->redirect($request->headers->get('referer') ?: $this->generateUrl('homepage'));
  100.     }
  101.     /**
  102.      * @Route("/{location}/{user}/subscription/confirm/remove", name="neighborhood_mail_remove_subscriber")
  103.      *
  104.      * @return RedirectResponse
  105.      *
  106.      * @throws BadRequestHttpException
  107.      */
  108.     public function unSubscribeLocationConfirm(User $userLocation $locationRequest $request)
  109.     {
  110.         /* Valid User Md5 valid */
  111.         if (md5($user->getId()).$this->container->getParameter('secret') !== $request->query->get('token')) {
  112.             throw new BadRequestHttpException('Permission Denied !');
  113.         }
  114.         // Create confirmation form (button)
  115.         $form $this->createForm(ConfirmFormType::class, null, [
  116.             'method' => 'POST',
  117.             'action' => $this->generateUrl(
  118.                 'neighborhood_mail_remove_subscriber',
  119.                 ['location' => $location->getId(),
  120.                     'user' => $user->getId(),
  121.                     'token' => $request->query->get('token'), ]
  122.             ),
  123.         ]);
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             if ($form->get('confirm')->isClicked()) {
  127.                 $this->neighborhoodManager->removeLocationSubscriber($user$location);
  128.                 $this->session->getFlashBag()->add(
  129.                     'success',
  130.                     $this->translator->trans('neighborhoods.discussion.subscriber_unset_message')
  131.                 );
  132.                 return $this->redirect($this->generateUrl('homepage'));
  133.             }
  134.         }
  135.         return $this->render('@AqarmapNeighborhood/Default/Email/unSubscribeLocationConfirm.html.twig', [
  136.             'form' => $form->createView(),
  137.             'location' => $location,
  138.         ]);
  139.     }
  140.     /**
  141.      * Neighborhood Main Action.
  142.      *
  143.      * @ParamConverter("location", options={"mapping": {"location_slug": "slug"}})
  144.      *
  145.      * @Route("/{location_slug}/", requirements={"location_slug": ".+?[^/]"}, name="neighborhood_main_page", methods={"GET"})
  146.      */
  147.     public function index(?Location $locationRequest $requestLocationManager $locationManager)
  148.     {
  149.         /** @var $em \Doctrine\ORM\EntityManager */
  150.         $em $this->getDoctrine()->getManager();
  151.         if (!$location || $location->getDisabled()) {
  152.             return $this->redirectToParentLocation($request->attributes->get('location_slug'));
  153.         }
  154.         $subLinksSections $em->getRepository(Section::class)->getSearchableSection();
  155.         if (== $location->getLevel()) {
  156.             return $this->render(
  157.                 '@AqarmapNeighborhood/Default/location_neighborhood_level.html.twig',
  158.                 [
  159.                     'location' => $location,
  160.                     'subLinksSections' => $subLinksSections,
  161.                 ]
  162.             );
  163.         }
  164.         $historyData $this->neighborhoodManager->generateStatisticsHistoryData($location);
  165.         foreach ($historyData['historyData'] as &$value) {
  166.             $value['data']['average_price'] = array_values($value['data']['average_price']);
  167.             $value['data']['demand_heat'] = array_values($value['data']['demand_heat']);
  168.             $value['data']['growth'] = array_values($value['data']['growth']);
  169.         }
  170.         $discussion = new Discussion();
  171.         $discussionForm $form $this->createForm(DiscussionType::class, $discussion, [
  172.             'action' => $this->generateUrl('neighborhood_discussion_create', ['location' => $location->getId()]),
  173.             'method' => 'POST',
  174.             'flatLocations' => serialize($locationManager->getLocationsArrayWithParent($request->getLocale())['searchable']) ?? serialize([]),
  175.         ]);
  176.         $statistics $em->getRepository('AqarmapNeighborhoodBundle:LocationStatistics')->findby(['location' => $location]);
  177.         $subNeighbourhoods = [];
  178.         foreach ($statistics as $statisticsObject) {
  179.             $subNeighbourhoods[$statisticsObject->getPropertyType()->getId()]['propertyType'] = $statisticsObject->getPropertyType();
  180.             $subNeighbourhoods[$statisticsObject->getPropertyType()->getId()]['locations'] = $em->getRepository(Location::class)
  181.                 ->getNeighbourhoodSubLocations(
  182.                     $location,
  183.                     $statisticsObject->getPropertyType()
  184.                 );
  185.         }
  186.         $contact_seller_form null;
  187.         if ($location->getListing()) {
  188.             $contact_seller_form $this->contactSellerForm($location
  189.                 ->getListing())->createView();
  190.         }
  191.         $subscribedUser null;
  192.         if ($this->getUser()) {
  193.             $subscribedUser $em->getRepository(UserInterest::class)->findOneBy([
  194.                 'user' => $this->getUser(),
  195.                 'location' => $location,
  196.                 'status' => UserInterestStatus::LIVE,
  197.             ]);
  198.         }
  199.         $locationStatistics $this->neighborhoodManager->getStatistics($location);
  200.         return $this->render('@AqarmapNeighborhood/Default/index.html.twig', [
  201.             'location' => $location,
  202.             'locationChildren' => $em->getRepository(Location::class)->getLocationChildren($location),
  203.             'location_statistics' => $locationStatistics,
  204.             'isStatisticsContainsAveragePrice' => $this->neighborhoodManager->isStatisticsContainsAveragePrice($locationStatistics),
  205.             'sections' => $em->getRepository(Section::class)->findBy(['searchable' => true]),
  206.             'updatedDate' => $em->getRepository('AqarmapNeighborhoodBundle:LocationStatistics')->getLastRowDate(),
  207.             'discussionForm' => $discussionForm->createView(),
  208.             'responsiveDiscussionForm' => $discussionForm->createView(),
  209.             'discussions' => $this->discussionManager->getTrendingWithLocation($location3),
  210.             'quick_registration_form' => $this->quickRegistrationForm()->createView(),
  211.             'contact_seller_form' => $contact_seller_form,
  212.             'subscribedUser' => $subscribedUser,
  213.             'defaultSection' => $em->getRepository(Section::class)->findOneBy(['main' => true]),
  214.             'historyData' => $historyData['historyData'],
  215.             'nearestNeighbourhoods' => $em->getRepository(Location::class)
  216.                 ->getNearestNeighbourhoods(
  217.                     $location
  218.                 ),
  219.             'subNeighbourhoods' => $subNeighbourhoods,
  220.             'subLinksSections' => $subLinksSections,
  221.             'form' => $this->createQuickLeadForm()->createView(),
  222.         ]);
  223.     }
  224.     private function redirectToParentLocation(?string $slug null): RedirectResponse
  225.     {
  226.         $parentLocation $this->getParentSlug($slug);
  227.         if (!empty($parentLocation)) {
  228.             return $this->redirect($this->generateUrl('neighborhood_main_page', [
  229.                 'location_slug' => $parentLocation,
  230.             ]), Response::HTTP_MOVED_PERMANENTLY);
  231.         }
  232.         return $this->redirect($this->generateUrl('neighborhood_all_locations'), Response::HTTP_MOVED_PERMANENTLY);
  233.     }
  234.     /**
  235.      * Neighborhood All Locations View.
  236.      *
  237.      * @Route("/", name="neighborhood_all_locations", methods={"GET"})
  238.      */
  239.     public function locations(): Response
  240.     {
  241.         /** @var $em \Doctrine\ORM\EntityManager */
  242.         $em $this->getDoctrine()->getManager();
  243.         return $this->render('@AqarmapNeighborhood/Default/locations.html.twig', [
  244.             'subLinksSections' => $em->getRepository(Section::class)->getSearchableSection(),
  245.             'locations' => $em
  246.                 ->getRepository(Location::class)
  247.                 ->getNeighbourhoodLocations(['level' => self::NEIGHBORHOOD_LOCATION_LEVEL]),
  248.         ]);
  249.     }
  250.     // ---------------------------------------------------------------------
  251.     /**
  252.      * Creates quick registration form.
  253.      *
  254.      * @return \Symfony\Component\Form\Form The form
  255.      */
  256.     public function quickRegistrationForm()
  257.     {
  258.         return $this->createForm(QuickRegistrationFormType::class, null, [
  259.             'method' => 'POST',
  260.             'action' => $this->generateUrl('aqarmap_user_quick_registration'),
  261.         ]);
  262.     }
  263.     // ------------------------------------------------------------------------------
  264.     /**
  265.      * Creates contact seller form.
  266.      *
  267.      * @param Listing $listing The listing entity
  268.      *
  269.      * @return \Symfony\Component\Form\Form The form
  270.      */
  271.     public function contactSellerForm(Listing $listing)
  272.     {
  273.         return $this->createForm(ContactSellerFormType::class, null, [
  274.             'method' => 'POST',
  275.             'action' => $this->generateUrl('aqarmap_listing_contact_seller', ['id' => $listing->getId()]),
  276.         ]);
  277.     }
  278.     /**
  279.      * Create Quick Lead Form.
  280.      */
  281.     private function createQuickLeadForm(): FormInterface
  282.     {
  283.         return $this->createForm(
  284.             QuickLeadType::class,
  285.             null,
  286.             [
  287.                 'action' => $this->generateUrl('add_quick_lead'),
  288.                 'method' => 'POST',
  289.             ]
  290.         );
  291.     }
  292.     /**
  293.      * @return string|null
  294.      */
  295.     private function getParentSlug(?string $slug null)
  296.     {
  297.         $parentLocation explode('/'$slug);
  298.         array_pop($parentLocation);
  299.         return implode('/'$parentLocation);
  300.     }
  301. }