src/Repository/Otpusk/CityRepository.php line 320

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Otpusk;
  3. use App\Entity\Otpusk\City;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\ORM\EntityRepository;
  6. use Doctrine\ORM\Query\Expr\Join;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use GeoIp2\Database\Reader;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use function Doctrine\ORM\QueryBuilder;
  11. class CityRepository extends ServiceEntityRepository
  12. {
  13.     private $imgUrl 'https://www.otpusk.com/';
  14.     private $dbNormalizer;
  15.     public function __construct(ManagerRegistry $registryParameterBagInterface $config)
  16.     {
  17.         parent::__construct($registryCity::class);
  18.         $this->dbNormalizer $config->get('db_normalizer');
  19.     }
  20.     public function checkDepartureCity($city$locale 'ru')
  21.     {
  22.         $name $this->dbNormalizer['city']['name'][$locale];
  23.         return $this->createQueryBuilder('c')
  24.             ->select('c.id AS id, c.'.$name.'Rd AS nameRd, c.fNameTr AS nameTr')
  25.             ->andWhere('c.fNameTr = :city')
  26.             ->setParameter('city'$city)
  27.             ->getQuery()
  28.             ->getOneOrNullResult()
  29.         ;
  30.     }
  31.     public function getDepartureCitiesForCountrySearchPage($citiesIds$locale 'ru'$dbNormalizer)
  32.     {
  33.         $name $dbNormalizer['city']['name'][$locale];
  34.         $qb $this->createQueryBuilder('c');
  35.         $qb $qb
  36.             ->select('c.id, c.'.$name.'Rd AS name, c.fNameTr AS nameTr')
  37.             ->where('c.id IN ('.$citiesIds.')')
  38.             ->getQuery()
  39.         ;
  40.         return $qb->execute();
  41.     }
  42.     public function getTourCitiesForPages($countryId$cityId null$locale 'ru'$dbNormalizer)
  43.     {
  44.         $name $dbNormalizer['city']['name'][$locale];
  45.         $qb $this->createQueryBuilder('c')
  46.             ->select('c.id AS id, c.'.$name.' AS name, c.fNameTr AS nameTr, c.'.$name.'Rd AS nameInGenitiveCase')
  47.             ->leftJoin('c.priceIndex''pi');
  48.         if(isset($cityId)){
  49.             $qb->innerJoin('App\Entity\Otpusk\City''t'\Doctrine\ORM\Query\Expr\Join::WITH'c.fCountryID = t.fCountryID')
  50.                 ->where('t.id = :city')
  51.                 ->setParameter('city'$cityId);
  52.         } else {
  53.             $qb->where('c.fCountryID = :country')
  54.                 ->setParameter('country'$countryId);
  55.         }
  56.         $qb->andWhere('pi.id IS NOT NULL')
  57.             ->andWhere('pi.date > :now')
  58.             ->setParameter('now', new \DateTime())
  59.             ->groupBy('c.id')
  60.             ->orderBy('c.fName''ASC')
  61.             ->having('COUNT(pi.id) > 0');
  62.         $result $qb->getQuery()->getArrayResult();
  63.         if (count($result) == 0) {
  64.             $qb $this->createQueryBuilder('c')
  65.                 ->select('c.id AS id, c.'.$name.' AS name, c.fNameTr AS nameTr, c.'.$name.'Rd AS nameInGenitiveCase');
  66.             if(isset($cityId)){
  67.                 $qb->innerJoin('App\Entity\Otpusk\City''t'\Doctrine\ORM\Query\Expr\Join::WITH'c.fCountryID = t.fCountryID')
  68.                     ->where('t.id = :city')
  69.                     ->setParameter('city'$cityId);
  70.             } else {
  71.                 $qb->where('c.fCountryID = :country')
  72.                     ->setParameter('country'$countryId);
  73.             }
  74.             $qb->groupBy('c.id')
  75.                 ->orderBy('c.fName''ASC');
  76.             $result $qb->getQuery()->getArrayResult();
  77.         }
  78.         return $result;
  79.     }
  80.     public function getPopCities($countryId$cityId null$locale 'ru'$isDeparture false$cityFrom$dbNormalizer)
  81.     {
  82.         $name $dbNormalizer['city']['name'][$locale];
  83.         $params = array(
  84.             'baseUrl' => $this->imgUrl,
  85.         );
  86.         $conn $this->getEntityManager()
  87.             ->getConnection();
  88.         $sql 'SELECT c.rec_id AS id, c.'.$name.' AS name, c.fNameTr AS nameTr, c.'.$name.'Rd AS nameInGenitiveCase, CONCAT(:baseUrl, "media/city/", LPAD(FLOOR(m.media_id / 100000) + 1, 4, 0), "/", LPAD(FLOOR((m.media_id - (FLOOR(m.media_id / 100000) * 100000))/ 1000) + 1, 2, 0), "/thumb_", m.media_id, "_city_desktop_gallery_2.jpeg") as desktop_gallery, CONCAT(:baseUrl, "media/city/", LPAD(FLOOR(m.media_id / 100000) + 1, 4, 0), "/", LPAD(FLOOR((m.media_id - (FLOOR(m.media_id / 100000) * 100000))/ 1000) + 1, 2, 0), "/thumb_", m.media_id, "_city_mobile_card.jpeg") as mobile_card
  89.                 FROM otpusk.tCities AS c
  90.                 LEFT JOIN otp_tours.media__gallery_media m ON c.gallery_id = m.gallery_id';
  91.         if(isset($cityId)){
  92.             $sql .= ' LEFT JOIN otpusk.tCities AS t ON c.fCountryID = t.fCountryID';
  93.         }
  94.         if ($isDeparture) {
  95.             $sql .= ' '.($isDeparture?'INNER':'LEFT'). ' JOIN otpusk.priceIndex pi ON c.rec_id = pi.cityId';
  96.         }
  97.         if(isset($cityId)){
  98.             $sql .= ' WHERE t.rec_id = :city';
  99.             $params['city'] = $cityId;
  100.         } else {
  101.             $sql .= " WHERE c.fCountryID = :country";
  102.             $params['country'] = $countryId;
  103.         }
  104.         $sql .= " AND c.cityWeight > 0  AND m.position = 1";
  105.         if ($isDeparture) {
  106.             $sql .= " AND c.fPriority = 2 AND pi.fromCityId = :fromCityId";
  107.             $params['fromCityId'] = $cityFrom;
  108.         }
  109.         $sql .= " GROUP BY c.rec_id";
  110.         if (FALSE && $isDeparture) {
  111.             $sql .= " HAVING COUNT(pi.rec_id) > 0";
  112.         }
  113.         $sql .= " ORDER BY c.cityWeight DESC, c.fName LIMIT 6";
  114.         $stmt $conn->prepare($sql);
  115.         foreach ($params as $index => $param) {
  116.             $stmt->bindValue($index$param);
  117.         }
  118.         return $stmt->execute()->fetchAll();
  119.     }
  120.     public function getByCategory($market$category$locale 'ru')
  121.     {
  122.         $sql $this->createQueryBuilder('c')
  123.             ->join('c.cityCategoryValues''cv')
  124.             ->leftJoin('cv.category''cat')
  125.             ->where('cat.name = :category')
  126.             ->andWhere('cv.market = :marketId')
  127.             ->setParameter('category'$category)
  128.             ->setParameter('marketId'$market['id'])
  129.             ->orderBy('cv.position')
  130.         ;
  131.         return $sql->getQuery()->getResult();
  132.     }
  133.     public function getSimilarById($cityId)
  134.     {
  135.         $params = array(
  136.             'cityId'   => $cityId,
  137.         );
  138.         $conn $this->getEntityManager()->getConnection();
  139.         $sql 'SELECT city_similar_in_id as id FROM CitiesSimilar cs WHERE cs.city_similar_id = :cityId ORDER BY cs.priority DESC';
  140.         $stmt $conn->prepare($sql);
  141.         foreach ($params as $index => $param) {
  142.             $stmt->bindValue($index$param);
  143.         }
  144.         return $stmt->execute()->fetchAll();
  145.     }
  146.     public function getGuidCitiesByIds($market$locale 'ru'$cityId null$dbNormalizer)
  147.     {
  148.         $date = new \DateTime();
  149.         $date->add(new \DateInterval('P7D'));
  150.         $nameCity       $dbNormalizer['city']['name'][$locale];
  151.         $cities = (is_null($cityId)) ? $this->getByCategory($market'cities-popular-cities') : $this->getSimilarById($cityId);
  152.         $cityIds = [];
  153.         foreach ($cities as $city) {
  154.             $cityIds[] = (is_null($cityId)) ? $city->getId() : $city['id'];
  155.         }
  156.         foreach ($cityIds as $k => $v) {
  157.             if (empty($v)) unset($cityIds[$k]);
  158.         }
  159.         if ($cityIds) {
  160.             $params = array(
  161.                 'baseUrl'   => $this->imgUrl,
  162.                 'date'      => $date->format('n'),
  163.                 'lang'      => $dbNormalizer['tRefs']['language'][$locale],
  164.             );
  165.             $conn $this->getEntityManager()
  166.                 ->getConnection();
  167.             $month strtolower($date->format('M'));
  168.             if ($month == 'dec'$month 'decm';
  169.             $sql 'SELECT c.rec_id AS id, c.' $nameCity ' AS name, c.fNameTr AS nameTr, co.fNameTr AS countryNameTr, c.' $nameCity 'Rd AS nameInGenitiveCase, cw.weight AS seasonWeight, t. ' $month ' AS weather, tw. ' $month ' AS water, r.fBrief AS text,
  170. CONCAT(:baseUrl, "media/city/", LPAD(FLOOR(m.media_id / 100000) + 1, 4, 0), "/", LPAD(FLOOR((m.media_id - (FLOOR(m.media_id / 100000) * 100000))/ 1000) + 1, 2, 0), "/thumb_", m.media_id, "_city_desktop_gallery_2.jpeg") as desktop_gallery, CONCAT(:baseUrl, "media/city/", LPAD(FLOOR(m.media_id / 100000) + 1, 4, 0), "/", LPAD(FLOOR((m.media_id - (FLOOR(m.media_id / 100000) * 100000))/ 1000) + 1, 2, 0), "/thumb_", m.media_id, "_city_mobile_card.jpeg") as mobile_card
  171.                 FROM otpusk.tCities AS c
  172.                 LEFT JOIN otp_tours.media__gallery_media m ON c.gallery_id = m.gallery_id
  173.                 LEFT JOIN otpusk.citiesWeight cw ON (c.rec_id = cw.cityId AND cw.month = :date)
  174.                 LEFT JOIN otpusk.tCountries co ON (c.fCountryID = co.rec_id)
  175.                 LEFT JOIN otp_misc.temperatures t ON (c.rec_id = t.cityId AND t.temperatureType = "weather")
  176.                 LEFT JOIN otp_misc.temperatures tw ON (c.rec_id = tw.cityId AND tw.temperatureType = "water")
  177.                 LEFT JOIN otpusk.tRefs r ON (c.rec_id = r.fBindID AND r.fRefTypeID = 94 AND r.fLang = :lang)
  178.                 WHERE c.rec_id IN (' implode(','$cityIds) . ')
  179.                 GROUP BY c.rec_id 
  180.                 ORDER BY FIELD(c.rec_id, ' implode(','$cityIds) . ')';
  181.             $stmt $conn->prepare($sql);
  182.             foreach ($params as $index => $param) {
  183.                 $stmt->bindValue($index$param);
  184.             }
  185.             return $stmt->execute()->fetchAll();
  186.         }
  187.         return [];
  188.     }
  189.     public function getCitiesByIds($dbNormalizer$locale 'ru'$cityIdsObject null)
  190.     {
  191.         $name $dbNormalizer['city']['name'][$locale];
  192.         $cityIds = [];
  193.         foreach ($cityIdsObject as $city) {
  194.             $cityIds[] = $city->getCity()->getId();
  195.         }
  196.         if ($cityIds) {
  197.             $params = array(
  198.                 'baseUrl' => $this->imgUrl,
  199.             );
  200.             $conn $this->getEntityManager()
  201.                 ->getConnection();
  202.             $sql 'SELECT c.rec_id AS id, c.' $name ' AS name,
  203. CONCAT(:baseUrl, "media/city/", LPAD(FLOOR(m.media_id / 100000) + 1, 4, 0), "/", LPAD(FLOOR((m.media_id - (FLOOR(m.media_id / 100000) * 100000))/ 1000) + 1, 2, 0), "/thumb_", m.media_id, "_city_desktop_gallery_2.jpeg") as desktop_gallery, CONCAT(:baseUrl, "media/city/", LPAD(FLOOR(m.media_id / 100000) + 1, 4, 0), "/", LPAD(FLOOR((m.media_id - (FLOOR(m.media_id / 100000) * 100000))/ 1000) + 1, 2, 0), "/thumb_", m.media_id, "_city_mobile_card.jpeg") as mobile_card
  204.                 FROM otpusk.tCities AS c
  205.                 LEFT JOIN otp_tours.media__gallery_media m ON c.gallery_id = m.gallery_id
  206.                 WHERE c.rec_id IN (' implode(','$cityIds) . ')
  207.                 GROUP BY c.rec_id 
  208.                 ORDER BY FIELD(c.rec_id, ' implode(','$cityIds) . ')';
  209.             $stmt $conn->prepare($sql);
  210.             foreach ($params as $index => $param) {
  211.                 $stmt->bindValue($index$param);
  212.             }
  213.             $cities $stmt->execute()->fetchAll();
  214.             $results = [];
  215.             foreach ($cities as $city) {
  216.                 $results[$city['id']] = $city;
  217.             }
  218.             return $results;
  219.         }
  220.         return [];
  221.     }
  222.     public function getByCityAliases($dbNormalizer$localestring $cityAlias)
  223.     {
  224.         $name $dbNormalizer['city']['name'][$locale];
  225.         $city =  $this->createQueryBuilder('city')
  226.             ->select('city.'.$name.'Rd as name')
  227.             ->where('city.fNameTr = :cityAlias')
  228.             ->orWhere('city.fNameTr = :cityAliasChanged')
  229.             ->orWhere('city.fNameTr = :cityAliasChangedA')
  230.             ->setParameter('cityAlias'$cityAlias)
  231.             ->setParameter('cityAliasChanged'$cityAlias)
  232.             ->setParameter('cityAliasChangedA'str_replace('_'' '$cityAlias))
  233.             ->getQuery()
  234.             ->getOneOrNullResult();
  235.         return $city;
  236.     }
  237.     public function getCityInfo($cityId$locale 'ru'$dbNormalizer)
  238.     {
  239.         $nameCountry    $dbNormalizer['country']['name'][$locale];
  240.         $nameCase       $dbNormalizer['country']['case'][$locale];
  241.         $nameCity       $dbNormalizer['city']['name'][$locale];
  242.         $nameCurrency   $dbNormalizer['country']['currency'][$locale];
  243.         $nameLanguage   $dbNormalizer['country']['language'][$locale];
  244.         $sql $this->createQueryBuilder('c')
  245.             ->select('c.id AS id, c.'.$nameCity.' AS name, c.'.$nameCity.'Rd AS nameRd, c.'.$nameCity.'Vn AS nameVn, c.'.$nameCity.'Pr AS namePr, c.'.$nameCity.'Dt AS nameDt, c.fNameTr AS nameTr, country.fNameTr AS countryTr, country.'.$nameCountry.' AS countryName, country.'.$nameCase.'Vn AS countryNameVn, country.foodCost as foodCostCountry, country.coffeeCost as coffeeCostCountry, c.foodCost, c.coffeeCost, country.'.$nameCurrency.' as currencyName, country.'.$nameLanguage.' as language, c.timeLocal, c.timeZone, country.timeLocal as timeLocalCounty, country.timeZone as timeZoneCounty, c.flightTime, country.flightTime as flightTimeCountry')
  246.             ->leftJoin('c.country''country'Join::WITH'country.id = c.fCountryID')
  247.             ->where('c.id = :city')
  248.             ->setParameter('city'$cityId)
  249.             ->getQuery();
  250.         return $sql->getOneOrNullResult();
  251.     }
  252.     public function getCityFaq($cityId$type$locale 'ru'$dbNormalizer)
  253.     {
  254.         $sql $this->createQueryBuilder('c')
  255.             ->select('f.fTitle AS title, f.fText AS text, f.fLang AS lang')
  256.             ->leftJoin('c.faqText''f')
  257.             ->leftJoin('f.fRefTypeID''fb')
  258.             ->where('c.id = :city')
  259.             ->andWhere('f.fLang = :lang')
  260.             ->andWhere('fb.fName = :type')
  261.             ->andWhere('fb.fBindType = :bind')
  262.             ->setParameters(array('city' => $cityId'type' => $type'bind' => 'city''lang' => $dbNormalizer['tRefs']['language'][$locale]))
  263.             ->orderBy('f.fSort''DESC')
  264.         ;
  265.         return $sql->getQuery()->getArrayResult();
  266.     }
  267.     public function getCityFaqNew($cityId$locale 'ru'$season null$month null$isDeparture false$cityFrom$dbNormalizer)
  268.     {
  269.         $code 'citytours';
  270.         if ($isDeparture$code .= 'departurecity';
  271.         if (!is_null($season)) $code .= $season;
  272.         if (!is_null($month)) $code .= $month;
  273.         $sql $this->createQueryBuilder('c')
  274.             ->select('f.fTitle AS title, f.fText AS text')
  275.             ->leftJoin('c.faqText''f')
  276.             ->leftJoin('f.fRefTypeID''fb')
  277.             ->where('c.id = :city')
  278.             ->andWhere('f.fLang = :lang')
  279.             ->andWhere('fb.fCode = :code');
  280.         if ($isDeparture) {
  281.             $sql
  282.                 ->andWhere('f.fFromCityId = :cityFrom')
  283.                 ->setParameter('cityFrom'$cityFrom);
  284.         }
  285.         $sql
  286.             ->setParameter('city'$cityId)
  287.             ->setParameter('code'$code)
  288.             ->setParameter('lang'$dbNormalizer['tRefs']['language'][$locale])
  289.             ->orderBy('f.fSort''DESC');
  290.         $result $sql->getQuery()->getOneOrNullResult();
  291.         if (!is_null($result) && is_resource($result['text'])) {
  292.             rewind($result['text']);
  293.             $result['text'] = stream_get_contents($result['text']);
  294.         }
  295.         return $result;
  296.     }
  297.     public function getWeather($cityId$lang 'rus')
  298.     {
  299.         $params = array(
  300.             'city'   => $cityId,
  301.             'lang'   => $lang,
  302.         );
  303.         $conn $this->getEntityManager()
  304.             ->getConnection();
  305.         $sql 'SELECT t.temperatureType AS lang, t.jan AS january, t.feb AS february, t.mar AS march, t.apr AS april, t.may AS may, t.jun AS june, t.jul AS july, t.aug AS august, t.sep AS september, t.oct AS october, t.nov AS november, t.decm AS december
  306.                 FROM otp_misc.temperatures t
  307.                 WHERE t.cityId = :city AND t.temperatureType = :lang';
  308.         $stmt $conn->prepare($sql);
  309.         foreach ($params as $index => $param) {
  310.             $stmt->bindValue($index$param);
  311.         }
  312.         return $stmt->execute()->fetchAll();
  313.     }
  314.     /**
  315.      * @param int|null $cityId
  316.      * @param string $type
  317.      * @return array
  318.      */
  319.     public function getTemperaturesByCity($cityId)
  320.     {
  321.         $params = array(
  322.             'cityId'   => $cityId,
  323.         );
  324.         $conn $this->getEntityManager()
  325.             ->getConnection();
  326.         $sql 'SELECT t.temperatureType as type, t.jan as Jan, t.feb as Feb, t.mar as Mar, t.apr as Apr, t.may as May, t.jun as Jun, t.jul as Jul, t.aug as Aug, t.sep as Sep, t.oct as Oct, t.nov as Nov, t.decm as "Dec"
  327.                 FROM otp_misc.temperatures t
  328.                 WHERE t.cityId = :cityId';
  329.         $stmt $conn->prepare($sql);
  330.         foreach ($params as $index => $param) {
  331.             $stmt->bindValue($index$param);
  332.         }
  333.         return $stmt->execute()->fetchAll();
  334.     }
  335.     public function getCityNames($market)
  336.     {
  337.         $conn $this->getEntityManager()
  338.             ->getConnection();
  339.         $topIds = [];
  340.         foreach ($market['mainCities'] as $mainCity) {
  341.             if (!is_null($mainCity['id'])) $topIds[] = $mainCity['id'];
  342.         }
  343.         $sql 'select d.fDeptCity as id, c.fName as name, c.fNameRd as nameRd 
  344.             from otp_tours.tOpDeptHotel d 
  345.             left join otpusk.tCities c ON d.fDeptCity=c.rec_id 
  346.             where c.fCountryID in ('.implode(','$market['mainCountries']).')
  347.             group by d.fDeptCity';
  348.         $stmt $conn->prepare($sql);
  349.         $data $stmt->execute()->fetchAll();
  350.         $results = [];
  351.         foreach ($data as $one) {
  352.             $results[$one['id']] = array(
  353.                 'name' => !empty($one['name']) ? $one['name'] : 'любой город',
  354.                 'nameRd' => !empty($one['nameRd']) ? $one['nameRd'] : 'любого города',
  355.                 'priority' => (in_array($one['id'], $topIds) ? true false)
  356.             );
  357.         }
  358.         return $results;
  359.     }
  360.     public function getCityByIp($ip null)
  361.     {
  362.         if (empty($ip)){
  363.             $ip $_SERVER['REMOTE_ADDR'];
  364.         }
  365.         $project_dir dirname($_SERVER['DOCUMENT_ROOT']);
  366.         if (!is_dir($project_dir)) $project_dir '/app';
  367.         $reader = new Reader($project_dir.'/public/upload/GeoLite2/GeoLite2-City/GeoLite2-City.mmdb');
  368.         $record $reader->city($ip);
  369.         $subdivisions = [];
  370.         foreach ($record->subdivisions AS $subdivision$subdivisions[] = [$subdivision->names['ru'],$subdivision->names['en'],$subdivision->isoCode];
  371.         $this->res = [
  372.             $record->location->latitude$record->location->longitude,
  373.             [$record->continent->names['ru'],$record->continent->names['en'], $record->continent->code],
  374.             [$record->country->names['ru'], $record->country->names['en'], $record->country->isoCode],
  375.             $subdivisions,
  376.             [$record->city->names['ru'], $record->city->names['en']]
  377.         ];
  378.         return $this->getIdByCityName(
  379.             $record->city->names['ru'],
  380.             $record->city->names['en'],
  381.             $record->location->latitude,
  382.             $record->location->longitude
  383.         );
  384.     }
  385.     public function getIdByCityName($name$nameEng$lat$long)
  386.     {
  387.         $sql $this->createQueryBuilder('c')
  388.             ->select('c.id AS cityId')
  389.             ->where('c.fNameEng = :name')
  390.             ->orWhere('c.fName = :nameEng')
  391.             ->orWhere('c.fSouthWest >= :latStart AND c.fSouthWest <= :latEnd AND c.fNorthEast >= :longStart AND c.fNorthEast <= :longEnd')
  392.             ->setParameter('name'$name)
  393.             ->setParameter('nameEng'$nameEng)
  394.             ->setParameter('latStart'$lat-0.1)
  395.             ->setParameter('latEnd'$lat+0.1)
  396.             ->setParameter('longStart'$long-0.1)
  397.             ->setParameter('longEnd'$long+0.1)
  398.         ;
  399.         $result $sql->getQuery()->getResult();
  400.         return isset($result[0]['cityId'])?$result[0]['cityId']:null;
  401.     }
  402.     public function getRes(){
  403.         return $this->res;
  404.     }
  405. }