#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.

This commit is contained in:
Andy Heathershaw 2018-07-12 21:55:01 +01:00
parent 790d354167
commit f96a9cd9f7
6 changed files with 40 additions and 2 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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'
];
}

View File

@ -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);

View File

@ -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!',

View File

@ -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.',