src/Bidcoz/Bundle/FrontendBundle/Controller/TicketController.php line 29

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\FrontendBundle\Controller;
  3. use Bidcoz\Bundle\CoreBundle\Controller\CoreController;
  4. use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
  5. use Bidcoz\Bundle\CoreBundle\Entity\Organization;
  6. use Bidcoz\Bundle\CoreBundle\Entity\Proxy\Contact;
  7. use Bidcoz\Bundle\CoreBundle\Entity\Proxy\MultipleTicketPurchaseProxy;
  8. use Bidcoz\Bundle\CoreBundle\Entity\Proxy\TicketPurchaseProxy;
  9. use Bidcoz\Bundle\CoreBundle\Entity\Ticket\TicketType;
  10. use Bidcoz\Bundle\FrontendBundle\Form\Type\MultipleTicketPurchaseType;
  11. use Bidcoz\Bundle\FrontendBundle\Form\Type\TicketPurchaseType;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @Route("/{orgSlug}/{campaignSlug}/tickets")
  18.  * @IsGranted("VIEW", subject="organization")
  19.  * @IsGranted("FRONT_END", subject="campaign")
  20.  */
  21. class TicketController extends CoreController
  22. {
  23.     /**
  24.      * @Route("/{ticketTypeIds}", defaults={"ticketTypeIds"=null}, name="campaign_tickets")
  25.      */
  26.     public function ticketsAction(Request $requestOrganization $organizationCampaign $campaign, ?string $ticketTypeIds)
  27.     {
  28.         if ($redirect $this->ticketAvailibility($campaign)) {
  29.             return $redirect;
  30.         }
  31.         if ($ticketTypeIds && !$this->getUser()) {
  32.             $this->addFlash('info''Please login or create an account before purchasing a ticket.');
  33.             throw $this->createAccessDeniedException('Unable to access this page!');
  34.         }
  35.         $ticketTypes       $this->getRepository('Ticket\TicketType')->findCampaignTicketTypes($campaign);
  36.         $ticketsSold       $this->getTicketManager()->getCampaignTicketsSold($campaign);
  37.         $ticketsRemaining  $this->getTicketManager()->getTicketsRemaining($campaign);
  38.         $ticketTypeIdsList $ticketTypeIds explode(','$ticketTypeIds) : [];
  39.         $ticketTypesList   = [];
  40.         $multipleTicketPurchaseProxy = new MultipleTicketPurchaseProxy();
  41.         foreach ($ticketTypeIdsList as $idx => $ticketTypeId) {
  42.             /** @var TicketType $ticketType */
  43.             $ticketType $this->getRepository('Ticket\TicketType')->find($ticketTypeId);
  44.             if (!$ticketType) {
  45.                 $this->addFlash('danger'sprintf('Ticket Level "%s" does not exist.'$ticketTypeId));
  46.                 $ticketTypeIdsList array_diff($ticketTypeIdsList, [$ticketTypeId]);
  47.                 continue;
  48.             } elseif (!in_array($ticketType$ticketTypestrue)) {
  49.                 $this->addFlash('danger'sprintf('Ticket Level "%s" is not available for this campaign.'$ticketType->getName()));
  50.                 $ticketTypeIdsList array_diff($ticketTypeIdsList, [$ticketTypeId]);
  51.                 continue;
  52.             } elseif ($this->getTicketExtension()->isTicketTypeSoldout($ticketType)) {
  53.                 $this->addFlash('danger'sprintf('Ticket Level "%s" is already soldout.'$ticketType->getName()));
  54.                 $ticketTypeIdsList array_diff($ticketTypeIdsList, [$ticketTypeId]);
  55.                 continue;
  56.             }
  57.             $proxy $this->createTicketProxy($ticketType$campaign$idx);
  58.             $multipleTicketPurchaseProxy->addTicketPurchase($proxy);
  59.             $ticketTypesList[$idx] = $ticketType;
  60.         }
  61.         $formOptions $this->getPurchaseTicketFormOptions($campaign);
  62.         $form        $this->getMultiplePurchaseTicketForm($multipleTicketPurchaseProxy$formOptions);
  63.         $form->handleRequest($request);
  64.         if ($form->isSubmitted() && $form->isValid()) {
  65.             if (!$this->getUser()) {
  66.                 $this->addFlash('info''Please login or create an account before purchasing a ticket.');
  67.                 throw $this->createAccessDeniedException('Unable to access this page!');
  68.             }
  69.             $user $this->getUser();
  70.             /** @var MultipleTicketPurchaseProxy $data */
  71.             $data $form->getData();
  72.             // Set the campaign on the contacts
  73.             foreach ($data->getTicketPurchases() as $ticketPurchase) {
  74.                 array_map(function ($contact) use ($campaign) {
  75.                     $contact->setCampaign($campaign);
  76.                 }, $ticketPurchase->getContacts());
  77.                 $this->getTicketManager()->createTicketPurchase($campaign$user$ticketPurchase);
  78.             }
  79.             $this->getEntityManager()->flush();
  80.             return $this->redirectToRoute('account_campaign_purchases', [
  81.                 'orgSlug'      => $organization->getSlug(),
  82.                 'campaignSlug' => $campaign->getSlug(),
  83.             ]);
  84.         }
  85.         return $this->render('@BidcozFrontend/Campaign/Tickets/index.html.twig'array_merge($formOptions, [
  86.             'organization'      => $organization,
  87.             'campaign'          => $campaign,
  88.             'ticketTypes'       => $ticketTypes,
  89.             'ticketsSold'       => $ticketsSold,
  90.             'ticketsRemaining'  => $ticketsRemaining,
  91.             'ticketTypeIdsList' => $ticketTypeIdsList,
  92.             'ticketTypesList'   => $ticketTypesList,
  93.             'form'              => $form->createView(),
  94.         ]));
  95.     }
  96.     /**
  97.      * @Route("/tickets/{ticketId}", name="campaign_ticket_view", methods={"GET"})
  98.      * @ParamConverter("ticketType", class="Bidcoz\Bundle\CoreBundle\Entity\Ticket\TicketType", options={"id" = "ticketId"})
  99.      */
  100.     public function viewTicketAction(Organization $organizationCampaign $campaignTicketType $ticketType)
  101.     {
  102.         if ($redirect $this->ticketAvailibility($campaign)) {
  103.             return $redirect;
  104.         }
  105.         $user $this->getUser();
  106.         if (!$user) {
  107.             $this->messageAccessDeniedException('You must login or create an account to purchase tickets.');
  108.         }
  109.         if ($this->getTicketExtension()->isTicketTypeSoldout($ticketType)) {
  110.             $this->addFlash('danger'sprintf('Ticket Level "%s" is already soldout.'$ticketType->getName()));
  111.             return $this->redirectToRoute('campaign_tickets', [
  112.                 'orgSlug'      => $organization->getSlug(),
  113.                 'campaignSlug' => $campaign->getSlug(),
  114.             ]);
  115.         }
  116.         $proxy       $this->createTicketProxy($ticketType$campaign);
  117.         $formOptions $this->getPurchaseTicketFormOptions($campaign);
  118.         $form        $this->getPurchaseTicketForm($proxy$formOptions);
  119.         return $this->render('@BidcozFrontend/Campaign/Tickets/buy.html.twig'array_merge($formOptions, [
  120.             'organization' => $organization,
  121.             'campaign'     => $campaign,
  122.             'ticketType'   => $ticketType,
  123.             'form'         => $form->createView(),
  124.         ]));
  125.     }
  126.     protected function createTicketProxy(TicketType $ticketTypeCampaign $campaign, ?int $idx null)
  127.     {
  128.         $user      $this->getUser();
  129.         $questions $this->getRepository('Ticket\TicketQuestion')->findCampaignTicketQuestions($campaigntrue);
  130.         if ($ticketType->getQuestionsDoNotApply()) {
  131.             $questions = [];
  132.         }
  133.         $ticketQuestions $ticketGroupQuestions = [];
  134.         foreach ($questions as $question) {
  135.             if ($question->isTicketGroupQuestion()) {
  136.                 $ticketGroupQuestions[] = $question;
  137.             } else {
  138.                 $ticketQuestions[] = $question;
  139.             }
  140.         }
  141.         $ticketGroupAnswers array_map(function ($ticketQuestion) use ($user) {
  142.             $question       $ticketQuestion->getQuestion();
  143.             return $this->getQuestionManager()->createTicketGroupAnswer($question$user);
  144.         }, $ticketGroupQuestions);
  145.         $proxy    = new TicketPurchaseProxy($ticketType$idx);
  146.         $contacts = [];
  147.         for ($x 0$x $ticketType->getNumAdmissions(); ++$x) {
  148.             $ticketAnswers array_map(function ($ticketQuestion) {
  149.                 $question  $ticketQuestion->getQuestion();
  150.                 return $this->getQuestionManager()->createTicketAnswerProxy($question);
  151.             }, $ticketQuestions);
  152.             $contact = new Contact();
  153.             $contact->setTicketAnswers($ticketAnswers);
  154.             if ($campaign->getOrganization()->isEnableCustomizations()) {
  155.                 $contact->setEmail($user->getEmail());
  156.                 $contact->setFirstName($user->getFirstName());
  157.                 $contact->setLastName($user->getLastName());
  158.             }
  159.             $contacts[] = $contact;
  160.         }
  161.         $proxy->setContacts($contacts);
  162.         $proxy->setTicketGroupAnswers($ticketGroupAnswers);
  163.         return $proxy;
  164.     }
  165.     protected function getPurchaseTicketFormOptions(Campaign $campaign)
  166.     {
  167.         $ticketDiscounts       $this->getRepository('Discount\TicketTypeDiscount')->findAllForCampaign($campaign);
  168.         $hasActiveDiscountCode count($ticketDiscounts) > 0;
  169.         return [
  170.             'campaign'           => $campaign,
  171.             'show_discount_code' => $hasActiveDiscountCode,
  172.         ];
  173.     }
  174.     protected function getPurchaseTicketForm(TicketPurchaseProxy $proxy, array $options)
  175.     {
  176.         return $this->createForm(TicketPurchaseType::class, $proxy$options);
  177.     }
  178.     protected function getMultiplePurchaseTicketForm(MultipleTicketPurchaseProxy $multipleTicketPurchaseProxy, array $options)
  179.     {
  180.         return $this->createForm(MultipleTicketPurchaseType::class, $multipleTicketPurchaseProxy$options);
  181.     }
  182.     protected function ticketAvailibility(Campaign $campaign)
  183.     {
  184.         if (!$this->getTicketManager()->areTicketsAvailable($campaign)) {
  185.             $this->addFlash('warning''Tickets are no longer available');
  186.             return $this->redirectToRoute('campaign_home', [
  187.                 'orgSlug'      => $campaign->getOrganization()->getSlug(),
  188.                 'campaignSlug' => $campaign->getSlug(),
  189.             ]);
  190.         }
  191.     }
  192. }