<?php
namespace App\Controller\Form;
use App\Common\SqlSrvConnector;
use App\Entity\EventSubmission;
use App\Form\EventType;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class EventFormController
*
* @package App\Controller
*/
#[Route(path: '/form', name: 'eventform.')]
#[IsGranted('ROLE_SUPER_ADMIN')]
class EventFormController extends AbstractController
{
private SqlSrvConnector $connector;
private ObjectManager $manager;
public function __construct(ManagerRegistry $doctrine)
{//ManagerRegistry
$this->connector = new SqlSrvConnector();
$this->manager = $doctrine->getManager();
}
/**
*
*
* @return RedirectResponse|Response
*/
#[Route(path: '/event/', name: 'form')]
public function formAction(
Request $request
): RedirectResponse|Response {
$this->connector->makeConnection();
$item = new EventSubmission();
//Process form
$form = $this->createForm(
EventType::class,
$item,
[
// Term list query: SELECT DISTINCT ZcoGruppeUserS FROM dbo.ZusatzCodes WHERE ZcoDstIndexL = 38;
// 38 is the event module
'exhibitions' => $this->getExhibitions(),
'seriesTitle' => $this->getSeriesTitles(),
'typeOfActivity' => $this->getTypeOfActivity(),
'typeOfClass' => $this->getTypeOfClass(),
'typeOfPerformance' => $this->getTypeOfPerformance(),
'typeOfSocial' => $this->getTypeOfSocial(),
'typeOfTalk' => $this->getTypeOfTalk(),
'typeOfVisit' => $this->getTypeOfVisit(),
'schoolDistrict' => $this->getSchoolDistrict(),
'flags' => $this->getFlags(),
'specificLocations' => $this->getSpecificLocations(),
'audience' => $this->getAudience(),
]
);
// Pass vars to form
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->manager->persist($item);
$this->manager->flush();
$this->manager->clear();
// TODO email when completed (not while in dev) smaevents@ku.edu
// TODO set this as en .env file
return $this->redirect($this->generateUrl('eventform.submit'));
}
$this->connector->closeConnection();
return $this->render(
'event_form/form.html.twig',
[
'form' => $form->createView(),
]
);
}
/**
* Get all exhibitions
*/
private function getExhibitions(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"
SELECT
Ausstellung.AusId AS ExhibitionID,
Ausstellung.AusTitelS AS Title
FROM Ausstellung
ORDER BY Title;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['Title']] = $row['Title'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
/**
* Get Event Series titles from M+
*/
private function getSeriesTitles(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Series Title' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getTypeOfActivity(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Activity' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getTypeOfClass(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Class' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getTypeOfPerformance(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Performance' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getTypeOfSocial(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Social' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getTypeOfTalk(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Talk' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getTypeOfVisit(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Type of Visit' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getSchoolDistrict(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'School District' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getFlags(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Flags' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getSpecificLocations(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Specific Location(s)' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
private function getAudience(): array
{
$options = [];
// Fire query
$query = sqlsrv_query(
$this->connector->getConnection(),
"SELECT DISTINCT ZcoCodeS FROM dbo.ZusatzCodes WHERE ZcoGruppeUserS = 'Audience' AND ZcoDstIndexL = 38;"
);
// Loop through result set
while ($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
$options[$row['ZcoCodeS']] = $row['ZcoCodeS'];
}
sqlsrv_free_stmt($query);
gc_collect_cycles();
return $options;
}
#[Route(path: '/event/submit', name: 'submit')]
public function submitForm(): Response
{
return $this->render('event_form/submit.html.twig', []);
}
#[Route(path: '/admin/event', name: 'admin')]
#[IsGranted(data: 'ROLE_ADMIN')]
public function index(): Response
{
$items = $this->manager->getRepository(EventSubmission::class)->findAll();
return $this->render('event_form/index.html.twig', ['data' => $items]);
}
#[Route(path: '/admin/event/{objectId<\d+>}', name: 'view')]
#[Entity(data: 'object', expr: 'repository.find(objectId)')]
#[IsGranted(data: 'ROLE_ADMIN')]
public function view(Request $request, EventSubmission $object): Response
{
$this->connector->makeConnection();
//Process form
$form = $this->createForm(
EventType::class,
$object,
[
// Term list query: SELECT DISTINCT ZcoGruppeUserS FROM dbo.ZusatzCodes WHERE ZcoDstIndexL = 38;
// 38 is the event module
'seriesTitle' => $this->getSeriesTitles(),
'typeOfActivity' => $this->getTypeOfActivity(),
'typeOfClass' => $this->getTypeOfClass(),
'typeOfPerformance' => $this->getTypeOfPerformance(),
'typeOfSocial' => $this->getTypeOfSocial(),
'typeOfTalk' => $this->getTypeOfTalk(),
'typeOfVisit' => $this->getTypeOfVisit(),
'schoolDistrict' => $this->getSchoolDistrict(),
'flags' => $this->getFlags(),
'specificLocations' => $this->getSpecificLocations(),
'audience' => $this->getAudience(),
]
);
// Pass vars to form
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->manager->persist($object);
$this->manager->flush();
$this->manager->clear();
return $this->redirect($this->generateUrl('eventform.admin'));
}
$this->connector->closeConnection();
return $this->render(
'event_form/form.html.twig',
[
'form' => $form->createView(),
]
);
// return $this->render('event_form/view.html.twig', ['item' => $object]);
}
#[Route(path: '/admin/event/{objectId<\d+>}/delete', name: 'delete')]
#[Entity(data: 'object', expr: 'repository.find(objectId)')]
#[IsGranted(data: 'ROLE_ADMIN')]
public function destroy(EventSubmission $object): RedirectResponse
{
$this->manager->remove($object);
$this->manager->flush();
return $this->redirectToRoute('eventform.admin', []);
}
}