diff --git a/app/Http/Controllers/Gallery/PhotoCommentController.php b/app/Http/Controllers/Gallery/PhotoCommentController.php index 0642147..9a477bc 100644 --- a/app/Http/Controllers/Gallery/PhotoCommentController.php +++ b/app/Http/Controllers/Gallery/PhotoCommentController.php @@ -10,6 +10,8 @@ use App\Helpers\PermissionsHelper; use App\Http\Controllers\Controller; use App\Http\Requests\StorePhotoCommentRequest; use App\Mail\ModeratePhotoComment; +use App\Mail\PhotoCommentApproved; +use App\Mail\PhotoCommentApprovedUser; use App\Permission; use App\Photo; use App\PhotoComment; @@ -38,7 +40,7 @@ class PhotoCommentController extends Controller return null; } - if (Gate::denies('moderate-comments', $photo)) + if (!User::currentOrAnonymous()->can('moderate-comments', $photo)) { App::abort(403); return null; @@ -52,6 +54,7 @@ class PhotoCommentController extends Controller $comment->approved_user_id = $this->getUser()->id; $comment->save(); + $this->notifyAlbumOwner($album, $photo, $comment); $request->getSession()->flash('success', trans('gallery.photo_comment_approved_successfully')); } else if ($request->has('reject')) @@ -124,7 +127,6 @@ class PhotoCommentController extends Controller if (is_null($parentComment)) { - //TODO $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully')); return redirect($photo->url()); } } @@ -158,7 +160,7 @@ class PhotoCommentController extends Controller // Auto-approve the comment if we're allowed to moderate comments $isAutoApproved = false; - if (Gate::allows('moderate-comments', $photo)) + if (User::currentOrAnonymous()->can('moderate-comments', $photo)) { $comment->approved_at = new \DateTime(); $comment->approved_user_id = $user->id; @@ -179,27 +181,25 @@ class PhotoCommentController extends Controller $isAutoApproved = true; } + $comment->save(); + // Send notification e-mails to moderators or album owner if (!$isAutoApproved) { $this->notifyAlbumModerators($album, $photo, $comment); - } - - $comment->save(); - - if ($isAutoApproved) - { - $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully')); + $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully_pending_moderation')); } else { - $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully_pending_moderation')); + $this->notifyAlbumOwner($album, $photo, $comment); + $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully')); } if ($request->isXmlHttpRequest()) { return response()->json(['redirect_url' => $photo->url()]); - } else + } + else { return redirect($photo->url()); } @@ -275,6 +275,24 @@ class PhotoCommentController extends Controller } } + /** + * Sends an e-mail notification to an album's owned that a comment has been posted/approved. + * @param Album $album + * @param Photo $photo + * @param PhotoComment $comment + */ + private function notifyAlbumOwner(Album $album, Photo $photo, PhotoComment $comment) + { + $owner = $album->user; + + $poster = new User(); + $poster->name = $comment->authorDisplayName(); + $poster->email = $comment->authorEmail(); + + Mail::to($owner)->send(new PhotoCommentApproved($owner, $album, $photo, $comment)); + Mail::to($poster)->send(new PhotoCommentApprovedUser($poster, $album, $photo, $comment)); + } + private function stripDisallowedHtmlTags($commentText) { $allowedHtmlTags = explode(',', UserConfig::get('photo_comments_allowed_html')); diff --git a/app/Mail/PhotoCommentApproved.php b/app/Mail/PhotoCommentApproved.php new file mode 100644 index 0000000..fb3a326 --- /dev/null +++ b/app/Mail/PhotoCommentApproved.php @@ -0,0 +1,60 @@ +user = $user; + $this->album = $album; + $this->photo = $photo; + $this->comment = $comment; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + $subject = trans('email.photo_comment_approved_subject', ['album_name' => $this->album->name]); + + return $this + ->subject($subject) + ->markdown(Theme::viewName('email.photo_comment_approved')) + ->with([ + 'album' => $this->album, + 'comment' => $this->comment, + 'photo' => $this->photo, + 'subject' => $subject, + 'user' => $this->user + ]); + } +} diff --git a/app/Mail/PhotoCommentApprovedUser.php b/app/Mail/PhotoCommentApprovedUser.php new file mode 100644 index 0000000..c791a6f --- /dev/null +++ b/app/Mail/PhotoCommentApprovedUser.php @@ -0,0 +1,60 @@ +user = $user; + $this->album = $album; + $this->photo = $photo; + $this->comment = $comment; + } + + /** + * Build the message. + * + * @return $this + */ + public function build() + { + $subject = trans('email.photo_comment_approved_user_subject', ['album_name' => $this->album->name]); + + return $this + ->subject($subject) + ->markdown(Theme::viewName('email.photo_comment_approved_user')) + ->with([ + 'album' => $this->album, + 'comment' => $this->comment, + 'photo' => $this->photo, + 'subject' => $subject, + 'user' => $this->user + ]); + } +} diff --git a/app/PhotoComment.php b/app/PhotoComment.php index f78f134..0f72751 100644 --- a/app/PhotoComment.php +++ b/app/PhotoComment.php @@ -22,6 +22,11 @@ class PhotoComment extends Model return is_null($this->createdBy) ? $this->name : $this->createdBy->name; } + public function authorEmail() + { + return is_null($this->createdBy) ? $this->email : $this->createdBy->email; + } + public function children() { return $this->hasMany(PhotoComment::class, 'parent_comment_id'); diff --git a/resources/lang/en/email.php b/resources/lang/en/email.php index 764cefc..472becd 100644 --- a/resources/lang/en/email.php +++ b/resources/lang/en/email.php @@ -15,5 +15,15 @@ return [ 'moderate_photo_comment_p1' => 'A new comment has been posted in the :album_name album.', 'moderate_photo_comment_p2' => 'Click the button below to open the photo\'s page and approve or reject the comment. You will need to login to see unapproved comments.', 'moderate_photo_comment_subject' => 'New comment requires moderation in :album_name', - 'test_email_subject' => 'Test e-mail from :app_name' + 'test_email_subject' => 'Test e-mail from :app_name', + 'photo_comment_approved_comment_label' => 'Comments:', + 'photo_comment_approved_email_label' => 'Author e-mail:', + 'photo_comment_approved_name_label' => 'Author name:', + 'photo_comment_approved_p1' => 'A new comment has been posted in the :album_name album.', + 'photo_comment_approved_p2' => 'Click the button below to view the photo\'s page to see the comment.', + 'photo_comment_approved_subject' => 'New comment posted in :album_name', + 'photo_comment_approved_user_comment_label' => 'Comments:', + 'photo_comment_approved_user_p1' => 'Your comment has been successfully posted in the :album_name album.', + 'photo_comment_approved_user_p2' => 'Click the button below to view the photo\'s page to see your comment.', + 'photo_comment_approved_user_subject' => 'Your comment was posted in :album_name' ]; \ No newline at end of file diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index affcb26..5e9c760 100644 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -99,5 +99,6 @@ return [ 'storage_service_region_label' => 'Region:', 'storage_tenant_name_label' => 'Tenant:', 'username_label' => 'Username:', - 'upload_action' => 'Upload' + 'upload_action' => 'Upload', + 'view_photo_comment_action' => 'View comment' ]; \ No newline at end of file diff --git a/resources/views/themes/base/email/photo_comment_approved.blade.php b/resources/views/themes/base/email/photo_comment_approved.blade.php new file mode 100644 index 0000000..2e64972 --- /dev/null +++ b/resources/views/themes/base/email/photo_comment_approved.blade.php @@ -0,0 +1,26 @@ +@component('mail::message') +@lang('email.generic_intro', ['user_name' => $user->name]) + + +@lang('email.photo_comment_approved_p1', ['album_name' => $album->name]) + + +@lang('email.photo_comment_approved_p2') + + +@lang('email.photo_comment_approved_name_label') {{ $comment->name }} + +@lang('email.photo_comment_approved_email_label') {{ $comment->email }} + +@lang('email.photo_comment_approved_comment_label') + +{!! $comment->comment !!} + +@component('mail::button', ['url' => $photo->url(), 'color' => 'blue']) + @lang('forms.view_photo_comment_action') +@endcomponent + +@lang('email.generic_regards')
+{{ UserConfig::get('app_name') }}
+{{ route('home') }} +@endcomponent \ No newline at end of file diff --git a/resources/views/themes/base/email/photo_comment_approved_user.blade.php b/resources/views/themes/base/email/photo_comment_approved_user.blade.php new file mode 100644 index 0000000..00c930a --- /dev/null +++ b/resources/views/themes/base/email/photo_comment_approved_user.blade.php @@ -0,0 +1,22 @@ +@component('mail::message') +@lang('email.generic_intro', ['user_name' => $user->name]) + + +@lang('email.photo_comment_approved_user_p1', ['album_name' => $album->name]) + + +@lang('email.photo_comment_approved_user_p2') + + +@lang('email.photo_comment_approved_user_comment_label') + +{!! $comment->comment !!} + +@component('mail::button', ['url' => $photo->url(), 'color' => 'blue']) + @lang('forms.view_photo_comment_action') +@endcomponent + +@lang('email.generic_regards')
+{{ UserConfig::get('app_name') }}
+{{ route('home') }} +@endcomponent \ No newline at end of file