src/Auction/Controller/AuctionController.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Auction\Controller;
  3. use App\Auction\Event\AuctionAttendEvent;
  4. use App\Auction\Event\AuctionEvents;
  5. use App\Auction\Form\Type\AuctionFilterType;
  6. use App\Profile\Form\Type\ProfileAttendType;
  7. use App\Profile\Model\Profile;
  8. use App\Profile\Service\ProfileService;
  9. use App\Timeline\Event\TimelinePostDeletedEvent;
  10. use App\Timeline\Event\TimelinePostEvents;
  11. use App\Timeline\Event\TimelinePostPublishedEvent;
  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\Auctionlot;
  18. use Pimcore\Model\DataObject\Profile as DataObjectProfile;
  19. use Pimcore\Model\DataObject\TimelinePost;
  20. use Pimcore\Translation\Translator;
  21. use Symfony\Component\Form\Form;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\Uid\Uuid;
  25. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. class AuctionController extends FrontendController
  28. {
  29.   #[Route('{_locale}/auction/{auction}/auctionlots'name'auction_auctionlots')]
  30.   public function auctionLots(Request $requestAuction $auction)
  31.   {
  32.     $order $request->get('order''asc');
  33.     $orderKey $request->get('orderKey''number + 0');
  34.     $limit = (int) $request->get('limit'20);
  35.     $auctionlots Auctionlot::getList();
  36.     $auctionlots->filterByAuction($auction);
  37.     $auctionlots->orderByHorseField($orderKey$order);
  38.     $auctionlots->setLimit($limit);
  39.     return $this->render('auction/auction_auctionlots.html.twig', [
  40.       'auction' => $auction,
  41.       'auctionlots' => $auctionlots,
  42.       'limit' => $limit,
  43.       'order' => $order,
  44.       'orderKey' => $orderKey,
  45.     ]);
  46.   }
  47.   /**
  48.    * List with auctions and filter.
  49.    */
  50.   public function auctions(Request $requestPaginatorInterface $paginator)
  51.   {
  52.     $filter $this->createForm(AuctionFilterType::class, [
  53.       'period' => $request->get('period''future'),
  54.     ], ['method' => 'get']);
  55.     $filter->handleRequest($request);
  56.     $auctions Auction::getList();
  57.     if ($filter->isSubmitted() && $filter->isValid()) {
  58.       $data = (object) $filter->getData();
  59.       $data->title $auctions->addConditionParam('title LIKE :title', ['title' => '%'.$data->title.'%']) : null;
  60.       $data->auctionhouse $auctions->addConditionParam('auctionhouse__id = :auctionhouse', ['auctionhouse' => $data->auctionhouse->getId()]) : null;
  61.       $data->type $auctions->addConditionParam('auctionType = :type', ['type' => $data->type]) : null;
  62.       $auctions->setOrderKey('end');
  63.       $data->period == 'past' $auctions->addConditionParam('end < :end', ['end' => Carbon::now()->format('Y-m-d')])->setOrder('desc') : null;
  64.       $data->period == 'future' $auctions->addConditionParam('end >= :end', ['end' => Carbon::now()->format('Y-m-d')])->setOrder('asc') : null;
  65.     } else {
  66.       $auctions->setOrder('asc');
  67.       $auctions->setOrderKey('end');
  68.       $auctions->addConditionParam('end >= :end', ['end' => Carbon::now()->format('Y-m-d')]);
  69.     }
  70.     $pagination $paginator->paginate(
  71.       $auctions,
  72.       $request->get('page'1),
  73.       10
  74.     );
  75.     $timelineposts TimelinePost::getList();
  76.     $timelineposts->onCreateQueryBuilder(function (QueryBuilder $queryBuilder) {
  77.       $queryBuilder->leftJoin('object_timelinepost''object_relations_timelinepost''relation''relation.src_id = object_timelinepost.oo_id AND relation.fieldname = "showPostAt"');
  78.       $queryBuilder->leftJoin('object_timelinepost''objects''auctions''auctions.o_id = relation.dest_id');
  79.       $queryBuilder->where('auctions.o_className = "Auction" AND object_timelinepost.o_published = 1');
  80.     });
  81.     $timelineposts->setOrderKey('postedAt');
  82.     $timelineposts->setOrder('desc');
  83.     $timelineposts->setLimit(3);
  84.     // $posts->addConditionParam('ServiceTimelinePost.serviceCategory = ?', ['servicecategory.offer']);
  85.     return $this->renderForm('auction/list.html.twig', [
  86.         'pagination' => $pagination,
  87.         'filter' => $filter,
  88.         'timelineposts' => $timelineposts,
  89.       ]);
  90.   }
  91.   #[Route('{_locale}/auction/{auctionId}/hovercard'name'auction_hovercard')]
  92.   public function hovercard(Request $request$auctionIdTranslatorInterface $translatorProfileService $profileService)
  93.   {
  94.     $hovercardUid Uuid::v4();
  95.     $user $this->getUser();
  96.     $request->setLocale($request->get('_locale''en'));
  97.     if (!$translator instanceof Translator) {
  98.       throw new \Exception('wrong translator service'1);
  99.     }
  100.     $translator->setlocale($request->getLocale());
  101.     $auction Auction::getById($auctionId);
  102.     // if (!$user instanceof Profile && !$user instanceof DataObjectProfile) {
  103.     //   throw new \Exception('user is not of type Profile');
  104.     // }
  105.     if ($user instanceof Profile || $user instanceof DataObjectProfile) {
  106.       $userIsAttending $user && count(array_filter($auction->getAttendees(), fn ($v) => $v->getObjectId() == $user->getId())) > 0;
  107.     } else {
  108.       $userIsAttending null;
  109.     }
  110.     $form null;
  111.     if ($user && $user instanceof Profile) {
  112.       $form $this->createForm(ProfileAttendType::class, [
  113.         'userId' => $user->getId(),
  114.         'uuid' => $hovercardUid,
  115.       ], [
  116.         'method' => 'POST',
  117.         'csrf_protection' => false,
  118.       ]);
  119.       $form->handleRequest($request);
  120.       if ($form->isSubmitted() && $form->isValid()) {
  121.         $hovercardUid $form->get('uuid')->getData();
  122.         if (!$form instanceof Form) {
  123.           throw new \Exception('field is not of type form');
  124.         }
  125.         if ($form->getClickedButton() === $form->get('attend')) {
  126.           $profileService->attendEvent($user$auction->getId());
  127.           $userIsAttending true;
  128.         }
  129.         if ($form->getClickedButton() === $form->get('cancel')) {
  130.           $profileService->cancelAttendEvent($user$auction->getId());
  131.           $userIsAttending false;
  132.         }
  133.         return $this->renderForm('auction/hovercard_attend.html.twig', [
  134.           'uuid' => $hovercardUid,
  135.           'auction' => $auction,
  136.           'form' => $form,
  137.           'userIsAttending' => $userIsAttending,
  138.         ]);
  139.       }
  140.     }
  141.     if (!$auction) {
  142.       throw $this->createNotFoundException("Auction with id {$auctionId} not found");
  143.     }
  144.     return $this->renderForm('auction/hovercard.html.twig', [
  145.       'uuid' => $hovercardUid,
  146.       'auction' => $auction,
  147.       'form' => $form,
  148.       'userIsAttending' => $userIsAttending,
  149.     ]);
  150.   }
  151.   #[Route('{_locale}/auction/{auctionId}/attend'name'auction_attend')]
  152.   public function attend(Request $request$auctionIdProfileService $profileServiceEventDispatcherInterface $eventDispatcher)
  153.   {
  154.     $user $this->getUser();
  155.     $auction Auction::getById($auctionId);
  156.     if (!$user instanceof Profile and !$user instanceof DataObjectProfile) {
  157.       throw new \Exception('user is not of type profile'1);
  158.     }
  159.     $userIsAttending $user && count(array_filter($auction->getAttendees(), fn ($v) => $v->getObjectId() == $user->getId())) > 0;
  160.     $form $this->createForm(ProfileAttendType::class, [
  161.       'userId' => $user->getId(),
  162.     ], [
  163.       'method' => 'POST',
  164.       'csrf_protection' => false,
  165.     ]);
  166.     $form->handleRequest($request);
  167.     if ($form->isSubmitted() && $form->isValid()) {
  168.       if (!$form instanceof Form) {
  169.         throw new \Exception('field is not of type form');
  170.       }
  171.       if ($form->getClickedButton() === $form->get('attend')) {
  172.         $post $profileService->attendEvent($user$auction->getId());
  173.         $event = new TimelinePostPublishedEvent($post);
  174.         $eventDispatcher->dispatch($eventTimelinePostEvents::PUBLISHED);
  175.         $event = new AuctionAttendEvent($auction$user);
  176.         $eventDispatcher->dispatch($eventAuctionEvents::ATTEND);
  177.         return $this->render('auction/auction_attend_success.html.twig', [
  178.           'auction' => $auction,
  179.           'post' => $post,
  180.           'user' => $user,
  181.           'formObject' => $form,
  182.           'createPostForm' => false,
  183.         ]);
  184.       } elseif ($form->getClickedButton() === $form->get('cancel')) {
  185.         $postId $profileService->cancelAttendEvent($user$auction->getId());
  186.         if ($postId) {
  187.           $event = new TimelinePostDeletedEvent($postId);
  188.           $eventDispatcher->dispatch($eventTimelinePostEvents::DELETED);
  189.         }
  190.         $event = new AuctionAttendEvent($auction$user);
  191.         $eventDispatcher->dispatch($eventAuctionEvents::UNATTEND);
  192.         return $this->render('auction/auction_attend_cancel.html.twig', [
  193.           'auction' => $auction,
  194.           'postId' => $postId,
  195.           'user' => $user,
  196.           'formObject' => $form,
  197.           'createPostForm' => false,
  198.         ]);
  199.       }
  200.     }
  201.     $response $this->renderForm('auction/auction_attend.html.twig', [
  202.       'auction' => $auction,
  203.       'user' => $user,
  204.       'form' => $form,
  205.       'target' => $request->get('turbo_id'),
  206.       'userIsAttending' => $userIsAttending,
  207.     ]);
  208.     return $response;
  209.   }
  210.   #[Route('{_locale}/auction/{auctionId}/attendees'name'auction_attendees')]
  211.   public function attendees(Request $request$auctionId)
  212.   {
  213.     $auction Auction::getById($auctionId);
  214.     $response $this->renderTemplate('auction/auction_attendees.html.twig', [
  215.       'auction' => $auction,
  216.     ]);
  217.     return $response;
  218.   }
  219. }