#4: Comments can now be approved and rejected from the front-end gallery
This commit is contained in:
@@ -7,37 +7,73 @@ use App\Facade\UserConfig;
|
||||
use App\Helpers\DbHelper;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\StorePhotoCommentRequest;
|
||||
use App\Photo;
|
||||
use App\PhotoComment;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class PhotoCommentController extends Controller
|
||||
{
|
||||
public function reply(Request $request, $albumUrlAlias, $photoFilename, $commentID)
|
||||
public function moderate(Request $request, $albumUrlAlias, $photoFilename, $commentID)
|
||||
{
|
||||
$album = DbHelper::getAlbumByPath($albumUrlAlias);
|
||||
if (is_null($album))
|
||||
$album = null;
|
||||
|
||||
/** @var Photo $photo */
|
||||
$photo = null;
|
||||
|
||||
/** @var PhotoComment $comment */
|
||||
$comment = null;
|
||||
|
||||
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, $album, $photo, $comment))
|
||||
{
|
||||
App::abort(404);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Gate::denies('moderate-comments', $photo))
|
||||
{
|
||||
App::abort(403);
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->authorizeForUser($this->getUser(), 'view', $album);
|
||||
|
||||
$photo = PhotoController::loadPhotoByAlbumAndFilename($album, $photoFilename);
|
||||
|
||||
if (!UserConfig::get('allow_photo_comments'))
|
||||
if (!$comment->isModerated())
|
||||
{
|
||||
// Not allowed to post comments
|
||||
App::abort(404);
|
||||
if ($request->has('approve'))
|
||||
{
|
||||
$comment->approved_at = new \DateTime();
|
||||
$comment->approved_user_id = $this->getUser()->id;
|
||||
$comment->save();
|
||||
|
||||
$request->getSession()->flash('success', trans('gallery.photo_comment_approved_successfully'));
|
||||
}
|
||||
else if ($request->has('reject'))
|
||||
{
|
||||
$comment->rejected_at = new \DateTime();
|
||||
$comment->rejected_user_id = $this->getUser()->id;
|
||||
$comment->save();
|
||||
|
||||
$request->getSession()->flash('success', trans('gallery.photo_comment_rejected_successfully'));
|
||||
}
|
||||
}
|
||||
|
||||
$comment = $photo->comments()->where('id', $commentID)->first();
|
||||
if (is_null($comment))
|
||||
return redirect($photo->url());
|
||||
}
|
||||
|
||||
public function reply(Request $request, $albumUrlAlias, $photoFilename, $commentID)
|
||||
{
|
||||
$album = null;
|
||||
|
||||
/** @var Photo $photo */
|
||||
$photo = null;
|
||||
|
||||
/** @var PhotoComment $comment */
|
||||
$comment = null;
|
||||
|
||||
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, $album, $photo, $comment))
|
||||
{
|
||||
App::abort(404);
|
||||
return;
|
||||
}
|
||||
|
||||
return Theme::render('partials.photo_comments_reply_form', [
|
||||
@@ -48,21 +84,17 @@ class PhotoCommentController extends Controller
|
||||
|
||||
public function store(Request $request, $albumUrlAlias, $photoFilename)
|
||||
{
|
||||
$album = DbHelper::getAlbumByPath($albumUrlAlias);
|
||||
if (is_null($album))
|
||||
$album = null;
|
||||
|
||||
/** @var Photo $photo */
|
||||
$photo = null;
|
||||
|
||||
/** @var PhotoComment $comment */
|
||||
$comment = null;
|
||||
|
||||
if (!$this->loadAlbumPhotoComment($albumUrlAlias, $photoFilename, 0, $album, $photo, $comment))
|
||||
{
|
||||
App::abort(404);
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->authorizeForUser($this->getUser(), 'view', $album);
|
||||
|
||||
$photo = PhotoController::loadPhotoByAlbumAndFilename($album, $photoFilename);
|
||||
|
||||
if (!UserConfig::get('allow_photo_comments'))
|
||||
{
|
||||
// Not allowed to post comments - redirect back to URL
|
||||
return redirect($photo->url());
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate and link the parent comment, if provided
|
||||
@@ -96,15 +128,32 @@ class PhotoCommentController extends Controller
|
||||
$comment->parent_comment_id = $parentComment->id;
|
||||
}
|
||||
|
||||
// Set the created user ID if we're logged in
|
||||
$user = $this->getUser();
|
||||
if (!is_null($user) && !$user->isAnonymous())
|
||||
{
|
||||
$comment->created_user_id = $user->id;
|
||||
}
|
||||
|
||||
// Auto-approve the comment if we're allowed to moderate comments
|
||||
$isAutoApproved = false;
|
||||
if (Gate::allows('moderate-comments', $photo))
|
||||
{
|
||||
$comment->approved_at = new \DateTime();
|
||||
$comment->approved_user_id = $user->id;
|
||||
$isAutoApproved = true;
|
||||
}
|
||||
|
||||
$comment->save();
|
||||
|
||||
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
|
||||
if ($isAutoApproved)
|
||||
{
|
||||
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully'));
|
||||
}
|
||||
else
|
||||
{
|
||||
$request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully_pending_moderation'));
|
||||
}
|
||||
|
||||
if ($request->isXmlHttpRequest())
|
||||
{
|
||||
@@ -132,4 +181,37 @@ class PhotoCommentController extends Controller
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loadAlbumPhotoComment($albumUrlAlias, $photoFilename, $commentID, &$album, &$photo, &$comment)
|
||||
{
|
||||
$album = DbHelper::getAlbumByPath($albumUrlAlias);
|
||||
if (is_null($album))
|
||||
{
|
||||
App::abort(404);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->authorizeForUser($this->getUser(), 'view', $album);
|
||||
|
||||
$photo = PhotoController::loadPhotoByAlbumAndFilename($album, $photoFilename);
|
||||
|
||||
if (!UserConfig::get('allow_photo_comments'))
|
||||
{
|
||||
// Not allowed to post comments
|
||||
App::abort(404);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (intval($commentID > 0))
|
||||
{
|
||||
$comment = $photo->comments()->where('id', $commentID)->first();
|
||||
if (is_null($comment))
|
||||
{
|
||||
App::abort(404);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+9
-7
@@ -52,13 +52,6 @@ class Photo extends Model
|
||||
return $this->belongsTo(Album::class);
|
||||
}
|
||||
|
||||
public function approvedComments()
|
||||
{
|
||||
return $this->hasMany(PhotoComment::class)
|
||||
->whereNull('parent_comment_id')
|
||||
->whereNotNull('approved_at');
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(PhotoComment::class);
|
||||
@@ -89,6 +82,15 @@ class Photo extends Model
|
||||
return $this->belongsToMany(Label::class, 'photo_labels');
|
||||
}
|
||||
|
||||
public function moderateCommentUrl($commentID = -1)
|
||||
{
|
||||
return route('moderatePhotoComment', [
|
||||
'albumUrlAlias' => $this->album->url_path,
|
||||
'photoFilename' => $this->storage_file_name,
|
||||
'commentID' => $commentID
|
||||
]);
|
||||
}
|
||||
|
||||
public function postCommentUrl()
|
||||
{
|
||||
return route('postPhotoComment', [
|
||||
|
||||
+18
-10
@@ -17,16 +17,6 @@ class PhotoComment extends Model
|
||||
'comment'
|
||||
];
|
||||
|
||||
public function approvedBy()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'approved_user_id');
|
||||
}
|
||||
|
||||
public function approvedChildren()
|
||||
{
|
||||
return $this->children()->whereNotNull('approved_at');
|
||||
}
|
||||
|
||||
public function authorDisplayName()
|
||||
{
|
||||
return is_null($this->createdBy) ? $this->name : $this->createdBy->name;
|
||||
@@ -56,6 +46,24 @@ class PhotoComment extends Model
|
||||
return $depth;
|
||||
}
|
||||
|
||||
public function isApproved()
|
||||
{
|
||||
return (
|
||||
!is_null($this->approved_user_id) &&
|
||||
!is_null($this->approved_at) &&
|
||||
is_null($this->rejected_user_id) &&
|
||||
is_null($this->rejected_at)
|
||||
);
|
||||
}
|
||||
|
||||
public function isModerated()
|
||||
{
|
||||
return (
|
||||
(!is_null($this->approved_user_id) && !is_null($this->approved_at)) ||
|
||||
(!is_null($this->rejected_user_id) && !is_null($this->rejected_at))
|
||||
);
|
||||
}
|
||||
|
||||
public function parent()
|
||||
{
|
||||
return $this->belongsTo(PhotoComment::class, 'parent_comment_id');
|
||||
|
||||
@@ -93,6 +93,17 @@ class AlbumPolicy
|
||||
return $this->userHasPermission($user, $album, 'manipulate-photos');
|
||||
}
|
||||
|
||||
public function moderateComments(User $user, Album $album)
|
||||
{
|
||||
if ($user->id == $album->user_id)
|
||||
{
|
||||
// The album's owner and can do everything
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->userHasPermission($user, $album, 'moderate-comments');
|
||||
}
|
||||
|
||||
public function uploadPhotos(User $user, Album $album)
|
||||
{
|
||||
if ($user->id == $album->user_id)
|
||||
|
||||
@@ -61,4 +61,15 @@ class PhotoPolicy
|
||||
|
||||
return $user->can('manipulate-photos', $photo->album);
|
||||
}
|
||||
|
||||
public function moderateComments(User $user, Photo $photo)
|
||||
{
|
||||
if ($user->id == $photo->user_id)
|
||||
{
|
||||
// The photo's owner can do everything
|
||||
return true;
|
||||
}
|
||||
|
||||
return $user->can('moderate-comments', $photo->album);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user