PhotosController::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 4
b 1
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace JeroenG\LaravelPhotoGallery\Controllers;
4
5
use Illuminate\Http\Request;
6
use JeroenG\LaravelPhotoGallery\Entities as Entity;
7
8
class PhotosController extends Controller
9
{
10
	/**
11
	 * Show the form for creating a new photo.
12
	 *
13
	 * @return \Illuminate\View\View
14
	 */
15
	public function create()
16
	{
17
		$albumArray = \Gallery::album()->all()->toArray();
18
		foreach ($albumArray as $album) {
19
		    $dropdown[$album->getId()] = $album->getName();
20
		}
21
		$data = array('type' => 'photo', 'dropdown' => $dropdown);
22
		return view('gallery::new', $data)->with('form', 'gallery::partials.new-photo');
23
	}
24
25
	/**
26
	 * Store a newly created photo in storage.
27
	 *
28
	 * @param  $request
29
	 * @param  $filesystem
30
	 * @return \Illuminate\View\View
31
	 */
32
	public function store(Request $request)
33
	{
34
		$this->validate($request, [
35
			'photo_path' => 'image|required',
36
    		'album_id' => 'required',
37
            'photo_name' => 'required',
38
            'photo_description' => 'max:255',
39
		]);
40
41
		$file = $request->file('photo_path');
42
		$filename = str_random(10).time().$file->getClientOriginalName();
43
		$file->move(public_path('uploads/photos'), $filename);
44
45
		$photo = new Entity\Photo();
46
		$photo->map([
47
			'file' => $filename,
48
    		'album_id' => $request->input('album_id'),
49
            'name' => $request->input('photo_name'),
50
            'description' => $request->input('photo_description'),
51
            'order' => 0,
52
		]);
53
54
		\Gallery::photo()->add($photo);
55
56
		return \Redirect::route('gallery')->with('alertsuccess', \Lang::get('gallery::gallery.creation'));
57
	}
58
59
	/**
60
	 * Display the specified photo.
61
	 *
62
	 * @param int $albumId Id of the album
63
	 * @param int $photoId Id of the photo
64
	 * @return \Illuminate\View\View
65
	 */
66
	public function show($albumId, $photoId)
67
	{
68
		$photo = \Gallery::photo()->find($photoId);
69
		return view('gallery::photo', ['photo' => $photo]);
70
	}
71
72
	/**
73
	 * Show the form for editing the specified photo.
74
	 *
75
	 * @param int $albumId Id of the album
76
	 * @param int $photoId Id of the photo
77
	 * @return \Illuminate\View\View
78
	 */
79
	public function edit($albumId, $photoId)
80
	{
81
		$photo = \Gallery::photo()->find($photoId);
82
		$albumArray = \Gallery::album()->all()->toArray();
83
		foreach ($albumArray as $album) {
84
		    $dropdown[$album->getId()] = $album->getName();
85
		}
86
		$data = array('type' => 'photo', 'dropdown' => $dropdown, 'photo' => $photo);
87
		return view('gallery::edit', $data)->with('form', 'gallery::partials.edit-photo');
88
89
	}
90
91
	/**
92
	 * Update the specified photo in the database.
93
	 *
94
	 * @param int $albumId Id of the album
95
	 * @param int $photoId Id of the photo
96
	 * @return \Illuminate\View\View
97
	 */
98
	public function update(Request $request, $albumId, $photoId)
99
	{
100
        $this->validate($request, [
101
    		'album_id' => 'required',
102
            'photo_name' => 'required',
103
            'photo_description' => 'max:255',
104
		]);
105
106
		$photo = new Entity\Photo();
107
		$photo->map([
108
			'id' => $photoId,
109
    		'album_id' => $request->input('album_id'),
110
            'name' => $request->input('photo_name'),
111
            'description' => $request->input('photo_description'),
112
            'order' => 0,
113
		]);
114
115
		\Gallery::photo()->save($photo);
116
117
		return \Redirect::route('gallery.album.photo.show', ['albumId' => $albumId, 'photoId' => $photoId])->with('alertsuccess', \Lang::get('gallery::gallery.update'));
118
		}
119
120
	/**
121
	 * Remove the specified photo from the database.
122
	 *
123
	 * @param int $albumId Id of the album
124
	 * @param int $photoId Id of the photo
125
	 * @return \Illuminate\View\View
126
	 */
127
	public function destroy($albumId, $photoId)
128
	{
129
		$photo = \Gallery::photo()->find($photoId);
130
		\Gallery::photo()->delete($photo);
131
		$file = "uploads/photos/" . $photo->getFile();
132
        unlink($file);
133
        return \Redirect::route("gallery.album.show", ['id' => $albumId])->with('alertsuccess', \Lang::get('gallery::gallery.removal'));
134
	}
135
}