src/Controller/MapController.php line 60

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