Neuigkeiten/News/Blog-Editing und Bugfixes

This commit is contained in:
2026-03-26 01:16:56 +01:00
parent b0d7b97c46
commit 351c73778a
10 changed files with 300 additions and 44 deletions

View File

@@ -3,13 +3,73 @@
namespace App\Http\Controllers;
use App\Models\Blog;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
class BlogController extends Controller
{
public function addBlog(Request $request){
if(Auth::check()){
try {
$blog = new Blog();
$blog->published_by = Auth::user()->id;
$blog->language = $request->input('language');
$blog->title = $request->input('title');
$blog->body = $request->input('body');
$blog->published = "1";
$blog->created_at = now();
$blog->updated_at = now();
$blog->save();
return ['messageStatus' => 'success', 'errorMessage' => ''];
} catch (\Illuminate\Database\QueryException $e) {
return ['messageStatus' => 'danger', 'errorMessage' => $e->getMessage()];
}
}
abort(404);
}
public function editBlog(Request $request){
if(Auth::check()){
try {
$blog = Blog::find($request->input('id'));
$blog->title = $request->input('title');
$blog->body = $request->input('body');
$blog->language = $request->input('language');
$blog->published = $request->input('published');
$blog->published_by = Auth::user()->id;
$blog->save();
return ['messageStatus' => 'success', 'errorMessage' => ''];
} catch (\Illuminate\Database\QueryException $e) {
return ['messageStatus' => 'failure', 'errorMessage' => $e->getMessage()];
}
}
abort(404);
}
public function deleteBlog(Request $request){
if(Auth::check()){
try {
$blog = Blog::find($request->input('id'));
if ($blog) {
$blog->delete();
}
return ['messageStatus' => 'success', 'errorMessage' => ''];
} catch(\Exception $e){
return ['messageStatus' => 'failure', 'errorMessage' => $e->getMessage()];
}
}
abort(404);
}
public static function blogPaginated(){
return Blog::where('language', App::getLocale())->where('published', 1)->orderBy('id', 'desc')->paginate(5);
}
public static function blogPaginatedEdit(){
return Blog::orderBy('id', 'desc')->paginate(5);
}
}

View File

@@ -42,7 +42,7 @@ class WebsiteController extends Controller
}
public function news(Request $request){
return view('content.news', ['blogs' => BlogController::blogPaginated()]);
return view('content.news', ['blogs' => BlogController::blogPaginated(App::getLocale(), true)]);
}
public function services(Request $request){
@@ -74,6 +74,11 @@ class WebsiteController extends Controller
}
public function editNews(){
if(Auth::check()){
return response()
->view('blog.edit', ['blogs' => BlogController::blogPaginatedEdit()])
->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
}
abort(404);
}
}

View File

@@ -9,5 +9,8 @@ class Blog extends Model
//
protected $table = 'blogs';
public function byUser(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo('App\Models\User', 'published_by', 'id');
}
}

23
public/css/blog/edit.css Normal file
View File

@@ -0,0 +1,23 @@
.blogEdit {
padding: 0.5em;
border: 1px solid black;
}
.blogEdit button {
padding: 0.7em;
font-size: 1em;
background-color: #ccc;
}
.blogEdit input[type=text] {
font-size: 1em;
}
.blogEdit textarea {
font-size: 1.2em;
}
.blogEdit select {
font-size: 1em;
padding: 0.5em;
}

View File

