src/Controller/MapController.php line 61

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