Added posibility to edit a job

This commit is contained in:
Jeroen De Meerleer 2017-04-15 12:58:33 +02:00
parent aa2d95a6a1
commit 16088f9fba
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 160 additions and 1 deletions

99
editjob.php Normal file
View File

@ -0,0 +1,99 @@
<?php
/*
* The MIT License
*
* Copyright 2017 Jeroen De Meerleer <me@jeroened.be>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
require_once "include/initialize.inc.php";
$jobID = $_GET['jobID'];
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$jobnameqry = $db->prepare("SELECT * FROM jobs WHERE jobID = ?");
$jobnameqry->execute(array($_GET['jobID']));
$jobnameResult = $jobnameqry->fetchAll(PDO::FETCH_ASSOC);
if ($jobnameResult[0]["user"] != $_SESSION["userID"]) {
header("location:/overview.php");
exit;
}
$name = $jobnameResult[0]['name'];
$url = $jobnameResult[0]['url'];
$delay = $jobnameResult[0]['delay'];
$nextrun = date("m/d/Y h:i A", $jobnameResult[0]['nextrun']);
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array('cache' => 'cache', "debug" => true));
$error = "";
if ($_GET["error"]) {
switch ($_GET["error"]) {
case "emptyfields":
$error = "Some fields were empty"; break;
case "invalidurl":
$error = "The URL is invalid"; break;
case "invaliddelay":
$error = "The delay is invalid"; break;
}
}
echo $twig->render('editjob.html.twig', array("name" => $name, "url" => $url, "delay" => $delay, "nextrun" => $nextrun, "jobID" => $jobID, "error" => $error));
}
elseif ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST['name']) || empty($_POST['url'] || empty($_POST['delay']))) {
header("location:addjob.php?error=emptyfields");
exit;
}
$url = $_POST['url'];
$name = $_POST['name'];
$delay = $_POST['delay'];
$nextrunObj = new DateTime($_POST['nextrun']);
$nextrun = $nextrunObj->getTimestamp();
if(filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
header("location:editjob.php?jobID=" . $jobID . "&error=invalidurl");
exit;
}
if(!is_numeric($delay)) {
header("location:editjob.php?jobID=" . $jobID . "&error=invaliddelay");
exit;
}
if(!is_numeric($nextrun)) {
header("location:editjob.php?jobID=" . $jobID . "&error=invalidnextrun");
exit;
}
$stmt = $db->prepare("UPDATE jobs SET name = ?, url = ?, delay = ?, nextrun = ? WHERE jobID = ?");
$stmt->execute(array($name, $url, $delay, $nextrun, $jobID));
header("location:overview.php?message=edited");
exit;
}
require_once 'include/finalize.inc.php';

View File

@ -37,6 +37,14 @@ if (isset($_GET['action'])) {
$message = "Job was sucessfully deleted";
}
}
$message = "";
if ($_GET["message"]) {
switch ($_GET["message"]) {
case "edited":
$message = "The cronjob has been edited"; break;
}
}
$allJobs = $db->prepare("SELECT * FROM jobs WHERE user = ?");
$allJobs->execute(array($_SESSION["userID"]));

View File

@ -0,0 +1,52 @@
{% extends "base.html.twig" %}
{% block content %}
<form class="form-horizontal" method="post" action="/editjob.php?jobID={{ jobID }}">
{% if not error == "" %}
<div class="alert alert-danger fade in">
<a href="#" class="close" data-dismiss="alert">&times;</a>
<strong>Error!</strong> {{ error }}
</div>
{% endif %}
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="blah" value="{{ name }}">
</div>
<div class="form-group">
<label for="url">url</label>
<input type="url" name="url" class="form-control" id="url" placeholder="https://" value="{{ url }}">
</div>
<div class="form-group">
<label for="name">Delay (in seconds)</label>
<div class="input-group">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Pattern <span class="caret"></span></button>
<ul class="dropdown-menu" id="patternDropdown">
<li data-val="60"><a href="#">Every minute</a></li>
<li data-val="3600"><a href="#">Every hour</a></li>
<li data-val="86400"><a href="#">Every day</a></li>
<li data-val="604800"><a href="#">Every week</a></li>
<li data-val="2419200"><a href="#">Every 4 weeks</a></li>
<li data-val="custom"><a href="#">Custom</a></li>
</ul>
</div><!-- /btn-group -->
<input type="number" class="form-control" id="delay" name="delay" value="{{ delay }}">
</div>
</div>
<div class="form-group">
<label for="nextrun">Next run</label>
<div class="input-group date" id="nextrunselector">
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
<input type="text" class="form-control" name="nextrun" value="{{ nextrun }}">
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% endblock %}

View File

@ -24,7 +24,7 @@
<td>
<div class="btn-group" role="group" aria-label="...">
<a href="runs.php?jobID={{ job.jobID }}" class="btn btn-default"><span class="glyphicon glyphicon-align-justify"></span></a>
<a href="overview.php?jobID={{ job.jobID }}&action=edit" class="btn btn-default"><span class="glyphicon glyphicon-edit"><span></a>
<a href="editjob.php?jobID={{ job.jobID }}" class="btn btn-default"><span class="glyphicon glyphicon-edit"><span></a>
<a onclick="return confirm('Are you sure you want to delete this job?')" href="overview.php?jobID={{ job.jobID }}&action=delete" class="btn btn-default"><span class="glyphicon glyphicon-remove"></span></a>
</div>
</td>