diff --git a/public/resources/job/add.css b/public/resources/job/add.css new file mode 100644 index 0000000..8d1f9da --- /dev/null +++ b/public/resources/job/add.css @@ -0,0 +1,3 @@ +.crontype-inputs { + display: none; +} \ No newline at end of file diff --git a/public/resources/job/add.js b/public/resources/job/add.js new file mode 100644 index 0000000..2e679ea --- /dev/null +++ b/public/resources/job/add.js @@ -0,0 +1,20 @@ +$(function() { + initDatePickers(); + initCronType(); +}); + +function initDatePickers() +{ + $('#nextrunselector').datetimepicker({format: 'L LTS'}); + $('#lastrunselector').datetimepicker({format: 'L LTS'}); +} + +function initCronType() +{ + $('.crontype-item').on('click', function() { + let type = $(this).data('type'); + $('.crontype').val(type); + $('.crontype-inputs').hide(); + $('.crontype-' + type).show(); + }) +} \ No newline at end of file diff --git a/public/resources/job/index.css b/public/resources/job/index.css new file mode 100644 index 0000000..85d92c6 --- /dev/null +++ b/public/resources/job/index.css @@ -0,0 +1,3 @@ +.text-end { + text-align: right; +} \ No newline at end of file diff --git a/src/Controller/JobController.php b/src/Controller/JobController.php index 913f437..45bfc2e 100644 --- a/src/Controller/JobController.php +++ b/src/Controller/JobController.php @@ -27,6 +27,21 @@ class JobController extends Controller public function addAction() { - return $this->render('job/add.html.twig'); + 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()); + $joboutput = $jobRepo->addJob($allValues); + if($joboutput['success']) { + $this->addFlash('success', $joboutput['message']); + return new RedirectResponse($this->generateRoute('job_index')); + } else { + $this->addFlash('danger', $joboutput['message']); + return new RedirectResponse($this->generateRoute('job_add')); + } + } else { + return new Response('Not implemented yet', Response::HTTP_TOO_EARLY); + } } } \ No newline at end of file diff --git a/src/Repository/Job.php b/src/Repository/Job.php index 180a58a..1294782 100644 --- a/src/Repository/Job.php +++ b/src/Repository/Job.php @@ -4,6 +4,7 @@ namespace JeroenED\Webcron\Repository; +use DateTime; use Doctrine\DBAL\Connection; class Job @@ -26,4 +27,44 @@ class Job } return $jobs; } + + public function addJob(array $values) + { + if(empty($values['type']) || + empty($values['name']) || + empty($values['delay']) || + empty($values['nextrun']) + ) { + return ['success' => false, 'message' => 'Some fields are empty']; + } + + if(empty($values['lastrun'])) { + $values['lastrun'] = NULL; + } else { + $values['lastrun'] = DateTime::createFromFormat('m/d/Y g:i:s A',$values['lastrun'])->getTimestamp(); + } + + $values['nextrun'] = DateTime::createFromFormat('m/d/Y g:i:s A', $values['nextrun'])->getTimestamp(); + $data['type'] = $values['type']; + + switch($data['type']) + { + case 'http': + $parsedUrl = parse_url($values['url']); + $data['url'] = $values['url']; + if(empty($parsedUrl['host'])) { + return ['success' => false, 'message' => 'Some data was invalid']; + } + $data['host'] = $parsedUrl['host']; + break; + } + + $data = json_encode($data); + $addJobSql = "INSERT INTO job(name, data, delay, nextrun, lastrun) VALUES (:name, :data, :delay, :nextrun, :lastrun)"; + + $addJobStmt = $this->dbcon->prepare($addJobSql); + $addJobStmt->execute([':name' => $values['name'], ':data' => $data, ':delay' => $values['delay'], ':nextrun' => $values['nextrun'], ':lastrun' => $values['lastrun'], ]); + + return ['success' => true, 'message' => 'Cronjob succesfully added']; + } } \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index 79fa1e4..e0457a5 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -7,12 +7,10 @@ - {% block extrastyles %}{% endblock %} - {% block extrascripts %}{% endblock %} @@ -36,6 +34,7 @@
+ {{ include('flashes.html.twig') }} {% block content %}{% endblock %}
diff --git a/templates/job/add.html.twig b/templates/job/add.html.twig index 49ad2f2..342cd9f 100644 --- a/templates/job/add.html.twig +++ b/templates/job/add.html.twig @@ -8,16 +8,6 @@ -
- - -
- -
- - -
-
@@ -28,22 +18,41 @@
- +
- - +
- + +
+
+ + +
+
+ {% endblock %} {% block extrastyles %} + {% endblock %} {% block extrascripts %} - + + {% endblock %} \ No newline at end of file diff --git a/templates/job/index.html.twig b/templates/job/index.html.twig index ab3fcf7..7791065 100644 --- a/templates/job/index.html.twig +++ b/templates/job/index.html.twig @@ -45,3 +45,7 @@ loading {% endblock %} + +{% block extrastyles %} + +{% endblock %}