Code refactoring and tidying up

Signed-off-by: Luke Tainton <luke@tainton.uk>
This commit is contained in:
2020-08-09 14:52:54 +01:00
parent 20c71572ac
commit 1f59131dc0
11 changed files with 206 additions and 101 deletions

View File

@@ -1,47 +1,6 @@
<?php
$PAGE_NAME = "New request";
require_once __DIR__ . "/../includes/header.php";
use Ramsey\Uuid\Uuid;
// If form submitted, save to database
if($_SERVER['REQUEST_METHOD'] == 'POST') {
try {
// Process ticket data
$tkt_uuid = Uuid::uuid4()->toString();
$stmt = "INSERT INTO tickets (uuid, title, description, created_by) VALUES (:tktuuid, :title, :description, :user)";
$sql = $db->prepare($stmt);
$sql->bindParam(':tktuuid', $tkt_uuid);
$sql->bindParam(':title', $_POST['title']);
$sql->bindParam(':description', $_POST['description']);
$sql->bindParam(':user', $_SESSION['uuid']);
$sql->execute();
} catch (PDOException $e) {
// echo("Error: <br>" . $e->getMessage() . "<br>");
$new_ticket_alert = array("danger", "Failed to save request: " . $e->getMessage());
}
// If file is uploaded, process that
if(isset($_FILES['file']) && $_FILES['file']['name'] != "") {
try {
$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$file_tmp = $_FILES['file']['tmp_name'];
move_uploaded_file($file_tmp,"/srv/attachments/".$file_name);
$stmt = "INSERT INTO ticket_uploads (ticket, user, filename) VALUES (:ticket, :user, :name)";
$sql = $db->prepare($stmt);
$sql->bindParam(':ticket', $tkt_uuid);
$sql->bindParam(':user', $_SESSION['uuid']);
$sql->bindParam(':name', $file_name);
$sql->execute();
} catch (PDOException $e) {
// echo("Error: <br>" . $e->getMessage() . "<br>");
$new_ticket_alert = array("danger", "Failed to upload file: " . $e->getMessage());
}
}
header('Location: /view?rid=' . $tkt_uuid, true);
}
if (!is_signed_in()) {
$new_ticket_alert = array("danger", "You need to log in to access this page.");
@@ -83,7 +42,7 @@
<section>
<div class="card mx-auto" style="width: 50%;margin-bottom: 50px;">
<form style="padding: 2%" action="/new" method="post" enctype="multipart/form-data">
<form style="padding: 2%" action="/actions/create" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title: </label>
<input type="text" class="form-control" id="title" name="title">