src/Network/Controller/NetworkProfileController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Network\Controller;
  3. use App\Auction\Form\Type\AuctionDetailFilterType;
  4. use App\Horse\Service\HorseService;
  5. use App\Network\Service\CropService;
  6. use App\Network\Service\NetworkProfileService;
  7. use App\Profile\Manager\ProfileManager;
  8. use App\Profile\Model\Profile;
  9. use App\Profile\Service\LoadMoreService;
  10. use App\Profile\Service\ProfileService;
  11. use App\Raceday\Form\Type\RacedayDetailFilterType;
  12. use Carbon\Carbon;
  13. use Doctrine\DBAL\Query\QueryBuilder;
  14. use Knp\Component\Pager\PaginatorInterface;
  15. use Pimcore\Controller\FrontendController;
  16. use Pimcore\Model\DataObject\Auction;
  17. use Pimcore\Model\DataObject\Profile as DataObjectProfile;
  18. use Pimcore\Model\DataObject\Raceday;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. #[Route([
  23.   'name' => 'network_profile_',
  24.   'localizedPaths' => [
  25.     'en' => '/{_locale}/network/profile',
  26.     'de' => '/{_locale}/netzwerk/profil',
  27.     'fr' => '/{_locale}/reseau/profil',
  28.   ],
  29. ])]
  30. class NetworkProfileController extends FrontendController
  31. {
  32.   #[Route('/{username}'name'detail')]
  33.   public function detail($usernameHorseService $horseServiceProfileService $profileServiceNetworkProfileService $networkProfileServiceRequest $request): Response
  34.   {
  35.     $profile Profile::getByUsername(rawurldecode($username), 1);
  36.     if (!$profile) {
  37.       throw $this->createNotFoundException('profile-not-found');
  38.     }
  39.     $subprofileKeys $profileService->getActiveSubProfileIdentifiers($profile);
  40.     $user $this->getUser();
  41.     $horses $networkProfileService->getAssociatedHorsesByProfile($subprofileKeys$profile);
  42.     $auctionEvents $networkProfileService->getFutureAuctionsByProfile($profile);
  43.     $raceEvents $networkProfileService->getFutureRacedaysByProfile($profile);
  44.     $marketplacePosts $networkProfileService->getLatestMarketplacePostsByProfile($profile);
  45.     $achievementCategories $networkProfileService->getAchievementCategoriesByProfile($profile);
  46.     $horseRacedays Raceday::getList();
  47.     $horseRacedays->onCreateQueryBuilder(function (QueryBuilder $queryBuilder) use ($profile) {
  48.       $queryBuilder->join('object_raceday''object_horse''horse''horses LIKE CONCAT("%", `horse`.`oo_id` , "%")');
  49.       $queryBuilder->where('object_raceday.date >= "'.Carbon::now()->format('Y-m-d').'" AND object_raceday.horses IS NOT NULL AND (horse.racingmanager__id ='.$profile->getId().' OR horse.jockey__id ='.$profile->getId().' OR horse.owner__id ='.$profile->getId().' OR horse.breeder__id ='.$profile->getId().' OR horse.trainer__id ='.$profile->getId().')');
  50.       $queryBuilder->orderBy('object_raceday.date''ASC');
  51.       $queryBuilder->distinct();
  52.       $queryBuilder->setMaxResults(3);
  53.     });
  54.     $horseAuctions Auction::getList();
  55.     $horseAuctions->onCreateQueryBuilder(function (QueryBuilder $queryBuilder) use ($profile) {
  56.       $queryBuilder->innerJoin('object_auction''object_horse''horse''horses LIKE CONCAT("%", `horse`.`oo_id` , "%")');
  57.       $queryBuilder->where('end >="'.Carbon::now()->format('Y-m-d').'" AND horses IS NOT NULL AND (horse.racingmanager__id ='.$profile->getId().' OR horse.jockey__id ='.$profile->getId().' OR horse.owner__id ='.$profile->getId().' OR horse.breeder__id ='.$profile->getId().' OR horse.trainer__id ='.$profile->getId().')');
  58.       $queryBuilder->distinct();
  59.       $queryBuilder->setMaxResults(3);
  60.     });
  61.     // dd($horseAuctions->load());
  62.     return $this->renderForm('network/profile/detail.html.twig', [
  63.       'profile' => $profile,
  64.       'user' => $user,
  65.       'highlightedPost' => $request->query->get('highlightedPost'),
  66.       'achievementCategories' => $achievementCategories,
  67.       'horses' => $horses,
  68.       'raceEvents' => $raceEvents,
  69.       'auctionEvents' => $auctionEvents,
  70.       'marketplacePosts' => $marketplacePosts,
  71.       'horseEvents' => ['auctions' => $horseAuctions'racedays' => $horseRacedays],
  72.     ]);
  73.   }
  74.   #[Route('/{id}/connect'name'connect')]
  75.   public function connect(Request $request$idProfileManager $profileManager)
  76.   {
  77.     /** @var Profile */
  78.     $user $this->getUser();
  79.     $profile Profile::getById($id);
  80.     $form $this->createFormBuilder()->getForm();
  81.     $form->handleRequest($request);
  82.     if ($form->isSubmitted() && $form->isValid()) {
  83.       $profileManager->toggleNetworkProfile($user$profile);
  84.     }
  85.     $request->setRequestFormat('text/html');
  86.     return $this->renderForm('network/profile/components/connect.html.twig', [
  87.       'isInNetwork' => $profileManager->isProfileInNetwork($user$profile),
  88.       'profile' => $profile,
  89.       'form' => $form,
  90.     ]);
  91.   }
  92.   #[Route('/{id}/bred/horse-list'name'bred_horse_list')]
  93.   public function bredHorseListAction(Request $requestHorseService $horseServiceProfileService $profileServiceLoadMoreService $loadMoreService$id)
  94.   {
  95.     $profile Profile::getById($id);
  96.     $subprofileKeys $profileService->getActiveSubProfileIdentifiers($profile);
  97.     $user $this->getUser();
  98.     $bredHorses = [];
  99.     if (in_array('BreederProfile'$subprofileKeys)) {
  100.       $bredHorses $horseService->getBredHorsesForProfile($profile);
  101.       $bredHorses->setOrderKey('birthyear');
  102.       $bredHorses->setOrder('desc');
  103.     }
  104.     $context $loadMoreService->generate(
  105.       $request,
  106.       'bred_list',
  107.       $bredHorses,
  108.       'network/profile/sections/profile_section_bred_horses.html.twig',
  109.       [
  110.         'profile' => $profile,
  111.         'user' => $user,
  112.       ]
  113.     );
  114.     return $this->renderForm(...$context);
  115.   }
  116.   #[Route('/{id}/trained/horse-list'name'trained_horse_list')]
  117.   public function trainedHorseListAction(Request $requestHorseService $horseServiceProfileService $profileService$idLoadMoreService $loadMoreService)
  118.   {
  119.     $profile Profile::getById($id);
  120.     $subprofileKeys $profileService->getActiveSubProfileIdentifiers($profile);
  121.     $user $this->getUser();
  122.     $trainedHorses = [];
  123.     if (in_array('TrainerProfile'$subprofileKeys)) {
  124.       $trainedHorses $horseService->getTrainedHorsesForProfile($profile);
  125.       $trainedHorses->setOrderKey('birthyear');
  126.       $trainedHorses->setOrder('desc');
  127.     }
  128.     $uniqueHorseOwner = [];
  129.     foreach ($trainedHorses as $key => $value) {
  130.       if (!in_array($value->getOwner(), $uniqueHorseOwnertrue)) {
  131.         $uniqueHorseOwner[] = $value->getOwner();
  132.       }
  133.     }
  134.     $uniqueHorseOwner array_filter($uniqueHorseOwner, function ($owner) {
  135.       if ($owner != null) {
  136.         return $owner::class === Profile::class;
  137.       } else {
  138.         return $owner === null;
  139.       }
  140.     });
  141.     $context $loadMoreService->generate(
  142.       $request,
  143.       'trained_list',
  144.       $trainedHorses,
  145.       'network/profile/sections/profile_section_trained_horses.html.twig',
  146.       [
  147.         'profile' => $profile,
  148.         'user' => $user,
  149.         'uniqueHorseOwner' => $uniqueHorseOwner,
  150.       ]
  151.     );
  152.     return $this->renderForm(...$context);
  153.   }
  154.   #[Route('/{id}/trained/horse-list/owner'name'trained_horse_trainer_list')]
  155.   public function uniqueOwnerAction(Request $requestLoadMoreService $loadMoreServiceHorseService $horseServiceProfileService $profileService$id)
  156.   {
  157.     $profile Profile::getById($id);
  158.     $subprofileKeys $profileService->getActiveSubProfileIdentifiers($profile);
  159.     $user $this->getUser();
  160.     $ownerId $request->get('owner');
  161.     if ($ownerId != null) {
  162.       $owner Profile::getById($ownerId);
  163.     } else {
  164.       $owner null;
  165.     }
  166.     $streamId 'unique-horse-owner-table-'.$ownerId;
  167.     $ownedHorsesBySex = [];
  168.     if (in_array('TrainerProfile'$subprofileKeys)) {
  169.       $ownedHorsesBySex $horseService->getTrainedHorsesForProfile($profile);
  170.       if ($owner != null) {
  171.         $ownedHorsesBySex->addConditionParam('owner__id = ?', [$ownerId]);
  172.       } else {
  173.         $ownedHorsesBySex->addConditionParam('owner__id IS NULL');
  174.       }
  175.       $ownedHorsesBySex->setOrderKey('birthyear');
  176.       $ownedHorsesBySex->setOrder('desc');
  177.     }
  178.     $additionalActionParams = [
  179.       'owner' => $ownerId,
  180.     ];
  181.     $context $loadMoreService->generate(
  182.       $request,
  183.       $streamId,
  184.       $ownedHorsesBySex,
  185.       'network/profile/components/profile_horse_list_by_owner.html.twig',
  186.       [
  187.         'profile' => $profile,
  188.         'user' => $user,
  189.         'frameId' => 'unique-horse-owner-'.$ownerId,
  190.         'owner' => $owner,
  191.       ],
  192.       0,
  193.       5,
  194.       5,
  195.       $additionalActionParams
  196.     );
  197.     return $this->renderForm(...$context);
  198.   }
  199.   #[Route('/{id}/owned/horse-list'name'owned_horse_list')]
  200.   public function ownedHorseListAction(Request $requestHorseService $horseServiceProfileService $profileService$idLoadMoreService $loadMoreService)
  201.   {
  202.     $profile Profile::getById($id);
  203.     $subprofileKeys $profileService->getActiveSubProfileIdentifiers($profile);
  204.     $user $this->getUser();
  205.     $ownedHorses = [];
  206.     if (in_array('OwnerProfile'$subprofileKeys)) {
  207.       $ownedHorses $horseService->getOwnedHorsesForProfile($profile);
  208.       $ownedHorses->setOrderKey('birthyear');
  209.       $ownedHorses->setOrder('desc');
  210.     }
  211.     $uniqueHorseTrainer = [];
  212.     $untrainedHorsesSexes = [];
  213.     foreach ($ownedHorses as $value) {
  214.       if ($value->getTrainer() && !in_array($value->getTrainer(), $uniqueHorseTrainer)) {
  215.         $uniqueHorseTrainer[] = $value->getTrainer();
  216.       } elseif (!$value->getTrainer() && !in_array($value->getSex(), $untrainedHorsesSexes)) {
  217.         $untrainedHorsesSexes[] = $value->getSex();
  218.       }
  219.     }
  220.     usort($uniqueHorseTrainer, fn ($a) => $a ? -1);
  221.     $uniqueHorseTrainer array_merge(array_splice($uniqueHorseTrainer, -1), $uniqueHorseTrainer);
  222.     $context $loadMoreService->generate(
  223.       $request,
  224.       'owned_list',
  225.       $ownedHorses,
  226.       'network/profile/sections/profile_section_owned_horses.html.twig',
  227.       [
  228.         'profile' => $profile,
  229.         'user' => $user,
  230.         'untrainedHorsesSexes' => $untrainedHorsesSexes,
  231.         'uniqueHorseTrainer' => $uniqueHorseTrainer,
  232.       ]
  233.     );
  234.     return $this->renderForm(...$context);
  235.   }
  236.   #[Route('/{id}/owned/horse-list/sexes'name'owned_horse_sex_list')]
  237.   public function uniqueHorseSexesAction(Request $requestLoadMoreService $loadMoreServiceHorseService $horseServiceProfileService $profileService$id)
  238.   {
  239.     $profile Profile::getById($id);
  240.     $subprofileKeys $profileService->getActiveSubProfileIdentifiers($profile);
  241.     $user $this->getUser();
  242.     $sex $request->get('sex');
  243.     $streamId 'unique-horse-sexes-table-'.str_replace('.'''$sex);
  244.     $ownedHorsesBySex = [];
  245.     if (in_array('OwnerProfile'$subprofileKeys)) {
  246.       $ownedHorsesBySex $horseService->getOwnedHorsesForProfile($profile);
  247.       $ownedHorsesBySex->addConditionParam('sex = ? AND (trainer__id IS NULL)', [$sex]);
  248.       $ownedHorsesBySex->setOrderKey('birthyear');
  249.       $ownedHorsesBySex->setOrder('desc');
  250.     }
  251.     // dd(array_filter($ownedHorsesBySex->load(), function ($entry) {
  252.     //   return $entry->getTrainer() === null;
  253.     // }));
  254.     $additionalActionParams = [
  255.       'sex' => $sex,
  256.     ];
  257.     $context $loadMoreService->generate(
  258.       $request,
  259.       $streamId,
  260.       $ownedHorsesBySex,
  261.       'network/profile/components/profile_horse_list_by_sex.html.twig',
  262.       [
  263.         'profile' => $profile,
  264.         'user' => $user,
  265.         'frameId' => 'unique-horse-sexes-'.$sex,
  266.         'sex' => $sex,
  267.       ],
  268.       0,
  269.       5,
  270.       5,
  271.       $additionalActionParams
  272.     );
  273.     return $this->renderForm(...$context);
  274.   }
  275.   #[Route('/{id}/owned/horse-list/trainer'name'owned_horse_trainer_list')]
  276.   public function uniqueHorseTrainerAction(Request $requestLoadMoreService $loadMoreServiceHorseService $horseServiceProfileService $profileService$id)
  277.   {
  278.     $profile Profile::getById($id);
  279.     $subprofileKeys $profileService->getActiveSubProfileIdentifiers($profile);
  280.     $user $this->getUser();
  281.     $trainerId $request->get('trainer');
  282.     $trainer Profile::getById($trainerId);
  283.     if ($trainer == null) {
  284.       return;
  285.     }
  286.     $streamId 'unique-horse-trainer-table-'.str_replace('.'''$trainerId);
  287.     $ownedHorsesBySex = [];
  288.     if (in_array('OwnerProfile'$subprofileKeys)) {
  289.       $ownedHorsesBySex $horseService->getOwnedHorsesForProfile($profile);
  290.       $ownedHorsesBySex->addConditionParam('trainer__id = ? and trainer__id IS NOT NULL', [$trainerId]);
  291.       $ownedHorsesBySex->setOrderKey('birthyear');
  292.       $ownedHorsesBySex->setOrder('desc');
  293.     }
  294.     $additionalActionParams = [
  295.       'trainer' => $trainerId,
  296.     ];
  297.     $context $loadMoreService->generate(
  298.       $request,
  299.       $streamId,
  300.       $ownedHorsesBySex,
  301.       'network/profile/components/profile_horse_list_by_trainer.html.twig',
  302.       [
  303.         'profile' => $profile,
  304.         'user' => $user,
  305.         'frameId' => 'unique-horse-trainer-'.$trainerId,
  306.         'trainer' => $trainer,
  307.       ],
  308.       0,
  309.       5,
  310.       5,
  311.       $additionalActionParams
  312.     );
  313.     // dd($ownedHorsesBySex->load());
  314.     return $this->renderForm(...$context);
  315.   }
  316.   #[Route('/{id}/managed/horse-list'name'managed_horse_list')]
  317.   public function managedHorseListAction(Request $requestLoadMoreService $loadMoreServiceHorseService $horseServiceProfileService $profileService$id)
  318.   {
  319.     $profile Profile::getById($id);
  320.     $subprofileKeys $profileService->getActiveSubProfileIdentifiers($profile);
  321.     $user $this->getUser();
  322.     $managedHorses = [];
  323.     if (in_array('RacingManagerProfile'$subprofileKeys)) {
  324.       $managedHorses $horseService->getManagedHorsesForProfile($profile);
  325.       $managedHorses->setOrderKey('birthyear');
  326.       $managedHorses->setOrder('desc');
  327.     }
  328.     $context $loadMoreService->generate(
  329.       $request,
  330.       'managed_list',
  331.       $managedHorses,
  332.       'network/profile/sections/profile_section_managed_horses.html.twig',
  333.       [
  334.         'profile' => $profile,
  335.         'user' => $user,
  336.       ]
  337.     );
  338.     return $this->renderForm(...$context);
  339.   }
  340.   #[Route('/{id}/managed/auction-list'name'auction_list')]
  341.   public function auctionList(Request $request$idPaginatorInterface $paginatorLoadMoreService $loadMoreServiceNetworkProfileService $networkProfileService)
  342.   {
  343.     $user $this->getUser();
  344.     $profile Profile::getById($id);
  345.     $period $request->get('period''future');
  346.     $auctions $networkProfileService->getAuctions($profile$period);
  347.     $filter $this->createForm(AuctionDetailFilterType::class, [
  348.       'period' => $period,
  349.     ], ['method' => 'get']);
  350.     $filter->handleRequest($request);
  351.     if ($filter->isSubmitted() && $filter->isValid()) {
  352.       $data = (object) $filter->getData();
  353.       $networkProfileService->getAuctions($profile$data->period);
  354.     }
  355.     $context $loadMoreService->generate(
  356.       $request,
  357.       'auctions_list',
  358.       $auctions,
  359.       'network/profile/sections/frames/auction_list_frame.html.twig',
  360.       [
  361.         'profile' => $profile,
  362.         'user' => $user,
  363.         'auctions' => $auctions,
  364.         'filter' => $filter,
  365.         'period' => $period,
  366.       ], 033, ['period' => $period]
  367.     );
  368.     // dd($context['parameters']['listing']->load());
  369.     return $this->renderForm(...$context);
  370.   }
  371.   #[Route('/{id}/managed/raceday-list'name'raceday_list')]
  372.   public function racedayList(Request $request$idPaginatorInterface $paginatorLoadMoreService $loadMoreServiceNetworkProfileService $networkProfileService)
  373.   {
  374.     $user $this->getUser();
  375.     $profile Profile::getById($id);
  376.     $period $request->get('period''future');
  377.     $racedays $networkProfileService->getRacedays($profile$period);
  378.     $filter $this->createForm(RacedayDetailFilterType::class, [
  379.       'period' => $period,
  380.     ], ['method' => 'get']);
  381.     $filter->handleRequest($request);
  382.     if ($filter->isSubmitted() && $filter->isValid()) {
  383.       $data = (object) $filter->getData();
  384.       $networkProfileService->getRacedays($profile$data->period);
  385.     }
  386.     $context $loadMoreService->generate(
  387.       $request,
  388.       'racedays_list',
  389.       $racedays,
  390.       'network/profile/sections/frames/raceday_list_frame.html.twig',
  391.       [
  392.         'profile' => $profile,
  393.         'user' => $user,
  394.         'auctions' => $racedays,
  395.         'filter' => $filter,
  396.         'period' => $period,
  397.       ], 033, ['period' => $period]
  398.     );
  399.     return $this->renderForm(...$context);
  400.   }
  401.   #[Route('/{id}/frame/news'name'news_frame')]
  402.   public function newsFrame(Request $request$idLoadMoreService $loadMoreService): Response
  403.   {
  404.     $profile Profile::getById($id);
  405.     $news $profile->getNews();
  406.     $context $loadMoreService->generate(
  407.       $request,
  408.       'profile-news-list',
  409.       $news,
  410.       'network/profile/news/profile_news_frame.html.twig',
  411.       [
  412.         'profile' => $profile,
  413.         'news' => $news,
  414.       ], 055
  415.     );
  416.     return $this->renderForm(...$context);
  417.   }
  418.   #[Route('/frame/achievements/{category}/{id}'name'achievement_frame')]
  419.   public function achievementsFrame(Request $request$category$idLoadMoreService $loadMoreServiceNetworkProfileService $networkProfileService): Response
  420.   {
  421.     $profile Profile::getById($id);
  422.     // $news = $profile->getNews();
  423.     $achievements $networkProfileService->getAchievements($profile);
  424.     $achievements->addConditionParam("category = '{$category}'");
  425.     // if ($category == 'achievements.group2') {
  426.     //   dd(count($achievements->load()));
  427.     // }
  428.     $context $loadMoreService->generate(
  429.       $request,
  430.       'achievements-'.$category,
  431.       $achievements,
  432.       'network/profile/sections/frames/profile_achievements_frame.html.twig',
  433.       [
  434.         'profile' => $profile,
  435.         'achievements' => $achievements,
  436.         'category' => $category,
  437.       ], 055
  438.     );
  439.     return $this->renderForm(...$context);
  440.   }
  441.   #[Route('/frame/cropAvatar'name'crop_avatar_frame')]
  442.   public function cropAvatarFrame(Request $requestCropService $cropService)
  443.   {
  444.     $user $this->getUser();
  445.     $formBuilder $this->createFormBuilder();
  446.     if (!$user instanceof Profile && !$user instanceof DataObjectProfile) {
  447.       throw new \Exception(sprintf('%s is not of type Profile'get_class($user)), 1);
  448.     }
  449.     $avatar $user->getAvatar();
  450.     if ($avatar) {
  451.       $avatarOptions = [
  452.         'aspectRatio' => 1,
  453.         'viewMode' => 3,
  454.         'dragMode' => 'move',
  455.         'autoCropArea' => 1,
  456.         'restore' => false,
  457.         'modal' => false,
  458.         'guides' => false,
  459.         'highlight' => false,
  460.         'cropBoxMovable' => false,
  461.         'cropBoxResizable' => false,
  462.         'toggleDragModeOnDblclick' => false,
  463.         'moveable' => true,
  464.         'background' => false,
  465.       ];
  466.       $response $cropService->crop($request$avatar$avatarOptions$user$formBuilder'network/profile/edit/crop-avatar.html.twig''network/profile/edit/crop-avatar-saved.html.twig');
  467.       if (array_key_exists('asset'$response)) {
  468.         $user->setAvatar($response['asset']);
  469.         $user->save();
  470.       }
  471.       return $this->renderForm($response['template'], $response['options']);
  472.     }
  473.   }
  474.   #[Route('/frame/cropBanner'name'crop_banner_frame')]
  475.   public function cropBannerFrame(Request $requestCropService $cropService)
  476.   {
  477.     $user $this->getUser();
  478.     $formBuilder $this->createFormBuilder();
  479.     if (!$user instanceof Profile && !$user instanceof DataObjectProfile) {
  480.       throw new \Exception(sprintf('%s is not of type Profile'get_class($user)), 1);
  481.     }
  482.     $banner $user->getBanner();
  483.     if ($banner) {
  484.       $bannerOptions = [
  485.         'aspectRatio' => 3.2 1,
  486.         'viewMode' => 3,
  487.         'dragMode' => 'move',
  488.         'autoCropArea' => 1,
  489.         'restore' => false,
  490.         'modal' => false,
  491.         'guides' => false,
  492.         'highlight' => false,
  493.         'cropBoxMovable' => false,
  494.         'cropBoxResizable' => false,
  495.         'toggleDragModeOnDblclick' => false,
  496.         'background' => false,
  497.       ];
  498.       $response $cropService->crop($request$banner$bannerOptions$user$formBuilder'network/profile/edit/crop-banner.html.twig''network/profile/edit/crop-banner-saved.html.twig');
  499.       if (array_key_exists('asset'$response)) {
  500.         $user->setBanner($response['asset']);
  501.         $user->save();
  502.       }
  503.       return $this->renderForm($response['template'], $response['options']);
  504.     }
  505.   }
  506. }