From 16088f9fba8e5e82de1e0255f61e3bdbdf97982e Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 15 Apr 2017 12:58:33 +0200 Subject: [PATCH] Added posibility to edit a job --- editjob.php | 99 ++++++++++++++++++++++++++++++++++++ overview.php | 8 +++ templates/editjob.html.twig | 52 +++++++++++++++++++ templates/overview.html.twig | 2 +- 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 editjob.php create mode 100644 templates/editjob.html.twig diff --git a/editjob.php b/editjob.php new file mode 100644 index 0000000..c346119 --- /dev/null +++ b/editjob.php @@ -0,0 +1,99 @@ +. + * + * 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'; \ No newline at end of file diff --git a/overview.php b/overview.php index 7216fe9..6d517d7 100644 --- a/overview.php +++ b/overview.php @@ -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"])); diff --git a/templates/editjob.html.twig b/templates/editjob.html.twig new file mode 100644 index 0000000..74b9492 --- /dev/null +++ b/templates/editjob.html.twig @@ -0,0 +1,52 @@ +{% extends "base.html.twig" %} + +{% block content %} +
+ {% if not error == "" %} +
+ × + Error! {{ error }} +
+ {% endif %} + + + +
+ + +
+
+ + +
+ +
+ +
+ + +
+
+
+ +
+ + + + +
+
+ +
+ +{% endblock %} \ No newline at end of file diff --git a/templates/overview.html.twig b/templates/overview.html.twig index 1d43093..274215f 100644 --- a/templates/overview.html.twig +++ b/templates/overview.html.twig @@ -24,7 +24,7 @@
- +