diff --git a/public/resources/job/add.js b/public/resources/job/add.js index 91ef8d8..5ea352a 100644 --- a/public/resources/job/add.js +++ b/public/resources/job/add.js @@ -1,6 +1,7 @@ $(function() { initDatePickers(); initCronType(); + initHostType(); initVarInputs(); initIntervalPattern(); bsCustomFileInput.init() @@ -22,6 +23,30 @@ function initCronType() $('.crontype-inputs:not(.hidden) input').prop('disabled', false); $('.crontype-inputs.hidden input').prop('disabled', true); + + if(type != 'http') { + $('.croncategory-group').addClass('btn-group'); + $('.hosttype-group').removeClass('hidden'); + } else { + $('.croncategory-group').removeClass('btn-group'); + $('.hosttype-group').addClass('hidden'); + $('.hosttype-inputs').addClass('hidden'); + + $('.hosttype-inputs:not(.hidden) input').prop('disabled', false); + $('.hosttype-inputs.hidden input').prop('disabled', true); + } + }) +} +function initHostType() +{ + $('.hosttype-item').on('click', function() { + let type = $(this).data('type'); + $('.hosttype').val(type); + $('.hosttype-inputs:not(.hidden)').addClass('hidden'); + $('.hosttype-' + type).removeClass('hidden'); + + $('.hosttype-inputs:not(.hidden) input').prop('disabled', false); + $('.hosttype-inputs.hidden input').prop('disabled', true); }) } diff --git a/src/Repository/Job.php b/src/Repository/Job.php index b63786b..9837a46 100644 --- a/src/Repository/Job.php +++ b/src/Repository/Job.php @@ -30,7 +30,7 @@ class Job public function addJob(array $values) { - if(empty($values['type']) || + if(empty($values['crontype']) || empty($values['name']) || empty($values['interval']) || empty($values['nextrun']) @@ -45,29 +45,12 @@ class Job } $values['nextrun'] = DateTime::createFromFormat('m/d/Y g:i:s A', $values['nextrun'])->getTimestamp(); - $data['type'] = $values['type']; + $data['crontype'] = $values['crontype']; + $data['hosttype'] = $values['hosttype']; - switch($data['type']) + switch($data['crontype']) { - case 'local': - $data['command'] = $values['command']; - $data['host'] = 'localhost'; - break; - case 'ssh': - $data['host'] = $values['host']; - $data['user'] = $values['user']; - if(!empty($values['privkey-password'])) { - $newsecretkey = count($values['var-value']); - $values['var-id'][$newsecretkey] = 'privkey-password'; - $values['var-issecret'][$newsecretkey] = true; - $values['var-value'][$newsecretkey] = $values['privkey-password']; - } - if(!empty($_FILES['privkey']['tmp_name'])) { - $newsecretkey = count($values['var-value']); - $values['var-id'][$newsecretkey] = 'ssh-privkey'; - $values['var-issecret'][$newsecretkey] = true; - $values['var-value'][$newsecretkey] = base64_encode(file_get_contents($_FILES['privkey']['tmp_name'])); - } + case 'command': $data['command'] = $values['command']; break; case 'reboot': @@ -103,6 +86,28 @@ class Job break; } + switch($data['hosttype']) { + case 'local': + $data['host'] = 'localhost'; + break; + case 'ssh': + $data['host'] = $values['host']; + $data['user'] = $values['user']; + if(!empty($values['privkey-password'])) { + $newsecretkey = count($values['var-value']); + $values['var-id'][$newsecretkey] = 'privkey-password'; + $values['var-issecret'][$newsecretkey] = true; + $values['var-value'][$newsecretkey] = $values['privkey-password']; + } + if(!empty($_FILES['privkey']['tmp_name'])) { + $newsecretkey = count($values['var-value']); + $values['var-id'][$newsecretkey] = 'ssh-privkey'; + $values['var-issecret'][$newsecretkey] = true; + $values['var-value'][$newsecretkey] = base64_encode(file_get_contents($_FILES['privkey']['tmp_name'])); + } + break; + } + if(!empty($values['var-value'])) { foreach($values['var-value'] as $key => $name) { if(!empty($name)) { @@ -121,7 +126,7 @@ class Job $addJobSql = "INSERT INTO job(name, data, interval, nextrun, lastrun) VALUES (:name, :data, :interval, :nextrun, :lastrun)"; $addJobStmt = $this->dbcon->prepare($addJobSql); - $addJobStmt->execute([':name' => $values['name'], ':data' => $data, ':interval' => $values['interval'], ':nextrun' => $values['nextrun'], ':lastrun' => $values['lastrun'], ]); + $addJobStmt->executeQuery([':name' => $values['name'], ':data' => $data, ':interval' => $values['interval'], ':nextrun' => $values['nextrun'], ':lastrun' => $values['lastrun'], ]); return ['success' => true, 'message' => 'Cronjob succesfully added']; } diff --git a/templates/job/add.html.twig b/templates/job/add.html.twig index 476fe75..737962f 100644 --- a/templates/job/add.html.twig +++ b/templates/job/add.html.twig @@ -37,58 +37,32 @@

Job details

-
-