src/Controller/TinyKnightGames/MarketplaceController.php line 22

  1. <?php
  2. namespace App\Controller\TinyKnightGames;
  3. use App\Entity\MarketItem;
  4. use App\Entity\ThetaWallet;
  5. use App\Form\MarketFilterType;
  6. use App\Repository\MarketItemRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Pagerfanta\Doctrine\ORM\QueryAdapter;
  9. use Pagerfanta\Pagerfanta;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class MarketplaceController extends AbstractController
  15. {
  16.     public function __construct(private ManagerRegistry $doctrine) {}
  17.     #[Route('/marketplace'name'marketplace'requirements: ['domain' => 'tinyknightgames.com|tinyknightgames.wip|tkgstage.com'], host'{domain}')]
  18.     public function index(MarketItemRepository $marketItemRepositoryRequest $request): Response
  19.     {
  20. //        $frameId = $request->headers->get('Turbo-Frame');
  21.         $form $this->createForm(MarketFilterType::class);
  22.         $form->handleRequest($request);
  23.         $params $request->query->all();
  24.         // Form Data
  25.         $sort null;
  26.         $genus null;
  27.         $collection null;
  28.         $categories = array();
  29.         $status null;
  30.         $state null;
  31.         // Prep Form Data to be sent in additional requests
  32.         // This is used in Forever Scroll with Turbo Frame (since the form won't be resubmitted everytime)
  33.         $newParams = array();
  34.         $newParams['category'] = null// Initialize and prep for multiple categories
  35.         if($data $form->getData()) {
  36.             if($data['sort']) {
  37.                 $sort $data['sort'];
  38.                 $newParams['sort'] = $data['sort'];
  39.             }
  40.             if($data['collection']){
  41.                 $collection $data['collection']->getId(); // EntityType returns Entity from form submission
  42.                 $newParams['collection'] = $data['collection']->getId();
  43.             }
  44.             if($data['genus']) {
  45.                 $genus $data['genus']->getId(); // EntityType returns Entity from form submission
  46.                 $newParams['genus'] = $data['genus']->getId();
  47.             }
  48.             if($data['category']) {
  49.                 foreach($data['category'] as $category) {
  50.                     $categories[] = $category->getId();
  51.                     $newParams['category'][] = $category->getId();
  52.                 }
  53.             }
  54.         }
  55.         if(!empty($params)) {
  56.             if(!empty($params['sort'])) {
  57.                 $sort $params['sort'];
  58.                 $newParams['sort'] = $params['sort'];
  59.             }
  60.             if(!empty($params['collection'])) {
  61.                 $collection $params['collection'];
  62.                 $newParams['collection'] = $params['collection'];
  63.             }
  64.             if(!empty($params['genus'])) {
  65.                 $genus $params['genus'];
  66.                 $newParams['genus'] = $params['genus'];
  67.             }
  68.             if(!empty($params['category'])) {
  69.                 foreach($params['category'] as $category) {
  70.                     $newParams['category'][] = $category;
  71.                     $categories[] = $category;
  72.                 }
  73.             }
  74.             if(!empty($params['state'])) {
  75.                 $state $params['state'];
  76.                 $newParams['state'] = $params['state'];
  77.             }
  78.         }
  79.         $queryBuilder $marketItemRepository->createFindAllQuerybuilder($sort$collection$genus$categories$state);
  80.         $adapter = new QueryAdapter($queryBuilder);
  81.         $pagerfanta Pagerfanta::createForCurrentPageWithMaxPerPage(
  82.             $adapter,
  83.             $request->query->get('page'1),
  84.             12
  85.         );
  86.         $response = new Response(null$form->isSubmitted() ? 422 200);
  87.         return $this->render('marketplace/index.html.twig', [
  88. //            'forSaleMarketItems' => $forSaleMarketItems,
  89.               'pager' => $pagerfanta,
  90.               'form' => $form,
  91.               'params' => $newParams,
  92.         ], $response);
  93.     }
  94.     // TODO change the route to accept a new ENTITY of MarketItem /market/sale/{marketItemId}
  95.     // TODO This will sync better with the blockchain
  96.     // TODO Add a different route to view NFT assets using this same template
  97.     // TODO /explore/asset/{database_id} (will still need to pass in nft_type to differentiate between catchable and equipment
  98. //    #[Route('/market/sale/{id}', name: 'market_sale')]
  99. //    public function marketSale(Request $request, $id): Response
  100. //    {
  101. //        $em = $this->getDoctrine()->getManager();
  102. //
  103. //        $assetType = $request->query->get('type');
  104. //
  105. //        if($assetType == 'catchable') {
  106. //            $asset = $em->getRepository(NftCatchable::class)->findOneBy(array('id' => $id));
  107. //            $totalMinted = $em->getRepository(NftCatchable::class)->getTotalMinted($asset->getCatchable(), $asset->getSeason());
  108. //            return $this->render('marketplace/asset-detail.html.twig', [
  109. //                'asset' => $asset,
  110. //                'assetType' => 'catchable',
  111. //                'marketSale' => true,
  112. //                'totalMinted' => $totalMinted,
  113. //            ]);
  114. //        } elseif ($assetType == 'equipment') {
  115. //            $asset = $em->getRepository(NftEquipment::class)->findOneBy(array('id' => $id));
  116. //            $totalMinted = $em->getRepository(NftEquipment::class)->getTotalMinted($asset->getEquipment());
  117. //            return $this->render('marketplace/asset-detail.html.twig', [
  118. //                'asset' => $asset,
  119. //                'assetType' => 'equipment',
  120. //                'marketSale' => true,
  121. //                'totalMinted' => $totalMinted,
  122. //            ]);
  123. //        }
  124. //
  125. //        // TODO Redirect to custom 404
  126. //        return $this->redirectToRoute('default');
  127. //    }
  128.     #[Route('/marketplace/sold'name'market_sold')]
  129.     public function marketSold(Request $request): Response
  130.     {
  131.         $em $this->doctrine->getManager();
  132.         $soldMarketItems $em->getRepository(MarketItem::class)->findBy(array('isSold' => true));
  133.         return $this->render('marketplace/sold.html.twig', [
  134.             'soldMarketItems' => $soldMarketItems,
  135.         ]);
  136.     }
  137.     #[Route('/marketplace/approval/{thetaWallet}'name'market_approval')]
  138.     public function updateIsApprovedMarket(string $thetaWalletRequest $request): Response
  139.     {
  140.         $em $this->doctrine->getManager();
  141.         $params $request->query->all();
  142.         $thetaWallet $em->getRepository(ThetaWallet::class)->findOneBy(array('address' => $thetaWallet));
  143.         if($thetaWallet) {
  144.             if($params['tokenContractId'] === '1') {
  145.                 $thetaWallet->setIsApprovedForAllMarket2(true);
  146.             } else if($params['tokenContractId'] === '4')  {
  147.                 $thetaWallet->setIsApprovedForAllMarketCrate(true);
  148.             }
  149.         } else {
  150.             $thetaWallet = new ThetaWallet();
  151.             $thetaWallet->setAddress($thetaWallet);
  152.             if($params['tokenContractId'] === '1') {
  153.                 $thetaWallet->setIsApprovedForAllMarket2(true);
  154.             } else if($params['tokenContractId'] === '4')  {
  155.                 $thetaWallet->setIsApprovedForAllMarketCrate(true);
  156.             }
  157.         }
  158.         $em->persist($thetaWallet);
  159.         $em->flush();
  160.         return new Response("Wallet updated");
  161.     }
  162. }