src/Controller/Form/EventFormController.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Form;
  3. use App\Common\SqlSrvConnector;
  4. use App\Entity\EventSubmission;
  5. use App\Form\EventType;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Doctrine\Persistence\ObjectManager;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * Class EventFormController
  17.  *
  18.  * @package App\Controller
  19.  */
  20. #[Route(path'/form'name'eventform.')]
  21. #[IsGranted('ROLE_SUPER_ADMIN')]
  22. class EventFormController extends AbstractController
  23. {
  24.     private SqlSrvConnector $connector;
  25.     private ObjectManager $manager;
  26.     public function __construct(ManagerRegistry $doctrine)
  27.     {//ManagerRegistry
  28.         $this->connector = new SqlSrvConnector();
  29.         $this->manager $doctrine->getManager();
  30.     }
  31.     /**
  32.      *
  33.      *
  34.      * @return RedirectResponse|Response
  35.      */
  36.     #[Route(path'/event/'name'form')]
  37.     public function formAction(
  38.         Request $request
  39.     ): RedirectResponse|Response {
  40.         $this->connector->makeConnection();
  41.         $item = new EventSubmission();
  42.         //Process form
  43.         $form $this->createForm(
  44.             EventType::class,
  45.             $item,
  46.             [
  47.                 // Term list query: SELECT DISTINCT ZcoGruppeUserS FROM dbo.ZusatzCodes WHERE ZcoDstIndexL = 38;
  48.                 // 38 is the event module
  49.                 'exhibitions' => $this->getExhibitions(),
  50.                 'seriesTitle' => $this->getSeriesTitles(),
  51.                 'typeOfActivity' => $this->getTypeOfActivity(),
  52.                 'typeOfClass' => $this->getTypeOfClass(),
  53.                 'typeOfPerformance' => $this->getTypeOfPerformance(),
  54.                 'typeOfSocial' => $this->getTypeOfSocial(),
  55.                 'typeOfTalk' => $this->getTypeOfTalk(),
  56.                 'typeOfVisit' => $this->getTypeOfVisit(),
  57.                 'schoolDistrict' => $this->getSchoolDistrict(),
  58.                 'flags' => $this->getFlags(),
  59.                 'specificLocations' => $this->getSpecificLocations(),
  60.                 'audience' => $this->getAudience(),
  61.             ]
  62.         );
  63.         // Pass vars to form
  64.         $form->handleRequest($request);
  65.         if ($form->isSubmitted() && $form->isValid()) {
  66.             $this->manager->persist($item);
  67.             $this->manager->flush();
  68.             $this->manager->clear();
  69.             // TODO email when completed (not while in dev) smaevents@ku.edu
  70.             // TODO set this as en .env file
  71.             return $this->redirect($this->generateUrl('eventform.submit'));
  72.         }
  73.         $this->connector->closeConnection();
  74.         return $this->render(
  75.             'event_form/form.html.twig',
  76.             [
  77.                 'form' => $form->createView(),
  78.             ]
  79.         );
  80.     }
  81.     /**
  82.      * Get all exhibitions
  83.      */
  84.     private function getExhibitions(): array
  85.     {
  86.         $options = [];
  87.         // Fire query
  88.         $query sqlsrv_query(
  89.             $this->connector->getConnection(),
  90.             "
  91.             SELECT
  92.                 Ausstellung.AusId AS ExhibitionID,
  93.                 Ausstellung.AusTitelS AS Title
  94.             FROM Ausstellung
  95.             ORDER BY Title;"
  96.         );
  97.         // Loop through result set
  98.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  99.             $options[$row['Title']] = $row['Title'];
  100.         }
  101.         sqlsrv_free_stmt($query);
  102.         gc_collect_cycles();
  103.         return $options;
  104.     }
  105.     /**
  106.      * Get Event Series titles from M+
  107.      */
  108.     private function getSeriesTitles(): array
  109.     {
  110.         $options = [];
  111.         // Fire query
  112.         $query sqlsrv_query(
  113.             $this->connector->getConnection(),
  114.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Series Title' AND ZcoDstIndexL = 38;"
  115.         );
  116.         // Loop through result set
  117.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  118.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  119.         }
  120.         sqlsrv_free_stmt($query);
  121.         gc_collect_cycles();
  122.         return $options;
  123.     }
  124.     private function getTypeOfActivity(): array
  125.     {
  126.         $options = [];
  127.         // Fire query
  128.         $query sqlsrv_query(
  129.             $this->connector->getConnection(),
  130.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Activity' AND ZcoDstIndexL = 38;"
  131.         );
  132.         // Loop through result set
  133.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  134.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  135.         }
  136.         sqlsrv_free_stmt($query);
  137.         gc_collect_cycles();
  138.         return $options;
  139.     }
  140.     private function getTypeOfClass(): array
  141.     {
  142.         $options = [];
  143.         // Fire query
  144.         $query sqlsrv_query(
  145.             $this->connector->getConnection(),
  146.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Class' AND ZcoDstIndexL = 38;"
  147.         );
  148.         // Loop through result set
  149.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  150.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  151.         }
  152.         sqlsrv_free_stmt($query);
  153.         gc_collect_cycles();
  154.         return $options;
  155.     }
  156.     private function getTypeOfPerformance(): array
  157.     {
  158.         $options = [];
  159.         // Fire query
  160.         $query sqlsrv_query(
  161.             $this->connector->getConnection(),
  162.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Performance' AND ZcoDstIndexL = 38;"
  163.         );
  164.         // Loop through result set
  165.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  166.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  167.         }
  168.         sqlsrv_free_stmt($query);
  169.         gc_collect_cycles();
  170.         return $options;
  171.     }
  172.     private function getTypeOfSocial(): array
  173.     {
  174.         $options = [];
  175.         // Fire query
  176.         $query sqlsrv_query(
  177.             $this->connector->getConnection(),
  178.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Social' AND ZcoDstIndexL = 38;"
  179.         );
  180.         // Loop through result set
  181.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  182.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  183.         }
  184.         sqlsrv_free_stmt($query);
  185.         gc_collect_cycles();
  186.         return $options;
  187.     }
  188.     private function getTypeOfTalk(): array
  189.     {
  190.         $options = [];
  191.         // Fire query
  192.         $query sqlsrv_query(
  193.             $this->connector->getConnection(),
  194.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Talk' AND ZcoDstIndexL = 38;"
  195.         );
  196.         // Loop through result set
  197.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  198.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  199.         }
  200.         sqlsrv_free_stmt($query);
  201.         gc_collect_cycles();
  202.         return $options;
  203.     }
  204.     private function getTypeOfVisit(): array
  205.     {
  206.         $options = [];
  207.         // Fire query
  208.         $query sqlsrv_query(
  209.             $this->connector->getConnection(),
  210.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Visit' AND ZcoDstIndexL = 38;"
  211.         );
  212.         // Loop through result set
  213.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  214.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  215.         }
  216.         sqlsrv_free_stmt($query);
  217.         gc_collect_cycles();
  218.         return $options;
  219.     }
  220.     private function getSchoolDistrict(): array
  221.     {
  222.         $options = [];
  223.         // Fire query
  224.         $query sqlsrv_query(
  225.             $this->connector->getConnection(),
  226.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'School District' AND ZcoDstIndexL = 38;"
  227.         );
  228.         // Loop through result set
  229.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  230.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  231.         }
  232.         sqlsrv_free_stmt($query);
  233.         gc_collect_cycles();
  234.         return $options;
  235.     }
  236.     private function getFlags(): array
  237.     {
  238.         $options = [];
  239.         // Fire query
  240.         $query sqlsrv_query(
  241.             $this->connector->getConnection(),
  242.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Flags' AND ZcoDstIndexL = 38;"
  243.         );
  244.         // Loop through result set
  245.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  246.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  247.         }
  248.         sqlsrv_free_stmt($query);
  249.         gc_collect_cycles();
  250.         return $options;
  251.     }
  252.     private function getSpecificLocations(): array
  253.     {
  254.         $options = [];
  255.         // Fire query
  256.         $query sqlsrv_query(
  257.             $this->connector->getConnection(),
  258.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Specific Location(s)' AND ZcoDstIndexL = 38;"
  259.         );
  260.         // Loop through result set
  261.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  262.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  263.         }
  264.         sqlsrv_free_stmt($query);
  265.         gc_collect_cycles();
  266.         return $options;
  267.     }
  268.     private function getAudience(): array
  269.     {
  270.         $options = [];
  271.         // Fire query
  272.         $query sqlsrv_query(
  273.             $this->connector->getConnection(),
  274.             "SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Audience' AND ZcoDstIndexL = 38;"
  275.         );
  276.         // Loop through result set
  277.         while ($row sqlsrv_fetch_array($querySQLSRV_FETCH_ASSOC)) {
  278.             $options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
  279.         }
  280.         sqlsrv_free_stmt($query);
  281.         gc_collect_cycles();
  282.         return $options;
  283.     }
  284.     #[Route(path'/event/submit'name'submit')]
  285.     public function submitForm(): Response
  286.     {
  287.         return $this->render('event_form/submit.html.twig', []);
  288.     }
  289.     #[Route(path'/admin/event'name'admin')]
  290.     #[IsGranted(data'ROLE_ADMIN')]
  291.     public function index(): Response
  292.     {
  293.         $items $this->manager->getRepository(EventSubmission::class)->findAll();
  294.         return $this->render('event_form/index.html.twig', ['data' => $items]);
  295.     }
  296.     #[Route(path'/admin/event/{objectId<\d+>}'name'view')]
  297.     #[Entity(data'object'expr'repository.find(objectId)')]
  298.     #[IsGranted(data'ROLE_ADMIN')]
  299.     public function view(Request $requestEventSubmission $object): Response
  300.     {
  301.         $this->connector->makeConnection();
  302.         //Process form
  303.         $form $this->createForm(
  304.             EventType::class,
  305.             $object,
  306.             [
  307.                 // Term list query: SELECT DISTINCT ZcoGruppeUserS FROM dbo.ZusatzCodes WHERE ZcoDstIndexL = 38;
  308.                 // 38 is the event module
  309.                 'seriesTitle' => $this->getSeriesTitles(),
  310.                 'typeOfActivity' => $this->getTypeOfActivity(),
  311.                 'typeOfClass' => $this->getTypeOfClass(),
  312.                 'typeOfPerformance' => $this->getTypeOfPerformance(),
  313.                 'typeOfSocial' => $this->getTypeOfSocial(),
  314.                 'typeOfTalk' => $this->getTypeOfTalk(),
  315.                 'typeOfVisit' => $this->getTypeOfVisit(),
  316.                 'schoolDistrict' => $this->getSchoolDistrict(),
  317.                 'flags' => $this->getFlags(),
  318.                 'specificLocations' => $this->getSpecificLocations(),
  319.                 'audience' => $this->getAudience(),
  320.             ]
  321.         );
  322.         // Pass vars to form
  323.         $form->handleRequest($request);
  324.         if ($form->isSubmitted() && $form->isValid()) {
  325.             $this->manager->persist($object);
  326.             $this->manager->flush();
  327.             $this->manager->clear();
  328.             return $this->redirect($this->generateUrl('eventform.admin'));
  329.         }
  330.         $this->connector->closeConnection();
  331.         return $this->render(
  332.             'event_form/form.html.twig',
  333.             [
  334.                 'form' => $form->createView(),
  335.             ]
  336.         );
  337.         // return $this->render('event_form/view.html.twig', ['item' => $object]);
  338.     }
  339.     #[Route(path'/admin/event/{objectId<\d+>}/delete'name'delete')]
  340.     #[Entity(data'object'expr'repository.find(objectId)')]
  341.     #[IsGranted(data'ROLE_ADMIN')]
  342.     public function destroy(EventSubmission $object): RedirectResponse
  343.     {
  344.         $this->manager->remove($object);
  345.         $this->manager->flush();
  346.         return $this->redirectToRoute('eventform.admin', []);
  347.     }
  348. }