#12: Added a checkbox when editing an album to create a redirect if the parent album changes. Added validation to the create redirect form

This commit is contained in:
Andy Heathershaw 2017-09-04 20:37:42 +01:00
parent 56deb6838a
commit b138af2199
6 changed files with 55 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -54,8 +54,16 @@
</select>
</div>
<div class="alert alert-warning">
<div id="change-parent-warning" class="alert alert-warning" style="display: none;">
@lang('admin.edit_album_change_parent_warning')
<div class="mt-3">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="preserve_url_redirect" checked="checked">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">@lang('admin.edit_album_change_parent_add_alias')</span>
</label>
</div>
</div>
<div class="text-right">
@ -66,4 +74,23 @@
</div>
</div>
</div>
@endsection
@endsection
@push('scripts')
<script type="text/javascript">
$(document).ready(function() {
// Show the change parent warning
var current_parent_id = '{{ $album->parent_album_id }}';
$('#parent-album').change(function() {
if ($('#parent-album').val() != current_parent_id)
{
$('#change-parent-warning').show();
}
else
{
$('#change-parent-warning').hide();
}
});
});
</script>
@endpush

View File

@ -39,7 +39,7 @@
{{ csrf_field() }}
<div class="form-group">
<label for="redirect-source">@lang('forms.album_redirect_source')</label>
<input type="text" class="form-control{{ $errors->has('source_url') ? ' is-invalid' : '' }}" id="redirect-source" name="source_url" placeholder="@lang('forms.album_redirect_source_placeholder')" aria-describedby="redirect-source-help">
<input type="text" class="form-control{{ $errors->has('source_url') ? ' is-invalid' : '' }}" id="redirect-source" name="source_url" placeholder="@lang('forms.album_redirect_source_placeholder')" value="{{ old('source_url') }}" aria-describedby="redirect-source-help">
<small id="redirect-source-help" class="form-text text-muted">@lang('forms.album_redirect_source_help')</small>
@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();