From f9bc890de0f8b881384decbb3a3cace3b324cce8 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Mon, 4 Sep 2017 17:23:31 +0100 Subject: [PATCH] #12: Added the ability to add and remove redirects from the album's admin page (Redirects tab) --- app/Album.php | 5 + .../Controllers/Admin/AlbumController.php | 40 ++++++- .../Requests/StoreAlbumRedirectRequest.php | 30 +++++ resources/lang/en/admin.php | 12 ++ resources/lang/en/forms.php | 7 +- .../themes/base/admin/show_album.blade.php | 9 +- .../partials/album_redirects_tab.blade.php | 104 ++++++++++++++++++ routes/web.php | 2 + 8 files changed, 200 insertions(+), 9 deletions(-) create mode 100644 app/Http/Requests/StoreAlbumRedirectRequest.php create mode 100644 resources/views/themes/base/partials/album_redirects_tab.blade.php diff --git a/app/Album.php b/app/Album.php index a5202ec..929dd8a 100644 --- a/app/Album.php +++ b/app/Album.php @@ -170,6 +170,11 @@ class Album extends Model return $this->hasMany(Photo::class); } + public function redirects() + { + return $this->hasMany(AlbumRedirect::class); + } + public function storage() { return $this->belongsTo(Storage::class); diff --git a/app/Http/Controllers/Admin/AlbumController.php b/app/Http/Controllers/Admin/AlbumController.php index aa6b101..941298c 100644 --- a/app/Http/Controllers/Admin/AlbumController.php +++ b/app/Http/Controllers/Admin/AlbumController.php @@ -1,14 +1,13 @@ $album]); } + public function deleteRedirect(Request $request, $id, $redirectId) + { + $this->authorizeAccessToAdminPanel('admin:manage-albums'); + + $album = $this->loadAlbum($id, 'delete'); + + /** @var AlbumRedirect $redirect */ + $redirect = $album->redirects()->where('id', $redirectId)->first(); + if (is_null($redirect)) + { + App::abort(404); + return; + } + + $redirect->delete(); + $request->session()->flash('success', trans('admin.delete_redirect_success_message')); + return redirect(route('albums.show', ['id' => $id, 'tab' => 'redirects'])); + } + /** * Remove the specified resource from storage. * @@ -429,6 +446,21 @@ class AlbumController extends Controller return redirect(route('albums.show', ['id' => $album->id])); } + public function storeRedirect(Requests\StoreAlbumRedirectRequest $request, $id) + { + $this->authorizeAccessToAdminPanel('admin:manage-albums'); + + $album = $this->loadAlbum($id); + + $redirect = new AlbumRedirect(); + $redirect->fill($request->only('source_url')); + $redirect->album_id = $album->id; + $redirect->save(); + + $request->session()->flash('success', trans('admin.create_redirect_success_message')); + return redirect(route('albums.show', ['id' => $id, 'tab' => 'redirects'])); + } + /** * Update the specified resource in storage. * diff --git a/app/Http/Requests/StoreAlbumRedirectRequest.php b/app/Http/Requests/StoreAlbumRedirectRequest.php new file mode 100644 index 0000000..2607f07 --- /dev/null +++ b/app/Http/Requests/StoreAlbumRedirectRequest.php @@ -0,0 +1,30 @@ + 'required|unique:album_redirects|max:255' + ]; + } +} \ No newline at end of file diff --git a/resources/lang/en/admin.php b/resources/lang/en/admin.php index 7f3e1d3..fcec7c2 100644 --- a/resources/lang/en/admin.php +++ b/resources/lang/en/admin.php @@ -24,6 +24,7 @@ return [ 'album_permissions_album' => 'Album Permissions', 'album_permissions_photo' => 'Photo Permissions', 'album_photos_tab' => 'Photos', + 'album_redirects_tab' => 'Redirects', 'album_saved_successfully' => 'The ":name" album was updated successfully.', 'album_security_tab' => 'Permissions', 'album_settings_tab' => 'Settings', @@ -45,6 +46,9 @@ return [ 'create_album_no_storage' => 'There are currently no storage locations set up. Please create a location to store your photos before creating an album.', 'create_group' => 'Create a group', 'create_group_intro' => 'Complete the form below to create a group in which to organise your users.', + 'create_redirect_heading' => 'Add a Redirect', + 'create_redirect_success_message' => 'The redirect was added successfully.', + 'create_redirect_text' => 'Enter the source address you would like to redirect and click the Create button to add a new redirect to this album.', 'create_storage' => 'Create storage location', 'create_storage_intro' => 'Complete the form below to create a new storage location to hold your photos. You can then select this storage location when you create an album.', 'create_user' => 'Create user', @@ -65,6 +69,9 @@ return [ 'delete_photo_message' => 'Are you sure you want to delete this photo? This action cannot be undone!', 'delete_photo_successful_message' => 'The photo ":name" was deleted successfully.', 'delete_photo_title' => 'Delete photo', + 'delete_redirect_confirm_message' => 'Are you sure you want to remove this redirect?', + 'delete_redirect_confirm_title' => 'Delete Album Redirect', + 'delete_redirect_success_message' => 'The redirect was deleted successfully.', 'delete_storage' => 'Delete storage location: :name', 'delete_storage_confirm' => 'Are you sure you want to permanently remove this storage location?', 'delete_storage_existing_albums' => 'At least one album is still using the storage location. Please delete all albums before removing the storage location.', @@ -84,6 +91,7 @@ return [ 'edit_storage_intro' => 'Use the form below to update the details of the :storage_name storage location.', 'edit_user_intro' => 'You can use the form below to edit the above user account. Changes take effect immediately.', 'edit_user_title' => 'Edit user account: :name', + 'existing_album_redirects' => 'The following redirects exist for this album.', 'group_deletion_failed' => 'An error occurred while removing the ":name" group: :error_message', 'group_deletion_successful' => 'The ":name" group was removed successfully.', 'group_details_tab' => 'Details', @@ -127,6 +135,10 @@ return [ 'rotate_left' => 'Rotate left', 'rotate_right' => 'Rotate right' ], + 'redirects_heading' => 'Redirects', + 'redirects_actions_heading' => 'Actions', + 'redirects_source_url_heading' => 'Source Address', + 'redirects_text' => 'To access this album using different addresses, you can use redirects. This is useful when you have moved the album to a different parent and the old address no longer works.', 'save_changes_heading' => 'Save changes', 'save_changes_intro' => 'If you have made any changes to the above fields, you\'ll need to hit the Save Changes button below.', 'security_groups_heading' => 'Group Permissions', diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index 6b7dfbe..e1b509e 100644 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -2,6 +2,9 @@ return [ 'activate_user_label' => 'Manually activate this account', 'admin_user_label' => 'User is an administrator', + 'album_redirect_source' => 'Source address:', + 'album_redirect_source_help' => 'The address must start with a forward-slash (/)', + 'album_redirect_source_placeholder' => '(e.g. /my-new-album)', 'album_source_label' => 'Storage location:', 'album_view_label' => 'View as:', 'apply_action' => 'Apply', @@ -27,6 +30,7 @@ return [ 'register_action' => 'Create account', 'remember_me_label' => 'Remember me', 'remove_action' => 'Remove', + 'save_action' => 'Save Changes', 'select' => 'Select', 'select_current_text' => '(current)', 'settings_hotlink_protection' => 'Prevent hot-linking to images', @@ -48,6 +52,5 @@ return [ 'storage_service_region_label' => 'Region:', 'storage_tenant_name_label' => 'Tenant:', 'username_label' => 'Username:', - 'upload_action' => 'Upload', - 'save_action' => 'Save Changes' + 'upload_action' => 'Upload' ]; \ No newline at end of file diff --git a/resources/views/themes/base/admin/show_album.blade.php b/resources/views/themes/base/admin/show_album.blade.php index a1a49fb..28b19e0 100644 --- a/resources/views/themes/base/admin/show_album.blade.php +++ b/resources/views/themes/base/admin/show_album.blade.php @@ -24,10 +24,11 @@ @can('upload-photos', $album) @include(Theme::viewName('partials.tab'), ['tab_name' => 'upload', 'tab_icon' => 'upload', 'tab_text' => trans('admin.album_upload_tab')]) @endcan + @include(Theme::viewName('partials.tab'), ['tab_name' => 'cameras', 'tab_icon' => 'camera', 'tab_text' => trans('admin.album_cameras_tab')]) @can('change-permissions', $album) @include(Theme::viewName('partials.tab'), ['tab_name' => 'permissions', 'tab_icon' => 'lock', 'tab_text' => trans('admin.album_security_tab')]) @endcan - @include(Theme::viewName('partials.tab'), ['tab_name' => 'cameras', 'tab_icon' => 'camera', 'tab_text' => trans('admin.album_cameras_tab')]) + @include(Theme::viewName('partials.tab'), ['tab_name' => 'redirects', 'tab_icon' => 'retweet', 'tab_text' => trans('admin.album_redirects_tab')]) @include(Theme::viewName('partials.tab'), ['tab_name' => 'settings', 'tab_icon' => 'cog', 'tab_text' => trans('admin.album_settings_tab')]) @@ -38,12 +39,14 @@ {{-- Upload --}} @include(Theme::viewName('partials.album_upload_tab')) @endcan + {{-- Cameras --}} + @include(Theme::viewName('partials.album_cameras_tab')) @can('change-permissions', $album) {{-- Permissions --}} @include(Theme::viewName('partials.album_permissions_tab')) @endcan - {{-- Cameras --}} - @include(Theme::viewName('partials.album_cameras_tab')) + {{-- Redirects --}} + @include(Theme::viewName('partials.album_redirects_tab')) {{-- Settings --}} @include(Theme::viewName('partials.album_settings_tab')) diff --git a/resources/views/themes/base/partials/album_redirects_tab.blade.php b/resources/views/themes/base/partials/album_redirects_tab.blade.php new file mode 100644 index 0000000..279367a --- /dev/null +++ b/resources/views/themes/base/partials/album_redirects_tab.blade.php @@ -0,0 +1,104 @@ +
+

