src/Repository/Otpusk/HotelsRCacheRepository.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Otpusk;
  3. use App\Entity\Otpusk\HotelsRCache;
  4. use App\Services\HelperService;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. /**
  8.  * @method HotelsRCache|null find($id, $lockMode = null, $lockVersion = null)
  9.  * @method HotelsRCache|null findOneBy(array $criteria, array $orderBy = null)
  10.  * @method HotelsRCache[]    findAll()
  11.  * @method HotelsRCache[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  12.  */
  13. class HotelsRCacheRepository extends ServiceEntityRepository
  14. {
  15.     private $helperService;
  16.     private $imgUrl 'https://newimg.otpusk.com/3_otpk/500x375/';
  17.     public function __construct(ManagerRegistry $registryHelperService $helperService)
  18.     {
  19.         parent::__construct($registryHotelsRCache::class);
  20.         $this->helperService $helperService;
  21.     }
  22.     /**
  23.      * Main sql string for landing pages
  24.      *
  25.      * @param int|null $countryId
  26.      * @param int|null $cityId
  27.      * @param int|null $stars
  28.      * @param string|null $type
  29.      * @param int|null $budget
  30.      * @param string|null $currency
  31.      * @return string
  32.      */
  33.     public function getMainSqlForToursPage($market$dbNormalizer$localeint $countryId nullint $cityId nullint $stars nullstring $type nullint $budget nullstring $currency null$vipStatus falseint $cityFrom)
  34.     {
  35.         $countryField   $dbNormalizer['country']['name'][$locale];
  36.         $cityField      $dbNormalizer['city']['name'][$locale];
  37.         $priceField     'price'.ucfirst($market['currency']['code']);
  38.         $sql "SELECT h.id, h.name, h.cityId, city.{$cityField} AS cityName, h.countryId, country.{$countryField} AS countryName, h.stars, h.rating_avg AS rating, h.rating_by_site AS ratingBySite, p.offerId, p.operatorId, p.price, p.currency, p.{$priceField} as marketPrice, p.date, p.transport, p.food, p.length, CONCAT(:baseUrl, h.image) as desktop_gallery, h.hrefName, p.fromCityId, cityFrom.{$cityField}Rd as fromCityRd
  39.                 FROM otpusk.hotelsRCache h 
  40.                 INNER JOIN otpusk.priceIndex p ON (h.id = p.rec_id)
  41.                 LEFT JOIN tCountries AS country ON country.rec_id = h.countryId
  42.                 LEFT JOIN tCities AS city ON city.rec_id = h.cityId
  43.                 LEFT JOIN tCities AS cityFrom ON cityFrom.rec_id = p.fromCityId
  44.                 WHERE p.date >= :dateBeg AND p.date <= :dateEnd
  45.                 ";
  46.         if ($cityFrom != $market['mainCities'][0]['id']) $sql .= ' AND p.fromCityId = :cityFrom';
  47.         if(isset($countryId) && $countryId 0){
  48.             $sql .= ' AND h.countryId = :countryId';
  49.         }
  50.         if(isset($cityId)){
  51.             $sql .= ' AND h.cityId = :cityId';
  52.         }
  53.         if(isset($stars) && $stars 0){
  54.             if($stars >= 3){
  55.                 $sql .= ' AND h.stars = :stars';
  56.             } else {
  57.                 $sql .= ' AND h.stars <= :stars';
  58.             }
  59.         }
  60.         if(isset($type)){
  61.             switch ($type){
  62.                 case "friends":
  63.                     $sql .= ' AND h.type_with_friends > 0';
  64.                     break;
  65.                 case "romantic":
  66.                     $sql .= ' AND h.type_romantic_rest > 0';
  67.                     break;
  68.                 case "family":
  69.                     $sql .= ' AND h.type_with_children > 0';
  70.                     break;
  71.                 case "peaceful":
  72.                     $sql .= ' AND h.type_peaceful_rest > 0';
  73.                     break;
  74.                 case "hot":
  75.                     $sql .= ' AND h.rating_avg >= 6';
  76.                     break;
  77.             }
  78.         }
  79.         if ($vipStatus$sql .= ' AND FIND_IN_SET(\'vip\', h.services)';
  80.         return $sql;
  81.     }
  82.     /**
  83.      * Execute sql for landing pages
  84.      *
  85.      * @param string|null $sql
  86.      * @param null $stars
  87.      * @param int|null $countryId
  88.      * @param int|null $cityId
  89.      * @param int $cityFrom
  90.      * @param null $period
  91.      * @param int|null $budget
  92.      * @return mixed
  93.      */
  94.     public function getToursData($marketstring $sql null$stars nullint $countryId nullint $cityId null$cityFrom$period nullint $budget null)
  95.     {
  96.         $params = array(
  97.             'dateBeg'   => $period['dateBeg'],
  98.             'dateEnd'   => $period['dateEnd'],
  99.             'baseUrl'   => $this->imgUrl,
  100.         );
  101.         if ($cityFrom != $market['mainCities'][0]['id']) $params['cityFrom'] = $cityFrom;
  102.         if(isset($countryId) && $countryId 0){
  103.             $params['countryId'] = $countryId;
  104.         }
  105.         if(isset($cityId)){
  106.             $params['cityId'] = $cityId;
  107.         }
  108.         if(isset($stars)){
  109.             $params['stars'] = $stars;
  110.         }
  111.         if(isset($budget)){
  112.             $params['budget'] = $budget;
  113.         }
  114.         $conn $this->getEntityManager()
  115.             ->getConnection();
  116.         $stmt $conn->prepare($sql);
  117.         foreach ($params as $index => $param) {
  118.             $stmt->bindValue($index$param);
  119.         }
  120.         $tours $this->addRatingBySites($stmt->execute()->fetchAll());
  121.         return $tours;
  122.     }
  123.     /**
  124.      * Additional information for best-quality tours
  125.      *
  126.      * @param null $type
  127.      * @param null $stars
  128.      * @param int|null $countryId
  129.      * @param int|null $cityId
  130.      * @param int $cityFrom
  131.      * @param null $period
  132.      * @param int|null $budget
  133.      * @param string|null $currency
  134.      * @return mixed
  135.      */
  136.     public function getBestQualityTours($market$dbNormalizer$locale$type null$stars nullint $countryId nullint $cityId null$cityFrom$period nullint $budget nullstring $currency null$isDeparture false)
  137.     {
  138.         $sql $this->getMainSqlForToursPage($market$dbNormalizer$locale$countryId$cityId$stars$type$budget$currencyfalse$cityFrom);
  139.         if (!$isDeparture) {
  140.             $sql .= " AND " $this->helperService->getWhere($market);
  141.             $sql .= " GROUP BY h.id ORDER BY " $this->helperService->getOrderBy($market'fromCityId') . ", p.best DESC LIMIT 12";
  142.         } else {
  143.             $sql .= " GROUP BY h.id ORDER BY p.best DESC LIMIT 12";
  144.         }
  145.         return $this->getToursData($market$sql$stars$countryId$cityId$cityFrom$period$budget);
  146.     }
  147.     /**
  148.      * Additional information for best-quality tours
  149.      *
  150.      * @param null $type
  151.      * @param null $stars
  152.      * @param int|null $countryId
  153.      * @param int|null $cityId
  154.      * @param int $cityFrom
  155.      * @param null $period
  156.      * @param int|null $budget
  157.      * @param string|null $currency
  158.      * @return mixed
  159.      */
  160.     public function getBestHotelsTours($market$dbNormalizer$locale$type null$stars nullint $countryId nullint $cityId null$cityFrom$period nullint $budget nullstring $currency null$isDeparture false)
  161.     {
  162.         $sql $this->getMainSqlForToursPage($market$dbNormalizer$locale$countryId$cityId$stars$type$budget$currencyfalse$cityFrom);
  163.         $sql .= " AND h.rating_avg >= 6 ";
  164.         if (!$isDeparture) {
  165.             $sql .= " AND " $this->helperService->getWhere($market);
  166.             $sql .= " GROUP BY h.id ORDER BY " $this->helperService->getOrderBy($market'fromCityId') . ", h.rating_avg DESC LIMIT 12";
  167.         } else {
  168.             $sql .= " GROUP BY h.id ORDER BY h.rating_avg DESC LIMIT 12";
  169.         }
  170.         return $this->getToursData($market$sql$stars$countryId$cityId$cityFrom$period$budget);
  171.     }
  172.     /**
  173.      * Additinal information for cheapest and hotest tours
  174.      *
  175.      * @param null $type
  176.      * @param int|null $countryId
  177.      * @param int|null $cityId
  178.      * @param int $cityFrom
  179.      * @param null $period
  180.      * @param null $budget
  181.      * @param null $currency
  182.      * @return mixed
  183.      */
  184.     public function getCheapestTours($market$dbNormalizer$locale$type nullint $countryId nullint $cityId null$cityFrom$period null$budget null$currency null$vipStatus false$isDeparture false)
  185.     {
  186.         $sql $this->getMainSqlForToursPage($market$dbNormalizer$locale$countryId$cityIdnull$type$budget$currency$vipStatus$cityFrom);
  187.         if (!$isDeparture) {
  188.             $sql .= " AND " $this->helperService->getWhere($market);
  189.             $sql .= " GROUP BY h.id ORDER BY " $this->helperService->getOrderBy($market'fromCityId') . ", marketPrice LIMIT 12";
  190.         } else {
  191.             $sql .= " GROUP BY h.id ORDER BY marketPrice LIMIT 12";
  192.         }
  193.         return $this->getToursData($market$sqlnull$countryId$cityId$cityFrom$period$budget);
  194.     }
  195.     /**
  196.      * Additinal information for tours by type
  197.      *
  198.      * @param string|null $type
  199.      * @param int|null $countryId
  200.      * @param int|null $cityId
  201.      * @param int $cityFrom
  202.      * @param null $period
  203.      * @param null $budget
  204.      * @param null $currency
  205.      * @param bool $order
  206.      * @return mixed
  207.      */
  208.     public function getTypeTours($market$dbNormalizer$localestring $type nullint $countryId nullint $cityId null$cityFrom$period null$budget null$currency null$order false$withoutRating false$vipStatus false$isDeparture false)
  209.     {
  210.         $sql $this->getMainSqlForToursPage($market$dbNormalizer$locale$countryId$cityIdnull$type$budget$currency$vipStatus$cityFrom);
  211.         if (!$withoutRating) {
  212.             $sql .= " AND h.rating_avg >= 7 ";
  213.         } elseif (!$vipStatus) {
  214.             $sql .= " AND h.rating_avg < 7 ";
  215.         }
  216.         if (!$isDeparture) {
  217.             $sql .= " AND " $this->helperService->getWhere($market);
  218.         }
  219.         if ($order) {
  220.             if (!$isDeparture) {
  221.                 $sql .= " GROUP BY h.id ORDER BY " $this->helperService->getOrderBy($market'fromCityId') . ", p.best DESC LIMIT 12";
  222.             } else {
  223.                 $sql .= " GROUP BY h.id ORDER BY p.best DESC LIMIT 12";
  224.             }
  225.         } else {
  226.             if (!$isDeparture) {
  227.                 $sql .= " GROUP BY h.id ORDER BY " $this->helperService->getOrderBy($market'fromCityId') . ", marketPrice LIMIT 12";
  228.             } else {
  229.                 $sql .= " GROUP BY h.id ORDER BY marketPrice LIMIT 12";
  230.             }
  231.         }
  232.         return $this->getToursData($market$sqlnull$countryId$cityId$cityFrom$period$budget);
  233.     }
  234.     public function addRatingBySites($tours)
  235.     {
  236.         $sites HotelsRCache::_SITENAMES;
  237.         foreach ($tours as $index => &$tour) {
  238.             $tour['ratingBySite'] = json_decode($tour['ratingBySite'], true);
  239.             if(is_array($tour['ratingBySite'])){
  240.                 foreach ($tour['ratingBySite'] as &$item) {
  241.                     $item['name'] = array_key_exists($item['site_id'], $sites) ? $sites[$item['site_id']] : '';
  242.                 }
  243.                 unset($item);
  244.             }
  245.         }
  246.         unset($tour);
  247.         return $tours;
  248.     }
  249.     /**
  250.      * @param $date
  251.      * @param int|null $countryId
  252.      * @param int|null $cityId
  253.      * @return mixed
  254.      * @throws \Doctrine\ORM\NoResultException
  255.      */
  256.     public function getServicesCount($date$countryId null$cityId null$period null)
  257.     {
  258.         $params = array();
  259.         $conn $this->getEntityManager()
  260.             ->getConnection();
  261.         $sql "SELECT MAX(h.services LIKE '%spa%') as spa,
  262.                     MAX(h.services LIKE '%aquapark%') as aquapark,
  263.                     MAX(h.services LIKE '%tennis_court%') as tennis_court,
  264.                     MAX(h.services LIKE '%golf%') as golf,
  265.                     MAX(h.services LIKE '%volleyball%') as volleyball,
  266.                     MAX(h.services LIKE '%basketball%') as basketball,
  267.                     MAX(h.services LIKE '%football%') as football,
  268.                     MAX(h.services LIKE '%equestrian%') as equestrian,
  269.                     MAX(h.services LIKE '%bikes%') as bikes,
  270.                     MAX(h.services LIKE '%ski_slopes%') as ski_slopes,
  271.                     MAX(h.services LIKE '%fitness%') as fitness,
  272.                     MAX(h.services LIKE '%aerobics%') as aerobics,
  273.                     MAX(h.services LIKE '%yoga%') as yoga,
  274.                     MAX(h.services LIKE '%water_sports%') as water_sports,
  275.                     MAX(h.services LIKE '%diving%') as diving,
  276.                     MAX(h.services LIKE '%surfing%') as surfing,
  277.                     MAX(h.services LIKE '%windsurfing%') as windsurfing,
  278.                     MAX(h.services LIKE '%sauna%') as sauna,
  279.                     MAX(h.services LIKE '%discotheque%') as discotheque
  280.                 FROM otpusk.hotelsRCache AS h
  281.                 INNER JOIN otpusk.priceIndex p ON (h.id = p.rec_id AND p.type='hotel' AND p.transport = h.countryTransport)";
  282.         if(isset($period)){
  283.             $sql .= " WHERE p.date >= :dateBeg AND p.date <= :dateEnd";
  284.             $params['dateBeg'] = $period['dateBeg'];
  285.             $params['dateEnd'] = $period['dateEnd'];
  286.         } else {
  287.             $sql .= " WHERE p.date > :date";
  288.             $params['date'] = $date;
  289.         }
  290.         if(isset($countryId)){
  291.             $sql .= ' AND h.countryId = :countryId';
  292.             $params['countryId'] = $countryId;
  293.         }
  294.         if(isset($cityId)){
  295.             $sql .= ' AND h.cityId = :cityId';
  296.             $params['cityId'] = $cityId;
  297.         }
  298.         $stmt $conn->prepare($sql);
  299.         foreach ($params as $index => $param) {
  300.             $stmt->bindValue($index$param);
  301.         }
  302.         return $stmt->execute()->fetchAll();
  303.     }
  304.     /**
  305.      * @param $date
  306.      * @param int|null $geoId
  307.      * @param string $type
  308.      * @return array
  309.      */
  310.     public function getMonthlyInfo($marketint $geoId null$type)
  311.     {
  312.         $date $this->getDateMonth();
  313.         if(!isset($geoId)) return array();
  314.         $conn $this->getEntityManager()->getConnection();
  315.         $priceField     'price'.ucfirst($market['currency']['code']);
  316.         $sql 'SELECT MIN(pi.'.$priceField.') AS price, pi.date
  317.                 FROM priceIndex AS pi
  318.                 WHERE pi.date > :date AND pi.' $type ' = :geoId
  319.                 GROUP BY MONTH(pi.date)';
  320. //        AND pi.fromCityId IN (:fromCityIds)
  321. //        $fromCityIds = [];
  322. //        foreach ($market['mainCities'] as $mainCity) {
  323. //            if (!is_null($mainCity['id'])) $fromCityIds[] = $mainCity['id'];
  324. //        }
  325.         $stmt $conn->prepare($sql);
  326.         $stmt->bindValue('date'$date);
  327.         $stmt->bindValue('geoId'$geoId);
  328. //        $stmt->bindValue('fromCityIds', implode(',', $fromCityIds));
  329.         return $stmt->execute()->fetchAll();
  330.     }
  331.     public function getDateMonth()
  332.     {
  333.         $date = new \DateTime();
  334.         $day $date->format('d');
  335.         if ($day 15) {
  336.             $newDate $date->modify('+1 day');
  337.         } else {
  338.             $newDate $date->modify('last day of this month')->modify('-14 days');
  339.         }
  340.         $date $newDate->format('Y-m-d');
  341.         return $date;
  342.     }
  343.     public function findHotToursForHP($market$locale$dbNormalizer)
  344.     {
  345.         $nameField      $dbNormalizer['hotTravels']['name'][$locale];
  346.         $countryField   $dbNormalizer['country']['name'][$locale];
  347.         $cityField      $dbNormalizer['city']['name'][$locale];
  348.         $priceField     'fPrice'.ucfirst($market['currency']['code']);
  349.         $sql "SELECT 
  350.                 ht.fType as type,
  351.                 ht.fTourID as tourId,
  352.                 ht.{$nameField} as name,
  353.                 ht.fUrl as url,
  354.                 ht.fDescription as description,
  355.                 ht.fOperatorID AS operatorId,
  356.                 ht.fCurrency AS currency,
  357.                 ht.fPrice AS price,
  358.                 ht.fAcmd AS acmd,
  359.                 ht.fFood AS food,
  360.                 ht.fTransport AS transport,
  361.                 ht.fDate AS date,
  362.                 ht.fDateFrom AS dateFrom,
  363.                 ht.fDateTo AS dateTo,
  364.                 ht.fLength AS length,
  365.                 ht.fLengthFrom AS lengthFrom,
  366.                 ht.fLengthTo AS lengthTo,
  367.                 ht.fSort AS sort,
  368.                 ht.fNoStat AS noStat,
  369.                 ht.fPosition AS position,
  370.                 ht.fOpen AS open,
  371.                 ht.fClose AS close, 
  372.                 IF(ht.fStars='one',1,IF(ht.fStars='two',2,IF(ht.fStars='three' OR ht.fStars='hv1',3,IF(ht.fStars='four',4,IF(ht.fStars='five' OR ht.fStars='hv2',5,0))))) as star,
  373.                 h.name AS hotel,
  374.                 city.{$cityField} AS cityName,
  375.                 h.cityCode AS city_url,
  376.                 country.{$countryField} AS countryName,
  377.                 h.countryHrefName AS country_url,
  378.                 h.stars as stars,
  379.                 h.rating_avg AS rating,
  380.                 h.rating_by_site AS ratingBySite,
  381.                 ht.fImage as image,
  382.                 ht.{$priceField} as price
  383.                 FROM tHotTravels AS ht 
  384.                 LEFT JOIN hotelsRCache AS h ON h.id = ht.fHotelID
  385.                 LEFT JOIN tCountries AS country ON country.rec_id = h.countryId
  386.                 LEFT JOIN tCities AS city ON city.rec_id = h.cityId
  387.                 WHERE ht.fType IN ('tour', 'travel') AND ht.fBaseCountryID IN (".implode(','$market['mainCountries']).") AND ht.fOpen <= NOW() AND ht.fClose > NOW() AND ht.fPrice > 0 AND ht.{$priceField} > 0 AND FIND_IN_SET(ht.fShow, 'main')
  388.                 GROUP BY ht.rec_id
  389.                 ORDER BY IF(ht.fPosition=0, 999, ht.fPosition) ASC, ht.fClose ASC 
  390.                 LIMIT 4
  391.                 ";
  392.         $conn $this->getEntityManager()
  393.             ->getConnection();
  394.         $stmt $conn->prepare($sql);
  395.         $tours $this->addRatingBySites($stmt->execute()->fetchAll());
  396.         return $tours;
  397.     }
  398. }