Explorar o código

Comment moderation

Highlight: context link

SQL looks like this:

```
select * from `comments` where `status` = 0 and `comments`.`deleted_at` is null order by `created_at` desc

select * from `articles` where `articles`.`id` in (2, 1) and `articles`.`deleted_at` is null
```
axkuhta hai 1 ano
pai
achega
9be890081a

+ 27 - 0
app/Http/Controllers/CommentController.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use App\Models\Comment;
+
+class CommentController extends Controller
+{
+    function moderation() {
+		$comments = Comment::with("commentable")->pending()->recent()->get();
+
+		return view("comment_moderation_queue", ["comments" => $comments]);
+    }
+
+    function allow(Comment $comment) {
+		$comment->allow();
+
+		return view("success");
+    }
+
+    function reject(Comment $comment) {
+		$comment->reject();
+
+		return view("success");
+    }
+}

+ 5 - 0
app/Models/Article.php

@@ -41,4 +41,9 @@ class Article extends Model
 		$this->status = static::STATUS_ARCHIVE;
 		$this->save();
 	}
+
+	// Для страницы модерации
+	function link() {
+		return "/article/$this->id";
+	}
 }

+ 18 - 0
app/Models/Comment.php

@@ -21,6 +21,14 @@ class Comment extends Model
 		"status"
 	];
 
+	function commentable() {
+		return $this->morphTo();
+	}
+
+	function scopePending($query) {
+		$query->where("status", static::STATUS_PENDING);
+	}
+
 	function scopePublished($query) {
 		$query->where("status", static::STATUS_PUBLISHED);
 	}
@@ -28,4 +36,14 @@ class Comment extends Model
 	function scopeRecent($query) {
 		$query->orderBy("created_at", "desc");
 	}
+
+	function allow() {
+		$this->status = static::STATUS_PUBLISHED;
+		$this->save();
+	}
+
+	function reject() {
+		$this->status = static::STATUS_REJECTED;
+		$this->save();
+	}
 }

+ 18 - 0
resources/views/comment_moderation_queue.blade.php

@@ -0,0 +1,18 @@
+@extends("layouts.app")
+
+@section("content")
+<h1>Очередь модерации комментариев</h1>
+<p>Здесь перечислены ожидающие модерации комментарии, от новых к старым.</p>
+
+@foreach ($comments as $comment)
+	<div class="comment">
+		<div>{{$comment->name}} ({{$comment->email}}, {{$comment->created_at}})</div>
+		<div>{{$comment->content}}</div>
+		<div>
+			<a href="/comment/{{ $comment->id }}/allow">Разрешить</a> | <a href="/comment/{{ $comment->id }}/reject">Не разрешать</a> | <a href="{{ $comment->commentable->link() }}">Контекст</a>
+		</div>
+		</p>
+	</div>
+@endforeach
+
+@endsection

+ 1 - 0
resources/views/include/header.blade.php

@@ -3,6 +3,7 @@
 <div class="container">
 <a href="/">Главная</a>
 <a href="/articles">Публикации</a>
+<a href="/comments/moderation">Модерация</a>
 <a href="/authors">Авторы</a>
 <a href="/books">Книги</a>
 </div>

+ 5 - 0
routes/web.php

@@ -54,3 +54,8 @@ Route::get('/article/{article}/edit', [Controllers\ArticleController::class, 'ed
 Route::post('/article/{article}/edit', [Controllers\ArticleController::class, 'store']);
 Route::post('/article/{article}/comment', [Controllers\ArticleController::class, 'comment']);
 Route::post('/article/add', [Controllers\ArticleController::class, 'store']);
+
+// Модерация комментариев
+Route::get('/comments/moderation', [Controllers\CommentController::class, 'moderation']);
+Route::get('/comment/{comment}/allow', [Controllers\CommentController::class, 'allow']);
+Route::get('/comment/{comment}/reject', [Controllers\CommentController::class, 'reject']);