src/Controller/EventsController.php line 153

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\EntityOtpusk\Cruises;
  4. use App\EntityOtpusk\Events;
  5. use App\EntityOtpusk\Operators;
  6. use App\EntityOtpusk\Statistics;
  7. use App\EntityOtpusk\Thotels AS Hotels;
  8. use App\EntityOtpusk\Tcities as City;
  9. use App\EntityOtpusk\Tcountries as Country;
  10. use App\Repository\AdvertsRepository;
  11. use App\Repository\ManagerLogsRepository;
  12. use App\Repository\StatisticsRepository;
  13. use App\Service\EventsService;
  14. use App\Service\StatisticsService;
  15. use App\Service\Ipv6Service;
  16. use App\Exception\EventsException;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpKernel\KernelInterface;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\Validator\Constraints\Country as Countries;
  23. class EventsController extends AbstractController
  24. {
  25.     /**
  26.      * @Route("/api/events", name="event", methods={"GET"})
  27.      * @param Request               $request
  28.      * @param KernelInterface       $kernel
  29.      * @param AdvertsRepository     $advertsRepository
  30.      * @param ManagerLogsRepository $managerLogsRepository
  31.      * @param EventsService         $eventsService
  32.      * @return JsonResponse
  33.      */
  34.     public function eventsAdd(
  35.         Request $request,
  36.         KernelInterface $kernel,
  37.         AdvertsRepository $advertsRepository,
  38.         ManagerLogsRepository $managerLogsRepository,
  39.         EventsService $eventsService
  40.     )
  41.     {
  42.         try {
  43.             if (empty($request->get('offerId'))) {
  44.                 throw new EventsException('Empty offerId');
  45.             }
  46.             if (empty($request->get('advertId'))) {
  47.                 throw new EventsException('Empty advertId');
  48.             }
  49.             $advertInfo $advertsRepository->findOneBy(['id' => $request->get('advertId')]);
  50.             if (!$advertInfo) {
  51.                 throw new EventsException('No found advertId');
  52.             }
  53.             if (!$advertInfo->getAgency()) {
  54.                 throw new EventsException('No found agency');
  55.             }
  56.             if (empty($request->get('regionId'))) {
  57.                 throw new EventsException('Empty regionId');
  58.             }
  59.             if (empty($request->get('officeId'))) {
  60.                 throw new EventsException('Empty officeId');
  61.             }
  62.             if (empty($request->get('event'))) {
  63.                 throw new EventsException('Empty event');
  64.             }
  65.             $cruiseId $request->get('cruiseId');
  66.             if ($cruiseId 0) {
  67.                 $action 'cruise';
  68.             } else {
  69.                 $action 'tour';
  70.             }
  71.             $event = new Events();
  72.             $event->setEventTypeName($request->get('event'));
  73.             header('x-event: '.$request->get('event').'='.$event->getEventType());
  74.             if (is_null($event->getEventType())) {
  75.                 throw new EventsException('No valid event');
  76.             }
  77.             $event->setEventAction($action);
  78.             //$event->setHotelId($request->get('hotelId'));
  79.             $event->setCruiseId($request->get('cruiseId'0));
  80.             $event->setAdvertId($request->get('advertId'));
  81.             $advert $advertsRepository->find($request->get('advertId'));
  82.             $agencyId $advert->getAgency()->getId();
  83.             $event->setAgencyId($request->get('agencyId'$agencyId)); // не обязательно, если что найдет из advertId
  84.             $event->setRegionId($request->get('regionId'));
  85.             $offerId $request->get('offerId');
  86.             $people = (int)substr($offerId16);
  87.             if (empty($people)) $people $request->get('people');
  88.             $offerId = (int)substr($offerId0,16);
  89.             header('X-offerId: '.$offerId);
  90.             header('X-people: '.$people);
  91.             $event->setOfferId($offerId);
  92.             $event->setCid($request->get('cid'));
  93.             $event->setOperatorId($request->get('operatorId'0)); // не обязательно, если что найдет из offerId
  94.             $event->setOfficeId($request->get('officeId'));
  95.             $event->setPeople($request->get('people'$people));
  96.             header('X-ip: '.$request->getClientIp());
  97.             $event->setIp(Ipv6Service::ip2long($request->getClientIp()));
  98.             $event->setEventDate(new \DateTime());
  99.             $event->setEventPosition($request->get('eventPosition'0)); // не обязательно
  100.             $price $eventsService->saveEvent($event$request->get('event'));
  101.             header('X-eventPrice: '.$event->getEventPrice());
  102.             header('X-eventPosition: '.$event->getEventPosition());
  103.             $res = array();
  104.             if (isset($_COOKIE['DM']) || $kernel->isDebug()) {
  105.                 $res['_event'] = $event $event->_toArray() : null;
  106.             }
  107.             if (isset($_COOKIE['DM']) || $kernel->isDebug()) {
  108.                 $res['_price'] = $price;
  109.             }
  110.             $eventsService->sendGA($request->get('cid'), $request->get('sid'), $event$request->get('event'));
  111.             //$price = 0;
  112.             return new JsonResponse([
  113.                     'status' => 'success',
  114.                     //'price'  => $price,
  115.                 ] + $res);
  116.         } catch (EventsException $e) {
  117.             return new JsonResponse([
  118.                 'status'  => 'error',
  119.                 'message' => $e->getMessage(),
  120.             ]);
  121.         }
  122.         return new JsonResponse([
  123.             'status' => 'error',
  124.             'text'   => 'Запрос не отправлен'
  125.         ]);
  126.     }
  127.     /**
  128.      * @Route("/api/stats", name="stats", methods={"GET"})
  129.      * @param Request               $request
  130.      * @param KernelInterface       $kernel
  131.      * @param ManagerLogsRepository $managerLogsRepository
  132.      * @param StatisticsRepository  $statisticsRepository
  133.      * @param StatisticsService     $statisticsService
  134.      * @return JsonResponse
  135.      */
  136.     public function statsAdd(
  137.         Request $request,
  138.         KernelInterface $kernel,
  139.         ManagerLogsRepository $managerLogsRepository,
  140.         StatisticsRepository $statisticsRepository,
  141.         StatisticsService $statisticsService
  142.     )
  143.     {
  144.         try {
  145.             if (empty($request->get('source'))) {
  146.                 throw new EventsException('Empty source');
  147.             }
  148.             if (empty($request->get('action'))) {
  149.                 throw new EventsException('Empty action');
  150.             }
  151.             if (empty($request->get('id'))) {
  152.                 throw new EventsException('Empty id');
  153.             }
  154.             $stats = new Statistics();
  155.             $sourses $stats->getEventSources();
  156.             $sourse $request->get('source');
  157.             if (is_numeric($sourse)) {
  158.                 $sourses array_flip($sourses);
  159.                 if (!isset($sourses[$sourse])) throw new EventsException('Error source');
  160.                 $stats->setEventSource($sourse);
  161.             } else if (!isset($sourses[$sourse])) {
  162.                 throw new EventsException('Error source!');
  163.             }
  164.             else $stats->setEventSource($sourses[$sourse]);
  165.             $actions $stats->getEventActions();
  166.             $action $request->get('action');
  167.             if (is_numeric($action)) {
  168.                 $actions array_flip($actions);
  169.                 if (!isset($actions[$action])) throw new EventsException('Error action');
  170.                 $stats->setEventAction($action);
  171.             } else if (!isset($actions[$action])) {
  172.                 throw new EventsException('Error action!');
  173.             }
  174.             else $stats->setEventAction($actions[$action]);
  175.             $res = array();
  176.             if (empty((int)$request->get('id'))) {
  177.                 throw new EventsException('Error id');
  178.             }
  179.             if (empty((int)$request->get('countryId'))) {
  180.                 throw new EventsException('Error country');
  181.             }
  182.             if ($stats->getEventAction()!=&& empty((int)$request->get('operatorId'))) {
  183.                 throw new EventsException('Error operator: '.$stats->getEventAction());
  184.             }
  185.             $stats->setId((int)$request->get('id'));
  186.             if ($request->get('operatorId')) {
  187.                 $stats->setOperatorId((int)$request->get('operatorId'));
  188.             }
  189.             if (in_array($stats->getEventAction(), [5,6])) {
  190.                 if (empty($request->get('operatorId'))){
  191.                     throw new EventsException('Error operator');
  192.                 }
  193.                 $stats->setCountryId((int)$request->get('countryId'));
  194.             } elseif ($stats->getEventAction() == 4) {
  195.                 if (empty($request->get('operatorId'))){
  196.                     throw new EventsException('Error operator');
  197.                 }
  198.                 $cruise $statisticsService->getCruiseCountry($stats);
  199.                 /** @var $cruise Cruises */
  200.                 if (!$cruise || empty($cruise->getOperatorId())) {
  201.                     throw new EventsException('Error operator.');
  202.                 }
  203.                 if ((int)$request->get('operatorId') != $cruise->getOperatorId()){
  204.                     throw new EventsException('Error operator!');
  205.                 }
  206.                 $stats->setCountryId((int)$request->get('countryId'));
  207.             } else {
  208.                 if ((int)$request->get('id')<5000 || (int)$request->get('id')>999999) {
  209.                     throw new EventsException('Error hotel');
  210.                 }
  211.                 if ($stats->getEventAction()!=3) {
  212.                     $operator $statisticsService->getOperator($stats);
  213.                     /** @var $operator Operators */
  214.                     if (!$operator || empty($operator->getId())) {
  215.                         throw new EventsException('Error operator: '.$stats->getEventAction());
  216.                     }
  217.                 }
  218.                 $geo $statisticsService->getHotelCountry($stats);
  219.                 if (!isset($geo['hotel'])) {
  220.                     throw new EventsException('Error hotel.');
  221.                 }
  222.                 if (!isset($geo['city'])) {
  223.                     throw new EventsException('Error city.');
  224.                 }
  225.                 if (!isset($geo['country'])) {
  226.                     throw new EventsException('Error country.');
  227.                 }
  228.                 /** @var $hotel Hotels */
  229.                 $hotel $geo['hotel'];
  230.                 if (!$hotel || empty($hotel->getId())) {
  231.                     throw new EventsException('Error hotel!');
  232.                 }
  233.                 $stats->setId($hotel->getId());
  234.                 /** @var $city City */
  235.                 $city $geo['city'];
  236.                 if (!$city || empty($city->getId())) {
  237.                     throw new EventsException('Error city!');
  238.                 }
  239.                 //$stats->setCityId($city->getId());
  240.                 /** @var $country Country */
  241.                 $country $geo['country'];
  242.                 if (!$country || empty($country->getId())) {
  243.                     throw new EventsException('Error country!');
  244.                 }
  245.                 if ((int)$request->get('countryId') != $country->getId()){
  246.                     throw new EventsException('Error country');
  247.                 }
  248.                 $stats->setCountryId($country->getId());
  249.             }
  250.             if ($request->get('formCity')) $stats->setFromCityId((int)$request->get('formCity'));
  251.             if ($request->get('checkIn')) {
  252.                 if (strtotime($request->get('checkIn')) < time() || strtotime($request->get('checkIn'))>mktime(0,0,0,date('m'),date('d'),date('Y')+2)){
  253.                     throw new EventsException('Error checkIn');
  254.                 }
  255.                 $stats->setCheckIn(new \DateTime($request->get('checkIn')));
  256.             }
  257.             else $stats->setCheckIn(new \DateTime('2000-01-01'));
  258.             if ($request->get('nights')) {
  259.                 if ($request->get('nights')<|| $request->get('nights')>33) {
  260.                     throw new EventsException('Error nights');
  261.                 }
  262.                 $stats->setNights((int)$request->get('nights'));
  263.             }
  264.             if ((int)$request->get('fromCity')) $stats->setFromCityId((int)$request->get('fromCity'));
  265.             if ($request->get('food') && in_array($request->get('food'),['ob','bb','hb','fb','ai','uai'])) $stats->setFood($request->get('food'));
  266.             if ($request->get('transport') && in_array($request->get('transport'),['no','air','bus','train','trainbus','ship'])) $stats->setTransport($request->get('transport'));
  267.             if ((int)$request->get('roomId')) $stats->setRoomId((int)$request->get('roomId'));
  268.             if ($request->get('price')) {
  269.                 if ((float)$request->get('price')<0) {
  270.                     throw new EventsException('Error price');
  271.                 }
  272.                 //$stats->setPrice($request->get('price'));
  273.                 $stats->setEventPrice($request->get('price'));
  274.             }
  275.             if ($request->get('people')>0) {
  276.                 $stats->setPeople((int)$request->get('people'));
  277.             } elseif ($request->get('offerId')) {
  278.                 $people = (int)substr($request->get('offerId'), 16);
  279.                 header('X-people: ' $people);
  280.                 $stats->setPeople((int)$people);
  281.             }
  282.             if ($request->get('cid')) $stats->setCid($request->get('cid'));
  283.             $stats->setIp(Ipv6Service::ip2long($request->getClientIp()));
  284.             $stats->setEventDate(new \DateTime());
  285.             $stats->setEventTime(new \DateTime());
  286.             if ((int)$request->get('position')) {
  287.                 if ($request->get('position')<0) {
  288.                     throw new EventsException('Error position');
  289.                 }
  290.                 $stats->setEventPosition((int)$request->get('position'0));
  291.             }
  292.             //dump($stats->_toArray());
  293.             $price $statisticsService->saveStatistics($stats);
  294.             if (isset($_COOKIE['DM']) || $kernel->isDebug()) {
  295.                 $res['_stats'] = $stats $stats->_toArray() : null;
  296.             }
  297.             return new JsonResponse([
  298.                     'status' => 'success',
  299.                     //'price'  => $price,
  300.                 ] + $res);
  301.         } catch (EventsException $e) {
  302.             return new JsonResponse([
  303.                 'status'  => 'error',
  304.                 'message' => $e->getMessage(),
  305.             ], 400);
  306.         }
  307.         return new JsonResponse([
  308.             'status' => 'error',
  309.             'text'   => 'Запрос не отправлен'
  310.         ], 400);
  311.     }
  312. }