From b138af219927f399e4ced0fd68b8a79244bce5ea Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Mon, 4 Sep 2017 20:37:42 +0100 Subject: [PATCH] #12: Added a checkbox when editing an album to create a redirect if the parent album changes. Added validation to the create redirect form --- .../Controllers/Admin/AlbumController.php | 14 +++++++++ .../Requests/StoreAlbumRedirectRequest.php | 9 +++++- resources/lang/en/admin.php | 1 + resources/lang/en/validation.php | 3 +- .../themes/base/admin/edit_album.blade.php | 31 +++++++++++++++++-- .../partials/album_redirects_tab.blade.php | 3 +- 6 files changed, 55 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Admin/AlbumController.php b/app/Http/Controllers/Admin/AlbumController.php index 941298c..eadd5be 100644 --- a/app/Http/Controllers/Admin/AlbumController.php +++ b/app/Http/Controllers/Admin/AlbumController.php @@ -494,6 +494,20 @@ class AlbumController extends Controller // Re-generate the URL path to ensure it's correct if the parent has changed if ($currentParentID != $album->parent_album_id) { + // Create a redirect if required + $redirectData = [ + 'album_id' => $album->id, + 'source_url' => sprintf('/%s', $album->url_path) + ]; + + if (strtolower($request->get('preserve_url_redirect')) == 'on' && AlbumRedirect::where($redirectData)->count() == 0) + { + $redirect = new AlbumRedirect(); + $redirect->fill($redirectData); + $redirect->save(); + } + + // Update the URL path $album->generateUrlPath(); } diff --git a/app/Http/Requests/StoreAlbumRedirectRequest.php b/app/Http/Requests/StoreAlbumRedirectRequest.php index 2607f07..236189d 100644 --- a/app/Http/Requests/StoreAlbumRedirectRequest.php +++ b/app/Http/Requests/StoreAlbumRedirectRequest.php @@ -16,6 +16,13 @@ class StoreAlbumRedirectRequest extends FormRequest return true; } + public function messages() + { + return [ + 'regex' => trans('validation.redirect_url_format') + ]; + } + /** * Get the validation rules that apply to the request. * @@ -24,7 +31,7 @@ class StoreAlbumRedirectRequest extends FormRequest public function rules() { return [ - 'source_url' => 'required|unique:album_redirects|max:255' + 'source_url' => 'required|unique:album_redirects|max:255|regex:/^\/[a-z0-9\-]+$/i' ]; } } \ No newline at end of file diff --git a/resources/lang/en/admin.php b/resources/lang/en/admin.php index fcec7c2..2c67ba7 100644 --- a/resources/lang/en/admin.php +++ b/resources/lang/en/admin.php @@ -82,6 +82,7 @@ return [ 'delete_user_warning' => 'This is a permanent action that cannot be reversed!', 'edit_album' => 'Edit photo album: :album_name', 'edit_album_action' => 'Edit album details', + 'edit_album_change_parent_add_alias' => 'Add a redirect to this album to prevent old links breaking.', 'edit_album_change_parent_warning' => 'Changing this album\'s parent will change the address used to access this album. This may break any bookmarks or links used by your visitors or search engines.', 'edit_album_intro' => 'Photo albums contain individual photographs in the same way as a physical photo album or memory book.', 'edit_album_intro2' => 'Complete the form below to edit the properties of the album: :album_name.', diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index 55ccb52..7716b02 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -116,5 +116,6 @@ return [ 'dir_empty' => 'The path must be an empty folder.', 'is_dir' => 'The folder must be a valid directory.', 'is_writeable' => 'Unable to write to this folder - please check permissions.', - 'photo_name_required' => 'The name field is required.' + 'photo_name_required' => 'The name field is required.', + 'redirect_url_format' => 'The URL must begin with a forward-slash and only contain a-z, A-Z, 0-9 and hyphens.' ]; diff --git a/resources/views/themes/base/admin/edit_album.blade.php b/resources/views/themes/base/admin/edit_album.blade.php index f95b938..a69dc65 100644 --- a/resources/views/themes/base/admin/edit_album.blade.php +++ b/resources/views/themes/base/admin/edit_album.blade.php @@ -54,8 +54,16 @@ -
+
@@ -66,4 +74,23 @@
-@endsection \ No newline at end of file +@endsection + +@push('scripts') + +@endpush \ No newline at end of file diff --git a/resources/views/themes/base/partials/album_redirects_tab.blade.php b/resources/views/themes/base/partials/album_redirects_tab.blade.php index 279367a..d5012d0 100644 --- a/resources/views/themes/base/partials/album_redirects_tab.blade.php +++ b/resources/views/themes/base/partials/album_redirects_tab.blade.php @@ -39,7 +39,7 @@ {{ csrf_field() }}
- + @lang('forms.album_redirect_source_help') @if ($errors->has('source_url')) @@ -93,7 +93,6 @@ $('.delete-redirect').click(function(e) { source_form = $(this).closest('form'); $('#confirm-redirect-delete-modal').modal(); - alert($(this).closest('a').html()); $('.redirect-url', '#confirm-redirect-delete-modal').html($(this).closest('a').html()); e.preventDefault();