webcron/src/Controller/JobController.php

89 lines
3.4 KiB
PHP
Raw Normal View History

2021-04-06 23:19:51 +02:00
<?php
namespace JeroenED\Webcron\Controller;
use JeroenED\Framework\Controller;
2021-04-08 12:54:49 +02:00
use JeroenED\Webcron\Repository\Job;
2021-05-24 14:08:30 +02:00
use Symfony\Component\HttpFoundation\JsonResponse;
2021-04-06 23:19:51 +02:00
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
2021-04-08 14:54:30 +02:00
class JobController extends Controller
2021-04-06 23:19:51 +02:00
{
2021-04-08 14:54:30 +02:00
public function DefaultAction()
{
2021-04-06 23:19:51 +02:00
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
2021-04-08 12:54:49 +02:00
$jobRepo = new Job($this->getDbCon());
$jobs = $jobRepo->getAllJobs();
return $this->render('job/index.html.twig', ['jobs' => $jobs]);
2021-04-06 23:19:51 +02:00
}
2021-04-08 14:54:30 +02:00
2021-05-24 14:08:30 +02:00
public function jobAction($id)
2021-04-08 14:54:30 +02:00
{
2021-05-24 13:40:42 +02:00
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
2021-04-13 14:44:58 +02:00
$jobRepo = new Job($this->getDbCon());
2021-05-24 14:08:30 +02:00
if($this->getRequest()->getMethod() == 'GET') {
$job = $jobRepo->getJob($id);
return new Response('Not implemented, yet', Response::HTTP_NOT_IMPLEMENTED);
} elseif($this->getRequest()->getMethod() == 'DELETE') {
$success = $jobRepo->deleteJob($id);
$this->addFlash('success', $success['message']);
return new JsonResponse(['return_path' => $this->generateRoute('job_index')]);
}
2021-04-08 14:54:30 +02:00
}
2021-04-12 12:23:50 +02:00
2021-05-21 13:09:48 +02:00
public function editAction($id)
{
2021-05-24 13:40:42 +02:00
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
2021-05-21 13:09:48 +02:00
if($this->getRequest()->getMethod() == 'GET') {
$jobRepo = new Job($this->getDbCon());
$job = $jobRepo->getJob($id, true);
return $this->render('job/edit.html.twig', $job);
} elseif($this->getRequest()->getMethod() == 'POST') {
$allValues = $this->getRequest()->request->all();
$jobRepo = new Job($this->getDbCon());
try {
$joboutput = $jobRepo->editJob($id, $allValues);
} catch (\InvalidArgumentException $e) {
$this->addFlash('danger', $e->getMessage());
return new RedirectResponse($this->generateRoute('job_edit', ['id' => $allValues['id']]));
}
$this->addFlash('success', $joboutput['message']);
return new RedirectResponse($this->generateRoute('job_index'));
}
}
2021-04-12 12:23:50 +02:00
public function addAction()
{
2021-05-24 13:40:42 +02:00
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
2021-05-24 14:08:30 +02:00
2021-04-13 14:07:11 +02:00
if($this->getRequest()->getMethod() == 'GET') {
return $this->render('job/add.html.twig');
} elseif ($this->getRequest()->getMethod() == 'POST') {
$allValues = $this->getRequest()->request->all();
$jobRepo = new Job($this->getDbCon());
2021-05-21 13:09:48 +02:00
try {
$joboutput = $jobRepo->addJob($allValues);
} catch (\InvalidArgumentException $e) {
$this->addFlash('danger', $e->getMessage());
2021-04-13 14:07:11 +02:00
return new RedirectResponse($this->generateRoute('job_add'));
}
2021-05-21 13:09:48 +02:00
$this->addFlash('success', $joboutput['message']);
return new RedirectResponse($this->generateRoute('job_index'));
2021-04-13 14:07:11 +02:00
} else {
return new Response('Not implemented yet', Response::HTTP_TOO_EARLY);
}
2021-04-12 12:23:50 +02:00
}
2021-04-06 23:19:51 +02:00
}