<?php
namespace App\Marketplace\Controller;
use App\Marketplace\Form\Type\SearchMarketplaceType;
use App\Marketplace\Service\MarketplaceService;
use App\Profile\Model\ProfileRoles;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\TimelinePost;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route([
'name' => 'marketplace_',
'localizedPaths' => [
'en' => '/{_locale}/marketplace',
'de' => '/{_locale}/marktplatz',
'fr' => '/{_locale}/marche',
],
])]
#[IsGranted(ProfileRoles::ROLE_USER)]
class MarketplaceController extends FrontendController
{
#[Route('/', name: 'index')]
public function index(Request $request, MarketplaceService $marketplaceSearchService): Response
{
$data = [
'sorting' => 'sorting.newest',
'type' => 'servicetype.all',
'category' => 'servicecategory.all',
'profile' => $request->get('profileId'),
];
if ($request->query->has('search_marketplace')) {
$data = array_merge($data, $request->query->get('search_marketplace'));
}
$searchForm = $this->createForm(SearchMarketplaceType::class, $data, [
'method' => 'GET',
'csrf_protection' => false,
]);
$searchForm->handleRequest($request);
$items = $marketplaceSearchService->search($searchForm->getData());
$results = [];
if ($items['meta']['hitCount'] > 0) {
$idList = array_map(fn ($item) => intval($item['_id']), $items['hits']);
foreach ($idList as $id) {
$results[] = TimelinePost::getById($id);
}
}
return $this->renderForm('marketplace/index.html.twig', [
'results' => $results,
'meta' => $items['meta'],
'searchForm' => $searchForm,
'filtersSet' => $request->query->all(),
]);
}
#[Route('/{id}', name: 'detail')]
public function detail($id, Request $request, MarketplaceService $marketplaceSearchService): Response
{
$post = TimelinePost::getById($id);
$searchForm = $this->createForm(SearchMarketplaceType::class, [], [
'method' => 'GET',
'csrf_protection' => false,
]);
$data = [
'sorting' => 'sorting.newest',
'type' => 'servicetype.all',
'category' => 'servicecategory.all',
];
if ($request->query->has('search_marketplace')) {
$data = array_merge($data, $request->query->get('search_marketplace'));
$searchForm->submit($data);
}
$actionsForm = null;
if ($this->getUser() && $this->getUser()->getId() == $post->getProfile()->getId()) {
$actionsForm = $this->createFormBuilder(null, [
'attr' => ['data-turbo' => 'false'],
'csrf_protection' => false,
])
->add('markAsSold', SubmitType::class)
->add('markAsReserved', SubmitType::class)->getForm();
$actionsForm->handleRequest($request);
if ($actionsForm->isSubmitted() && $actionsForm->isValid()) {
if ($actionsForm->get('markAsSold')->isClicked()) {
$isSold = $post->getPostType()->getServiceTimelinePost()->getIsSold();
$post->getPostType()->getServiceTimelinePost()->setIsSold(!$isSold);
$post->save();
}
if ($actionsForm->get('markAsReserved')->isClicked()) {
$isReserved = $post->getPostType()->getServiceTimelinePost()->getIsReserved();
$post->getPostType()->getServiceTimelinePost()->setIsReserved(!$isReserved);
$post->save();
}
}
}
return $this->renderForm('marketplace/detail.html.twig', [
'actionsForm' => $actionsForm,
'searchForm' => $searchForm,
'post' => $post,
]);
}
// #[Route('/profile/{profileId}', name: 'by_profile')]
// public function marketByProfile(Request $request, MarketplaceService $marketplaceSearchService, $profileId): Response
// {
// $data = [
// 'sorting' => 'sorting.newest',
// 'type' => 'servicetype.all',
// 'category' => 'servicecategory.all',
// 'profile' => $profileId,
// ];
// if ($request->query->has('search_marketplace')) {
// $data = array_merge($data, $request->query->get('search_marketplace'));
// }
// $searchForm = $this->createForm(SearchMarketplaceType::class, $data, [
// 'method' => 'GET',
// 'csrf_protection' => false,
// ]);
// $searchForm->handleRequest($request);
// $items = $marketplaceSearchService->search($searchForm->getData());
// $results = [];
// if ($items['meta']['hitCount'] > 0) {
// $idList = array_map(fn ($item) => intval($item['_id']), $items['hits']);
// foreach ($idList as $id) {
// $results[] = TimelinePost::getById($id);
// }
// }
// // $results = array_filter($results, fn ($entry) => $entry->getProfile()->getId() == $profileId);
// return $this->renderForm('marketplace/index.html.twig', [
// 'results' => $results,
// 'meta' => $items['meta'],
// 'searchForm' => $searchForm,
// ]);
// }
}