<?php
namespace App\Network\Controller;
use App\Auction\Manager\AuctionManager;
use App\Network\Form\Type\EditHorseDataType;
use App\Network\Service\NetworkAchievementService;
use App\Network\Service\NetworkHorseService;
use App\Profile\Service\LoadMoreService;
use Carbon\Carbon;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Horse;
use Pimcore\Model\DataObject\News;
use Pimcore\Model\DataObject\TimelinePost;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route([
'name' => 'network_horse_',
'localizedPaths' => [
'en' => '/{_locale}/network/horse',
'de' => '/{_locale}/netzwerk/pferd',
'fr' => '/{_locale}/reseau/cheval',
],
])]
class NetworkHorseController extends FrontendController
{
#[Route('/{id}/hovercard', name: 'hovercard')]
public function hovercard(Request $request, $id)
{
$horse = Horse::getById($id);
if (!$horse->isPublished()) {
return new Response('404', 404);
}
if (!$horse) {
throw $this->createNotFoundException("Horse with id {$id} not found");
}
return $this->renderTemplate('horse/hovercard.html.twig', [
'horse' => $horse,
]);
}
#[Route('/{id}/{slug}', name: 'detail')]
public function detail(AuctionManager $auctionManager, NetworkAchievementService $achievementService, NetworkHorseService $networkHorseService, $id, Request $request, $slug = ''): Response
{
$horse = Horse::getById($id);
if (!$horse->isPublished()) {
return $this->redirectToRoute('document_1');
}
// Get Timeline Posts for Horse
$posts = TimelinePost::getList([
'objectbricks' => ['RaceTimelinePost', 'HorseTimelinePost', 'HorseAtAuctionTimelinePost'],
'condition' => "RaceTimelinePost.horse__id = {$id} OR HorseTimelinePost.horse__id = {$id} OR HorseAtAuctionTimelinePost.horse__id = {$id}",
]);
$achievements = $networkHorseService->getAchievements($horse);
$achievementCategories = $networkHorseService->getNeededAchievementCategories($achievements);
$auctionHistory = $networkHorseService->getAuctionhistory($horse);
return $this->renderForm('network/horse/detail.html.twig', [
'horse' => $horse,
'hasPosts' => $posts->getTotalCount(),
'auctionHistory' => $auctionHistory,
'now' => new Carbon(),
'achievements' => $achievements,
'achievementCategories' => $achievementCategories,
]);
}
#[Route('/horse/{id}/news', name: 'news_frame')]
public function horseNewsFrame(Request $request, LoadMoreService $loadMoreService, $id)
{
$horse = Horse::getById($id);
$news = News::getList()
->setCondition("horses like '%,".$id.",%'")
->setOrderKey('date')
->setOrder('desc');
$context = $loadMoreService->generate(
$request,
'horse-news-list',
$news,
'network/horse/components/news_list_frame.html.twig',
[
'horse' => $horse,
'news' => $news,
], 0, 5, 5
);
$request->setRequestFormat('text/html');
return $this->renderForm(...$context);
}
#[Route('/horse/{id}/data', name: 'data')]
public function horseDataWidget($id) {
$horse = Horse::getById($id);
return $this->render('network/horse/widgets/horse_widget_data_content.html.twig', [
'horse' => $horse
]);
}
#[Route('/horse/{id}/data/edit', name: 'data_edit')]
public function horseDataWidgetEdit(Request $request, $id) {
if (!($this->isGranted('ROLE_CLUB_HORSE') || $this->isGranted('ROLE_CLUB_JOCKEY'))) {
$e = $this->createAccessDeniedException('not allowed');
$e->setAttributes(['ROLE_CLUB_HORSE', 'ROLE_CLUB_JOCKEY']);
throw $e;
}
$horse = Horse::getById($id);
$form = $this->createForm(EditHorseDataType::class, $horse);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$horse = $form->getData()->save();
return $this->render('network/horse/widgets/horse_widget_data_content.html.twig', [
'horse' => $horse
], new Response('', 200, [
'content-type' => 'text/html; charset=UTF-8'
]));
}
return $this->renderForm('network/horse/widgets/horse_widget_data_edit.html.twig', [
'horse' => $horse,
'form' => $form
]);
}
}