<?php
namespace App\Services;
use App\Entity\Otpusk\City;
use App\Entity\Otpusk\Country;
use App\Repository\Otpusk\GeoBlockRepository;
use App\Services\Tours\TourService;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Yaml\Yaml;
use Symfony\Contracts\Translation\TranslatorInterface;
class PageGenerator
{
private $seasonPeriod = array(
'winter' => 'зимой',
'spring' => 'весной',
'summer' => 'летом',
'autumn' => 'осенью',
'january' => 'на январь',
'february' => 'на февраль',
'march' => 'на март',
'april' => 'на апрель',
'may' => 'на май',
'june' => 'на июнь',
'july' => 'на июль',
'august' => 'на август',
'september' => 'на сентябрь',
'october' => 'на октябрь',
'november' => 'на ноябрь',
'december' => 'на декабрь',
);
public $seasonDates = [
'winter' => [
'from' => '01.12',
'to' => '14.12',
],
'spring' => [
'from' => '01.03',
'to' => '14.03',
],
'summer' => [
'from' => '01.06',
'to' => '14.06',
],
'autumn' => [
'from' => '01.09',
'to' => '14.09',
],
'january' => [
'from' => '01.01',
'to' => '14.01',
],
'february' => [
'from' => '01.02',
'to' => '14.02',
],
'march' => [
'from' => '01.03',
'to' => '14.03',
],
'april' => [
'from' => '01.04',
'to' => '14.04',
],
'may' => [
'from' => '01.05',
'to' => '14.05',
],
'june' => [
'from' => '01.06',
'to' => '14.06',
],
'july' => [
'from' => '01.07',
'to' => '14.07',
],
'august' => [
'from' => '01.08',
'to' => '14.08',
],
'september' => [
'from' => '01.09',
'to' => '14.09',
],
'october' => [
'from' => '01.10',
'to' => '14.10',
],
'november' => [
'from' => '01.11',
'to' => '14.11',
],
'december' => [
'from' => '01.12',
'to' => '14.12',
],
];
private $em;
private $pages;
private $translator;
private $geoBlockRepository;
private $dbNormalizer;
public function __construct(
EntityManager $entityManager,
TourService $pages,
TranslatorInterface $translator,
GeoBlockRepository $geoBlockRepository,
ParameterBagInterface $config
)
{
$this->em = $entityManager;
$this->pages = $pages;
$this->translator = $translator;
$this->geoBlockRepository = $geoBlockRepository;
$this->dbNormalizer = $config->get('db_normalizer');
}
public function getCities($country, $city = null, $locale = 'ru')
{
$city = (int)$city > 0 && $city != '' ? (int)$city : null;
$country = $city > 0 || $country == 0 ? null : (int)$country;
return $this->em->getRepository(City::class)->getTourCitiesForPages($country, $city, $locale, $this->dbNormalizer);
}
public function getLandingPage($market, $country, $city = null, $month = null, $season = null, $budget = null, $currency = null, $cityFrom = null, $locale = 'ru', $isDeparture = false, $isRubric = false)
{
$teritory = $this->pages->getIdByAlias($country, $city);
$cityId = $teritory['cityId'];
$countryId = $teritory['countryId'];
$month = $month > 0 ? $month : null;
$season = $month > 0 || $season == '' ? null : $season;
$budget = $budget > 0 && $budget != '' ? $budget : null;
$currency = $currency != '' ? $currency : null;
$cityFrom = $cityFrom != '' ? $cityFrom : $market['mainCities'][0]['id'];
$cityFrom = $this->em->getRepository(City::class)->find($cityFrom);
if (!is_object($cityFrom))
throw new NotFoundHttpException("Can't find city");
return $this->pages->getGeneralToursPage($market, $this->dbNormalizer, $countryId, $cityId, $season, $month, $budget, $currency, $cityFrom->getId(), $locale, $isDeparture, $isRubric);
}
public function getSelectionPage(string $marketCode, int $selectionId, $locale = 'ru')
{
return $this->pages->getSelectionPage($marketCode, $selectionId, $locale);
}
public function getSelectionsForGeoById(string $marketCode, int $geoId)
{
return $this->pages->getSelectionsForGeoById($marketCode, $geoId);
}
public function getSelectionSeo(string $selectionTitle, $locale = 'ru')
{
$value = Yaml::parseFile('../seofile.' . $locale . '.yaml');
$value = $value['seo']['selection'];
return array(
'title' => sprintf($value['title'], $selectionTitle),
'description' => sprintf($value['description'], $selectionTitle),
'keywords' => sprintf($value['keywords'], $selectionTitle),
'h1' => sprintf($value['h1'], $selectionTitle),
);
}
public function getRubricSeo($countryObj, $rubricObj = null, $locale)
{
$value = Yaml::parseFile('../seofile.' . $locale . '.yaml');
$value = (is_null($rubricObj)) ? $value['seo']['cities'] : $value['seo']['rubric'];
$countryFrom = $this->em->getRepository(Country::class)->getByCountryAliases($this->dbNormalizer, $locale, $countryObj->getNameTr());
$dateNow = new \DateTime();
$year = $dateNow->format('Y');
$country = $countryFrom['name'];
$second = (is_null($rubricObj)) ? $year : $rubricObj->translate($locale)->getTitleSeo();
return array(
'title' => sprintf($value['title'], $country, $second),
'description' => sprintf($value['description'], $country, $second),
'keywords' => sprintf($value['keywords'], $country, $second),
'h1' => (is_null($rubricObj)) ? sprintf($value['h1'], $country) : sprintf($value['h1'], $country, $second),
);
}
public function getTourSeoAction($market, string $countryAlias, string $cityAlias = null, $budget = null, $currency = null, $cityFrom = null, $locale = 'ru', $isDeparture = false)
{
$budget = $budget > 0 && $budget != '' ? $budget : null;
$currency = $currency != '' ? $currency : null;
$cityFrom = $cityFrom != '' ? $cityFrom : $market['mainCities'][0]['id'];
$cityFrom = $this->em->getRepository(City::class)->find($cityFrom);
if (!is_object($cityFrom))
throw new NotFoundHttpException("Can't find city");
return $this->getToursSeo($market, $countryAlias, $cityAlias, $budget, $currency, $cityFrom->getNameTr(), $locale, $isDeparture);
}
public function getTourSeasonSeoAction($market, string $countryAlias, string $seasonName, string $cityAlias = null, $cityFrom = null, $locale = 'ru')
{
$cityFrom = $cityFrom != '' ? $cityFrom : $market['mainCities'][0]['id'];
$cityFrom = $this->em->getRepository(City::class)->find($cityFrom);
if (!is_object($cityFrom))
throw new NotFoundHttpException("Can't find city");
return $this->getToursSeasonSeo($market, $countryAlias, $cityAlias, $seasonName, $cityFrom->getNameTr(), $locale);
}
public function getToursSeo($market, string $countryAlias, string $cityAlias = null, $budget = null, $currency = null, $cityFrom, $locale = 'ru', $isDeparture = false)
{
$country = $this->em->getRepository(Country::class)
->getSeoByAlias($market, $this->dbNormalizer, $countryAlias, $cityAlias, null, $locale);
if (is_null($country) || 0 >= count($country))
throw new NotFoundHttpException("Can't find country");
elseif (isset($cityAlias) && !array_key_exists('cityNameVn', $country[0]))
throw new NotFoundHttpException("Can't find city");
$name = isset($cityAlias) ? $country[0]['cityNameVn'] : $country[0]['nameVn'];
$namePr = isset($cityAlias) ? $country[0]['cityNamePr'] : $country[0]['namePr'];
$price = '';
if (isset($currency) && isset($budget)) {
switch ($currency) {
case 'usd':
$price = " " . $this->translator->trans('до') . " $budget " . $this->translator->trans('долларов');
break;
case 'eur':
$price = " " . $this->translator->trans('до') . " $budget " . $this->translator->trans('евро');
break;
default:
$price = " " . $this->translator->trans('до') . " $budget " . $this->translator->trans('гривень');
break;
}
}
$cityFrom = $this->em->getRepository(City::class)->getByCityAliases($this->dbNormalizer, $locale, $cityFrom);
$mainCities = '';
foreach ($country as $city) {
if (isset($city['price']) && isset($city['currency']) && !is_null($city['price']) && !is_null($city['currency'])) {
switch ($city['currency']) {
case 'usd':
$curr = '$';
break;
case 'eur':
$curr = '€';
break;
default:
$curr = '₴';
break;
}
if (array_key_exists('fName', $city))
$mainCities .= $city['fName'] . ' ' . $this->translator->trans('от') . ' ' . round($city['price']) . $curr . ', ';
}
}
$mainCities = substr($mainCities, 0, strlen($mainCities) - 2);
$value = Yaml::parseFile('../seofile.' . $locale . '.yaml');
$value = $value['seo']['tours']['country'];
$cityFromName = $cityFrom['name'];
$fromText = ($isDeparture) ? ' ' . $this->translator->trans('из') . ' ' . $cityFromName : '';
$fromDesc = ($isDeparture) ? $cityFromName : $this->translator->trans('Киева, Львова, Вашего города');
$result = array(
'title' => sprintf($value['title'], $name, $fromText, date('Y'), $namePr),
'description' => sprintf($value['description'], $name, $price, $cityFromName, date('Y'), $mainCities, $namePr, $fromDesc),
'keywords' => sprintf($value['keywords'], $name, $fromText),
'h1' => sprintf($value['h1'], $name, $fromText),
'lowPrice' => $country['lowPrice']['lowPrice'],
);
return $result;
}
public function getToursSeasonSeo($market, string $countryAlias, string $cityAlias = null, string $season = null, $cityFrom, $locale = 'ru')
{
$month = null;
$month = $this->getMonthNumber($season);
$year = $this->checkYear($season, $month);
$country = $this->em->getRepository(Country::class)
->getSeoByAlias($market, $this->dbNormalizer, $countryAlias, $cityAlias, $month, $locale);
if (is_null($country) || 0 >= count($country))
throw new NotFoundHttpException("Can't find country");
elseif (isset($cityAlias) && !array_key_exists('cityNameVn', $country[0]))
throw new NotFoundHttpException("Can't find city");
$name = isset($cityAlias) ? $country[0]['cityNameVn'] : $country[0]['nameVn'];
$namePr = isset($cityAlias) ? $country[0]['cityNamePr'] : $country[0]['namePr'];
$cityFrom = $this->em->getRepository(City::class)->getByCityAliases($this->dbNormalizer, $locale, $cityFrom);
if (array_key_exists($season, $this->seasonPeriod)) {
$season = ' ' . $this->translator->trans($this->seasonPeriod[$season]) . ' ';
} else {
throw new NotFoundHttpException("Can't find season");
}
$mainCities = '';
foreach ($country as $city) {
if (isset($city['price']) && isset($city['currency']) && !is_null($city['price']) && !is_null($city['currency'])) {
switch ($city['currency']) {
case 'usd':
$curr = '$';
break;
case 'eur':
$curr = '€';
break;
default:
$curr = '₴';
break;
}
if (array_key_exists('fName', $city))
$mainCities .= $city['fName'] . ' ' . $this->translator->trans('от') . ' ' . round($city['price']) . $curr . ', ';
}
}
$mainCities = substr($mainCities, 0, strlen($mainCities) - 2);
$value = Yaml::parseFile('../seofile.' . $locale . '.yaml');
$value = $value['seo']['tours']['season'];
$cityFromName = $cityFrom['name'];
$result = array(
//'title' => sprintf($value['title'], $name, $season, $year, $cityFromName, $namePr),
'title' => sprintf($value['title'], $name, $season, $year, $namePr),
'description' => sprintf($value['description'], $name, $season, $year, $cityFromName, $mainCities, $namePr),
'keywords' => sprintf($value['keywords'], $name, substr($season, 0, strlen($season) - 1), $cityFromName),
'h1' => sprintf($value['h1'], $name, substr($season, 0, strlen($season) - 1)),
'lowPrice' => $country['lowPrice']['lowPrice'],
);
return $result;
}
public function getPageCountries($market, $locale = 'ru')
{
return $this->pages->getPageCountries($market, $this->dbNormalizer, $locale);
}
public function getRubrics($countryObj, $rubricObj = null, $locale = 'ru', $citiesStatus = false)
{
return $this->pages->getRubrics($this->dbNormalizer, $countryObj, $rubricObj, $locale, $citiesStatus);
}
public function getSeoCountries($locale = 'ru')
{
$value = Yaml::parseFile('../seofile.' . $locale . '.yaml');
$value = $value['seo']['countries'];
return array(
'title' => sprintf($value['title']),
'description' => sprintf($value['description']),
'keywords' => sprintf($value['keywords']),
'h1' => sprintf($value['h1']),
);
}
public function getMonthNumber($season)
{
switch ($season) {
case 'winter':
return date('m', strtotime('01 dec 2020'));
break;
case 'spring':
return date('m', strtotime('01 mar 2020'));
break;
case 'summer':
return date('m', strtotime('01 jun 2020'));
break;
case 'autumn':
return date('m', strtotime('01 sep 2020'));
break;
default:
return date('m', strtotime('01 ' . $season . ' 2020'));
break;
}
}
public function checkYear($season, $month)
{
switch ($season) {
case 'winter':
case 'spring':
case 'summer':
case 'autumn':
return date('m') <= ($month % 12) + 2 ? date('Y') : date('Y') + 1;
break;
default:
return date('m') <= $month ? date('Y') : date('Y') + 1;
break;
}
}
}