<?php
namespace Bidcoz\Bundle\FrontendBundle\Controller;
use Bidcoz\Bundle\CoreBundle\Controller\CoreController;
use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
use Bidcoz\Bundle\CoreBundle\Entity\Organization;
use Bidcoz\Bundle\CoreBundle\Entity\Sponsorship\Sponsorship;
use Bidcoz\Bundle\CoreBundle\Entity\User;
use Bidcoz\Bundle\FrontendBundle\Form\Type\SponsorshipType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/{orgSlug}/{campaignSlug}/sponsors")
* @IsGranted("VIEW", subject="organization")
* @IsGranted("FRONT_END", subject="campaign")
*/
class SponsorshipController extends CoreController
{
/**
* @Route("", name="all_sponsors", methods={"GET"})
*/
public function searchAction(Organization $organization, Campaign $campaign)
{
$sponsorships = $this->getRepository('Sponsorship\Sponsorship')->findActiveForCampaign($campaign);
$sponsorshipLevels = $this->getRepository('Sponsorship\SponsorshipLevel')->findAllActiveForCampaign($campaign);
return $this->render('@BidcozFrontend/Sponsorship/all.html.twig', [
'organization' => $organization,
'campaign' => $campaign,
'sponsorships' => $sponsorships,
'levels' => $sponsorshipLevels,
]);
}
/**
* @Route("/advertise", name="campaign_sponsor")
*/
public function createAction(Request $request, Organization $organization, Campaign $campaign)
{
$user = $this->getUser();
$sponsorship = $this->createSponsorship($campaign, $user);
$form = $this->getSponsorshipForm($campaign, $sponsorship);
if ('POST' === $request->getMethod()) {
if (!$user) {
$this->messageAccessDeniedException('You must login or create an account to create a sponsorship');
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getEntityManager()->persist($sponsorship);
$this->getPurchaseManager()->createSponsorshipPurchase($campaign, $user, $sponsorship);
$this->getEntityManager()->flush();
return $this->redirectToRoute('account_campaign_purchases', [
'orgSlug' => $organization->getSlug(),
'campaignSlug' => $campaign->getSlug(),
]);
}
}
return $this->render('@BidcozFrontend/Sponsorship/new.html.twig', [
'organization' => $organization,
'campaign' => $campaign,
'sponsorship' => $sponsorship,
'levels' => $this->getRepository('Sponsorship\SponsorshipLevel')->findAllActiveForCampaign($campaign),
'form' => $form->createView(),
]);
}
protected function createSponsorship(Campaign $campaign, User $user = null)
{
$sponsorship = new Sponsorship($campaign);
$sponsorship->setActive(false);
if ($user) {
$sponsorship->setDonor($user);
}
return $sponsorship;
}
protected function getSponsorshipForm(Campaign $campaign, Sponsorship $sponsorship)
{
return $this->createForm(SponsorshipType::class, $sponsorship, [
'campaign' => $campaign,
]);
}
}