src/Controller/NewSearchController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AccommodationType;
  4. use App\Entity\Property;
  5. use App\Entity\PropertyTags;
  6. use App\Entity\PropertyTagsApplied;
  7. use App\Entity\Regions;
  8. use App\Entity\Searches;
  9. use App\Entity\SearchResults;
  10. use App\Service\GlobalFunctions;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class NewSearchController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/search", name="search")
  19.      */
  20.     public function index()
  21.     {
  22.         $em $this->getDoctrine()->getManager();
  23.         $SearchOutput = [];
  24.         // Output all rooms with staff picks at the top - shuffled
  25.         $StaffPickTag $this->getDoctrine()->getRepository(PropertyTags::class)->find(161);
  26.         $StaffPicksApplied $this->getDoctrine()->getRepository(PropertyTagsApplied::class)->findBy(['Tag' => $StaffPickTag]);
  27.         foreach ($StaffPicksApplied as $thisApplied) {
  28.             $AccommType $this->getDoctrine()->getRepository(AccommodationType::class)->find($thisApplied->getRoom()->getId());
  29.             if (null == $AccommType->getProperty()) {
  30.                 $GetProperty $this->getDoctrine()->getRepository(Property::class)->findOneBy(['ListingCode' => $AccommType->getPropertyCode()]);
  31.                 $AccommType->setProperty($GetProperty);
  32.                 $em->persist($AccommType);
  33.                 $em->flush();
  34.             }
  35.             if (== $AccommType->getEnabled() && 'Listed' == $AccommType->getProperty()->getListingStatus()) {
  36.                 $SearchOutput[$thisApplied->getRoom()->getId()] = $AccommType;
  37.             }
  38.         }
  39.         $AllRooms $this->getDoctrine()->getRepository(AccommodationType::class)->findAll();
  40.         foreach ($AllRooms as $thisRoom) {
  41.             if (null == $thisRoom->getProperty()) {
  42.                 $GetProperty $this->getDoctrine()->getRepository(Property::class)->findOneBy(['ListingCode' => $thisRoom->getPropertyCode()]);
  43.                 $thisRoom->setProperty($GetProperty);
  44.                 $em $this->getDoctrine()->getManager();
  45.                 $em->persist($thisRoom);
  46.                 $em->flush();
  47.             }
  48.             if (null !== $thisRoom->getProperty()) {
  49.                 if (== $thisRoom->getEnabled() && 'Listed' == $thisRoom->getProperty()->getListingStatus()) {
  50.                     if (!array_key_exists($thisRoom->getId(), $SearchOutput)) {
  51.                         if ('' == $thisRoom->getSearchHeadline() or 'Display Name of Listing' == $thisRoom->getSearchHeadline()) {
  52.                         } else {
  53.                             $SearchOutput[$thisRoom->getId()] = $thisRoom;
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.         shuffle($SearchOutput);
  60.         $GetSearch null;
  61.         $Regions $this->getDoctrine()->getRepository(Regions::class)->findBy([], ['SortOrder' => 'ASC']);
  62.         $Tags $this->getDoctrine()->getRepository(PropertyTags::class)->findBy(['TagType' => 'experience']);
  63.         $AccommTypeTags $this->getDoctrine()->getRepository(PropertyTags::class)->findBy(['TagType' => 'accomm_type']);
  64.         $CurrentRecentlyViewed $this->get('session')->get('recently_viewed');
  65.         $RecentString str_replace('['''$CurrentRecentlyViewed);
  66.         $ExpRecent explode(']'$RecentString);
  67.         array_pop($ExpRecent);
  68.         $RecentlyViewed = [];
  69.         $i 0;
  70.         foreach ($ExpRecent as $ThisRecent) {
  71.             if ($i 4) {
  72.                 $GetRecent $this->getDoctrine()->getRepository(AccommodationType::class)->findOneBy([
  73.                     'RoomCode' => $ThisRecent,
  74.                 ]);
  75.                 $RecentlyViewed[] = $GetRecent;
  76.             }
  77.             ++$i;
  78.         }
  79.         return $this->render('new_search/index.html.twig', [
  80.             'rooms' => $SearchOutput,
  81.             'search' => $GetSearch,
  82.             'regions' => $Regions,
  83.             'tags' => $Tags,
  84.             'accomm_type_tags' => $AccommTypeTags,
  85.             'recentlyviewed' => $RecentlyViewed,
  86.             'search_header' => '',
  87.         ]);
  88.     }
  89.     /**
  90.      * @Route("/search/{search_key}", name="search_key")
  91.      */
  92.     public function searchkey($search_key)
  93.     {
  94.         $em $this->getDoctrine()->getManager();
  95.         $SearchOutput = [];
  96.         $GetSearch $this->getDoctrine()->getRepository(Searches::class)->findOneBy(['SearchKey' => $search_key]);
  97.         $SearchResults $this->getDoctrine()->getRepository(SearchResults::class)->findBy(['Search' => $GetSearch], ['SearchRank' => 'DESC']);
  98.         foreach ($SearchResults as $thisResult) {
  99.             if ('' == $thisResult->getAccommType()->getSearchHeadline() or 'Display Name of Listing' == $thisResult->getAccommType()->getSearchHeadline()) {
  100.             } else {
  101.                 $SearchOutput[] = $thisResult->getAccommType();
  102.             }
  103.         }
  104.         $Regions $this->getDoctrine()->getRepository(Regions::class)->findBy([], ['SortOrder' => 'ASC']);
  105.         $Tags $this->getDoctrine()->getRepository(PropertyTags::class)->findBy(['TagType' => 'experience']);
  106.         $AccommTypeTags $this->getDoctrine()->getRepository(PropertyTags::class)->findBy(['TagType' => 'accomm_type']);
  107.         $CurrentRecentlyViewed $this->get('session')->get('recently_viewed');
  108.         $RecentString str_replace('['''$CurrentRecentlyViewed);
  109.         $ExpRecent explode(']'$RecentString);
  110.         array_pop($ExpRecent);
  111.         $RecentlyViewed = [];
  112.         $i 0;
  113.         foreach ($ExpRecent as $ThisRecent) {
  114.             if ($i 4) {
  115.                 $GetRecent $this->getDoctrine()->getRepository(AccommodationType::class)->findOneBy([
  116.                     'RoomCode' => $ThisRecent,
  117.                 ]);
  118.                 $RecentlyViewed[] = $GetRecent;
  119.             }
  120.             ++$i;
  121.         }
  122.         // Search Headers
  123.         $SearchHeader '';
  124.         $SearchFilters str_replace('['''$GetSearch->getSelectedFilters());
  125.         $CountFilters substr_count($SearchFilters']');
  126.         if (== $CountFilters) {
  127.             $SingleFilter str_replace(']'''$SearchFilters);
  128.             $GetTag $this->getDoctrine()->getRepository(PropertyTags::class)->find($SingleFilter);
  129.             $TagParam $GetTag->getParams();
  130.             if ('[SH]' == substr($TagParam04)) {
  131.                 $SearchHeader str_replace('[SH]'''$TagParam);
  132.             }
  133.         }
  134.         return $this->render('new_search/index.html.twig', [
  135.             'rooms' => $SearchOutput,
  136.             'search' => $GetSearch,
  137.             'regions' => $Regions,
  138.             'tags' => $Tags,
  139.             'accomm_type_tags' => $AccommTypeTags,
  140.             'recentlyviewed' => $RecentlyViewed,
  141.             'search_header' => $SearchHeader,
  142.         ]);
  143.     }
  144.     public function wordSearch($needle$haystack)
  145.     {
  146.         $needle str_replace('-'' '$needle);
  147.         $haystack str_replace('-'' '$haystack);
  148.         /** @var string $needle */
  149.         $needle str_replace("'"''$needle);
  150.         /** @var string $haystack */
  151.         $haystack str_replace("'"''$haystack);
  152.         $needleArray explode(' 'strtolower($needle));
  153.         $haystackArray explode(' 'strtolower($haystack));
  154.         /*// remove any null words
  155.         if (($key = array_search("and", $needleArray)) !== false) {
  156.             unset($needleArray[$key]);
  157.         }
  158.         if (($key = array_search("the", $needleArray)) !== false) {
  159.             unset($needleArray[$key]);
  160.         }*/
  161.         $word_hits 0;
  162.         // search words
  163.         foreach ($needleArray as $needleWord) {
  164.             if (in_array($needleWord$haystackArray)) {
  165.                 ++$word_hits;
  166.             }
  167.         }
  168.         return $word_hits;
  169.     }
  170.     /**
  171.      * @Route("/searchinput", name="search_input")
  172.      */
  173.     public function input(Request $requestGlobalFunctions $GlobalFn)
  174.     {
  175.         $em $this->getDoctrine()->getManager();
  176.         $regions $request->request->get('regions');
  177.         $check_in $request->request->get('check_in');
  178.         $check_out $request->request->get('check_out');
  179.         $guests $request->request->get('guests');
  180.         $price_range_min $request->request->get('price_range_min');
  181.         $price_range_max $request->request->get('price_range_max');
  182.         $selected_filters $request->request->get('selected_filters');
  183.         $query $request->request->get('query');
  184.         if ('' !== $check_in) {
  185.             $check_in_exp explode('/'$check_in);
  186.             $check_in $check_in_exp[2].'-'.$check_in_exp[1].'-'.$check_in_exp[0];
  187.             $check_out_exp explode('/'$check_out);
  188.             $check_out $check_out_exp[2].'-'.$check_out_exp[1].'-'.$check_out_exp[0];
  189.         }
  190.         $Key substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 032);
  191.         $Search = new Searches();
  192.         if ('anon.' !== $this->get('security.token_storage')->getToken()->getUser()) {
  193.             $Search->setUser($this->get('security.token_storage')->getToken()->getUser());
  194.         }
  195.         $Search->setSearchKey($Key);
  196.         $Search->setCreated(new \DateTime());
  197.         $Search->setRegions($regions);
  198.         if ('' !== $check_in) {
  199.             $Search->setCheckIn(new \DateTime($check_in));
  200.             $Search->setCheckOut(new \DateTime($check_out));
  201.         }
  202.         if ($guests 0) {
  203.             $Search->setGuests($guests);
  204.         }
  205.         $Search->setPriceRangeMin($price_range_min);
  206.         $Search->setPriceRangeMax($price_range_max);
  207.         $Search->setSelectedFilters($selected_filters);
  208.         $Search->setQuery($query);
  209.         $em->persist($Search);
  210.         $em->flush();
  211.         // Populate Search
  212.         $AllAccomms $this->getDoctrine()->getRepository(AccommodationType::class)->findBy(['Enabled' => 1]);
  213.         $SearchOutput = [];
  214.         // Filter regions
  215.         foreach ($AllAccomms as $thisAccomm) {
  216.             if (null == $thisAccomm->getProperty()) {
  217.                 $GetProperty $this->getDoctrine()->getRepository(Property::class)->findOneBy(['ListingCode' => $thisAccomm->getPropertyCode()]);
  218.                 $thisAccomm->setProperty($GetProperty);
  219.                 $em $this->getDoctrine()->getManager();
  220.                 $em->persist($thisAccomm);
  221.                 $em->flush();
  222.             }
  223.             if ('' == $regions) {
  224.                 if (null !== $thisAccomm->getProperty()) {
  225.                     if ('Listed' == $thisAccomm->getProperty()->getListingStatus() && == $thisAccomm->getEnabled()) {
  226.                         $SearchOutput[$thisAccomm->getId()] = $thisAccomm;
  227.                     }
  228.                 }
  229.             } else {
  230.                 $regions str_replace('['''$regions);
  231.                 $expregions explode(']'$regions);
  232.                 if (null == $thisAccomm->getRegion()) {
  233.                     if (null !== $thisAccomm->getProperty()) {
  234.                         $PhysAddr3 $thisAccomm->getProperty()->getPhysAddr3();
  235.                         $GetRegion $this->getDoctrine()->getRepository(Regions::class)->findOneBy(['Name' => $PhysAddr3]);
  236.                         if (null !== $GetRegion) {
  237.                             $thisAccomm->setRegion($GetRegion);
  238.                         }
  239.                         $em->persist($thisAccomm);
  240.                         $em->flush();
  241.                     }
  242.                 }
  243.                 if (null !== $thisAccomm->getRegion()) {
  244.                     foreach ($expregions as $thisRegion) {
  245.                         if ($thisAccomm->getRegion()->getId() == $thisRegion) {
  246.                             if ('Listed' == $thisAccomm->getProperty()->getListingStatus() && == $thisAccomm->getEnabled()) {
  247.                                 $SearchOutput[$thisAccomm->getId()] = $thisAccomm;
  248.                             }
  249.                         }
  250.                     }
  251.                 }
  252.                 if (null !== $thisAccomm->getAdditionalRegions() && !isset($SearchOutput[$thisAccomm->getId()])) {
  253.                     foreach ($thisAccomm->getAdditionalRegions() as $thisRegion) {
  254.                         foreach ($expregions as $thisSearchRegion) {
  255.                             if ($thisRegion->getId() == $thisSearchRegion) {
  256.                                 if ('Listed' == $thisAccomm->getProperty()->getListingStatus() && == $thisAccomm->getEnabled()) {
  257.                                     $SearchOutput[$thisAccomm->getId()] = $thisAccomm;
  258.                                 }
  259.                             }
  260.                         }
  261.                     }
  262.                 }
  263.             }
  264.         }
  265.         // Filter check in and out
  266.         if (null !== $Search->getCheckIn()) {
  267.             foreach ($SearchOutput as $AccommId => $thisAccomm) {
  268.                 $Available $GlobalFn->date_range_availability($AccommId$Search->getCheckIn()->format('d/m/Y'), $Search->getCheckOut()->format('d/m/Y'));
  269.                 if ('available' !== $Available) {
  270.                     unset($SearchOutput[$AccommId]);
  271.                 }
  272.             }
  273.         }
  274.         // Filter out guest numbers
  275.         if ($Search->getGuests() > 0) {
  276.             foreach ($SearchOutput as $AccommId => $thisAccomm) {
  277.                 $TotalGuests $thisAccomm->getMaxGuests() + $thisAccomm->getMaxAdditionalGuests();
  278.                 if ($TotalGuests $Search->getGuests()) {
  279.                     unset($SearchOutput[$AccommId]);
  280.                 }
  281.             }
  282.         }
  283.         // Filter out price range
  284.         foreach ($SearchOutput as $AccommId => $thisAccomm) {
  285.             if ($thisAccomm->getNormalPrice() < $Search->getPriceRangeMin() or $thisAccomm->getNormalPrice() > $Search->getPriceRangeMax()) {
  286.                 unset($SearchOutput[$AccommId]);
  287.             }
  288.         }
  289.         // Filter out filters
  290.         if ('' !== $selected_filters) {
  291.             $selected_filters str_replace('['''$selected_filters);
  292.             $expselected_filters explode(']'$selected_filters);
  293.             array_pop($expselected_filters);
  294.             foreach ($expselected_filters as $thisFilter) {
  295.                 $GetTag $this->getDoctrine()->getRepository(PropertyTags::class)->find($thisFilter);
  296.                 $GetAppliedFilters $this->getDoctrine()->getRepository(PropertyTagsApplied::class)->findBy(['Tag' => $GetTag]);
  297.                 foreach ($SearchOutput as $AccommId => $thisAccomm) {
  298.                     $Matches 0;
  299.                     foreach ($GetAppliedFilters as $thisApplied) {
  300.                         if ($thisApplied->getRoom()->getId() == $AccommId) {
  301.                             $Matches 1;
  302.                         }
  303.                     }
  304.                     if (== $Matches) {
  305.                         unset($SearchOutput[$AccommId]);
  306.                     }
  307.                 }
  308.             }
  309.         }
  310.         // Filter search terms
  311.         if ('' !== $Search->getQuery()) {
  312.             foreach ($SearchOutput as $AccommId => $thisAccomm) {
  313.                 // Check if description matches number of words
  314.                 $DescriptionHits $this->wordSearch($Search->getQuery(), $thisAccomm->getRoomDescription());
  315.                 // Check if description matches number of words
  316.                 $PropertyDescriptionHits $this->wordSearch($Search->getQuery(), $thisAccomm->getProperty()->getPropertySummary());
  317.                 // Check if property name matches as above
  318.                 $PropertyNameHits $this->wordSearch($Search->getQuery(), $thisAccomm->getProperty()->getPropertyTitle()) * 5;
  319.                 // Check if healine name contains any words, ranking higher the more matched words
  320.                 $RoomNamePartial $this->wordSearch($Search->getQuery(), $thisAccomm->getRoomName()) * 20;
  321.                 $HeadlineNamePartial $this->wordSearch($Search->getQuery(), $thisAccomm->getSearchHeadline()) * 50;
  322.                 // Check if physaddr2  contains any words, ranking higher the more matched words
  323.                 $PhysAddr2Partial $this->wordSearch($Search->getQuery(), $thisAccomm->getProperty()->getPhysAddr2()) * 20;
  324.                 // Check if physaddr2  contains any words, ranking higher the more matched words
  325.                 $PhysAddr3Partial $this->wordSearch($Search->getQuery(), $thisAccomm->getProperty()->getPhysAddr3()) * 20;
  326.                 // Check if search headline or room name exact match
  327.                 if (strtolower($Search->getQuery()) == strtolower($thisAccomm->getRoomName()) or strtolower($Search->getQuery()) == strtolower($thisAccomm->getSearchHeadline())) {
  328.                     $Rank 999999999;
  329.                 } else {
  330.                     $Rank $PropertyDescriptionHits $DescriptionHits $PropertyNameHits $RoomNamePartial $HeadlineNamePartial $PhysAddr2Partial $PhysAddr3Partial;
  331.                 }
  332.                 if ($Rank 0) {
  333.                     $Result = new SearchResults();
  334.                     $Result->setSearch($Search);
  335.                     $Result->setAccommType($thisAccomm);
  336.                     $Result->setRank($Rank);
  337.                     $em->persist($Result);
  338.                 }
  339.             }
  340.         } else {
  341.             // Add search to db if not searched
  342.             $Rank 1;
  343.             foreach ($SearchOutput as $AccommId => $thisAccomm) {
  344.                 $Result = new SearchResults();
  345.                 $Result->setSearch($Search);
  346.                 $Result->setAccommType($thisAccomm);
  347.                 $Result->setRank($Rank);
  348.                 $em->persist($Result);
  349.                 ++$Rank;
  350.             }
  351.         }
  352.         $em->flush();
  353.         return new Response($Key);
  354.         // Filter filters
  355.     }
  356. }