@@ -83,6 +83,7 @@ html, body {
.eingabemaske {
border: 5px inset;
font-size: 1.2em;
padding: 0.5em;
}
.eingabemaske table, th, td {
@@ -97,6 +98,7 @@ html, body {
padding: 1em;
border: 4px outset;
font-size: 1.2em;
background-color: #ccc;
}
#fehlermeldung {

96
public/js/blog/blog.js Normal file
View File

@@ -0,0 +1,96 @@
$(function() {
$('select[autocomplete="off"]').each(function() {
let selectedValue = $(this).find('option[selected]').val();
if (selectedValue) {
$(this).val(selectedValue);
}
});
});
function addBlog(){
let b = {};
b.title = $('#blogTitle_new').val();
b.body = $('#blogBody_new').val();
b.published = $('#blogPublished_new').is(":checked") ? "1" : "0";
b.language = $('#blogLanguage_new').val();
console.log(b);
if(window.confirm('Eintrag wirklich speichern?')){
$.ajax({
type: "POST",
url: "/intern/nachrichten/add",
data: b,
dataType: "json",
success:function(r){
if(r.messageStatus === "success"){
$('#blogTitle_new').val("");
$('#blogBody_new').val("");
$('#blogLanguage_new').val("de");
location.reload();
} else {
alert(r.errorMessage);
}
},
error:function(r, a, e){
alert(e);
}
});
}
}
function editBlog(blogId){
let b = {};
b.id = blogId;
b.title = $('#blogTitle_'+blogId).val();
b.body = $('#blogBody_'+blogId).val();
b.published = $('#blogPublished_'+blogId).is(":checked") ? "1" : "0";
b.language = $('#blogLanguage_'+blogId).val();
if(window.confirm('Eintrag wirklich speichern?')){
$.ajax({
type: "PUT",
url: "/intern/nachrichten/edit",
data: b,
dataType: "json",
success:function(r){
if(r.messageStatus === "success"){
location.reload();
} else {
alert(r.errorMessage);
}
},
error:function(r, a, e){
alert(e);
}
});
}
}
function deleteBlog(blogId){
let b = {};
b.id = blogId;
if(window.confirm('Eintrag wirklich löschen?')){
$.ajax({
type:"DELETE",
url:"/intern/nachrichten/delete",
data:b,
dataType:"json",
success:function(r){
if(r.messageStatus === "success"){
$('#blogDelete_' + blogId).closest('.blogEdit').fadeOut(300, function() {
$(this).remove();
location.reload();
});
} else {
alert(r.errorMessage);
}
},
error:function(r, a, e){
alert(e);
}
})
}
}

View File

@@ -0,0 +1,63 @@
@extends('layout.app')
@section('scripts')
<link rel="stylesheet" href="{{ asset("css/blog/edit.css") . "?rnd=" . rand(0, 1000000000) }}">
<script src="{{ asset('/js/blog/blog.js') }}"></script>
@endsection
@section('content')
<h2>Post hinzufügen</h2>
<div class="blogEdit">
Veröffentlichen? <input type="checkbox" id="blogPublished_new" checked><br /><br />
Sprache:
<select id="blogLanguage_new" name="language" autocomplete="off">
<option value="de">Deutsch</option>
<option value="en">Englisch</option>
</select><br /><br />
<label for="blogTitle_new">Titel:<br />
</label><input id="blogTitle_new" type="text" value="" placeholder="Titel eingeben...">
<br /><br />
<label for="blogBody_new">Inhalt:</label><br />
<textarea cols="64" rows="8" maxlength="2000" id="blogBody_new" placeholder="Text/HTML eingeben..."></textarea>
<br />
<br />
<button onclick="addBlog()" id="blogEdit_new">Hinzufügen</button><br /><br />
</div>
<h2>Posts ändern/löschen</h2>
<br />
@foreach($blogs as $blog)
<div class="blogEdit">
<b>Erstellt von: {{$blog->byUser()->first()->name}} - Erstellt am: {{ $blog->created_at }} - Verändert am: {{ $blog->updated_at }}</b><br /><br />
Veröffentlicht: <input type="checkbox" id="blogPublished_{{$blog->id}}"{{ $blog->published ? " checked" : "" }}><br /><br />
Sprache:
<select id="blogLanguage_{{ $blog->id }}" name="language" autocomplete="off">
<option value="de"{{ ($blog->language == "de" ? " selected" : "") }}>Deutsch</option>
<option value="en"{{ ($blog->language == "en" ? " selected" : "") }}>Englisch</option>
</select><br /><br />
<button onclick="deleteBlog({{ $blog->id }})" id="blogDelete_{{ $blog->id }}">Löschen</button><br /><br />
<label for="blogTitle_{{ $blog->id }}">Titel:<br />
</label><input id="blogTitle_{{ $blog->id }}" type="text" value="{{ $blog->title }}">
<br /><br />
<label for="blogBody_{{$blog->id}}">Inhalt:</label><br />
<textarea cols="64" rows="8" maxlength="2000" id="blogBody_{{$blog->id}}">{!! $blog->body !!}</textarea>
<br />
<br />
<button onclick="editBlog({{ $blog->id }})" id="blogEdit_{{ $blog->id }}">Speichern</button><br /><br />
</div>
@endforeach
<br /><br />
{{ $blogs->links() }}
<br /><br />
@endsection

View File

@@ -54,6 +54,6 @@
@yield('content')
</div>
</div>
</div>
</body>
</html>

View File

@@ -18,7 +18,7 @@
title="Nur echte Adressen der elektronischen Post verwenden!"/></td>
</tr>
<tr>
<td><label for="username">(Vollständiger) Name:</label></td>
<td><label for="username">Name/Nick:</label></td>
<td><input size="32" type="text" id="username"/></td>
</tr>
<tr>

View File

@@ -1,5 +1,6 @@
<?php
use App\Http\Controllers\BlogController;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\WebsiteController;
use App\Http\Controllers\TickerController;
@@ -32,3 +33,6 @@ Route::put('/intern/ticker/add', [TickerController::class, 'addTicker']);
Route::get('/intern/nachrichten', [WebsiteController::class, 'editNews'])->name('editNews');
Route::post('/intern/nachrichten/add', [BlogController::class, 'addBlog']);
Route::delete('/intern/nachrichten/delete', [BlogController::class, 'deleteBlog']);
Route::put('/intern/nachrichten/edit', [BlogController::class, 'editBlog']);