@lang('admin.redirects_heading')

+

@lang('admin.redirects_text')

+ @if ($album->redirects()->count() > 0) +

@lang('admin.existing_album_redirects')

+ + + + + + + + + + @foreach ($album->redirects as $redirect) + + + + + @endforeach + +
@lang('admin.redirects_source_url_heading')@lang('admin.redirects_actions_heading')
{{ route('home') }}/a{{ $redirect->source_url }} +
+ {{ csrf_field() }} + {{ method_field('DELETE') }} + + +
+
+
+ + @endif + +
+
@lang('admin.create_redirect_heading')
+

@lang('admin.create_redirect_text')

+ +
+ {{ csrf_field() }} +
+ + + @lang('forms.album_redirect_source_help') + + @if ($errors->has('source_url')) +
+ {{ $errors->first('source_url') }} +
+ @endif +
+ +
+ +
+
+ + + +
+ +@push('scripts') + +@endpush \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 90f4983..8a42497 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,6 +25,8 @@ Route::group(['prefix' => 'admin'], function () { Route::get('albums/{id}/delete', 'Admin\AlbumController@delete')->name('albums.delete'); Route::post('albums/{id}/set-group-permissions', 'Admin\AlbumController@setGroupPermissions')->name('albums.set_group_permissions'); Route::post('albums/{id}/set-user-permissions', 'Admin\AlbumController@setUserPermissions')->name('albums.set_user_permissions'); + Route::delete('albums/{id}/delete-redirect/{redirectId}', 'Admin\AlbumController@deleteRedirect')->name('albums.delete_redirect'); + Route::post('albums/{id}/store-redirect', 'Admin\AlbumController@storeRedirect')->name('albums.store_redirect'); Route::resource('albums', 'Admin\AlbumController'); // Photo management