<?php
namespace App\Network\Controller;
use App\Auction\Form\Type\AuctionDetailFilterType;
use App\Horse\Service\HorseService;
use App\Network\Service\CropService;
use App\Network\Service\NetworkProfileService;
use App\Profile\Manager\ProfileManager;
use App\Profile\Model\Profile;
use App\Profile\Service\LoadMoreService;
use App\Profile\Service\ProfileService;
use App\Raceday\Form\Type\RacedayDetailFilterType;
use Carbon\Carbon;
use Doctrine\DBAL\Query\QueryBuilder;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Auction;
use Pimcore\Model\DataObject\Profile as DataObjectProfile;
use Pimcore\Model\DataObject\Raceday;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route([
'name' => 'network_profile_',
'localizedPaths' => [
'en' => '/{_locale}/network/profile',
'de' => '/{_locale}/netzwerk/profil',
'fr' => '/{_locale}/reseau/profil',
],
])]
class NetworkProfileController extends FrontendController
{
#[Route('/{username}', name: 'detail')]
public function detail($username, HorseService $horseService, ProfileService $profileService, NetworkProfileService $networkProfileService, Request $request): Response
{
$profile = Profile::getByUsername(rawurldecode($username), 1);
if (!$profile) {
throw $this->createNotFoundException('profile-not-found');
}
$subprofileKeys = $profileService->getActiveSubProfileIdentifiers($profile);
$user = $this->getUser();
$horses = $networkProfileService->getAssociatedHorsesByProfile($subprofileKeys, $profile);
$auctionEvents = $networkProfileService->getFutureAuctionsByProfile($profile);
$raceEvents = $networkProfileService->getFutureRacedaysByProfile($profile);
$marketplacePosts = $networkProfileService->getLatestMarketplacePostsByProfile($profile);
$achievementCategories = $networkProfileService->getAchievementCategoriesByProfile($profile);
$horseRacedays = Raceday::getList();
$horseRacedays->onCreateQueryBuilder(function (QueryBuilder $queryBuilder) use ($profile) {
$queryBuilder->join('object_raceday', 'object_horse', 'horse', 'horses LIKE CONCAT("%", `horse`.`oo_id` , "%")');
$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().')');
$queryBuilder->orderBy('object_raceday.date', 'ASC');
$queryBuilder->distinct();
$queryBuilder->setMaxResults(3);
});
$horseAuctions = Auction::getList();
$horseAuctions->onCreateQueryBuilder(function (QueryBuilder $queryBuilder) use ($profile) {
$queryBuilder->innerJoin('object_auction', 'object_horse', 'horse', 'horses LIKE CONCAT("%", `horse`.`oo_id` , "%")');
$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().')');
$queryBuilder->distinct();
$queryBuilder->setMaxResults(3);
});
// dd($horseAuctions->load());
return $this->renderForm('network/profile/detail.html.twig', [
'profile' => $profile,
'user' => $user,
'highlightedPost' => $request->query->get('highlightedPost'),
'achievementCategories' => $achievementCategories,
'horses' => $horses,
'raceEvents' => $raceEvents,
'auctionEvents' => $auctionEvents,
'marketplacePosts' => $marketplacePosts,
'horseEvents' => ['auctions' => $horseAuctions, 'racedays' => $horseRacedays],
]);
}
#[Route('/{id}/connect', name: 'connect')]
public function connect(Request $request, $id, ProfileManager $profileManager)
{
/** @var Profile */
$user = $this->getUser();
$profile = Profile::getById($id);
$form = $this->createFormBuilder()->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$profileManager->toggleNetworkProfile($user, $profile);
}
$request->setRequestFormat('text/html');
return $this->renderForm('network/profile/components/connect.html.twig', [
'isInNetwork' => $profileManager->isProfileInNetwork($user, $profile),
'profile' => $profile,
'form' => $form,
]);
}
#[Route('/{id}/bred/horse-list', name: 'bred_horse_list')]
public function bredHorseListAction(Request $request, HorseService $horseService, ProfileService $profileService, LoadMoreService $loadMoreService, $id)
{
$profile = Profile::getById($id);
$subprofileKeys = $profileService->getActiveSubProfileIdentifiers($profile);
$user = $this->getUser();
$bredHorses = [];
if (in_array('BreederProfile', $subprofileKeys)) {
$bredHorses = $horseService->getBredHorsesForProfile($profile);
$bredHorses->setOrderKey('birthyear');
$bredHorses->setOrder('desc');
}
$context = $loadMoreService->generate(
$request,
'bred_list',
$bredHorses,
'network/profile/sections/profile_section_bred_horses.html.twig',
[
'profile' => $profile,
'user' => $user,
]
);
return $this->renderForm(...$context);
}
#[Route('/{id}/trained/horse-list', name: 'trained_horse_list')]
public function trainedHorseListAction(Request $request, HorseService $horseService, ProfileService $profileService, $id, LoadMoreService $loadMoreService)
{
$profile = Profile::getById($id);
$subprofileKeys = $profileService->getActiveSubProfileIdentifiers($profile);
$user = $this->getUser();
$trainedHorses = [];
if (in_array('TrainerProfile', $subprofileKeys)) {
$trainedHorses = $horseService->getTrainedHorsesForProfile($profile);
$trainedHorses->setOrderKey('birthyear');
$trainedHorses->setOrder('desc');
}
$uniqueHorseOwner = [];
foreach ($trainedHorses as $key => $value) {
if (!in_array($value->getOwner(), $uniqueHorseOwner, true)) {
$uniqueHorseOwner[] = $value->getOwner();
}
}
$uniqueHorseOwner = array_filter($uniqueHorseOwner, function ($owner) {
if ($owner != null) {
return $owner::class === Profile::class;
} else {
return $owner === null;
}
});
$context = $loadMoreService->generate(
$request,
'trained_list',
$trainedHorses,
'network/profile/sections/profile_section_trained_horses.html.twig',
[
'profile' => $profile,
'user' => $user,
'uniqueHorseOwner' => $uniqueHorseOwner,
]
);
return $this->renderForm(...$context);
}
#[Route('/{id}/trained/horse-list/owner', name: 'trained_horse_trainer_list')]
public function uniqueOwnerAction(Request $request, LoadMoreService $loadMoreService, HorseService $horseService, ProfileService $profileService, $id)
{
$profile = Profile::getById($id);
$subprofileKeys = $profileService->getActiveSubProfileIdentifiers($profile);
$user = $this->getUser();
$ownerId = $request->get('owner');
if ($ownerId != null) {
$owner = Profile::getById($ownerId);
} else {
$owner = null;
}
$streamId = 'unique-horse-owner-table-'.$ownerId;
$ownedHorsesBySex = [];
if (in_array('TrainerProfile', $subprofileKeys)) {
$ownedHorsesBySex = $horseService->getTrainedHorsesForProfile($profile);
if ($owner != null) {
$ownedHorsesBySex->addConditionParam('owner__id = ?', [$ownerId]);
} else {
$ownedHorsesBySex->addConditionParam('owner__id IS NULL');
}
$ownedHorsesBySex->setOrderKey('birthyear');
$ownedHorsesBySex->setOrder('desc');
}
$additionalActionParams = [
'owner' => $ownerId,
];
$context = $loadMoreService->generate(
$request,
$streamId,
$ownedHorsesBySex,
'network/profile/components/profile_horse_list_by_owner.html.twig',
[
'profile' => $profile,
'user' => $user,
'frameId' => 'unique-horse-owner-'.$ownerId,
'owner' => $owner,
],
0,
5,
5,
$additionalActionParams
);
return $this->renderForm(...$context);
}
#[Route('/{id}/owned/horse-list', name: 'owned_horse_list')]
public function ownedHorseListAction(Request $request, HorseService $horseService, ProfileService $profileService, $id, LoadMoreService $loadMoreService)
{
$profile = Profile::getById($id);
$subprofileKeys = $profileService->getActiveSubProfileIdentifiers($profile);
$user = $this->getUser();
$ownedHorses = [];
if (in_array('OwnerProfile', $subprofileKeys)) {
$ownedHorses = $horseService->getOwnedHorsesForProfile($profile);
$ownedHorses->setOrderKey('birthyear');
$ownedHorses->setOrder('desc');
}
$uniqueHorseTrainer = [];
$untrainedHorsesSexes = [];
foreach ($ownedHorses as $value) {
if ($value->getTrainer() && !in_array($value->getTrainer(), $uniqueHorseTrainer)) {
$uniqueHorseTrainer[] = $value->getTrainer();
} elseif (!$value->getTrainer() && !in_array($value->getSex(), $untrainedHorsesSexes)) {
$untrainedHorsesSexes[] = $value->getSex();
}
}
usort($uniqueHorseTrainer, fn ($a) => $a ? -1 : 1);
$uniqueHorseTrainer = array_merge(array_splice($uniqueHorseTrainer, -1), $uniqueHorseTrainer);
$context = $loadMoreService->generate(
$request,
'owned_list',
$ownedHorses,
'network/profile/sections/profile_section_owned_horses.html.twig',
[
'profile' => $profile,
'user' => $user,
'untrainedHorsesSexes' => $untrainedHorsesSexes,
'uniqueHorseTrainer' => $uniqueHorseTrainer,
]
);
return $this->renderForm(...$context);
}
#[Route('/{id}/owned/horse-list/sexes', name: 'owned_horse_sex_list')]
public function uniqueHorseSexesAction(Request $request, LoadMoreService $loadMoreService, HorseService $horseService, ProfileService $profileService, $id)
{
$profile = Profile::getById($id);
$subprofileKeys = $profileService->getActiveSubProfileIdentifiers($profile);
$user = $this->getUser();
$sex = $request->get('sex');
$streamId = 'unique-horse-sexes-table-'.str_replace('.', '', $sex);
$ownedHorsesBySex = [];
if (in_array('OwnerProfile', $subprofileKeys)) {
$ownedHorsesBySex = $horseService->getOwnedHorsesForProfile($profile);
$ownedHorsesBySex->addConditionParam('sex = ? AND (trainer__id IS NULL)', [$sex]);
$ownedHorsesBySex->setOrderKey('birthyear');
$ownedHorsesBySex->setOrder('desc');
}
// dd(array_filter($ownedHorsesBySex->load(), function ($entry) {
// return $entry->getTrainer() === null;
// }));
$additionalActionParams = [
'sex' => $sex,
];
$context = $loadMoreService->generate(
$request,
$streamId,
$ownedHorsesBySex,
'network/profile/components/profile_horse_list_by_sex.html.twig',
[
'profile' => $profile,
'user' => $user,
'frameId' => 'unique-horse-sexes-'.$sex,
'sex' => $sex,
],
0,
5,
5,
$additionalActionParams
);
return $this->renderForm(...$context);
}
#[Route('/{id}/owned/horse-list/trainer', name: 'owned_horse_trainer_list')]
public function uniqueHorseTrainerAction(Request $request, LoadMoreService $loadMoreService, HorseService $horseService, ProfileService $profileService, $id)
{
$profile = Profile::getById($id);
$subprofileKeys = $profileService->getActiveSubProfileIdentifiers($profile);
$user = $this->getUser();
$trainerId = $request->get('trainer');
$trainer = Profile::getById($trainerId);
if ($trainer == null) {
return;
}
$streamId = 'unique-horse-trainer-table-'.str_replace('.', '', $trainerId);
$ownedHorsesBySex = [];
if (in_array('OwnerProfile', $subprofileKeys)) {
$ownedHorsesBySex = $horseService->getOwnedHorsesForProfile($profile);
$ownedHorsesBySex->addConditionParam('trainer__id = ? and trainer__id IS NOT NULL', [$trainerId]);
$ownedHorsesBySex->setOrderKey('birthyear');
$ownedHorsesBySex->setOrder('desc');
}
$additionalActionParams = [
'trainer' => $trainerId,
];
$context = $loadMoreService->generate(
$request,
$streamId,
$ownedHorsesBySex,
'network/profile/components/profile_horse_list_by_trainer.html.twig',
[
'profile' => $profile,
'user' => $user,
'frameId' => 'unique-horse-trainer-'.$trainerId,
'trainer' => $trainer,
],
0,
5,
5,
$additionalActionParams
);
// dd($ownedHorsesBySex->load());
return $this->renderForm(...$context);
}
#[Route('/{id}/managed/horse-list', name: 'managed_horse_list')]
public function managedHorseListAction(Request $request, LoadMoreService $loadMoreService, HorseService $horseService, ProfileService $profileService, $id)
{
$profile = Profile::getById($id);
$subprofileKeys = $profileService->getActiveSubProfileIdentifiers($profile);
$user = $this->getUser();
$managedHorses = [];
if (in_array('RacingManagerProfile', $subprofileKeys)) {
$managedHorses = $horseService->getManagedHorsesForProfile($profile);
$managedHorses->setOrderKey('birthyear');
$managedHorses->setOrder('desc');
}
$context = $loadMoreService->generate(
$request,
'managed_list',
$managedHorses,
'network/profile/sections/profile_section_managed_horses.html.twig',
[
'profile' => $profile,
'user' => $user,
]
);
return $this->renderForm(...$context);
}
#[Route('/{id}/managed/auction-list', name: 'auction_list')]
public function auctionList(Request $request, $id, PaginatorInterface $paginator, LoadMoreService $loadMoreService, NetworkProfileService $networkProfileService)
{
$user = $this->getUser();
$profile = Profile::getById($id);
$period = $request->get('period', 'future');
$auctions = $networkProfileService->getAuctions($profile, $period);
$filter = $this->createForm(AuctionDetailFilterType::class, [
'period' => $period,
], ['method' => 'get']);
$filter->handleRequest($request);
if ($filter->isSubmitted() && $filter->isValid()) {
$data = (object) $filter->getData();
$networkProfileService->getAuctions($profile, $data->period);
}
$context = $loadMoreService->generate(
$request,
'auctions_list',
$auctions,
'network/profile/sections/frames/auction_list_frame.html.twig',
[
'profile' => $profile,
'user' => $user,
'auctions' => $auctions,
'filter' => $filter,
'period' => $period,
], 0, 3, 3, ['period' => $period]
);
// dd($context['parameters']['listing']->load());
return $this->renderForm(...$context);
}
#[Route('/{id}/managed/raceday-list', name: 'raceday_list')]
public function racedayList(Request $request, $id, PaginatorInterface $paginator, LoadMoreService $loadMoreService, NetworkProfileService $networkProfileService)
{
$user = $this->getUser();
$profile = Profile::getById($id);
$period = $request->get('period', 'future');
$racedays = $networkProfileService->getRacedays($profile, $period);
$filter = $this->createForm(RacedayDetailFilterType::class, [
'period' => $period,
], ['method' => 'get']);
$filter->handleRequest($request);
if ($filter->isSubmitted() && $filter->isValid()) {
$data = (object) $filter->getData();
$networkProfileService->getRacedays($profile, $data->period);
}
$context = $loadMoreService->generate(
$request,
'racedays_list',
$racedays,
'network/profile/sections/frames/raceday_list_frame.html.twig',
[
'profile' => $profile,
'user' => $user,
'auctions' => $racedays,
'filter' => $filter,
'period' => $period,
], 0, 3, 3, ['period' => $period]
);
return $this->renderForm(...$context);
}
#[Route('/{id}/frame/news', name: 'news_frame')]
public function newsFrame(Request $request, $id, LoadMoreService $loadMoreService): Response
{
$profile = Profile::getById($id);
$news = $profile->getNews();
$context = $loadMoreService->generate(
$request,
'profile-news-list',
$news,
'network/profile/news/profile_news_frame.html.twig',
[
'profile' => $profile,
'news' => $news,
], 0, 5, 5
);
return $this->renderForm(...$context);
}
#[Route('/frame/achievements/{category}/{id}', name: 'achievement_frame')]
public function achievementsFrame(Request $request, $category, $id, LoadMoreService $loadMoreService, NetworkProfileService $networkProfileService): Response
{
$profile = Profile::getById($id);
// $news = $profile->getNews();
$achievements = $networkProfileService->getAchievements($profile);
$achievements->addConditionParam("category = '{$category}'");
// if ($category == 'achievements.group2') {
// dd(count($achievements->load()));
// }
$context = $loadMoreService->generate(
$request,
'achievements-'.$category,
$achievements,
'network/profile/sections/frames/profile_achievements_frame.html.twig',
[
'profile' => $profile,
'achievements' => $achievements,
'category' => $category,
], 0, 5, 5
);
return $this->renderForm(...$context);
}
#[Route('/frame/cropAvatar', name: 'crop_avatar_frame')]
public function cropAvatarFrame(Request $request, CropService $cropService)
{
$user = $this->getUser();
$formBuilder = $this->createFormBuilder();
if (!$user instanceof Profile && !$user instanceof DataObjectProfile) {
throw new \Exception(sprintf('%s is not of type Profile', get_class($user)), 1);
}
$avatar = $user->getAvatar();
if ($avatar) {
$avatarOptions = [
'aspectRatio' => 1 / 1,
'viewMode' => 3,
'dragMode' => 'move',
'autoCropArea' => 1,
'restore' => false,
'modal' => false,
'guides' => false,
'highlight' => false,
'cropBoxMovable' => false,
'cropBoxResizable' => false,
'toggleDragModeOnDblclick' => false,
'moveable' => true,
'background' => false,
];
$response = $cropService->crop($request, $avatar, $avatarOptions, $user, $formBuilder, 'network/profile/edit/crop-avatar.html.twig', 'network/profile/edit/crop-avatar-saved.html.twig');
if (array_key_exists('asset', $response)) {
$user->setAvatar($response['asset']);
$user->save();
}
return $this->renderForm($response['template'], $response['options']);
}
}
#[Route('/frame/cropBanner', name: 'crop_banner_frame')]
public function cropBannerFrame(Request $request, CropService $cropService)
{
$user = $this->getUser();
$formBuilder = $this->createFormBuilder();
if (!$user instanceof Profile && !$user instanceof DataObjectProfile) {
throw new \Exception(sprintf('%s is not of type Profile', get_class($user)), 1);
}
$banner = $user->getBanner();
if ($banner) {
$bannerOptions = [
'aspectRatio' => 3.2 / 1,
'viewMode' => 3,
'dragMode' => 'move',
'autoCropArea' => 1,
'restore' => false,
'modal' => false,
'guides' => false,
'highlight' => false,
'cropBoxMovable' => false,
'cropBoxResizable' => false,
'toggleDragModeOnDblclick' => false,
'background' => false,
];
$response = $cropService->crop($request, $banner, $bannerOptions, $user, $formBuilder, 'network/profile/edit/crop-banner.html.twig', 'network/profile/edit/crop-banner-saved.html.twig');
if (array_key_exists('asset', $response)) {
$user->setBanner($response['asset']);
$user->save();
}
return $this->renderForm($response['template'], $response['options']);
}
}
}