src/Repository/Otpusk/CountryRepository.php line 203

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Otpusk;
  3. use App\Entity\Otpusk\Country;
  4. use App\Services\HelperService;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use GeoIp2\Database\Reader;
  8. class CountryRepository extends ServiceEntityRepository
  9. {
  10.     private $helperService;
  11.     private $imageSource 'https://www.otpusk.com/foto/2/800x600/';
  12.     public function __construct(ManagerRegistry $registryHelperService $helperService)
  13.     {
  14.         parent::__construct($registryCountry::class);
  15.         $this->helperService $helperService;
  16.     }
  17.     public function getCountryInfo($countryId$locale 'ru'$dbNormalizer)
  18.     {
  19.         $name           $dbNormalizer['country']['name'][$locale];
  20.         $nameCase       $dbNormalizer['country']['case'][$locale];
  21.         $nameCurrency   $dbNormalizer['country']['currency'][$locale];
  22.         $nameLanguage   $dbNormalizer['country']['language'][$locale];
  23.         $sql $this->createQueryBuilder('c')
  24.             ->select('c.id AS id, c.foodCost, c.coffeeCost, c.'.$nameCurrency.' as currencyName, c.'.$nameLanguage.' as language, c.timeLocal, c.priceSegment, c.flightTime, c.fCurrency as currency, c.fCode AS code, c.'.$name.' AS name, c.'.$nameCase.'Rd AS nameRd, c.'.$nameCase.'Vn AS nameVn, c.'.$nameCase.'Pr AS namePr, c.'.$nameCase.'Dt AS nameDt, c.fNameTr AS nameTr, c.fNameTr AS countryTr, c.'.$name.' AS countryName')
  25.             ->where('c.id = :country')
  26.             ->setParameter('country'$countryId)
  27.             ->getQuery();
  28.         return $sql->getOneOrNullResult();
  29.     }
  30.     public function getIdByAlias($country$city)
  31.     {
  32.         $sql $this->createQueryBuilder('c')
  33.             ->select('c.id AS countryId')
  34.             ->where('c.fNameTr = :country')
  35.             ->orWhere('c.fNameTr = :countryChange')
  36.             ->orWhere('c.fNameTr = :countryChangeTwo')
  37.             ->setParameter('country'$country)
  38.             ->setParameter('countryChange'$country)
  39.             ->setParameter('countryChangeTwo'str_replace('_'' '$country))
  40.             ;
  41.         if(isset($city)){
  42.             $sql->addSelect('ct.id AS cityId')
  43.                 ->leftJoin('c.cities''ct')
  44.                 ->andWhere('ct.fNameTr = :city')
  45.                 ->setParameter('city'$city);
  46.         }
  47.         return $sql->getQuery()->getOneOrNullResult();
  48.     }
  49.     /**
  50.      * @param $country
  51.      * @param $city
  52.      * @param null $season
  53.      * @return mixed
  54.      */
  55.     public function getSeoByAlias($market$dbNormalizer$country$city$season null$locale 'ru')
  56.     {
  57.         $nameCase   $dbNormalizer['country']['case'][$locale];
  58.         $nameCity   $dbNormalizer['city']['name'][$locale];
  59.         $priceField 'price'.ucfirst($market['currency']['code']);
  60.         $sql $this->createQueryBuilder('c')
  61.             ->select('c.'.$nameCase.'Vn AS nameVn, c.'.$nameCase.'Pr AS namePr, city.'.$nameCity.' AS fName, price.price, price.currency')
  62.             ->leftJoin('c.cities''city')
  63.             ->leftJoin('App\Entity\Otpusk\PriceIndex''price'\Doctrine\ORM\Query\Expr\Join::WITH'price.id = city.id AND price.type=\'city\'')
  64.             ->leftJoin('App\Entity\Otpusk\ExchangeRate''e'\Doctrine\ORM\Query\Expr\Join::WITH'price.currency = e.id')
  65.             ->where('c.fNameTr = :country OR c.fNameTr = :countryRp')
  66.             ->groupBy('price.id')
  67.             ->orderBy('price.price')
  68.             ->setMaxResults(3)
  69.             ->setParameters(array('country' => $country'countryRp' => str_replace('_''-'$country)));
  70.         ;
  71.         if(isset($city)){
  72.             $sql->addSelect('ct.'.$nameCity.'Vn AS cityNameVn, ct.'.$nameCity.'Pr AS cityNamePr')
  73.                 ->leftJoin('c.cities''ct')
  74.                 ->andWhere('ct.fNameTr = :city')
  75.                 ->setParameter('city'$city);
  76.         }
  77.         $result $sql->getQuery()->execute();
  78.         $sql $this->createQueryBuilder('c')
  79.             ->select('MIN(p.'.$priceField.') AS lowPrice')
  80.             ->andWhere('c.fNameTr = :country OR c.fNameTr = :countryRp')
  81.             ->andWhere($this->helperService->getWhereForORM($market))
  82.             ->andWhere('p.transport != \'no\'')
  83.             ->andWhere('p.'.$priceField.' > 0')
  84.             ->setParameters(array('country' => $country'countryRp' => str_replace('_''-'$country)));
  85.         if(isset($city)){
  86.             $sql->leftJoin('c.cities''ct')
  87.                 ->leftJoin('App\Entity\Otpusk\PriceIndex''p'\Doctrine\ORM\Query\Expr\Join::WITH'p.id = ct.id')
  88.                 ->andWhere('ct.fNameTr = :city')
  89.                 ->setParameter('city'$city);
  90.         } else {
  91.             $sql->leftJoin('App\Entity\Otpusk\PriceIndex''p'\Doctrine\ORM\Query\Expr\Join::WITH'p.id = c.id');
  92.         }
  93.         if(isset($season)){
  94.             $sql->andWhere('p.date LIKE :month')
  95.                 ->setParameter('month''%-'.$season.'-%');
  96.         }
  97.         $result['lowPrice'] = $sql->getQuery()->getOneOrNullResult();
  98.         return $result;
  99.     }
  100.     public function getCountryFaq($countryId$type$locale 'ru'$dbNormalizer)
  101.     {
  102.         $params = array(
  103.             'country' => $countryId,
  104.             'type' => $type,
  105.             'bind' => 'country',
  106.             'lang' => $dbNormalizer['tRefs']['language'][$locale]
  107.         );
  108.         $conn $this->getEntityManager()
  109.             ->getConnection();
  110.         $sql 'SELECT
  111.               f.fTitle AS title,
  112.               f.fText AS text,
  113.               f.fLang AS lang
  114.             FROM
  115.               otpusk.tCountries c
  116.               LEFT JOIN otpusk.tRefs f ON c.rec_id = f.fBindId
  117.               LEFT JOIN otpusk.tRefsType fb ON f.fRefTypeID = fb.rec_id
  118.             WHERE
  119.               c.rec_id = :country
  120.               AND f.fLang = :lang
  121.               AND fb.fName = :type
  122.               AND fb.fBindType = :bind
  123.             ORDER BY
  124.               f.fSort DESC';
  125.         $stmt $conn->prepare($sql);
  126.         foreach ($params as $index => $param) {
  127.             $stmt->bindValue($index$param);
  128.         }
  129.         return $stmt->execute()->fetchAll();
  130.     }
  131.     public function getCountriesDescription($arr$type$locale 'ru'$dbNormalizer)
  132.     {
  133.         $countryIds = [0];
  134.         foreach ($arr as $one) {
  135.             $countryIds[] = $one['id'];
  136.         }
  137.         $params = array(
  138.             'type' => $type,
  139.             'bind' => 'country',
  140.             'lang' => $dbNormalizer['tRefs']['language'][$locale]
  141.         );
  142.         $conn $this->getEntityManager()
  143.             ->getConnection();
  144.         $sql 'SELECT
  145.               c.rec_id AS id,
  146.               f.fText AS text
  147.             FROM
  148.               otpusk.tCountries c
  149.               LEFT JOIN otpusk.tRefs f ON c.rec_id = f.fBindId
  150.               LEFT JOIN otpusk.tRefsType fb ON f.fRefTypeID = fb.rec_id
  151.             WHERE
  152.               c.rec_id IN ('.implode(',',$countryIds).')
  153.               AND f.fLang = :lang
  154.               AND fb.fName = :type
  155.               AND fb.fBindType = :bind
  156.             ORDER BY
  157.               f.fSort DESC';
  158.         $stmt $conn->prepare($sql);
  159.         foreach ($params as $index => $param) {
  160.             $stmt->bindValue($index$param);
  161.         }
  162.         $result $stmt->execute()->fetchAll();
  163.         foreach ($arr as $key => $one) {
  164.             $arr[$key]['description'] = null;
  165.             $found_key array_search($one['id'], array_column($result'id'));
  166.             if (false !== $found_key$arr[$key]['description'] = $result[$found_key];
  167.         }
  168.         return $arr;
  169.     }
  170.     public function getCountryFaqNew($countryId$locale 'ru'$season null$month null$isDeparture false$cityFrom$isRubric false$dbNormalizer)
  171.     {
  172.         $code = (!$isRubric) ? 'countrytours' 'countryresorts';
  173.         if ($isDeparture$code .= 'departurecity';
  174.         if (!is_null($season)) $code .= $season;
  175.         if (!is_null($month)) $code .= $month;
  176.         $params = array(
  177.             'country' => $countryId,
  178.             'code' => $code,
  179.             'lang' => $dbNormalizer['tRefs']['language'][$locale]
  180.         );
  181.         $conn $this->getEntityManager()
  182.             ->getConnection();
  183.         $sql 'SELECT
  184.               f.fTitle AS title,
  185.               f.fText AS text
  186.             FROM
  187.               otpusk.tCountries c
  188.               LEFT JOIN otpusk.tRefs f ON c.rec_id = f.fBindId
  189.               LEFT JOIN otpusk.tRefsType fb ON f.fRefTypeID = fb.rec_id
  190.             WHERE
  191.               c.rec_id = :country
  192.               AND f.fLang = :lang
  193.               AND fb.fCode = :code';
  194.         if ($isDeparture) {
  195.             $sql .= ' AND f.fFromCityId = :cityFrom';
  196.             $params['cityFrom'] =  $cityFrom;
  197.         }
  198.         $sql .= ' ORDER BY f.fSort DESC';
  199.         $stmt $conn->prepare($sql);
  200.         foreach ($params as $index => $param) {
  201.             $stmt->bindValue($index$param);
  202.         }
  203.         $result $stmt->execute()->fetchAll();
  204.         if (!empty($result)) return $result[0];
  205.         return null;
  206.     }
  207.     public function getByCountryAliases($dbNormalizer$localestring $countryAlias)
  208.     {
  209.         $name $dbNormalizer['country']['case'][$locale];
  210.         $country =  $this->createQueryBuilder('country')
  211.             ->select('country.'.$name.'Rd as name')
  212.             ->where('country.fNameTr = :countryAlias')
  213.             ->orWhere('country.fNameTr = :countryAliasChanged')
  214.             ->orWhere('country.fNameTr = :countryAliasChangedA')
  215.             ->setParameter('countryAlias'$countryAlias)
  216.             ->setParameter('countryAliasChanged'$countryAlias)
  217.             ->setParameter('countryAliasChangedA'str_replace('_'' '$countryAlias))
  218.             ->getQuery()
  219.             ->getOneOrNullResult();
  220.         return $country;
  221.     }
  222.     /**
  223.      * @param int|null $countryId
  224.      * @param string $type
  225.      * @return array
  226.      */
  227.     public function getTemperaturesByCountry($countryId)
  228.     {
  229.         $params = array(
  230.             'countryId'   => $countryId,
  231.         );
  232.         $conn $this->getEntityManager()
  233.             ->getConnection();
  234.         $sql 'SELECT t.temperatureType as type, AVG(t.jan) as Jan, AVG(t.feb) as Feb, AVG(t.mar) as Mar, AVG(t.apr) as Apr, AVG(t.may) as May, AVG(t.jun) as Jun, AVG(t.jul) as Jul, AVG(t.aug) as Aug, AVG(t.sep) as Sep, AVG(t.oct) as Oct, AVG(t.nov) as Nov, AVG(t.decm) as "Dec"
  235.                 FROM otp_misc.temperatures t
  236.                 WHERE t.countryId = :countryId
  237.                 GROUP BY t.temperatureType';
  238.         $stmt $conn->prepare($sql);
  239.         foreach ($params as $index => $param) {
  240.             $stmt->bindValue($index$param);
  241.         }
  242.         return $stmt->execute()->fetchAll();
  243.     }
  244.     public function getByCategory($market$category$locale 'ru')
  245.     {
  246.         $sql $this->createQueryBuilder('c')
  247.             ->join('c.countryCategoryValues''cv')
  248.             ->leftJoin('cv.category''cat')
  249.             ->where('cat.name = :category')
  250.             ->andWhere('cv.market = :marketId')
  251.             ->setParameter('category'$category)
  252.             ->setParameter('marketId'$market['id'])
  253.             ->orderBy('cv.position')
  254.         ;
  255.         return $sql->getQuery()->getResult();
  256.     }
  257.     public function getExcursions()
  258.     {
  259.         $conn $this->getEntityManager()
  260.             ->getConnection();
  261.         $sql 'SELECT c2.* FROM otp_tours.cruiseCategories c1
  262.             LEFT JOIN otp_tours.cruiseCategories c2 ON (c2.parentId=c1.id AND c2.isActive=1 AND c2.toShow=1)
  263.             WHERE c1.isActive=1 AND c1.name="Достопримечательности"
  264.             ORDER BY c2.position';
  265.         $stmt $conn->prepare($sql);
  266.         $results $stmt->execute()->fetchAll();
  267.         foreach ($results as $key => $result) {
  268.             $results[$key]['image'] = null;
  269.             $options \json_decode($result['options'], 1);
  270.             if ($options && isset($options['catImage'])) $results[$key]['image'] = 'https://new.otpusk.com/img/category/'.$options['catImage'];
  271.         }
  272.         return $results;
  273.     }
  274.     public function getSimilarById($countryId$locale 'ru'$dbNormalizer)
  275.     {
  276.         $name $dbNormalizer['country']['name'][$locale];
  277.         $result $this->createQueryBuilder('c')
  278.             ->select('cc.id, cc.'.$name.' as name, cc.fNameTr as slug, cc.image as image')
  279.             ->join('c.similarCountries''s')
  280.             ->join('s.countrySimilarIn''cc')
  281.             ->where('c.id = :countryId')
  282.             ->setParameter('countryId'$countryId)
  283.             ->orderBy('s.position''DESC')
  284.             ->getQuery()
  285.             ->getResult()
  286.         ;
  287.         foreach ($result as $k => $v) {
  288.             $result[$k]['image'] = $this->getImagePath($v['image']);
  289.         }
  290.         return $result;
  291.     }
  292.     public function getImagePath($image)
  293.     {
  294.         if (!is_null($image)) {
  295.             return $this->imageSource.sprintf(
  296.                     '%02d/%02d/%02d/%02d/',
  297.                     ($image 100000000) % 100, ($image 1000000) % 100, ($image 10000) % 100, ($image 100) % 100
  298.                 ) . $image '.webp';
  299.         }
  300.         return null;
  301.     }
  302.     public function getCountryByIp($ip null)
  303.     {
  304.         if (empty($ip)){
  305.             $ip $_SERVER['REMOTE_ADDR'];
  306.         }
  307.         $project_dir dirname($_SERVER['DOCUMENT_ROOT']);
  308.         if (!is_dir($project_dir)) $project_dir '/app';
  309.         $reader = new Reader($project_dir.'/public/upload/GeoLite2/GeoLite2-Country/GeoLite2-Country.mmdb');
  310.         $record $reader->country($ip);
  311.         $this->res = [
  312.             [$record->continent->names['ru'],$record->continent->names['en'], $record->continent->code],
  313.             [$record->country->names['ru'], $record->country->names['en'], $record->country->isoCode],
  314.         ];
  315.         return $this->getIdByCountryName(
  316.             $record->country->isoCode,
  317.             $record->country->names['ru'],
  318.             $record->country->names['en'],
  319.         );
  320.     }
  321.     public function getIdByCountryName($code$name$nameEng)
  322.     {
  323.         $sql $this->createQueryBuilder('c')
  324.             ->where('c.fCountry = :name')
  325.             ->orWhere('c.nameEng = :nameEng')
  326.             ->orWhere('c.fCode = :code')
  327.             ->orWhere('c.fIATA = :code')
  328.             ->setParameter('name'$name)
  329.             ->setParameter('nameEng'$nameEng)
  330.             ->setParameter('code'$code)
  331.         ;
  332.         $country $sql->getQuery()->getResult();
  333.         return isset($country[0])?$country[0]->getId():null;
  334.     }
  335.     public function getRes(){
  336.         return $this->res;
  337.     }
  338. }