src/AppBundle/Controller/UnitController.php line 218

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use AppBundle\Entity\Attribute;
  4. use AppBundle\Entity\Page;
  5. use AppBundle\Entity\ProjectPortal;
  6. use AppBundle\Entity\UnitPortal;
  7. use AppBundle\Form\Type\UnitsFilterType;
  8. use AppBundle\Util\ChromeItemsHelperTrait;
  9. use Psr\Log\LoggerInterface;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Translation\Translator;
  16. /**
  17.  * Units controller.
  18.  *
  19.  * @Route("units")
  20.  */
  21. class UnitController extends Controller
  22. {
  23.     use ChromeItemsHelperTrait;
  24.     /**
  25.      * @Route("/", name="units_list")
  26.      * @param Request $request
  27.      * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
  28.      */
  29.     public function listAction(Request $requestTranslator $translator)
  30.     {
  31.         $page $request->get('page'1);
  32.         $limit $request->get('limit'30);
  33.         $view_type $request->get('view''table');
  34.         $locale $request->getLocale();
  35.         $paginator $this->get('knp_paginator');
  36.         $em $this->getDoctrine()->getManager();
  37.         $units_repo $em->getRepository(UnitPortal::class);
  38.         $attributes $em->getRepository(Attribute::class)->getIndexedByKey();
  39.         $form $this->createForm(UnitsFilterType::class);
  40.         $form->handleRequest($request);
  41.         $filterData $form->getData() ?? [];
  42.         $view_type $filterData['view'] ?? 'table';
  43.         $sort $filterData['sort'] ?? null;
  44.         $direction $filterData['direction'] ?? null;
  45.         // get filtered/sorted attributes
  46.         $units $units_repo->getPriceList($filterData$page$limit);
  47.         // handle AJAX request for pagination (load more button)
  48.         if ($request->isXmlHttpRequest()) {
  49.             if ($view_type == 'table') {
  50.                 $template 'AppBundle:Unit:rows.html.twig';
  51.             } else {
  52.                 $template 'AppBundle:Unit:grid.html.twig';
  53.             }
  54.             $view $this->renderView($template, [
  55.                 'projectsPortalData' => $this->getPortalData(),
  56.                 'units' => $units,
  57.             ]);
  58.             return $this->json([
  59.                 'view' => $view,
  60.                 'hide_button' => empty($units) || count($units) < $limit,
  61.             ]);
  62.         }
  63.         $page $em->getRepository(Page::class)
  64.             ->findOneBy(['type' => Page::PRICE_LIST'published' => true]);
  65.         //TODO: static approach. Possible solution: create page type for unit detail and use it along with pricelist type
  66.         //TODO: or you can create admin module for managing static breadcrumbs according to route
  67.         $crumbsPath = [];
  68.         $breadHomePage $em->getRepository(Page::class)->getHomePage();
  69.         //check if homepage is defined
  70.         if ($breadHomePage) {
  71.             $crumbsPath["/"] = (string)$breadHomePage->getTitle();
  72.         } else {
  73.             $crumbsPath["/"] = "Homepage";
  74.         }
  75.         $crumbsPath["/units"] = (string)$page->getTitle();
  76.         # TODO: remove attribute_values table and replace it with attribute_enum_values
  77.         // pagination is used only for sorting applying, so it is no manage to display any data
  78.         $pagination $paginator->paginate([]);
  79.         $ctx = [
  80.             'pagination' => $pagination,
  81.             'params' => ['units_filter[sort]' => $sort],
  82.             'crumbsPath' => $crumbsPath,
  83.             'units' => $units,
  84.             'projectsPortalData' => $this->getPortalData(),
  85.             'page' => $page,
  86.             'view' => $view_type,
  87.             'attributes' => $attributes,
  88.             'hide_button' => empty($units) || count($units) < $limit,
  89.             'customForm' => (new UnitsFilterType($this->get('service_container')))->renderCustomFilterTemplate($filterData),
  90.             'form' => $form->createView(),
  91.         ];
  92.         $this->prepareItemsInController($request$page$em$translator$ctx);
  93.         // override banner if we are looking at one project only
  94.         if (!empty($filterData[UnitsFilterType::PROJECT])) {
  95.             $projectPortal $em->getRepository(ProjectPortal::class)
  96.                 ->findOneBy(['rpEntity' => $filterData[UnitsFilterType::PROJECT]]);
  97.             if ($projectPortal->getBanner()) {
  98.                 $ctx['bannerSlides'] = $projectPortal->getBanner()->getPreparedStructure();
  99.             }
  100.         }
  101.         return $this->render('AppBundle:Unit:list.html.twig'$ctx);
  102.     }
  103.     /**
  104.      * @Route("/getContactForm", name="get_contact_form")
  105.      * @Method("GET")
  106.      * @return \Symfony\Component\HttpFoundation\Response
  107.      */
  108.     public function getContactFormAction()
  109.     {
  110.         return $this->render('AppBundle:Form:contact_form.html.twig');
  111.     }
  112.     /**
  113.      * @Route("/getWhistleblowingForm", name="get_whistleblowing_form")
  114.      * @Method("GET")
  115.      * @return \Symfony\Component\HttpFoundation\Response
  116.      */
  117.     public function getWhistleblowingFormAction()
  118.     {
  119.         return $this->render('AppBundle:Form:whistleblowing_form.html.twig');
  120.     }
  121.     /**
  122.      * @Route("/{id}", name="units_details")
  123.      * @Method("GET")
  124.      * @return \Symfony\Component\HttpFoundation\Response
  125.      */
  126.     public function showAction(Request $request$idTranslator $translatorLoggerInterface $logger)
  127.     {
  128.         if ($request->isXmlHttpRequest()) {
  129.             return $this->json([
  130.                 'redirect_url' => $request->getRequestUri()
  131.             ]);
  132.         }
  133.         $unit $this->findUnitByIdOrInternalId($id);
  134.         if (!$unit) {
  135.             return new RedirectResponse("/units");
  136.         }
  137.         $em $this->getDoctrine()->getManager();
  138.         $page $em->getRepository(Page::class)
  139.             ->findOneBy(['type' => Page::PRICE_LIST'published' => true]);
  140.         $projectID $unit->getRpEntity()->getProject()->getId();
  141.         $projectPortal $em->getRepository(ProjectPortal::class)
  142.             ->findOneBy(['rpEntity' => $projectID]);
  143.         $projectPage $em->getRepository(Page::class)
  144.             ->findOneBy(['type' => Page::PROJECT_PAGE'published' => true'projectPortal' => $projectPortal]);
  145.         $crumbsPath = [];
  146.         $breadHomePage $em->getRepository(Page::class)->getHomePage();
  147.         // check if homepage is defined
  148.         if ($breadHomePage) {
  149.             $crumbsPath["/"] = (string)$breadHomePage->getTitle();
  150.         } else {
  151.             $crumbsPath["/"] = "Homepage";
  152.         }
  153.         $crumbsPath["/units"] = (string)$page->getTitle();
  154.         $crumbsPath["/units/" $id] = $unit->getRpEntity()->getName();
  155.         //$unitContentPage = null;
  156.         $unitContentPage $projectPage->getUnitContent();
  157.         $unitContentPage $unitContentPage !== null $unitContentPage->getContentBlocks() : "";
  158.         $ctx = [
  159.             'unit' => $unit,
  160.             'page' => $page,
  161.             'unitContent' => $unitContentPage,
  162.             'crumbsPath' => $crumbsPath,
  163.             'project_page' => $projectPage,
  164.             'projectsPortalData' => $this->getPortalData(),
  165.         ];
  166.         $this->prepareItemsInController($request$page$em$translator$ctx);
  167.         if ($projectPortal->getBanner()) {
  168.             $ctx['bannerSlides'] = $projectPortal->getBanner()->getPreparedStructure();
  169.         }
  170.         $internalId $unit->getRpEntity()->getName();
  171.         if (strpos($internalId'.1') !== false) {
  172.             // ground floor unit, replace the . with _
  173.             $ctx['hashtag'] = str_replace(".""_"$internalId);
  174.         } else {
  175.             // other unit, replace:
  176.             //  C1.[digit] -> C1_
  177.             //  C2.[digit] -> C2_
  178.             //  D1.[digit] -> D_
  179.             //  D2.[digit] -> D_
  180.             $ctx['hashtag'] =
  181.                 preg_replace("/D2\.[0-9]/""D_",
  182.                     preg_replace("/D1\.[0-9]/""D_",
  183.                         preg_replace("/C2\.[0-9]/""C2_",
  184.                             preg_replace("/C1\.[0-9]/""C1_"$internalId))));
  185.         }
  186.         return $this->render('AppBundle:Unit:detail.html.twig'$ctx);
  187.     }
  188.     private function findUnitByIdOrInternalId($id)
  189.     {
  190.         $em $this->getDoctrine()->getManager();
  191.         $zeroedId $id;
  192.         if (strpos($id"SM2-") !== false) {
  193.             $zeroedId str_replace("SM2-""SM2-0"$id);
  194.         }
  195.         if (ctype_digit($id)) {
  196.             $sql "SELECT up FROM AppBundle\Entity\UnitPortal up JOIN up.rpEntity u WHERE up.id = :id OR JSON_GET_TEXT(u.attributes, 'flat_internal_id') = :id OR JSON_GET_TEXT(u.attributes, 'flat_internal_id') = :zeroedid ORDER BY up.id DESC";
  197.         } else {
  198.             $sql "SELECT up FROM AppBundle\Entity\UnitPortal up JOIN up.rpEntity u WHERE JSON_GET_TEXT(u.attributes, 'flat_internal_id') = :id OR JSON_GET_TEXT(u.attributes, 'flat_internal_id') = :zeroedid ORDER BY up.id DESC";
  199.         }
  200.         $query $em->createQuery($sql)
  201.             ->setMaxResults(1)
  202.             ->setParameters(["id" => $id"zeroedid" => $zeroedId]);
  203.         return $query->getOneOrNullResult();
  204.     }
  205.     /**
  206.      * Save unit coordinates based on floor plan
  207.      *
  208.      * @Route("/{id}/coordinates", name="units_save_coordinates")
  209.      * @Method("POST")
  210.      * @param Request $request
  211.      * @param UnitPortal $unit
  212.      * @return \Symfony\Component\HttpFoundation\Response
  213.      */
  214.     public function saveCoordinates(Request $requestUnitPortal $unit)
  215.     {
  216.         $coordinates $request->get('coordinates');
  217.         $unit->setCoordinates($coordinates);
  218.         $em $this->getDoctrine()->getManager();
  219.         $em->persist($unit);
  220.         $em->flush();
  221.         return $this->json([
  222.             'status' => 'success'
  223.         ]);
  224.     }
  225.     public function getPortalData()
  226.     {
  227.         $em $this->getDoctrine()->getManager();
  228.         $projectPortal_repo $em->getRepository(ProjectPortal::class);
  229.         $projectsPortal $projectPortal_repo->getProjectsDisplayName();
  230.         $projectsPortalData = [];
  231.         foreach ($projectsPortal as $key => $project) {
  232.             $projectsPortalData[$project->getRpEntity()->getId()] =
  233.                 [
  234.                     'displayName' => $project->getDisplayName(),
  235.                     'project' => $project
  236.                 ];
  237.         }
  238.         return $projectsPortalData;
  239.     }
  240. }