From 0ebd7a1c5fa7436cfc6afe49b6e903de624610ae Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Mon, 17 Sep 2018 14:15:06 +0100 Subject: [PATCH] #4: Comments can now be posted from a photo page in the gallery, and are saved in the database in the photo_comments table. --- .../Gallery/PhotoCommentController.php | 50 ++++++++++++++++++ .../Controllers/Gallery/PhotoController.php | 3 +- app/Photo.php | 8 +++ app/PhotoComment.php | 19 +++++++ ..._17_132906_create_photo_comments_table.php | 52 +++++++++++++++++++ resources/lang/en/forms.php | 3 ++ resources/lang/en/gallery.php | 4 ++ .../views/themes/base/gallery/photo.blade.php | 10 ++-- .../photo_comments_reply_form.blade.php | 22 ++++++++ .../partials/gallery_photo_comments.blade.php | 10 ++++ routes/web.php | 3 ++ 11 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 app/Http/Controllers/Gallery/PhotoCommentController.php create mode 100644 app/PhotoComment.php create mode 100644 database/migrations/2018_09_17_132906_create_photo_comments_table.php create mode 100644 resources/views/themes/base/gallery/photo_comments_reply_form.blade.php create mode 100644 resources/views/themes/base/partials/gallery_photo_comments.blade.php diff --git a/app/Http/Controllers/Gallery/PhotoCommentController.php b/app/Http/Controllers/Gallery/PhotoCommentController.php new file mode 100644 index 0000000..c4b0804 --- /dev/null +++ b/app/Http/Controllers/Gallery/PhotoCommentController.php @@ -0,0 +1,50 @@ +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()); + } + + $comment = new PhotoComment(); + $comment->photo_id = $photo->id; + $comment->fill($request->only(['commentor_email', 'commentor_name', 'comment_text'])); + + $user = $this->getUser(); + if (!is_null($user) && !$user->isAnonymous()) + { + $comment->created_user_id = $user->id; + } + + $comment->save(); + + $request->getSession()->flash('success', trans('gallery.photo_comment_posted_successfully')); + + return redirect($photo->url()); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Gallery/PhotoController.php b/app/Http/Controllers/Gallery/PhotoController.php index bd50bb0..98a3eb8 100644 --- a/app/Http/Controllers/Gallery/PhotoController.php +++ b/app/Http/Controllers/Gallery/PhotoController.php @@ -157,7 +157,8 @@ class PhotoController extends Controller 'is_original_allowed' => $isOriginalAllowed, 'next_photo' => $nextPhoto, 'photo' => $photo, - 'previous_photo' => $previousPhoto + 'previous_photo' => $previousPhoto, + 'success' => $request->getSession()->get('success') ]); } diff --git a/app/Photo.php b/app/Photo.php index 486e979..bf224ff 100644 --- a/app/Photo.php +++ b/app/Photo.php @@ -77,6 +77,14 @@ class Photo extends Model return $this->belongsToMany(Label::class, 'photo_labels'); } + public function postCommentUrl() + { + return route('postPhotoComment', [ + 'albumUrlAlias' => $this->album->url_path, + 'photoFilename' => $this->storage_file_name + ]); + } + public function thumbnailUrl($thumbnailName = null, $cacheBust = true) { $url = $this->album->getAlbumSource()->getUrlToPhoto($this, $thumbnailName); diff --git a/app/PhotoComment.php b/app/PhotoComment.php new file mode 100644 index 0000000..293b477 --- /dev/null +++ b/app/PhotoComment.php @@ -0,0 +1,19 @@ +bigIncrements('id'); + $table->unsignedBigInteger('photo_id'); + $table->string('commentor_name'); + $table->string('commentor_email'); + $table->text('comment_text'); + $table->unsignedInteger('created_user_id')->nullable(true); + $table->unsignedInteger('approved_user_id')->nullable(true); + $table->dateTime('approved_at')->nullable(true); + $table->unsignedBigInteger('parent_comment_id')->nullable(true); + $table->timestamps(); + + $table->foreign('photo_id') + ->references('id')->on('photos') + ->onDelete('cascade'); + $table->foreign('created_user_id') + ->references('id')->on('users') + ->onDelete('cascade'); + $table->foreign('approved_user_id') + ->references('id')->on('users') + ->onDelete('cascade'); + $table->foreign('parent_comment_id') + ->references('id')->on('photo_comments') + ->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('photo_comments'); + } +} diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index e579b3d..a45679b 100644 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -26,6 +26,7 @@ return [ 'edit_action' => 'Edit', 'edit_album_action' => 'Edit this album', 'email_label' => 'E-mail address:', + 'email_placeholder' => 'name@example.com', 'enable_profile_page_label' => 'Allow others to see my profile page', 'inherit_album_permissions' => 'Inherit permissions from parent album', 'labels_label' => 'Labels:', @@ -35,6 +36,8 @@ return [ 'parent_album_placeholder' => 'None (top-level album)', 'password_label' => 'Password:', 'password_confirm_label' => 'Confirm password:', + 'photo_comment_submit_action' => 'Post comment', + 'photo_comment_text_label' => 'Your message/comments:', 'please_select' => '- Select an Option -', 'private_album_label' => 'Private album (only visible to me)', 'profile_alias_label' => 'Alias:', diff --git a/resources/lang/en/gallery.php b/resources/lang/en/gallery.php index 485683a..301cb06 100644 --- a/resources/lang/en/gallery.php +++ b/resources/lang/en/gallery.php @@ -33,6 +33,10 @@ return [ 'other_albums_description' => 'You may also be interested in the following albums.', 'other_albums_description_empty' => 'The :album_name album does not contain any photos - however you may also be interested in the following albums.', 'other_albums_heading' => 'More Albums in :album_name', + 'photo_comment_posted_successfully' => 'Your comment was posted successfully and will appear after it has been moderated.', + 'photo_comments_heading' => 'Comments', + 'photo_comments_reply_form_heading' => 'Leave a reply', + 'photo_comments_reply_form_p1' => 'Complete the form below to start a new reply thread.', 'photos' => 'photo|photos', 'previous_button' => '« Previous Photo', 'show_more_albums' => '... and :count other|... and :count others', diff --git a/resources/views/themes/base/gallery/photo.blade.php b/resources/views/themes/base/gallery/photo.blade.php index e380356..61fcc74 100644 --- a/resources/views/themes/base/gallery/photo.blade.php +++ b/resources/views/themes/base/gallery/photo.blade.php @@ -21,7 +21,7 @@
- -
+
Information about this photo:
diff --git a/resources/views/themes/base/gallery/photo_comments_reply_form.blade.php b/resources/views/themes/base/gallery/photo_comments_reply_form.blade.php new file mode 100644 index 0000000..37bd616 --- /dev/null +++ b/resources/views/themes/base/gallery/photo_comments_reply_form.blade.php @@ -0,0 +1,22 @@ +
+ {{ csrf_field() }} + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
\ No newline at end of file diff --git a/resources/views/themes/base/partials/gallery_photo_comments.blade.php b/resources/views/themes/base/partials/gallery_photo_comments.blade.php new file mode 100644 index 0000000..0f553c3 --- /dev/null +++ b/resources/views/themes/base/partials/gallery_photo_comments.blade.php @@ -0,0 +1,10 @@ +
+
+

@lang('gallery.photo_comments_heading')

+ +

@lang('gallery.photo_comments_reply_form_heading')

+

@lang('gallery.photo_comments_reply_form_p1')

+
+ @include(Theme::viewName('gallery.photo_comments_reply_form')) +
+
\ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 302b66a..12888f4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -106,6 +106,9 @@ Route::get('a/{albumUrlAlias}', 'Gallery\AlbumController@index') Route::get('exif/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@showExifData') ->name('viewExifData') ->where('albumUrlAlias', '.*'); +Route::post('p/{albumUrlAlias}/{photoFilename}/comments', 'Gallery\PhotoCommentController@store') + ->name('postPhotoComment') + ->where('albumUrlAlias', '.*'); Route::get('p/{albumUrlAlias}/{photoFilename}', 'Gallery\PhotoController@show') ->name('viewPhoto') ->where('albumUrlAlias', '.*');