src/Services/PageGenerator.php line 220

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\Otpusk\City;
  4. use App\Entity\Otpusk\Country;
  5. use App\Repository\Otpusk\GeoBlockRepository;
  6. use App\Services\Tours\TourService;
  7. use Doctrine\ORM\EntityManager;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Symfony\Component\Yaml\Yaml;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class PageGenerator
  13. {
  14.     private $seasonPeriod = array(
  15.         'winter' => 'зимой',
  16.         'spring' => 'весной',
  17.         'summer' => 'летом',
  18.         'autumn' => 'осенью',
  19.         'january' => 'на январь',
  20.         'february' => 'на февраль',
  21.         'march' => 'на март',
  22.         'april' => 'на апрель',
  23.         'may' => 'на май',
  24.         'june' => 'на июнь',
  25.         'july' => 'на июль',
  26.         'august' => 'на август',
  27.         'september' => 'на сентябрь',
  28.         'october' => 'на октябрь',
  29.         'november' => 'на ноябрь',
  30.         'december' => 'на декабрь',
  31.     );
  32.     public $seasonDates = [
  33.         'winter' => [
  34.             'from' => '01.12',
  35.             'to' => '14.12',
  36.         ],
  37.         'spring' => [
  38.             'from' => '01.03',
  39.             'to' => '14.03',
  40.         ],
  41.         'summer' => [
  42.             'from' => '01.06',
  43.             'to' => '14.06',
  44.         ],
  45.         'autumn' => [
  46.             'from' => '01.09',
  47.             'to' => '14.09',
  48.         ],
  49.         'january' => [
  50.             'from' => '01.01',
  51.             'to' => '14.01',
  52.         ],
  53.         'february' => [
  54.             'from' => '01.02',
  55.             'to' => '14.02',
  56.         ],
  57.         'march' => [
  58.             'from' => '01.03',
  59.             'to' => '14.03',
  60.         ],
  61.         'april' => [
  62.             'from' => '01.04',
  63.             'to' => '14.04',
  64.         ],
  65.         'may' => [
  66.             'from' => '01.05',
  67.             'to' => '14.05',
  68.         ],
  69.         'june' => [
  70.             'from' => '01.06',
  71.             'to' => '14.06',
  72.         ],
  73.         'july' => [
  74.             'from' => '01.07',
  75.             'to' => '14.07',
  76.         ],
  77.         'august' => [
  78.             'from' => '01.08',
  79.             'to' => '14.08',
  80.         ],
  81.         'september' => [
  82.             'from' => '01.09',
  83.             'to' => '14.09',
  84.         ],
  85.         'october' => [
  86.             'from' => '01.10',
  87.             'to' => '14.10',
  88.         ],
  89.         'november' => [
  90.             'from' => '01.11',
  91.             'to' => '14.11',
  92.         ],
  93.         'december' => [
  94.             'from' => '01.12',
  95.             'to' => '14.12',
  96.         ],
  97.     ];
  98.     private $em;
  99.     private $pages;
  100.     private $translator;
  101.     private $geoBlockRepository;
  102.     private $dbNormalizer;
  103.     public function __construct(
  104.         EntityManager $entityManager,
  105.         TourService $pages,
  106.         TranslatorInterface $translator,
  107.         GeoBlockRepository $geoBlockRepository,
  108.         ParameterBagInterface $config
  109.     )
  110.     {
  111.         $this->em $entityManager;
  112.         $this->pages $pages;
  113.         $this->translator $translator;
  114.         $this->geoBlockRepository $geoBlockRepository;
  115.         $this->dbNormalizer $config->get('db_normalizer');
  116.     }
  117.     public function getCities($country$city null$locale 'ru')
  118.     {
  119.         $city = (int)$city && $city != '' ? (int)$city null;
  120.         $country $city || $country == null : (int)$country;
  121.         return $this->em->getRepository(City::class)->getTourCitiesForPages($country$city$locale$this->dbNormalizer);
  122.     }
  123.     public function getLandingPage($market$country$city null$month null$season null$budget null$currency null$cityFrom null$locale 'ru'$isDeparture false$isRubric false)
  124.     {
  125.         $teritory $this->pages->getIdByAlias($country$city);
  126.         $cityId $teritory['cityId'];
  127.         $countryId $teritory['countryId'];
  128.         $month $month $month null;
  129.         $season $month || $season == '' null $season;
  130.         $budget $budget && $budget != '' $budget null;
  131.         $currency $currency != '' $currency null;
  132.         $cityFrom $cityFrom != '' $cityFrom $market['mainCities'][0]['id'];
  133.         $cityFrom $this->em->getRepository(City::class)->find($cityFrom);
  134.         if (!is_object($cityFrom))
  135.             throw new NotFoundHttpException("Can't find city");
  136.         return $this->pages->getGeneralToursPage($market$this->dbNormalizer$countryId$cityId$season$month$budget$currency$cityFrom->getId(), $locale$isDeparture$isRubric);
  137.     }
  138.     public function getSelectionPage(string $marketCodeint $selectionId$locale 'ru')
  139.     {
  140.         return $this->pages->getSelectionPage($marketCode$selectionId$locale);
  141.     }
  142.     public function getSelectionsForGeoById(string $marketCodeint $geoId)
  143.     {
  144.         return $this->pages->getSelectionsForGeoById($marketCode$geoId);
  145.     }
  146.     public function getSelectionSeo(string $selectionTitle$locale 'ru')
  147.     {
  148.         $value Yaml::parseFile('../seofile.' $locale '.yaml');
  149.         $value $value['seo']['selection'];
  150.         return array(
  151.             'title' => sprintf($value['title'], $selectionTitle),
  152.             'description' => sprintf($value['description'], $selectionTitle),
  153.             'keywords' => sprintf($value['keywords'], $selectionTitle),
  154.             'h1' => sprintf($value['h1'], $selectionTitle),
  155.         );
  156.     }
  157.     public function getRubricSeo($countryObj$rubricObj null$locale)
  158.     {
  159.         $value Yaml::parseFile('../seofile.' $locale '.yaml');
  160.         $value = (is_null($rubricObj)) ? $value['seo']['cities'] : $value['seo']['rubric'];
  161.         $countryFrom $this->em->getRepository(Country::class)->getByCountryAliases($this->dbNormalizer$locale$countryObj->getNameTr());
  162.         $dateNow = new \DateTime();
  163.         $year $dateNow->format('Y');
  164.         $country $countryFrom['name'];
  165.         $second = (is_null($rubricObj)) ? $year $rubricObj->translate($locale)->getTitleSeo();
  166.         return array(
  167.             'title' => sprintf($value['title'], $country$second),
  168.             'description' => sprintf($value['description'], $country$second),
  169.             'keywords' => sprintf($value['keywords'], $country$second),
  170.             'h1' => (is_null($rubricObj)) ? sprintf($value['h1'], $country) : sprintf($value['h1'], $country$second),
  171.         );
  172.     }
  173.     public function getTourSeoAction($marketstring $countryAliasstring $cityAlias null$budget null$currency null$cityFrom null$locale 'ru'$isDeparture false)
  174.     {
  175.         $budget $budget && $budget != '' $budget null;
  176.         $currency $currency != '' $currency null;
  177.         $cityFrom $cityFrom != '' $cityFrom $market['mainCities'][0]['id'];
  178.         $cityFrom $this->em->getRepository(City::class)->find($cityFrom);
  179.         if (!is_object($cityFrom))
  180.             throw new NotFoundHttpException("Can't find city");
  181.         return $this->getToursSeo($market$countryAlias$cityAlias$budget$currency$cityFrom->getNameTr(), $locale$isDeparture);
  182.     }
  183.     public function getTourSeasonSeoAction($marketstring $countryAliasstring $seasonNamestring $cityAlias null$cityFrom null$locale 'ru')
  184.     {
  185.         $cityFrom $cityFrom != '' $cityFrom $market['mainCities'][0]['id'];
  186.         $cityFrom $this->em->getRepository(City::class)->find($cityFrom);
  187.         if (!is_object($cityFrom))
  188.             throw new NotFoundHttpException("Can't find city");
  189.         return $this->getToursSeasonSeo($market$countryAlias$cityAlias$seasonName$cityFrom->getNameTr(), $locale);
  190.     }
  191.     public function getToursSeo($marketstring $countryAliasstring $cityAlias null$budget null$currency null$cityFrom$locale 'ru'$isDeparture false)
  192.     {
  193.         $country $this->em->getRepository(Country::class)
  194.             ->getSeoByAlias($market$this->dbNormalizer$countryAlias$cityAliasnull$locale);
  195.         if (is_null($country) || >= count($country))
  196.             throw new NotFoundHttpException("Can't find country");
  197.         elseif (isset($cityAlias) && !array_key_exists('cityNameVn'$country[0]))
  198.             throw new NotFoundHttpException("Can't find city");
  199.         $name = isset($cityAlias) ? $country[0]['cityNameVn'] : $country[0]['nameVn'];
  200.         $namePr = isset($cityAlias) ? $country[0]['cityNamePr'] : $country[0]['namePr'];
  201.         $price '';
  202.         if (isset($currency) && isset($budget)) {
  203.             switch ($currency) {
  204.                 case 'usd':
  205.                     $price " " $this->translator->trans('до') . $budget " $this->translator->trans('долларов');
  206.                     break;
  207.                 case 'eur':
  208.                     $price " " $this->translator->trans('до') . $budget " $this->translator->trans('евро');
  209.                     break;
  210.                 default:
  211.                     $price " " $this->translator->trans('до') . $budget " $this->translator->trans('гривень');
  212.                     break;
  213.             }
  214.         }
  215.         $cityFrom $this->em->getRepository(City::class)->getByCityAliases($this->dbNormalizer$locale$cityFrom);
  216.         $mainCities '';
  217.         foreach ($country as $city) {
  218.             if (isset($city['price']) && isset($city['currency']) && !is_null($city['price']) && !is_null($city['currency'])) {
  219.                 switch ($city['currency']) {
  220.                     case 'usd':
  221.                         $curr '$';
  222.                         break;
  223.                     case 'eur':
  224.                         $curr '€';
  225.                         break;
  226.                     default:
  227.                         $curr '₴';
  228.                         break;
  229.                 }
  230.                 if (array_key_exists('fName'$city))
  231.                     $mainCities .= $city['fName'] . ' ' $this->translator->trans('от') . ' ' round($city['price']) . $curr ', ';
  232.             }
  233.         }
  234.         $mainCities substr($mainCities0strlen($mainCities) - 2);
  235.         $value Yaml::parseFile('../seofile.' $locale '.yaml');
  236.         $value $value['seo']['tours']['country'];
  237.         $cityFromName $cityFrom['name'];
  238.         $fromText = ($isDeparture) ? ' ' $this->translator->trans('из') . ' ' $cityFromName '';
  239.         $fromDesc = ($isDeparture) ? $cityFromName $this->translator->trans('Киева, Львова, Вашего города');
  240.         $result = array(
  241.             'title' => sprintf($value['title'], $name$fromTextdate('Y'), $namePr),
  242.             'description' => sprintf($value['description'], $name$price$cityFromNamedate('Y'), $mainCities$namePr$fromDesc),
  243.             'keywords' => sprintf($value['keywords'], $name$fromText),
  244.             'h1' => sprintf($value['h1'], $name$fromText),
  245.             'lowPrice' => $country['lowPrice']['lowPrice'],
  246.         );
  247.         return $result;
  248.     }
  249.     public function getToursSeasonSeo($marketstring $countryAliasstring $cityAlias nullstring $season null$cityFrom$locale 'ru')
  250.     {
  251.         $month null;
  252.         $month $this->getMonthNumber($season);
  253.         $year $this->checkYear($season$month);
  254.         $country $this->em->getRepository(Country::class)
  255.             ->getSeoByAlias($market$this->dbNormalizer$countryAlias$cityAlias$month$locale);
  256.         if (is_null($country) || >= count($country))
  257.             throw new NotFoundHttpException("Can't find country");
  258.         elseif (isset($cityAlias) && !array_key_exists('cityNameVn'$country[0]))
  259.             throw new NotFoundHttpException("Can't find city");
  260.         $name = isset($cityAlias) ? $country[0]['cityNameVn'] : $country[0]['nameVn'];
  261.         $namePr = isset($cityAlias) ? $country[0]['cityNamePr'] : $country[0]['namePr'];
  262.         $cityFrom $this->em->getRepository(City::class)->getByCityAliases($this->dbNormalizer$locale$cityFrom);
  263.         if (array_key_exists($season$this->seasonPeriod)) {
  264.             $season ' ' $this->translator->trans($this->seasonPeriod[$season]) . ' ';
  265.         } else {
  266.             throw new NotFoundHttpException("Can't find season");
  267.         }
  268.         $mainCities '';
  269.         foreach ($country as $city) {
  270.             if (isset($city['price']) && isset($city['currency']) && !is_null($city['price']) && !is_null($city['currency'])) {
  271.                 switch ($city['currency']) {
  272.                     case 'usd':
  273.                         $curr '$';
  274.                         break;
  275.                     case 'eur':
  276.                         $curr '€';
  277.                         break;
  278.                     default:
  279.                         $curr '₴';
  280.                         break;
  281.                 }
  282.                 if (array_key_exists('fName'$city))
  283.                     $mainCities .= $city['fName'] . ' ' $this->translator->trans('от') . ' ' round($city['price']) . $curr ', ';
  284.             }
  285.         }
  286.         $mainCities substr($mainCities0strlen($mainCities) - 2);
  287.         $value Yaml::parseFile('../seofile.' $locale '.yaml');
  288.         $value $value['seo']['tours']['season'];
  289.         $cityFromName $cityFrom['name'];
  290.         $result = array(
  291.             //'title' => sprintf($value['title'], $name, $season, $year, $cityFromName, $namePr),
  292.             'title' => sprintf($value['title'], $name$season$year$namePr),
  293.             'description' => sprintf($value['description'], $name$season$year$cityFromName$mainCities$namePr),
  294.             'keywords' => sprintf($value['keywords'], $namesubstr($season0strlen($season) - 1), $cityFromName),
  295.             'h1' => sprintf($value['h1'], $namesubstr($season0strlen($season) - 1)),
  296.             'lowPrice' => $country['lowPrice']['lowPrice'],
  297.         );
  298.         return $result;
  299.     }
  300.     public function getPageCountries($market$locale 'ru')
  301.     {
  302.         return $this->pages->getPageCountries($market$this->dbNormalizer$locale);
  303.     }
  304.     public function getRubrics($countryObj$rubricObj null$locale 'ru'$citiesStatus false)
  305.     {
  306.         return $this->pages->getRubrics($this->dbNormalizer$countryObj$rubricObj$locale$citiesStatus);
  307.     }
  308.     public function getSeoCountries($locale 'ru')
  309.     {
  310.         $value Yaml::parseFile('../seofile.' $locale '.yaml');
  311.         $value $value['seo']['countries'];
  312.         return array(
  313.             'title' => sprintf($value['title']),
  314.             'description' => sprintf($value['description']),
  315.             'keywords' => sprintf($value['keywords']),
  316.             'h1' => sprintf($value['h1']),
  317.         );
  318.     }
  319.     public function getMonthNumber($season)
  320.     {
  321.         switch ($season) {
  322.             case 'winter':
  323.                 return date('m'strtotime('01 dec 2020'));
  324.                 break;
  325.             case 'spring':
  326.                 return date('m'strtotime('01 mar 2020'));
  327.                 break;
  328.             case 'summer':
  329.                 return date('m'strtotime('01 jun 2020'));
  330.                 break;
  331.             case 'autumn':
  332.                 return date('m'strtotime('01 sep 2020'));
  333.                 break;
  334.             default:
  335.                 return date('m'strtotime('01 ' $season ' 2020'));
  336.                 break;
  337.         }
  338.     }
  339.     public function checkYear($season$month)
  340.     {
  341.         switch ($season) {
  342.             case 'winter':
  343.             case 'spring':
  344.             case 'summer':
  345.             case 'autumn':
  346.                 return date('m') <= ($month 12) + date('Y') : date('Y') + 1;
  347.                 break;
  348.             default:
  349.                 return date('m') <= $month date('Y') : date('Y') + 1;
  350.                 break;
  351.         }
  352.     }
  353. }