src/Controller/TinyKnightGames/MarketplaceController.php line 22
<?php
namespace App\Controller\TinyKnightGames;
use App\Entity\MarketItem;
use App\Entity\ThetaWallet;
use App\Form\MarketFilterType;
use App\Repository\MarketItemRepository;
use Doctrine\Persistence\ManagerRegistry;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class MarketplaceController extends AbstractController
{
public function __construct(private ManagerRegistry $doctrine) {}
#[Route('/marketplace', name: 'marketplace', requirements: ['domain' => 'tinyknightgames.com|tinyknightgames.wip|tkgstage.com'], host: '{domain}')]
public function index(MarketItemRepository $marketItemRepository, Request $request): Response
{
// $frameId = $request->headers->get('Turbo-Frame');
$form = $this->createForm(MarketFilterType::class);
$form->handleRequest($request);
$params = $request->query->all();
// Form Data
$sort = null;
$genus = null;
$collection = null;
$categories = array();
$status = null;
$state = null;
// Prep Form Data to be sent in additional requests
// This is used in Forever Scroll with Turbo Frame (since the form won't be resubmitted everytime)
$newParams = array();
$newParams['category'] = null; // Initialize and prep for multiple categories
if($data = $form->getData()) {
if($data['sort']) {
$sort = $data['sort'];
$newParams['sort'] = $data['sort'];
}
if($data['collection']){
$collection = $data['collection']->getId(); // EntityType returns Entity from form submission
$newParams['collection'] = $data['collection']->getId();
}
if($data['genus']) {
$genus = $data['genus']->getId(); // EntityType returns Entity from form submission
$newParams['genus'] = $data['genus']->getId();
}
if($data['category']) {
foreach($data['category'] as $category) {
$categories[] = $category->getId();
$newParams['category'][] = $category->getId();
}
}
}
if(!empty($params)) {
if(!empty($params['sort'])) {
$sort = $params['sort'];
$newParams['sort'] = $params['sort'];
}
if(!empty($params['collection'])) {
$collection = $params['collection'];
$newParams['collection'] = $params['collection'];
}
if(!empty($params['genus'])) {
$genus = $params['genus'];
$newParams['genus'] = $params['genus'];
}
if(!empty($params['category'])) {
foreach($params['category'] as $category) {
$newParams['category'][] = $category;
$categories[] = $category;
}
}
if(!empty($params['state'])) {
$state = $params['state'];
$newParams['state'] = $params['state'];
}
}
$queryBuilder = $marketItemRepository->createFindAllQuerybuilder($sort, $collection, $genus, $categories, $state);
$adapter = new QueryAdapter($queryBuilder);
$pagerfanta = Pagerfanta::createForCurrentPageWithMaxPerPage(
$adapter,
$request->query->get('page', 1),
12
);
$response = new Response(null, $form->isSubmitted() ? 422 : 200);
return $this->render('marketplace/index.html.twig', [
// 'forSaleMarketItems' => $forSaleMarketItems,
'pager' => $pagerfanta,
'form' => $form,
'params' => $newParams,
], $response);
}
// TODO change the route to accept a new ENTITY of MarketItem /market/sale/{marketItemId}
// TODO This will sync better with the blockchain
// TODO Add a different route to view NFT assets using this same template
// TODO /explore/asset/{database_id} (will still need to pass in nft_type to differentiate between catchable and equipment
// #[Route('/market/sale/{id}', name: 'market_sale')]
// public function marketSale(Request $request, $id): Response
// {
// $em = $this->getDoctrine()->getManager();
//
// $assetType = $request->query->get('type');
//
// if($assetType == 'catchable') {
// $asset = $em->getRepository(NftCatchable::class)->findOneBy(array('id' => $id));
// $totalMinted = $em->getRepository(NftCatchable::class)->getTotalMinted($asset->getCatchable(), $asset->getSeason());
// return $this->render('marketplace/asset-detail.html.twig', [
// 'asset' => $asset,
// 'assetType' => 'catchable',
// 'marketSale' => true,
// 'totalMinted' => $totalMinted,
// ]);
// } elseif ($assetType == 'equipment') {
// $asset = $em->getRepository(NftEquipment::class)->findOneBy(array('id' => $id));
// $totalMinted = $em->getRepository(NftEquipment::class)->getTotalMinted($asset->getEquipment());
// return $this->render('marketplace/asset-detail.html.twig', [
// 'asset' => $asset,
// 'assetType' => 'equipment',
// 'marketSale' => true,
// 'totalMinted' => $totalMinted,
// ]);
// }
//
// // TODO Redirect to custom 404
// return $this->redirectToRoute('default');
// }
#[Route('/marketplace/sold', name: 'market_sold')]
public function marketSold(Request $request): Response
{
$em = $this->doctrine->getManager();
$soldMarketItems = $em->getRepository(MarketItem::class)->findBy(array('isSold' => true));
return $this->render('marketplace/sold.html.twig', [
'soldMarketItems' => $soldMarketItems,
]);
}
#[Route('/marketplace/approval/{thetaWallet}', name: 'market_approval')]
public function updateIsApprovedMarket(string $thetaWallet, Request $request): Response
{
$em = $this->doctrine->getManager();
$params = $request->query->all();
$thetaWallet = $em->getRepository(ThetaWallet::class)->findOneBy(array('address' => $thetaWallet));
if($thetaWallet) {
if($params['tokenContractId'] === '1') {
$thetaWallet->setIsApprovedForAllMarket2(true);
} else if($params['tokenContractId'] === '4') {
$thetaWallet->setIsApprovedForAllMarketCrate(true);
}
} else {
$thetaWallet = new ThetaWallet();
$thetaWallet->setAddress($thetaWallet);
if($params['tokenContractId'] === '1') {
$thetaWallet->setIsApprovedForAllMarket2(true);
} else if($params['tokenContractId'] === '4') {
$thetaWallet->setIsApprovedForAllMarketCrate(true);
}
}
$em->persist($thetaWallet);
$em->flush();
return new Response("Wallet updated");
}
}