283 lines
16 KiB
PHP
283 lines
16 KiB
PHP
<?php
|
|
require "../_config.php";
|
|
require "../vendor/blog-engine/php/Authentication/AuthController.php";
|
|
require "../vendor/blog-engine/php/Posts/PostController.php";
|
|
|
|
use BlogEngine\Posts\PostController;
|
|
use BlogEngine\Authentication\AuthController;
|
|
|
|
session_start();
|
|
|
|
if(!isset($_SESSION['uToken_BE']))
|
|
header("location: ../be_login.php?goto=be_panel.php?s=posts");
|
|
|
|
$auth = new AuthController();
|
|
$conn = $auth->Connection();
|
|
|
|
$postcon = new PostController($auth);
|
|
|
|
$post = null;
|
|
$tags = [];
|
|
$now = Date("Y-m-d H:i", strtotime("now"));
|
|
if(isset($_POST['edit'])){
|
|
$get_post = "SELECT id_post, name, title, content, comments, added_at, updated_at, CASE WHEN status = 'public' THEN '<b>Publiczny</b>' WHEN status = 'archive' THEN '<i>Schowany</i>' ELSE '<i>Wersja robocza</i>' END AS widocznosc FROM posts WHERE id_post = {$_POST['edit']}";
|
|
$res_post = mysqli_fetch_row(mysqli_query($conn, $get_post));
|
|
|
|
$post = [
|
|
"ID" => $res_post[0],
|
|
"Nazwa" => $res_post[1],
|
|
"Tytuł" => $res_post[2],
|
|
"Treść" => $res_post[3],
|
|
"Komentarze" => $res_post[4],
|
|
"Dodano" => $res_post[5],
|
|
"Zaktualizowano" => $res_post[6],
|
|
"Widocznosc" => $res_post[7]
|
|
];
|
|
|
|
$get_tags = "SELECT id_tag, show_name, slug, CASE WHEN (SELECT 1 FROM tag_to_post WHERE id_post = {$post['ID']} and tag_to_post.id_tag = tags.id_tag) = 1 THEN TRUE ELSE FALSE END AS selected FROM tags GROUP BY id_tag;";
|
|
$res_tags = mysqli_query($conn, $get_tags);
|
|
while($row = mysqli_fetch_assoc($res_tags)){
|
|
array_push($tags, [
|
|
"ID" => $row['id_tag'],
|
|
"Nazwa" => $row['show_name'],
|
|
"Kod" => $row['slug'],
|
|
"Aktywne" => $row['selected']
|
|
]);
|
|
}
|
|
} else {
|
|
$get_tags = "SELECT id_tag, show_name, slug FROM tags;";
|
|
$res_tags = mysqli_query($conn, $get_tags);
|
|
while($row = mysqli_fetch_assoc($res_tags)){
|
|
array_push($tags, [
|
|
"ID" => $row['id_tag'],
|
|
"Nazwa" => $row['show_name'],
|
|
"Kod" => $row['slug'],
|
|
"Aktywne" => false
|
|
]);
|
|
}
|
|
}
|
|
|
|
if(isset($_POST['add']))
|
|
$postcon->CreatePost($_POST['title'], $_POST['content'], $_POST['name'], $_POST['tags'], $_POST['comments_status'], (isset($_POST['now_time']) ? true : $_POST['add_time']), "public");
|
|
|
|
if(isset($_POST['draft']))
|
|
$postcon->DraftPost($_POST['draft'], $_POST['title'], $_POST['content'], $_POST['name'], $_POST['tags'], $_POST['comments_status']);
|
|
|
|
if(isset($_POST['delete']))
|
|
$postcon->DeletePost($_POST['delete']);
|
|
|
|
if(isset($_POST['update']))
|
|
$postcon->UpdatePost($_POST['update'], $_POST['title'], $_POST['content'], $_POST['name'], $_POST['tags'], $_POST['comments_status']);
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="pl">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<title>Panel <?php echo BLOG_TITLE ?></title>
|
|
<link rel="stylesheet" href="../vendor/blog-engine/blog.css" />
|
|
<link rel="stylesheet" href="../vendor/summernote/summernote-lite.min.css" />
|
|
|
|
<script src="../vendor/blog-engine/blog.js" defer></script>
|
|
<script src="../vendor/blog-engine/js/jquery-3.7.0.min.js"></script>
|
|
<script src="../vendor/twbs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="../vendor/summernote/summernote-lite.min.js" defer></script>
|
|
|
|
</head>
|
|
<body>
|
|
<div class="container pt-3">
|
|
<div id="alerts"></div>
|
|
<h2 class="mb-5"><button class="btn btn-primary" onclick="location.href='../be_panel.php?s=posts'">Wróć</button> <?php echo ($post != null?"Modyfikowanie posta: '{$post['Tytuł']}'":"Dodawanie nowego posta") ?></h2>
|
|
<form method="POST" action="be_post.php">
|
|
<div class="row mb-3">
|
|
<div class="col-md-8 d-grid">
|
|
<div class="form-floating">
|
|
<input type="text" class="form-control" id="title" name="title" placeholder="Tytuł posta" <?php if($post != null) echo "value='{$post['Tytuł']}'" ?>>
|
|
<label for="title">Tytuł posta</label>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4 d-flex btn btn-group">
|
|
<?php echo ($post != null?"<button type='submit' class='btn btn-success' id='update_it' name='update' value='{$post['ID']}'>Zaktualizuj</button>":"<button type='submit' class='btn btn-success' name='add'>Dodaj</button>") ?>
|
|
<button type="submit" class="btn btn-primary" name="draft" value="<?php echo ($post != null?$post['ID']:0) ?>">Zapisz jako wersja robocza</button>
|
|
<button type="button" data-bs-toggle="modal" data-bs-target="#deletePost" class="btn btn-danger" <?php if($post == null) echo "disabled" ?>>Usuń</button>
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-8">
|
|
<input type="hidden" id="content" name="content" value="<?php if($post != null) echo $post['Treść'] ?>" />
|
|
<div id="summernote"></div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h4 class="card-title">Ustawienia posta</h4>
|
|
<div class='form-check mb-3'><input type='checkbox' id="autosave" class='form-check-input' checked><label for='autosave'>Automatyczne zapisywanie posta do wersji roboczej co 1 minutę</label> </div>
|
|
<input type="hidden" name="id_post" id="id_post" value="<?php echo ($post != null?$post['ID']:0); ?>" />
|
|
<h5>Status posta</h5>
|
|
<p><?php echo ($post != null?$post['Widocznosc']:"W trakcie tworzenia") ?></p>
|
|
<h5>Kto może publikować komentarze</h5>
|
|
<select name="comments_status" id="comments_status" class="form-select mb-3">
|
|
<option value="open" <?php if($post != null && $post['Komentarze'] == 'open') echo "selected" ?>>Każdy</option>
|
|
<option value="registered" <?php if($post != null && $post['Komentarze'] == 'registered') echo "selected" ?>>Zalogowani</option>
|
|
<option value="closed" <?php if($post != null && $post['Komentarze'] == 'closed') echo "selected" ?>>Nikt</option>
|
|
</select>
|
|
<h5>Nazwa linku</h5>
|
|
<input type="text" name="name" id="name" value="<?php if($post != null) echo $post['Nazwa'] ?>" class="form-control mb-3" pattern="^[a-zA-Z0-9_-]*$"/>
|
|
<h5>Tagi posta</h5>
|
|
<div id="tags" class="d-flex">
|
|
<?php
|
|
foreach ($tags as $tag){
|
|
$checked = ($tag['Aktywne']?" checked":"");
|
|
echo "
|
|
<div class='form-check mb-3'>
|
|
<input type='checkbox' class='form-check-input' name='tags[]' id='tag{$tag['ID']}' value='{$tag['ID']}'$checked>
|
|
<label for='tag{$tag['ID']}'>{$tag['Nazwa']} ({$tag['Kod']})</label>
|
|
</div>
|
|
";
|
|
}
|
|
?>
|
|
</div>
|
|
<button type="button" class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#newTag">Utwórz nowy tag</button>
|
|
<h5>Data dodania</h5>
|
|
<?php
|
|
echo ($post != null?
|
|
"<p class='mb-3'>" . date("d M Y H:i:s", strtotime($post['Dodano'])) . "</p>
|
|
":
|
|
"<input type='datetime-local' class='form-control mb-1' name='add_date' value='$now'>
|
|
<div class='form-check'>
|
|
<input type='checkbox' name='now_time' class='form-check-input' checked>
|
|
<label for='now_time'>Godzina dodania równa godzinie naciśnięcia przycisku</label>
|
|
</div>");
|
|
?>
|
|
<div class="modal fade" id="newTag" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h1 class="modal-title fs-5">Tworzenie nowego tagu</h1>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="row">
|
|
<div class="col">
|
|
<h6>Nazwa wyświetlana taga</h6>
|
|
<input type="text" id="show_name" class="form-control mb-3" placeholder="Typowe Posty" />
|
|
</div>
|
|
<div class="col">
|
|
<h6>Nazwa kodowa tagu</h6>
|
|
<input type="text" id="slug" class="form-control mb-3" pattern="^[a-zA-Z0-9_-]*$" placeholder="typowe_posty" />
|
|
</div>
|
|
</div>
|
|
<div class="text-end">
|
|
<button type="button" class="btn btn-primary" data-bs-dismiss="modal" id="addtag">Utwórz tag</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
if($post != null){
|
|
echo '<div class="modal fade" id="deletePost" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h1 class="modal-title fs-5">Czy na pewno chcesz usunąć post?</h1>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<p>Po usunięciu nie odzyskasz już tego posta. Pamiętaj, że możesz zawsze zarchiwizować swój post</p>
|
|
<div class="text-end">
|
|
<button type="submit" class="btn btn-danger" value="' . $post['ID'] . '" name="delete">Zatwierdź usunięcie</button>
|
|
<button type="button" class="btn btn-success" data-bs-dismiss="modal">Anuluj</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>';
|
|
}
|
|
?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
<script>
|
|
let title = document.getElementById('title');
|
|
let insert = document.getElementById('content');
|
|
let autosave_option = document.getElementById("autosave");
|
|
|
|
let autosave = autosave_option.value;
|
|
let autosave_status = "";
|
|
|
|
$(document).ready(function(e){
|
|
$('#summernote').summernote({
|
|
height: 600,
|
|
minHeight: null,
|
|
maxHeight: null,
|
|
focus: true,
|
|
value: "no"
|
|
}).summernote('pasteHTML', '<?php if($post != null) echo $post['Treść'] ?>');
|
|
});
|
|
|
|
$('#summernote').on('summernote.keyup', function(we, e) {
|
|
insert.value = $('#summernote').summernote('code');
|
|
});
|
|
|
|
document.getElementById('addtag').addEventListener('click', function (e){
|
|
createTag(document.getElementById('show_name').value, document.getElementById('slug').value, document.getElementById('tags'));
|
|
}, true);
|
|
|
|
if(document.getElementById("update_it") != null) {
|
|
document.getElementById('update_it').addEventListener('click', function () {
|
|
insert.value = $('#summernote').summernote('code');
|
|
}, true);
|
|
}
|
|
|
|
title.addEventListener('keyup', function (e){
|
|
if(title.value.length < 60)
|
|
document.getElementById('name').value = title.value.toLowerCase().replace(" ", "_");
|
|
}, true);
|
|
|
|
autosave_option.addEventListener('change', () => {
|
|
autosave = autosave_option.value;
|
|
}, true);
|
|
|
|
setInterval(function(){
|
|
if(autosave){
|
|
let tags = document.getElementsByName("tags[]");
|
|
let selected_tags = "";
|
|
for(let i = 0; i < tags.length; i++){
|
|
if(tags[i].checked)
|
|
selected_tags += "&tags[]=" + tags[i].value;
|
|
}
|
|
|
|
let xhttp = new XMLHttpRequest();
|
|
xhttp.open("POST", "../vendor/blog-engine/php/Posts/Calls/AutosaveCall.php");
|
|
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
xhttp.onreadystatechange = function(e){
|
|
if(this.readyState === 4 && this.status === 200){
|
|
let wrapper = document.createElement("div");
|
|
wrapper.innerHTML = "<div class='alert alert-primary' role='alert' id='alert'><div>" + this.responseText + "</div><button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
|
|
document.getElementById('alerts').append(wrapper);
|
|
alert();
|
|
}
|
|
}
|
|
xhttp.send(
|
|
"id=" + document.getElementById("id_post").value +
|
|
"&name=" + document.getElementById("name").value +
|
|
"&title=" + document.getElementById('title').value +
|
|
"&content=" + document.getElementById('content').value +
|
|
"&commentStatus=" + document.getElementById("comments_status").value +
|
|
selected_tags
|
|
);
|
|
}
|
|
}, 60000);
|
|
|
|
function alert(){
|
|
setTimeout(function (){
|
|
document.getElementById('alerts').removeChild(document.getElementById('alerts').firstChild);
|
|
}, 10000);
|
|
}
|
|
</script>
|
|
</html>
|