From 932f7017dc181b0a61ee223aa962d7c8d009b2fa Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Thu, 1 Sep 2016 17:31:16 +0100 Subject: [PATCH] Albums can now be edited. De-duplicated code in the AlbumController --- app/Album.php | 9 ++-- .../Controllers/Admin/AlbumController.php | 39 ++++++++++------- resources/lang/en/forms.php | 3 +- resources/views/admin/delete_album.blade.php | 2 +- resources/views/admin/edit_album.blade.php | 42 +++++++++++++++++++ resources/views/admin/index.blade.php | 6 ++- resources/views/admin/list_albums.blade.php | 9 +++- 7 files changed, 86 insertions(+), 24 deletions(-) create mode 100644 resources/views/admin/edit_album.blade.php diff --git a/app/Album.php b/app/Album.php index 1b2c724..0fe9cf1 100644 --- a/app/Album.php +++ b/app/Album.php @@ -27,12 +27,11 @@ class Album extends Model protected $hidden = [ ]; - public static function fromRequest(Request $request) + public function fromRequest(Request $request) { - $album = new Album(); - $album->name = $request->get('name'); - $album->description = $request->get('description'); + $this->name = $request->get('name'); + $this->description = $request->get('description'); - return $album; + return $this; } } \ No newline at end of file diff --git a/app/Http/Controllers/Admin/AlbumController.php b/app/Http/Controllers/Admin/AlbumController.php index 6e01ad7..c701ad0 100644 --- a/app/Http/Controllers/Admin/AlbumController.php +++ b/app/Http/Controllers/Admin/AlbumController.php @@ -42,11 +42,7 @@ class AlbumController extends Controller { $this->authorize('admin-access'); - $album = Album::all()->where('id', intval($id))->first(); - if (is_null($album)) - { - App::abort(404); - } + $album = $this->loadAlbum($id); return view('admin.delete_album', ['album' => $album]); } @@ -61,8 +57,8 @@ class AlbumController extends Controller { $this->authorize('admin-access'); - $album = Album::fromRequest($request); - $album->save(); + $album = new Album(); + $album->fromRequest($request)->save(); return redirect(route('albums.index')); } @@ -77,11 +73,7 @@ class AlbumController extends Controller { $this->authorize('admin-access'); - $album = Album::all()->where('id', intval($id))->first(); - if (is_null($album)) - { - App::abort(404); - } + $album = $this->loadAlbum($id); return view('admin.show_album', ['album' => $album]); } @@ -95,6 +87,10 @@ class AlbumController extends Controller public function edit($id) { $this->authorize('admin-access'); + + $album = $this->loadAlbum($id); + + return view('admin.edit_album', ['album' => $album]); } /** @@ -104,9 +100,14 @@ class AlbumController extends Controller * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Requests\StoreAlbumRequest $request, $id) { $this->authorize('admin-access'); + + $album = $this->loadAlbum($id); + $album->fromRequest($request)->save(); + + return view('admin.show_album', ['album' => $album]); } /** @@ -119,13 +120,21 @@ class AlbumController extends Controller { $this->authorize('admin-access'); + $album = $this->loadAlbum($id); + $album->delete(); + + return redirect(route('albums.index')); + } + + private function loadAlbum($id) + { $album = Album::all()->where('id', intval($id))->first(); if (is_null($album)) { App::abort(404); + return null; } - $album->delete(); - return redirect(route('albums.index')); + return $album; } } \ No newline at end of file diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index 4b23273..98eaf01 100644 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -5,5 +5,6 @@ return [ 'delete_action' => 'Delete', 'description_label' => 'Description:', 'edit_action' => 'Edit', - 'name_label' => 'Name:' + 'name_label' => 'Name:', + 'save_action' => 'Save Changes' ]; \ No newline at end of file diff --git a/resources/views/admin/delete_album.blade.php b/resources/views/admin/delete_album.blade.php index 9046bea..79de1e4 100644 --- a/resources/views/admin/delete_album.blade.php +++ b/resources/views/admin/delete_album.blade.php @@ -11,7 +11,7 @@ @lang('admin.delete_album_warning')
- {!! Form::open(['url' => route('albums.destroy', ['id' => $album->id]), 'method' => 'DELETE']) !!} + {!! Form::open(['route' => ['albums.destroy', $album->id], 'method' => 'DELETE']) !!} @lang('forms.cancel_action') {!! Form::submit(trans('forms.delete_action'), ['class' => 'btn btn-danger']) !!} {!! Form::close() !!} diff --git a/resources/views/admin/edit_album.blade.php b/resources/views/admin/edit_album.blade.php new file mode 100644 index 0000000..ada6a27 --- /dev/null +++ b/resources/views/admin/edit_album.blade.php @@ -0,0 +1,42 @@ +@extends('layouts.app') +@section('title', 'Gallery Admin') + +@section('content') +
+
+
+

@lang('admin.create_album')

+

@lang('admin.create_album_intro')

+

@lang('admin.create_album_intro2')

+
+ + @if (count($errors) > 0) +
+
    + @foreach ($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif + + {!! Form::model($album, ['route' => ['albums.update', $album->id], 'method' => 'PUT']) !!} +
+ {!! Form::label('name', trans('forms.name_label'), ['class' => 'control-label']) !!} + {!! Form::text('name', old('name'), ['class' => 'form-control']) !!} +
+ +
+ {!! Form::label('description', trans('forms.description_label'), ['class' => 'control-label']) !!} + {!! Form::textarea('description', old('description'), ['class' => 'form-control']) !!} +
+ +
+ {!! Form::submit(trans('forms.save_action'), ['class' => 'btn btn-success']) !!} + @lang('forms.cancel_action') +
+ {!! Form::close() !!} +
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/admin/index.blade.php b/resources/views/admin/index.blade.php index ac9b802..3aa880f 100644 --- a/resources/views/admin/index.blade.php +++ b/resources/views/admin/index.blade.php @@ -8,7 +8,11 @@
@lang('admin.stats_panel')
-

{{ $album_count }} {{ trans_choice('admin.stats_albums', $album_count) }}

+

+ + {{ $album_count }} {{ trans_choice('admin.stats_albums', $album_count) }} + +

diff --git a/resources/views/admin/list_albums.blade.php b/resources/views/admin/list_albums.blade.php index a221b57..f8e6419 100644 --- a/resources/views/admin/list_albums.blade.php +++ b/resources/views/admin/list_albums.blade.php @@ -24,7 +24,10 @@ @foreach ($albums as $album) - {{ $album->name }} + + {{ $album->name }}
+

{{ $album->description }}

+ @lang('forms.edit_action') @lang('forms.delete_action') @@ -33,6 +36,10 @@ @endforeach + +
+ @lang('admin.create_album_link') +
@endif