+
diff --git a/app/Http/Controllers/BlogController.php b/app/Http/Controllers/BlogController.php index 3bb4dd5..f7b249d 100644 --- a/app/Http/Controllers/BlogController.php +++ b/app/Http/Controllers/BlogController.php @@ -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); + } } diff --git a/app/Http/Controllers/WebsiteController.php b/app/Http/Controllers/WebsiteController.php index 5529d31..5047dbb 100644 --- a/app/Http/Controllers/WebsiteController.php +++ b/app/Http/Controllers/WebsiteController.php @@ -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); } } diff --git a/app/Models/Blog.php b/app/Models/Blog.php index cd00ee9..715b7e8 100644 --- a/app/Models/Blog.php +++ b/app/Models/Blog.php @@ -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'); + } } diff --git a/public/css/blog/edit.css b/public/css/blog/edit.css new file mode 100644 index 0000000..b583bf5 --- /dev/null +++ b/public/css/blog/edit.css @@ -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; +} diff --git a/public/css/style.css b/public/css/style.css index 28caec9..4658128 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -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 { diff --git a/public/js/blog/blog.js b/public/js/blog/blog.js new file mode 100644 index 0000000..a389251 --- /dev/null +++ b/public/js/blog/blog.js @@ -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); + } + }) + } +} diff --git a/resources/views/blog/edit.blade.php b/resources/views/blog/edit.blade.php new file mode 100644 index 0000000..681a9b2 --- /dev/null +++ b/resources/views/blog/edit.blade.php @@ -0,0 +1,63 @@ +@extends('layout.app') +@section('scripts') + + +@endsection + +@section('content') + +