Added job deletions

This commit is contained in:
Jeroen De Meerleer 2021-05-24 14:08:30 +02:00
parent 8b4f311e5e
commit 4b06060929
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
6 changed files with 58 additions and 6 deletions

View File

@ -26,8 +26,17 @@ job_index:
job_view:
path: '/job/{id}'
methods: [ 'GET' ]
defaults:
_controller: JeroenED\Webcron\Controller\JobController::viewAction
_controller: JeroenED\Webcron\Controller\JobController::jobAction
requirements:
id: \d+
job_delete:
path: '/job/{id}'
methods: [ 'DELETE' ]
defaults:
_controller: JeroenED\Webcron\Controller\JobController::jobAction
requirements:
id: \d+

View File

@ -0,0 +1,21 @@
$(function() {
initDeleteButtons();
})
function initDeleteButtons() {
$('.delete-btn').on('click', function() {
let me = $(this)
let href = me.data('href');
let confirmation = me.data('confirmation');
if(confirm(confirmation)) {
$.ajax({
url: href,
method: 'DELETE',
success: function(data) {
window.location.href = data.return_path;
}
})
}
})
}

View File

@ -5,6 +5,7 @@ namespace JeroenED\Webcron\Controller;
use JeroenED\Framework\Controller;
use JeroenED\Webcron\Repository\Job;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
@ -20,13 +21,21 @@ class JobController extends Controller
return $this->render('job/index.html.twig', ['jobs' => $jobs]);
}
public function viewAction($id)
public function jobAction($id)
{
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
$jobRepo = new Job($this->getDbCon());
$job = $jobRepo->getJob($id);
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')]);
}
}
public function editAction($id)
@ -58,7 +67,7 @@ class JobController extends Controller
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
if($this->getRequest()->getMethod() == 'GET') {
return $this->render('job/add.html.twig');
} elseif ($this->getRequest()->getMethod() == 'POST') {

View File

@ -230,4 +230,14 @@ class Job
}
return $jobRslt;
}
public function deleteJob(int $id)
{
$addJobSql = "DELETE FROM job WHERE id = :id";
$addJobStmt = $this->dbcon->prepare($addJobSql);
$addJobStmt->executeQuery([':id' => $id]);
return ['success' => true, 'message' => 'Cronjob succesfully deleted'];
}
}

View File

@ -9,7 +9,7 @@
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
{% block extrastyles %}{% endblock %}
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
{% block extrascripts %}{% endblock %}

View File

@ -24,7 +24,7 @@
<a href="#" data-id="{{ job.id }}" class="runcron btn btn-outline-primary"><i class="fa fa-play" aria-hidden="true"></i></a>
<a href="{{ path('job_view', {'id': job.id}) }}" class="btn btn-outline-primary"><i class="fa fa-search" aria-hidden="true"></i></a>
<a href="{{ path('job_edit', {'id': job.id}) }}" class="btn btn-outline-primary"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>
<a onclick="return confirm('Are you sure you want to delete this job?')" href="overview.php?id={{ job.id }}&action=delete" class="btn btn-outline-primary"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
<a href="#" data-confirmation="Are you sure you want to delete this job?" data-href="{{ path('job_delete', {'id': job.id}) }}" class="delete-btn btn btn-outline-primary"><i class="fa fa-trash-o" aria-hidden="true"></i></a>
</td>
</tr>
{% endfor %}
@ -53,3 +53,6 @@
{% block extrastyles %}
<link rel="stylesheet" href="/resources/job/index.css" />
{% endblock %}
{% block extrascripts %}
<script type="text/javascript" src="/resources/job/index.js"></script>
{% endblock %}