<?php
namespace Bidcoz\Bundle\FrontendBundle\Controller\Auction;
use Bidcoz\Bundle\CoreBundle\Controller\CoreController;
use Bidcoz\Bundle\CoreBundle\Entity\Auction\Auction;
use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
use Bidcoz\Bundle\CoreBundle\Entity\Organization;
use Bidcoz\Bundle\FrontendBundle\Filter\ItemFilterType;
use Bidcoz\Bundle\FrontendBundle\Filter\Order\ItemOrderType;
use Doctrine\ORM\QueryBuilder;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/{orgSlug}/{campaignSlug}/auction")
* @IsGranted("VIEW", subject="organization")
* @IsGranted("FRONT_END", subject="campaign")
*/
class AuctionController extends CoreController
{
/**
* @Route("/list", name="auction_catalog")
*/
public function catalogAction(Request $request, Organization $organization, Campaign $campaign, Auction $auction)
{
$qb = $this->getRepository('Auction\Item')->getAuctionQueryBuilder($auction, true, false);
$filterForm = $this->getItemFilterForm($auction);
$hasFilter = $this->applyFilters($request, $filterForm, $qb);
$orderForm = $this->getItemOrderForm($auction);
$hasOrder = $this->applyOrder($request, $orderForm, $qb);
$items = $this->getPagination($qb);
return $this->render('@BidcozFrontend/Auction/catalog.html.twig', [
'type' => 'auction',
'title' => 'Auction Catalog',
'organization' => $organization,
'campaign' => $campaign,
'auction' => $auction,
'filterForm' => $filterForm->createView(),
'orderForm' => $orderForm->createView(),
'items' => $items,
'hasFilter' => $hasFilter || $hasOrder,
'requireCC' => !$this->isGranted('WITH_CC', $campaign),
]);
}
protected function getItemFilterForm(Auction $auction)
{
return $this->createForm(ItemFilterType::class, null, [
'auction' => $auction,
'permission_manager' => $this->getAuctionPermissionManager(),
]);
}
protected function getItemOrderForm(Auction $auction)
{
return $this->createForm(ItemOrderType::class);
}
protected function applyOrder(Request $request, Form $orderForm, QueryBuilder $qb)
{
try {
if ($request->query->has($orderForm->getName())) {
$orderForm->submit($request->query->get($orderForm->getName()));
$sortKey = $orderForm->get('order')->getData();
$orderQuery = ItemOrderType::getMap()[$sortKey];
$fieldsAndOrder = explode(',', $orderQuery);
foreach ($fieldsAndOrder as $idx => $fieldAndOrder) {
[$field, $order] = explode(':', $fieldAndOrder);
// If sorting by bid count, remove fixed priced items
if ('bidCount' === $field) {
$qb->andWhere($qb->expr()->neq('i.saleType', ':fixed_price_sale_type'));
}
if (0 === $idx) {
$qb->orderBy($field, $order);
} else {
$qb->addorderBy($field, $order);
}
}
return true;
}
} catch (\Exception $e) {
}
return false;
}
}