src/Bidcoz/Bundle/FrontendBundle/Controller/VolunteerController.php line 37

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\RegistrationPurchaseProxy;
  7. use Bidcoz\Bundle\CoreBundle\Entity\User;
  8. use Bidcoz\Bundle\CoreBundle\Entity\Volunteer\Assignment;
  9. use Bidcoz\Bundle\CoreBundle\Entity\Volunteer\Task;
  10. use Bidcoz\Bundle\FrontendBundle\Form\Type\RegistrationPurchaseType;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @Route("/{orgSlug}/{campaignSlug}/crewup")
  18.  * @IsGranted("VIEW", subject="organization")
  19.  * @IsGranted("FRONT_END", subject="campaign")
  20.  */
  21. class VolunteerController extends CoreController
  22. {
  23.     /**
  24.      * @Route("", name="campaign_volunteer")
  25.      */
  26.     public function volunteerAction(Organization $organizationCampaign $campaign)
  27.     {
  28.         $tasks $this->getRepository('Volunteer\Task')->findAllForCampaign($campaign);
  29.         $loggedUser $this->getUser();
  30.         return $this->render('@BidcozFrontend/Campaign/Volunteer/index.html.twig', [
  31.             'organization' => $organization,
  32.             'campaign'     => $campaign,
  33.             'tasks'        => $tasks,
  34.             'loggedUser'   => $loggedUser
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/task/{taskId}", name="campaign_volunteer_for_task")
  39.      *
  40.      * @ParamConverter("task", class="Bidcoz\Bundle\CoreBundle\Entity\Volunteer\Task", options={"id" = "taskId"})
  41.      */
  42.     public function volunteerForTaskAction(Request $requestOrganization $organizationCampaign $campaignTask $task)
  43.     {
  44.         $user $this->getUser();
  45.         if (!$user) {
  46.             // Redirect unauthenticated users to campaign login with target back to this action
  47.             $path $this->generateUrl('campaign_login_user', [
  48.                 'orgSlug' => $organization->getSlug(),
  49.                 'campaignSlug' => $campaign->getSlug(),
  50.                 '_target_path' => $request->getRequestUri(),
  51.             ]);
  52.             return $this->redirect($path);
  53.         }
  54.         if ('POST' === $request->getMethod()) {
  55.             $assignment $this->getEntityManager()->getRepository(Assignment::class)->findOneBy(['user' => $user'task' => $task]);
  56.             if ($assignment === null) {
  57.                 $this->getVolunteerManager()->createTaskAssignment($task$user);
  58.                 $this->getEntityManager()->flush();
  59.             }
  60.             $this->addFlash('success'sprintf('You have been assigned to %s'$task->getTitle()));
  61.         }
  62.         return $this->redirectToRoute('campaign_volunteer', [
  63.             'orgSlug'      => $organization->getSlug(),
  64.             'campaignSlug' => $campaign->getSlug(),
  65.         ]);
  66.     }
  67.     /**
  68.      * Unassign the current user from a volunteer task.
  69.      *
  70.      * @Route("/task/{taskId}/release", name="campaign_volunteer_release_task")
  71.      * @ParamConverter("task", class="Bidcoz\Bundle\CoreBundle\Entity\Volunteer\Task", options={"id" = "taskId"})
  72.      */
  73.     public function releaseTaskAction(Request $requestOrganization $organizationCampaign $campaignTask $task)
  74.     {
  75.         $user $this->getUser();
  76.         if (!$user) {
  77.             // Redirect unauthenticated users to campaign login with target back to this action
  78.             $path $this->generateUrl('campaign_login_user', [
  79.                 'orgSlug' => $organization->getSlug(),
  80.                 'campaignSlug' => $campaign->getSlug(),
  81.                 '_target_path' => $request->getRequestUri(),
  82.             ]);
  83.             return $this->redirect($path);
  84.         }
  85.         if ('POST' === $request->getMethod()) {
  86.             $em $this->getEntityManager();
  87.             // Enforce 24-hour cutoff based on dateNeededFrom; allow if null
  88.             $startAt $task->getDateNeededFrom();
  89.             if ($startAt instanceof \DateTimeInterface) {
  90.                 $now = new \DateTimeImmutable('now');
  91.                 $diffSeconds $startAt->getTimestamp() - $now->getTimestamp();
  92.                 if ($diffSeconds <= 24 3600) {
  93.                     $contactUrl $this->generateUrl('campaign_contact', [
  94.                         'orgSlug'      => $organization->getSlug(),
  95.                         'campaignSlug' => $campaign->getSlug(),
  96.                     ]);
  97.                     $this->addFlash('warning'"You're an important part of this crew! Since this tasks starts in less than 24 hours, <a href=\"$contactUrl\" class=\"text-primary\">please contact the event administrator</a> if you need to cancel so we can make sure your role is covered.");
  98.                     return $this->redirectToRoute('campaign_volunteer', [
  99.                         'orgSlug'      => $organization->getSlug(),
  100.                         'campaignSlug' => $campaign->getSlug(),
  101.                     ]);
  102.                 }
  103.             }
  104.             // Remove all of the user's assignments for this task (in case of duplicates)
  105.             $assignments $em->getRepository(Assignment::class)->findBy(['user' => $user'task' => $task]);
  106.             if ($assignments) {
  107.                 foreach ($assignments as $assignment) {
  108.                     $em->remove($assignment);
  109.                 }
  110.                 $em->flush();
  111.                 // Notify contact administrators that a spot has opened up
  112.                 $volunteerName method_exists($user'getName') && $user->getName() ? $user->getName() : trim(($user->getFirstName() ?? '') . ' ' . ($user->getLastName() ?? ''));
  113.                 $this->getEmailManager()->sendEmailToAdmins($campaign'signup.cancelled', [
  114.                     'volunteerName' => $volunteerName,
  115.                     'taskName' => $task->getTitle(),
  116.                 ]);
  117.                 $this->addFlash('success'sprintf('You have been unassigned from %s'$task->getTitle()));
  118.             } else {
  119.                 // Not assigned; no action needed, but provide feedback
  120.                 $this->addFlash('info''You were not assigned to this task.');
  121.             }
  122.         }
  123.         return $this->redirectToRoute('campaign_volunteer', [
  124.             'orgSlug'      => $organization->getSlug(),
  125.             'campaignSlug' => $campaign->getSlug(),
  126.         ]);
  127.     }
  128.     protected function createRegistrationProxy(Task $taskCampaign $campaign)
  129.     {
  130.         $user      $this->getUser();
  131.         $questions $this->getRepository('Volunteer\RegistrationQuestion')->findActiveByCampaign($campaign);
  132.         $registrationAnswers array_map(function ($registrationQuestion) use ($user) {
  133.             $question $registrationQuestion->getQuestion();
  134.             return $this->getQuestionManager()->createRegistrationAnswer($question$user);
  135.         }, $questions);
  136.         $proxy = new RegistrationPurchaseProxy($task);
  137.         $proxy->setRegistrationAnswers($registrationAnswers);
  138.         return $proxy;
  139.     }
  140.     protected function getPurchaseRegistrationFormOptions(Campaign $campaignTask $taskUser $user)
  141.     {
  142.         return [
  143.             'require_name'    => $task->getRequireName(),
  144.             'require_address' => $task->getRequireAddress(),
  145.             'require_email'   => $task->getRequireEmail(),
  146.             'require_phone'   => $task->getRequirePhone(),
  147.             'require_waiver'  => $campaign->getRequireRegistrationWaiver(),
  148.             'waiver_text'     => $campaign->getRegistrationWaiver(),
  149.             'user'            => $user,
  150.         ];
  151.     }
  152.     protected function getPurchaseRegistrationForm(RegistrationPurchaseProxy $proxy, array $options)
  153.     {
  154.         return $this->createForm(RegistrationPurchaseType::class, $proxy$options);
  155.     }
  156. }