src/Marketplace/Controller/MarketplaceController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Marketplace\Controller;
  3. use App\Marketplace\Form\Type\SearchMarketplaceType;
  4. use App\Marketplace\Service\MarketplaceService;
  5. use App\Profile\Model\ProfileRoles;
  6. use Pimcore\Controller\FrontendController;
  7. use Pimcore\Model\DataObject\TimelinePost;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. #[Route([
  14.   'name' => 'marketplace_',
  15.   'localizedPaths' => [
  16.     'en' => '/{_locale}/marketplace',
  17.     'de' => '/{_locale}/marktplatz',
  18.     'fr' => '/{_locale}/marche',
  19.   ],
  20. ])]
  21. #[IsGranted(ProfileRoles::ROLE_USER)]
  22. class MarketplaceController extends FrontendController
  23. {
  24.   #[Route('/'name'index')]
  25.   public function index(Request $requestMarketplaceService $marketplaceSearchService): Response
  26.   {
  27.     $data = [
  28.       'sorting' => 'sorting.newest',
  29.       'type' => 'servicetype.all',
  30.       'category' => 'servicecategory.all',
  31.       'profile' => $request->get('profileId'),
  32.     ];
  33.     if ($request->query->has('search_marketplace')) {
  34.       $data array_merge($data$request->query->get('search_marketplace'));
  35.     }
  36.     $searchForm $this->createForm(SearchMarketplaceType::class, $data, [
  37.       'method' => 'GET',
  38.       'csrf_protection' => false,
  39.     ]);
  40.     $searchForm->handleRequest($request);
  41.     $items $marketplaceSearchService->search($searchForm->getData());
  42.     $results = [];
  43.     if ($items['meta']['hitCount'] > 0) {
  44.       $idList array_map(fn ($item) => intval($item['_id']), $items['hits']);
  45.       foreach ($idList as $id) {
  46.         $results[] = TimelinePost::getById($id);
  47.       }
  48.     }
  49.     return $this->renderForm('marketplace/index.html.twig', [
  50.       'results' => $results,
  51.       'meta' => $items['meta'],
  52.       'searchForm' => $searchForm,
  53.       'filtersSet' => $request->query->all(),
  54.     ]);
  55.   }
  56.   #[Route('/{id}'name'detail')]
  57.   public function detail($idRequest $requestMarketplaceService $marketplaceSearchService): Response
  58.   {
  59.     $post TimelinePost::getById($id);
  60.     $searchForm $this->createForm(SearchMarketplaceType::class, [], [
  61.       'method' => 'GET',
  62.       'csrf_protection' => false,
  63.     ]);
  64.     $data = [
  65.       'sorting' => 'sorting.newest',
  66.       'type' => 'servicetype.all',
  67.       'category' => 'servicecategory.all',
  68.     ];
  69.     if ($request->query->has('search_marketplace')) {
  70.       $data array_merge($data$request->query->get('search_marketplace'));
  71.       $searchForm->submit($data);
  72.     }
  73.     $actionsForm null;
  74.     if ($this->getUser() && $this->getUser()->getId() == $post->getProfile()->getId()) {
  75.       $actionsForm $this->createFormBuilder(null, [
  76.         'attr' => ['data-turbo' => 'false'],
  77.         'csrf_protection' => false,
  78.       ])
  79.         ->add('markAsSold'SubmitType::class)
  80.         ->add('markAsReserved'SubmitType::class)->getForm();
  81.       $actionsForm->handleRequest($request);
  82.       if ($actionsForm->isSubmitted() && $actionsForm->isValid()) {
  83.         if ($actionsForm->get('markAsSold')->isClicked()) {
  84.           $isSold $post->getPostType()->getServiceTimelinePost()->getIsSold();
  85.           $post->getPostType()->getServiceTimelinePost()->setIsSold(!$isSold);
  86.           $post->save();
  87.         }
  88.         if ($actionsForm->get('markAsReserved')->isClicked()) {
  89.           $isReserved $post->getPostType()->getServiceTimelinePost()->getIsReserved();
  90.           $post->getPostType()->getServiceTimelinePost()->setIsReserved(!$isReserved);
  91.           $post->save();
  92.         }
  93.       }
  94.     }
  95.     return $this->renderForm('marketplace/detail.html.twig', [
  96.       'actionsForm' => $actionsForm,
  97.       'searchForm' => $searchForm,
  98.       'post' => $post,
  99.     ]);
  100.   }
  101.   // #[Route('/profile/{profileId}', name: 'by_profile')]
  102.   // public function marketByProfile(Request $request, MarketplaceService $marketplaceSearchService, $profileId): Response
  103.   // {
  104.   //   $data = [
  105.   //     'sorting' => 'sorting.newest',
  106.   //     'type' => 'servicetype.all',
  107.   //     'category' => 'servicecategory.all',
  108.   //     'profile' => $profileId,
  109.   //   ];
  110.   //   if ($request->query->has('search_marketplace')) {
  111.   //     $data = array_merge($data, $request->query->get('search_marketplace'));
  112.   //   }
  113.   //   $searchForm = $this->createForm(SearchMarketplaceType::class, $data, [
  114.   //     'method' => 'GET',
  115.   //     'csrf_protection' => false,
  116.   //   ]);
  117.   //   $searchForm->handleRequest($request);
  118.   //   $items = $marketplaceSearchService->search($searchForm->getData());
  119.   //   $results = [];
  120.   //   if ($items['meta']['hitCount'] > 0) {
  121.   //     $idList = array_map(fn ($item) => intval($item['_id']), $items['hits']);
  122.   //     foreach ($idList as $id) {
  123.   //       $results[] = TimelinePost::getById($id);
  124.   //     }
  125.   //   }
  126.   //   // $results = array_filter($results, fn ($entry) => $entry->getProfile()->getId() == $profileId);
  127.   //   return $this->renderForm('marketplace/index.html.twig', [
  128.   //     'results' => $results,
  129.   //     'meta' => $items['meta'],
  130.   //     'searchForm' => $searchForm,
  131.   //   ]);
  132.   // }
  133. }