src/Controller/MapController.php line 57

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