From f96a9cd9f75f8beb24ac3254d46a2fae43ff48ba Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Thu, 12 Jul 2018 21:55:01 +0100 Subject: [PATCH] #58: It is now possible to create albums named the same within different parent albums. Albums with child albums can now not be deleted, as this could leave duplicate albums in the same parent album. --- app/Helpers/ValidationHelper.php | 29 +++++++++++++++++++ .../Controllers/Admin/AlbumController.php | 6 ++++ app/Http/Requests/StoreAlbumRequest.php | 4 +-- app/Providers/AppServiceProvider.php | 1 + resources/lang/en/admin.php | 1 + resources/lang/en/validation.php | 1 + 6 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/Helpers/ValidationHelper.php b/app/Helpers/ValidationHelper.php index 97effd0..f4aeb05 100644 --- a/app/Helpers/ValidationHelper.php +++ b/app/Helpers/ValidationHelper.php @@ -2,8 +2,37 @@ namespace App\Helpers; +use Illuminate\Support\Facades\DB; + class ValidationHelper { + public function albumPathUnique($attribute, $value, $parameters, $validator) + { + $data = $validator->getData(); + $parentID = intval($data['parent_album_id']); + $name = $data['name']; + + if ($parentID === 0) + { + $parentID = null; + } + + $queryParams = [ + ['name', $name], + ['parent_album_id', $parentID] + ]; + + if (count($parameters) > 0) + { + $existingAlbumID = intval($parameters[0]); + $queryParams[] = ['id', '<>', $existingAlbumID]; + } + + $count = DB::table('albums')->where($queryParams)->count(); + + return ($count == 0); + } + public function directoryExists($attribute, $value, $parameters, $validator) { return file_exists($value) && is_dir($value); diff --git a/app/Http/Controllers/Admin/AlbumController.php b/app/Http/Controllers/Admin/AlbumController.php index 24ddbed..3344886 100644 --- a/app/Http/Controllers/Admin/AlbumController.php +++ b/app/Http/Controllers/Admin/AlbumController.php @@ -122,6 +122,12 @@ class AlbumController extends Controller $album = $this->loadAlbum($id, 'delete'); + if ($album->children()->count() > 0) + { + $request->session()->flash('error', trans('admin.delete_album_failed_children', ['album' => $album->name])); + return redirect(route('albums.index')); + } + // Delete all the photo files /** @var Photo $photo */ foreach ($album->photos as $photo) diff --git a/app/Http/Requests/StoreAlbumRequest.php b/app/Http/Requests/StoreAlbumRequest.php index 9d4e859..2642ba7 100644 --- a/app/Http/Requests/StoreAlbumRequest.php +++ b/app/Http/Requests/StoreAlbumRequest.php @@ -28,7 +28,7 @@ class StoreAlbumRequest extends FormRequest case 'POST': return [ 'description' => '', - 'name' => 'required|unique:albums|max:255', + 'name' => 'required|album_path_unique|max:255', 'storage_id' => 'required|sometimes' ]; @@ -38,7 +38,7 @@ class StoreAlbumRequest extends FormRequest return [ 'description' => 'sometimes', - 'name' => 'required|sometimes|max:255|unique:albums,name,' . $albumId, + 'name' => 'required|sometimes|max:255|album_path_unique:' . $albumId, 'storage_id' => 'required|sometimes' ]; } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index a253e0d..9c8acd2 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -50,6 +50,7 @@ class AppServiceProvider extends ServiceProvider Validator::extend('is_dir', (ValidationHelper::class . '@directoryExists')); Validator::extend('dir_empty', (ValidationHelper::class . '@isDirectoryEmpty')); Validator::extend('is_writeable', (ValidationHelper::class . '@isPathWriteable')); + Validator::extend('album_path_unique', (ValidationHelper::class . '@albumPathUnique')); // Model observers Album::observe(AlbumObserver::class); diff --git a/resources/lang/en/admin.php b/resources/lang/en/admin.php index 3aca811..5d974d6 100644 --- a/resources/lang/en/admin.php +++ b/resources/lang/en/admin.php @@ -78,6 +78,7 @@ return [ 'default_storage_legend' => 'Default storage location for new albums.', 'delete_album' => 'Delete album :name', 'delete_album_confirm' => 'Are you sure you want to permanently delete this album and all its contents?', + 'delete_album_failed_children' => 'The album ":album" contains child albums and cannot be deleted. Please delete or move the child albums first.', 'delete_album_success_message' => 'The album ":album" was deleted successfully.', 'delete_album_warning' => 'This is a permanent action that cannot be undone!', 'delete_bulk_photos_message' => 'Are you sure you want to delete the selected photos? This action cannot be undone!', diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index 7716b02..2e5e786 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -113,6 +113,7 @@ return [ 'attributes' => [], // Added by Andy H. for custom validators + 'album_path_unique' => 'An album with the same name already exists.', '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.',