<?php
namespace App\Controller;
use App\EntityOtpusk\Cruises;
use App\EntityOtpusk\Events;
use App\EntityOtpusk\Operators;
use App\EntityOtpusk\Statistics;
use App\EntityOtpusk\Thotels AS Hotels;
use App\EntityOtpusk\Tcities as City;
use App\EntityOtpusk\Tcountries as Country;
use App\Repository\AdvertsRepository;
use App\Repository\ManagerLogsRepository;
use App\Repository\StatisticsRepository;
use App\Service\EventsService;
use App\Service\StatisticsService;
use App\Service\Ipv6Service;
use App\Exception\EventsException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\Country as Countries;
class EventsController extends AbstractController
{
/**
* @Route("/api/events", name="event", methods={"GET"})
* @param Request $request
* @param KernelInterface $kernel
* @param AdvertsRepository $advertsRepository
* @param ManagerLogsRepository $managerLogsRepository
* @param EventsService $eventsService
* @return JsonResponse
*/
public function eventsAdd(
Request $request,
KernelInterface $kernel,
AdvertsRepository $advertsRepository,
ManagerLogsRepository $managerLogsRepository,
EventsService $eventsService
)
{
try {
if (empty($request->get('offerId'))) {
throw new EventsException('Empty offerId');
}
if (empty($request->get('advertId'))) {
throw new EventsException('Empty advertId');
}
$advertInfo = $advertsRepository->findOneBy(['id' => $request->get('advertId')]);
if (!$advertInfo) {
throw new EventsException('No found advertId');
}
if (!$advertInfo->getAgency()) {
throw new EventsException('No found agency');
}
if (empty($request->get('regionId'))) {
throw new EventsException('Empty regionId');
}
if (empty($request->get('officeId'))) {
throw new EventsException('Empty officeId');
}
if (empty($request->get('event'))) {
throw new EventsException('Empty event');
}
$cruiseId = $request->get('cruiseId');
if ($cruiseId > 0) {
$action = 'cruise';
} else {
$action = 'tour';
}
$event = new Events();
$event->setEventTypeName($request->get('event'));
header('x-event: '.$request->get('event').'='.$event->getEventType());
if (is_null($event->getEventType())) {
throw new EventsException('No valid event');
}
$event->setEventAction($action);
//$event->setHotelId($request->get('hotelId'));
$event->setCruiseId($request->get('cruiseId', 0));
$event->setAdvertId($request->get('advertId'));
$advert = $advertsRepository->find($request->get('advertId'));
$agencyId = $advert->getAgency()->getId();
$event->setAgencyId($request->get('agencyId', $agencyId)); // не обязательно, если что найдет из advertId
$event->setRegionId($request->get('regionId'));
$offerId = $request->get('offerId');
$people = (int)substr($offerId, 16);
if (empty($people)) $people = $request->get('people');
$offerId = (int)substr($offerId, 0,16);
header('X-offerId: '.$offerId);
header('X-people: '.$people);
$event->setOfferId($offerId);
$event->setCid($request->get('cid'));
$event->setOperatorId($request->get('operatorId', 0)); // не обязательно, если что найдет из offerId
$event->setOfficeId($request->get('officeId'));
$event->setPeople($request->get('people', $people));
header('X-ip: '.$request->getClientIp());
$event->setIp(Ipv6Service::ip2long($request->getClientIp()));
$event->setEventDate(new \DateTime());
$event->setEventPosition($request->get('eventPosition', 0)); // не обязательно
$price = $eventsService->saveEvent($event, $request->get('event'));
header('X-eventPrice: '.$event->getEventPrice());
header('X-eventPosition: '.$event->getEventPosition());
$res = array();
if (isset($_COOKIE['DM']) || $kernel->isDebug()) {
$res['_event'] = $event ? $event->_toArray() : null;
}
if (isset($_COOKIE['DM']) || $kernel->isDebug()) {
$res['_price'] = $price;
}
$eventsService->sendGA($request->get('cid'), $request->get('sid'), $event, $request->get('event'));
//$price = 0;
return new JsonResponse([
'status' => 'success',
//'price' => $price,
] + $res);
} catch (EventsException $e) {
return new JsonResponse([
'status' => 'error',
'message' => $e->getMessage(),
]);
}
return new JsonResponse([
'status' => 'error',
'text' => 'Запрос не отправлен'
]);
}
/**
* @Route("/api/stats", name="stats", methods={"GET"})
* @param Request $request
* @param KernelInterface $kernel
* @param ManagerLogsRepository $managerLogsRepository
* @param StatisticsRepository $statisticsRepository
* @param StatisticsService $statisticsService
* @return JsonResponse
*/
public function statsAdd(
Request $request,
KernelInterface $kernel,
ManagerLogsRepository $managerLogsRepository,
StatisticsRepository $statisticsRepository,
StatisticsService $statisticsService
)
{
try {
if (empty($request->get('source'))) {
throw new EventsException('Empty source');
}
if (empty($request->get('action'))) {
throw new EventsException('Empty action');
}
if (empty($request->get('id'))) {
throw new EventsException('Empty id');
}
$stats = new Statistics();
$sourses = $stats->getEventSources();
$sourse = $request->get('source');
if (is_numeric($sourse)) {
$sourses = array_flip($sourses);
if (!isset($sourses[$sourse])) throw new EventsException('Error source');
$stats->setEventSource($sourse);
} else if (!isset($sourses[$sourse])) {
throw new EventsException('Error source!');
}
else $stats->setEventSource($sourses[$sourse]);
$actions = $stats->getEventActions();
$action = $request->get('action');
if (is_numeric($action)) {
$actions = array_flip($actions);
if (!isset($actions[$action])) throw new EventsException('Error action');
$stats->setEventAction($action);
} else if (!isset($actions[$action])) {
throw new EventsException('Error action!');
}
else $stats->setEventAction($actions[$action]);
$res = array();
if (empty((int)$request->get('id'))) {
throw new EventsException('Error id');
}
if (empty((int)$request->get('countryId'))) {
throw new EventsException('Error country');
}
if ($stats->getEventAction()!=3 && empty((int)$request->get('operatorId'))) {
throw new EventsException('Error operator: '.$stats->getEventAction());
}
$stats->setId((int)$request->get('id'));
if ($request->get('operatorId')) {
$stats->setOperatorId((int)$request->get('operatorId'));
}
if (in_array($stats->getEventAction(), [5,6])) {
if (empty($request->get('operatorId'))){
throw new EventsException('Error operator');
}
$stats->setCountryId((int)$request->get('countryId'));
} elseif ($stats->getEventAction() == 4) {
if (empty($request->get('operatorId'))){
throw new EventsException('Error operator');
}
$cruise = $statisticsService->getCruiseCountry($stats);
/** @var $cruise Cruises */
if (!$cruise || empty($cruise->getOperatorId())) {
throw new EventsException('Error operator.');
}
if ((int)$request->get('operatorId') != $cruise->getOperatorId()){
throw new EventsException('Error operator!');
}
$stats->setCountryId((int)$request->get('countryId'));
} else {
if ((int)$request->get('id')<5000 || (int)$request->get('id')>999999) {
throw new EventsException('Error hotel');
}
if ($stats->getEventAction()!=3) {
$operator = $statisticsService->getOperator($stats);
/** @var $operator Operators */
if (!$operator || empty($operator->getId())) {
throw new EventsException('Error operator: '.$stats->getEventAction());
}
}
$geo = $statisticsService->getHotelCountry($stats);
if (!isset($geo['hotel'])) {
throw new EventsException('Error hotel.');
}
if (!isset($geo['city'])) {
throw new EventsException('Error city.');
}
if (!isset($geo['country'])) {
throw new EventsException('Error country.');
}
/** @var $hotel Hotels */
$hotel = $geo['hotel'];
if (!$hotel || empty($hotel->getId())) {
throw new EventsException('Error hotel!');
}
$stats->setId($hotel->getId());
/** @var $city City */
$city = $geo['city'];
if (!$city || empty($city->getId())) {
throw new EventsException('Error city!');
}
//$stats->setCityId($city->getId());
/** @var $country Country */
$country = $geo['country'];
if (!$country || empty($country->getId())) {
throw new EventsException('Error country!');
}
if ((int)$request->get('countryId') != $country->getId()){
throw new EventsException('Error country');
}
$stats->setCountryId($country->getId());
}
if ($request->get('formCity')) $stats->setFromCityId((int)$request->get('formCity'));
if ($request->get('checkIn')) {
if (strtotime($request->get('checkIn')) < time() || strtotime($request->get('checkIn'))>mktime(0,0,0,date('m'),date('d'),date('Y')+2)){
throw new EventsException('Error checkIn');
}
$stats->setCheckIn(new \DateTime($request->get('checkIn')));
}
else $stats->setCheckIn(new \DateTime('2000-01-01'));
if ($request->get('nights')) {
if ($request->get('nights')<0 || $request->get('nights')>33) {
throw new EventsException('Error nights');
}
$stats->setNights((int)$request->get('nights'));
}
if ((int)$request->get('fromCity')) $stats->setFromCityId((int)$request->get('fromCity'));
if ($request->get('food') && in_array($request->get('food'),['ob','bb','hb','fb','ai','uai'])) $stats->setFood($request->get('food'));
if ($request->get('transport') && in_array($request->get('transport'),['no','air','bus','train','trainbus','ship'])) $stats->setTransport($request->get('transport'));
if ((int)$request->get('roomId')) $stats->setRoomId((int)$request->get('roomId'));
if ($request->get('price')) {
if ((float)$request->get('price')<0) {
throw new EventsException('Error price');
}
//$stats->setPrice($request->get('price'));
$stats->setEventPrice($request->get('price'));
}
if ($request->get('people')>0) {
$stats->setPeople((int)$request->get('people'));
} elseif ($request->get('offerId')) {
$people = (int)substr($request->get('offerId'), 16);
header('X-people: ' . $people);
$stats->setPeople((int)$people);
}
if ($request->get('cid')) $stats->setCid($request->get('cid'));
$stats->setIp(Ipv6Service::ip2long($request->getClientIp()));
$stats->setEventDate(new \DateTime());
$stats->setEventTime(new \DateTime());
if ((int)$request->get('position')) {
if ($request->get('position')<0) {
throw new EventsException('Error position');
}
$stats->setEventPosition((int)$request->get('position', 0));
}
//dump($stats->_toArray());
$price = $statisticsService->saveStatistics($stats);
if (isset($_COOKIE['DM']) || $kernel->isDebug()) {
$res['_stats'] = $stats ? $stats->_toArray() : null;
}
return new JsonResponse([
'status' => 'success',
//'price' => $price,
] + $res);
} catch (EventsException $e) {
return new JsonResponse([
'status' => 'error',
'message' => $e->getMessage(),
], 400);
}
return new JsonResponse([
'status' => 'error',
'text' => 'Запрос не отправлен'
], 400);
}
}