v0.1.2 - Added edit function

fixed create function
Deleted old update
This commit is contained in:
Anders 2021-06-08 14:08:24 +02:00
parent 2c89fb79f6
commit 576992b365
8 changed files with 67 additions and 71 deletions

View File

@ -5,6 +5,9 @@ use Backend\Model\Post;
header('Content-Type: application/json;charset=UTF-8');
if(isset($_POST['id']))
$data = Post::find($_POST['id']);
else
$data = Post::all();
echo $data;

View File

@ -4,14 +4,12 @@ require "../../bootstrap.php";
use Backend\Model\Post;
if(isset($_POST['update'])){
if(isset($_POST['id'])){
$post = Post::query()->find($_POST['id']);
$post->title = $_POST['upTitle'];
$post->description = $_POST['upDescription'];
if(!$post->update()){
echo "Error could not update";
}else{
header("location: ../../Frontend/admin.php");
if($post->update()){
return 1;
}
}

View File

@ -13,9 +13,6 @@ if(isset($_POST['title'])){
$post->description = $_POST['description'];
//Call the save function.
if(!$post->save()){
return http_response_code(400);
}else{
echo $post->id;
}
if($post->save())
echo json_encode(array($post->id, $post->created_at));
}

View File

@ -7,16 +7,20 @@ function createPost(title, desc) {
$.ajax({
url: '../Backend/controller/upload.php',
type: 'POST',
dataType: 'json',
data: data,
success: function (data) {
var d = new Date(data[1])
$('#table_id').DataTable().row.add([
d.getDate() + '/' + (d.getMonth()+1)+'-'+d.getFullYear(),
title,
desc,
'<a class="w-1em" href="edit.php?id='+ data + '"><img src="Assets/Images/Icons/pencil-dark.svg" alt="Edit"></a>',
'<a class="w-1em" onClick="deletePostSwal(' + data + ')"><img src="Assets/Images/Icons/trashcan-dark.svg" alt="Delete"></a>'
'<a class="w-1em" onclick="editPost('+ data[0] + ')"><img src="Assets/Images/Icons/pencil-dark.svg" alt="Edit"></a>',
'<a class="w-1em" onClick="deletePostSwal(' + data[0] + ')"><img src="Assets/Images/Icons/trashcan-dark.svg" alt="Delete"></a>'
]).draw().node().id = '1234';
},
error: function () {
error: function (data) {
console.log("error");
console.log(data);
console.log("error");
}
});

View File

@ -1,21 +1,55 @@
/*function deletePost(id) {
return $.ajax({
function editPost(id) {
$.ajax({
type: 'POST',
url: '../Backend/controller/deletePost.php',
data: {'delete_id':id},
url: '../Backend/controller/getPost.php',
data: { id: id },
success:function (data) {
Swal.fire({
title: 'Rediger Post',
html: '<div class="form-group row mb-4 justify-content-center"><label for="title" class="col-md-12 col-form-label text-md-right">Titel</label><div class="col-md-9"><input type="text" class="form-control" id="title" name="title" maxlength="255" value="'+ data.title +'" required autofocus></div></div><div class="form-group row mb-4 justify-content-center"><label for="title" class="col-md-12 col-form-label text-md-right">Beskrivelse</label><div class="col-md-9"><textarea type="text" class="form-control" id="description" name="description" maxlength="2000" required autofocus>'+ data.description +'</textarea>',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Rediger',
focusConfirm: false,
preConfirm: () => {
const title = Swal.getPopup().querySelector('#title').value
const desc = Swal.getPopup().querySelector('#description').value
if (!title || !desc) {
Swal.showValidationMessage(`Skriv noget i begge felter!`)
} else {
data.title = title;
data.description = desc;
}
return {title: title, desc: desc}
}
}).then((result) => {
$.ajax({
type: 'POST',
url: '../Backend/controller/update.php',
data: {'id':id, 'upTitle': data.title, 'upDescription': data.description},
success:function () {
Swal.fire(
'Posten er slettet!',
'Posten er redigeret!',
'',
'success'
)
).then(function() {
document.getElementById(id).children[1].innerHTML = data.title;
})
},
error:function (data) {
Swal.fire(
'Posten kunne ikke blive slettet',
'Posten kunne ikke blive redigeret',
'',
'error'
)
}
});
}*/
})
},
error:function (data) {
console.log(data);
}
});
/**/
}

View File

@ -23,7 +23,7 @@ $(document).ready(function() {
table.row.add([
d.getDate() + '/' + (d.getMonth()+1)+'-'+d.getFullYear(),
data[i].title,
'<a class="w-1em" href="edit.php?id='+ data[i].id + '"><img src="Assets/Images/Icons/pencil-dark.svg" alt="Edit"></a>',
'<a class="w-1em" onclick="editPost('+ data[i].id + ')"><img src="Assets/Images/Icons/pencil-dark.svg" alt="Edit"></a>',
'<a class="w-1em" onClick="deletePostSwal(' + data[i].id + ')"><img src="Assets/Images/Icons/trashcan-dark.svg" alt="Delete"></a>'
]).draw().node().id = data[i].id;
}

View File

@ -1,13 +0,0 @@
function updatePost(id) {
$.ajax({
type: 'POST',
url: '../Backend/controller/update.php',
data: {'update_id':id},
success:function () {
console.log("Update was a success");
},
error:function (data) {
console.log("Update was not a success");
}
});
}

View File

@ -1,27 +0,0 @@
<?php
require "../Backend/controller/accessControl.php";
require "../bootstrap.php";
use Backend\Model\Post;
$id = $_GET['id'];
$data = Post::query()->find($id);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<form action="../Backend/controller/update.php" method="POST">
<input type="number" name="id" hidden value="<?php echo $id ?>">
<input type="text" name="upTitle" placeholder="Title" value="<?php echo $data->title ?>">
<input type="text" name="upDescription" placeholder="Description" value="<?php echo $data->description; ?>">
<input type="submit" name="update">
</form>
</body>
</html>