src/Bidcoz/Bundle/FrontendBundle/Controller/SponsorshipController.php line 42

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\Sponsorship\Sponsorship;
  7. use Bidcoz\Bundle\CoreBundle\Entity\User;
  8. use Bidcoz\Bundle\FrontendBundle\Form\Type\SponsorshipType;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * @Route("/{orgSlug}/{campaignSlug}/sponsors")
  15.  * @IsGranted("VIEW", subject="organization")
  16.  * @IsGranted("FRONT_END", subject="campaign")
  17.  */
  18. class SponsorshipController extends CoreController
  19. {
  20.     /**
  21.      * @Route("", name="all_sponsors", methods={"GET"})
  22.      */
  23.     public function searchAction(Organization $organizationCampaign $campaign)
  24.     {
  25.         $sponsorships      $this->getRepository('Sponsorship\Sponsorship')->findActiveForCampaign($campaign);
  26.         $sponsorshipLevels $this->getRepository('Sponsorship\SponsorshipLevel')->findAllActiveForCampaign($campaign);
  27.         return $this->render('@BidcozFrontend/Sponsorship/all.html.twig', [
  28.             'organization' => $organization,
  29.             'campaign'     => $campaign,
  30.             'sponsorships' => $sponsorships,
  31.             'levels'       => $sponsorshipLevels,
  32.         ]);
  33.     }
  34.     /**
  35.      * @Route("/advertise", name="campaign_sponsor")
  36.      */
  37.     public function createAction(Request $requestOrganization $organizationCampaign $campaign)
  38.     {
  39.         $user        $this->getUser();
  40.         $sponsorship $this->createSponsorship($campaign$user);
  41.         $form        $this->getSponsorshipForm($campaign$sponsorship);
  42.         if ('POST' === $request->getMethod()) {
  43.             if (!$user) {
  44.                 $this->messageAccessDeniedException('You must login or create an account to create a sponsorship');
  45.             }
  46.             $form->handleRequest($request);
  47.             if ($form->isSubmitted() && $form->isValid()) {
  48.                 $this->getEntityManager()->persist($sponsorship);
  49.                 $this->getPurchaseManager()->createSponsorshipPurchase($campaign$user$sponsorship);
  50.                 $this->getEntityManager()->flush();
  51.                 return $this->redirectToRoute('account_campaign_purchases', [
  52.                     'orgSlug'      => $organization->getSlug(),
  53.                     'campaignSlug' => $campaign->getSlug(),
  54.                 ]);
  55.             }
  56.         }
  57.         return $this->render('@BidcozFrontend/Sponsorship/new.html.twig', [
  58.             'organization' => $organization,
  59.             'campaign'     => $campaign,
  60.             'sponsorship'  => $sponsorship,
  61.             'levels'       => $this->getRepository('Sponsorship\SponsorshipLevel')->findAllActiveForCampaign($campaign),
  62.             'form'         => $form->createView(),
  63.         ]);
  64.     }
  65.     protected function createSponsorship(Campaign $campaignUser $user null)
  66.     {
  67.         $sponsorship = new Sponsorship($campaign);
  68.         $sponsorship->setActive(false);
  69.         if ($user) {
  70.             $sponsorship->setDonor($user);
  71.         }
  72.         return $sponsorship;
  73.     }
  74.     protected function getSponsorshipForm(Campaign $campaignSponsorship $sponsorship)
  75.     {
  76.         return $this->createForm(SponsorshipType::class, $sponsorship, [
  77.             'campaign' => $campaign,
  78.         ]);
  79.     }
  80. }