Delete Posts

Edit Posts
Back to blog
This commit is contained in:
mmk2410 2015-12-06 15:19:16 +01:00
parent fbca74ccec
commit 6e6e26ad26
11 changed files with 574 additions and 121 deletions

62
rcc/res/delete.js Normal file
View file

@ -0,0 +1,62 @@
/**
* Created by mmk2410 on 12/6/15.
*
* JavaScript for the functionality to delete blogs
*/
function main() {
// listener and function for recieving the posts of the selected blogs
$("#delete_get_posts").click(function () {
var selectedBlog = $("#delete_selected_blog").val();
$.get("res/get_posts.php", {
blog: selectedBlog
}, function (data) {
$("#delete_select_post").remove();
$("#delete_select_post_info").remove();
$("#delete_post_button").remove();
$("#delete_get_posts").after("<p id='delete_select_post'></p>");
$("#delete_get_posts").after("<p id='delete_select_post_info'>Now select the post you want to delete. " +
"Remember that once a post is deleted it can't be restored.</p>");
$("#delete_select_post").append("<select id='delete_selected_post'></select>");
$.each($.parseJSON(data), function (index, value) {
var post = value.substring(0, value.length - 3);
$("#delete_selected_post").append("<option value='" + post + "'>" + post + "</option>");
});
$("#delete_select_post").after("<a class='button' id='delete_post_button' " +
"onclick='deletePostButton()'>DELETE POST</a>")
});
});
}
/**
* Delete the selected posts
*/
function deletePostButton() {
var selectedBlog = $("#delete_selected_blog").val();
var selectedPost = $("#delete_selected_post").val();
$.get("res/delete_post.php", {
blog: selectedBlog,
post: selectedPost
}, function (data) {
$("#delete_select_post").remove();
$("#delete_select_post_info").remove();
$("#delete_post_button").remove();
if (data == "901") {
alert("ERROR 901: No post as get argument given.");
} else if (data == "921") {
alert("ERROR 921: No post with given argument available.");
} else if (data == "941") {
alert("ERROR 941: No blog as get argument given");
} else if (data == "961") {
alert("ERROR 961: Error while deleting the file. Check if the web server has the permission to do so.");
} else if (data == "0") {
alert("Post successfully deleted.");
}
});
}
$(document).ready(main());

25
rcc/res/delete_post.php Normal file
View file

@ -0,0 +1,25 @@
<?php
/**
* User: mmk2410
* Date: 12/6/15
*
* Error Codes:
* 901 No post given as get argument
* 921 No post with the given name available
* 941 No blog given as get argument
* 961 Error while deleting the post
*/
$post = $_GET["post"];
$blog = $_GET["blog"];
if (!isset($post)) {
echo "901";
} else if (!isset($blog)) {
echo "941";
} else if (!file_exists("./../../articles/$blog/$post.md")) {
echo "921";
} else {
if (unlink("./../../articles/$blog/$post.md")) {
echo "0";
}
echo "961";
}

72
rcc/res/edit.js Normal file
View file

@ -0,0 +1,72 @@
/**
* Created by mmk2410 on 12/6/15.
*
* JavaScript for the functionality to delete blogs
*/
function main() {
// listener and function for recieving the posts of the selected blogs
$("#edit_get_posts").click(function () {
var selectedBlog = $("#edit_selected_blog").val();
$.get("res/get_posts.php", {
blog: selectedBlog
}, function (data) {
$("#edit_select_post").remove();
$("#edit_select_post_info").remove();
$("#edit_post_button").remove();
$("#edit_get_posts").after("<p id='edit_select_post'></p>");
$("#edit_get_posts").after("<p id='edit_select_post_info'>Now select the post you want to edit.</p>");
$("#edit_select_post").append("<select id='edit_selected_post'></select>");
$.each($.parseJSON(data), function (index, value) {
var post = value.substring(0, value.length - 3);
$("#edit_selected_post").append("<option value='" + post + "'>" + post + "</option>");
});
$("#edit_select_post").after("<a class='button' id='edit_post_button' " +
"onclick='editPostButton()'>EDIT POST</a>")
});
});
$("#save_changes").click(function () {
var postTitle = $("#title").val();
var postDate = $("#date").val();
var postAuthor = $("#author").val();
var postTags = $("#tags").val();
var postText = $("#text").val();
var file = "../../articles/" + getVariables['blog'] + "/" + getVariables['post'] + ".md";
console.log(file);
$.post("../res/save.php", {
title: postTitle,
date: postDate,
author: postAuthor,
tags: postTags,
text: postText,
file: file
}, function (data) {
if (data == "0") {
alert("File successfully changed.");
window.open("../");
} else if (data == "1") {
alert("Error while saving the changes.");
} else if (data == "-1") {
alert("file");
}
});
});
}
/**
* Delete the selected posts
*/
function editPostButton() {
var selectedBlog = $("#edit_selected_blog").val();
var selectedPost = $("#edit_selected_post").val();
var href = "./edit/?blog=" + selectedBlog + "&post=" + selectedPost;
window.open(href);
}
$(document).ready(main());

25
rcc/res/get_posts.php Normal file
View file

@ -0,0 +1,25 @@
<?php
/**
* User: mmk2410
* Date: 12/6/15
*
* Error Codes:
* 901 No blog given as get argument
* 921 No blog with the given name available
*/
$blog = $_GET["blog"];
if (!isset($blog)) {
echo "901";
} else if (!file_exists("./../../blogs/$blog.md")) {
echo "921";
} else {
$posts = array();
$i = 0;
foreach (scandir("./../../articles/$blog/") as $article) {
if (substr($article, -3) == ".md") {
$posts[$i] = $article;
$i++;
}
}
print json_encode($posts);
}

View file

@ -97,6 +97,15 @@ body{
border-bottom: none!important;
}
.subheadline {
font-size: 20px;
color: #383838 !important;
text-decoration: none;
display: block;
padding-bottom: 6px;
border-bottom: none !important;
}
.card > p{
font-size: 14px;
line-height: 24px;

22
rcc/res/save.php Normal file
View file

@ -0,0 +1,22 @@
<?php
$title = $_POST["title"];
$date = $_POST["date"];
$author = $_POST["author"];
$tags = $_POST["tags"];
$text = $_POST["text"];
$filename = $_POST["file"];
$md = <<<EOD
%TITLE: $title
%DATE: $date
%AUTHOR: $author
%TAGS: $tags
$text
EOD;
if (file_put_contents($filename, $md)) {
echo 0;
} else if (file_exists(($filename))) {
echo 1;
} else {
echo -1;
}