src/Controller/MapController.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Location\City;
  4. use App\Entity\Profile\Genders;
  5. use App\Entity\Saloon\Saloon;
  6. use App\Event\Profile\ProfilesShownEvent;
  7. use App\Form\FilterMapForm;
  8. use App\Repository\CityRepository;
  9. use App\Repository\ProfileRepository;
  10. use App\Repository\ReadModel\ProfileMapReadModel;
  11. use App\Repository\SaloonRepository;
  12. use App\Repository\ServiceRepository;
  13. use App\Service\Features;
  14. use App\Service\ProfileList;
  15. use App\Specification\Profile\ProfileHasMapCoordinates;
  16. use App\Specification\Profile\ProfileIsSuitableForTheMap;
  17. use App\Specification\Profile\ProfileIdINOrderedByINValues;
  18. use App\Specification\Profile\ProfileIsLocated;
  19. use App\Specification\QueryModifier\PossibleSaloonAdBoardPlacement;
  20. use App\Specification\QueryModifier\PossibleSaloonPlacementHiding;
  21. use App\Specification\Saloon\SaloonIsNotHidden;
  22. use App\Specification\QueryModifier\SaloonThumbnail;
  23. use App\Specification\Saloon\SaloonIsActive;
  24. use App\Specification\Saloon\SaloonIsSuitableForTheMap;
  25. use Happyr\DoctrineSpecification\Spec;
  26. use Psr\Cache\CacheItemPoolInterface;
  27. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  28. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  29. use Symfony\Component\Asset\Packages;
  30. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  31. use Symfony\Component\HttpFoundation\JsonResponse;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use Symfony\Component\HttpFoundation\Response;
  34. use Symfony\Contracts\Cache\ItemInterface;
  35. use Symfony\Contracts\Translation\TranslatorInterface;
  36. class MapController extends AbstractController
  37. {
  38.     use ProfileMinPriceTrait;
  39.     const MAP_PROFILES_CACHE_ITEM_NAME 'map_profiles_';
  40.     public function __construct(
  41.         private ProfileRepository $profileRepository,
  42.         private CityRepository $cityRepository,
  43.         private Features $features,
  44.         private Packages $assetPackage,
  45.         private SaloonRepository $saloonRepository,
  46.         private ProfileList $profileList,
  47.         private EventDispatcherInterface $eventDispatcher,
  48.         private ServiceRepository $serviceRepository,
  49.         private CacheItemPoolInterface $profilesFilterCache,
  50.     ) {}
  51.     #[ParamConverter("city"converter"city_converter")]
  52.     public function page(City $city): Response
  53.     {
  54.         return $this->render('Map/page.html.twig', [
  55.             'cityUriIdentity' => $city->getUriIdentity(),
  56.             'cityLatitude' => $city->getMapCoordinate()->getLatitude(),
  57.             'cityLongitude' => $city->getMapCoordinate()->getLongitude(),
  58.             'multipleCities' => (int)$this->features->multiple_cities(),
  59.         ]);
  60.     }
  61.     public function form(City $city): Response
  62.     {
  63.         $form $this->createForm(FilterMapForm::class, null, ['data' => ['city_id' => $city->getId()]]);
  64.         return $this->render('Map/form.html.twig', [
  65.             'form' => $form->createView(),
  66.         ]);
  67.     }
  68.     public function filter(Request $requestTranslatorInterface $translator): Response
  69.     {
  70.         $params json_decode($request->request->get('form'), true);
  71.         $form $this->createForm(FilterMapForm::class);
  72.         $form->submit($params);
  73.         $scale $request->request->get('scale') ?? 0;
  74.         if($scale <= 8) {
  75.             $coordsRoundPrecision 1;
  76.         } else if($scale <= 14) {
  77.             $coordsRoundPrecision 2;
  78.         } else {
  79.             $coordsRoundPrecision 4;
  80.         }
  81.         $city $this->cityRepository->find($params['city_id']);
  82.         $profiles $this->profileList->listForMap(
  83.             $citynull$form->getData(), [
  84.                 new ProfileHasMapCoordinates(),
  85.                 new ProfileIsSuitableForTheMap(),
  86.             ], truenull,
  87.             ProfileList::ORDER_NONE, [Genders::FEMALE], $coordsRoundPrecision,
  88.         );
  89.         $specs Spec::andX(
  90.             new SaloonIsSuitableForTheMap(),
  91.             new SaloonThumbnail(),
  92.             ProfileIsLocated::withinCity($city),
  93.             new ProfileHasMapCoordinates(),
  94.         );
  95.         $saloons $this->saloonRepository->listForMapMatchingSpec($specs$coordsRoundPrecision);
  96.         $rowConverter = function($row) {
  97.             $row array_values($row);
  98.             //id
  99.             $row[0] = array_map('intval'explode(','$row[0]));
  100.             //coords
  101.             $row[3] = array_map('floatval'explode(','$row[$row[2] == 3]));
  102.             //is_masseur
  103.             if(isset($row[4])) {
  104.                 $row[4] = array_map('intval'explode(','$row[4]));
  105.             }
  106.             array_splice($row11);
  107.             return $row;
  108.         };
  109.         $out = [
  110.             'profiles' => array_map($rowConverter$profiles),
  111.             'saloons' => array_map($rowConverter$saloons),
  112.         ];
  113.         return $this->json($out);
  114.     }
  115.     public function detail(Request $requestTranslatorInterface $translator): Response
  116.     {
  117.         $services $this->serviceRepository->allIndexedById();
  118.         $profileIds = ($requestedProfiles $request->request->get('profiles')) ? explode(','$requestedProfiles) : [];
  119.         $saloonIds = ($requestedSaloons $request->request->get('saloons')) ? explode(','$requestedSaloons) : [];
  120.         $result = !empty($profileIds) ? $this->profileRepository->fetchMapProfilesByIds(new ProfileIdINOrderedByINValues($profileIds)) : [];
  121.         $profiles = [];
  122.         foreach ($result as /** @var ProfileMapReadModel $profile */$profile) {
  123.             if(!$profile->mapLatitude || !$profile->mapLongitude)
  124.                 continue;
  125.             $path $profile->avatar['path'];
  126.             $path str_starts_with($path'/') ? $path substr($path6, -4);
  127.             $hasApartment $profile->apartmentOneHourPrice || $profile->apartmentTwoHoursPrice || $profile->apartmentNightPrice;
  128.             $hasTakeout $profile->takeOutOneHourPrice || $profile->takeOutTwoHoursPrice || $profile->takeOutNightPrice;
  129.             $tags = [];
  130.             if($hasApartment && !$hasTakeout)
  131.                 $tags[] = 1;
  132.             elseif(!$hasApartment && $hasTakeout)
  133.                 $tags[] = 2;
  134.             elseif ($hasApartment && $hasTakeout)
  135.                 $tags[] = 3;
  136.             foreach ($profile->services as $serviceId) {
  137.                 $serviceName mb_strtolower($services[$serviceId]->getName()->getTranslation('ru'));
  138.                 switch($serviceName) {
  139.                     case 'секс классический'$tags[] = 4; break;
  140.                     case 'секс анальный'$tags[] = 5; break;
  141.                     case 'минет без резинки'$tags[] = 6; break;
  142.                     case 'куннилингус'$tags[] = 7; break;
  143.                     case 'окончание в рот'$tags[] = 8; break;
  144.                     case 'массаж': if(false === in_array(9$tags)) $tags[] = 9; break;
  145.                 }
  146.             }
  147.             $profiles[] = [
  148.                 1,
  149.                 (float)rtrim(substr($profile->mapLatitude07), '0'),
  150.                 (float)rtrim(substr($profile->mapLongitude07), '0'),
  151.                 $profile->uriIdentity,
  152.                 $profile->name,
  153.                 $path//$profile->avatar['path'],
  154.                 //$profile->avatar['type'] ? 'avatar' : 'photo',
  155.                 str_replace(' '''$profile->phoneNumber),
  156.                 $profile->station ?? 0,
  157.                 $profile->apartmentOneHourPrice ?? $profile->takeOutOneHourPrice ?? 0,
  158.                 $profile->apartmentTwoHoursPrice ?? $profile->takeOutTwoHoursPrice ?? 0,
  159.                 $profile->apartmentNightPrice ?? $profile->takeOutNightPrice ?? 0,
  160.                 (int)$profile->isApproved,
  161.                 (int)$profile->isMasseur,
  162.                 (int)$profile->hasComments,
  163.                 (int)$profile->hasSelfies,
  164.                 (int)$profile->hasVideos,
  165.                 $profile->age ?? 0,
  166.                 $profile->breastSize ?? 0,
  167.                 $profile->height ?? 0,
  168.                 $profile->weight ?? 0,
  169.                 $tags,
  170.                 $profile->id,
  171.                 (int)$profile->isPaid ?? 0,
  172.             ];
  173.         }
  174.         $result = !empty($saloonIds) ? $this->saloonRepository->matchingSpecRaw(new ProfileIdINOrderedByINValues($saloonIds), nullfalse) : [];
  175.         $saloons = [];
  176.         foreach ($result as /** @var Saloon $saloon */ $saloon) {
  177.             $photoPath null !== ($mainPhoto $saloon->getThumbnail()) ? $mainPhoto->getPath() : '';
  178.             $photoPath str_starts_with($photoPath'/') ? $photoPath str_replace(".jpg"""substr($photoPath6));
  179.             $saloons[] = [
  180.                 2,
  181.                 (float)rtrim(substr($saloon->getMapCoordinate()->getLatitude(), 07), '0'),
  182.                 (float)rtrim(substr($saloon->getMapCoordinate()->getLongitude(), 07), '0'),
  183.                 $saloon->getUriIdentity(),
  184.                 $translator->trans($saloon->getName()),
  185.                 $photoPath,
  186.                 //'thumb',
  187.                 str_replace(' '''$saloon->getPhoneNumber()),
  188.                 $saloon->getStations()->count() ? $saloon->getStations()->first()->getId() : 0,
  189.                 $saloon->getApartmentsPricing()->getOneHourPrice() ?? $saloon->getTakeOutPricing()->getOneHourPrice() ?? 0,
  190.                 $saloon->getApartmentsPricing()->getTwoHoursPrice() ?? $saloon->getTakeOutPricing()->getTwoHoursPrice() ?? 0,
  191.                 $saloon->getApartmentsPricing()->getNightPrice() ?? $saloon->getTakeOutPricing()->getNightPrice() ?? 0,
  192.                 //$saloon->getExpressPricing()->isProvided() ? $saloon->getExpressPricing()->getPrice() ?? 0 : 0,
  193.                 (int)($saloon?->getAdBoardPlacement() !== null),
  194.                 $saloon->getId(),
  195.             ];
  196.         }
  197.         return new JsonResponse([
  198.             'profiles' => $profiles,
  199.             'saloons' => $saloons,
  200.         ]);
  201.     }
  202.     public function processProfileShows(Request $requestProfileRepository $profileRepository): JsonResponse
  203.     {
  204.         $id $request->query->get('id');
  205.         $profile $profileRepository->find($id);
  206.         if($profile) {
  207.             $this->eventDispatcher->dispatch(new ProfilesShownEvent([$profile->getId()], 'map'), ProfilesShownEvent::NAME);
  208.         }
  209.         return $this->json([]);
  210.     }
  211.     public function cachedMapProfilesResultByIds(ProfileIdINOrderedByINValues $specification)
  212.     {
  213.         $key sha1(self::MAP_PROFILES_CACHE_ITEM_NAME implode(','$specification->getIds()));
  214.         return $this->profilesFilterCache->get($key, function (ItemInterface $item) use ($specification) {
  215.             return $this->profileRepository->fetchMapProfilesByIds($specification);
  216.         });
  217.     }
  218. }