1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelJumbotronImages\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
use Intervention\Image\ImageManagerStatic as Image; |
8
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
9
|
|
|
use DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage; |
10
|
|
|
|
11
|
|
|
class JumbotronImageController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Display the specified resource. |
15
|
|
|
* |
16
|
|
|
* @param \Illuminate\Http\Request $request |
17
|
|
|
* @return \Illuminate\Http\Response |
18
|
|
|
*/ |
19
|
3 |
|
public function index(Request $request) |
20
|
|
|
{ |
21
|
3 |
|
$searchKeywords = $request->input('keywords'); |
22
|
|
|
//$searchCategory = $request->input('category_id'); |
23
|
3 |
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
24
|
|
|
|
25
|
3 |
|
if ($searchKeywords) { |
26
|
|
|
$jumbotronImages = JumbotronImage:: |
27
|
|
|
select('jumbotron_image_translations.jumbotron_image_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale') |
28
|
|
|
->join('jumbotron_image_translations', 'jumbotron_images.id', '=', 'jumbotron_image_translations.jumbotron_image_id') |
29
|
|
|
->orderBy('title') |
30
|
|
|
->where('title', 'like', '%'.$searchKeywords.'%') |
31
|
|
|
->where('locale', 'en') |
32
|
|
|
->paginate(20); |
33
|
|
|
} else { |
34
|
|
|
$jumbotronImages = JumbotronImage:: |
35
|
3 |
|
select('jumbotron_image_translations.jumbotron_image_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale') |
36
|
3 |
|
->join('jumbotron_image_translations', 'jumbotron_images.id', '=', 'jumbotron_image_translations.jumbotron_image_id') |
37
|
3 |
|
->where('locale', 'en') |
38
|
3 |
|
->orderBy('title') |
39
|
3 |
|
->paginate(20); |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
return view('laravel-jumbotron-images::jumbotronImages.index', compact('jumbotronImages')) |
43
|
3 |
|
->with('i', (request()->input('page', 1) - 1) * 20) |
44
|
3 |
|
->with('searchKeywords', $searchKeywords) |
45
|
3 |
|
->with('countriesAvailableForTranslations', $countriesAvailableForTranslations); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/***************************************************************************/ |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Show the form for creating a new resource. |
52
|
|
|
* |
53
|
|
|
* @return \Illuminate\Http\Response |
54
|
|
|
*/ |
55
|
1 |
|
public function create() |
56
|
|
|
{ |
57
|
1 |
|
return view('laravel-jumbotron-images::jumbotronImages.create') |
58
|
1 |
|
->with('jumbotronHeightArray', $this->getJumbotronHeightArray()) |
59
|
1 |
|
->with('coverOpacityArray', $this->getCoverOpacityArray()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/***************************************************************************/ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Store a newly created resource in storage. |
66
|
|
|
* |
67
|
|
|
* @param \Illuminate\Http\Request $request |
68
|
|
|
* @return \Illuminate\Http\Response |
69
|
|
|
*/ |
70
|
1 |
|
public function store(Request $request) |
71
|
|
|
{ |
72
|
1 |
|
$jumbotronImage = new JumbotronImage(); |
73
|
|
|
|
74
|
|
|
// Set the default language to edit the quote in English |
75
|
1 |
|
App::setLocale('en'); |
76
|
|
|
|
77
|
1 |
|
$this->saveOnDb($request, $jumbotronImage); |
78
|
|
|
|
79
|
1 |
|
return redirect()->route('jumbotron-images.index') |
80
|
1 |
|
->with('success', 'Jumbotron image added succesfully'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/***************************************************************************/ |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Display the specified resource. |
87
|
|
|
* |
88
|
|
|
* @param int $jumbotronImageId |
89
|
|
|
* @return \Illuminate\Http\Response |
90
|
|
|
*/ |
91
|
1 |
|
public function show($jumbotronImageId = null) |
92
|
|
|
{ |
93
|
1 |
|
$jumbotronImage = JumbotronImage::find($jumbotronImageId); |
94
|
|
|
|
95
|
1 |
|
return view('laravel-jumbotron-images::jumbotronImages.show', compact('jumbotronImage')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/***************************************************************************/ |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Show the form for editing the specified resource. |
102
|
|
|
* |
103
|
|
|
* @param int $jumbotronImageId |
104
|
|
|
* @return \Illuminate\Http\Response |
105
|
|
|
*/ |
106
|
1 |
|
public function edit($jumbotronImageId = null) |
107
|
|
|
{ |
108
|
1 |
|
$jumbotronImage = JumbotronImage::find($jumbotronImageId); |
109
|
|
|
|
110
|
1 |
|
return view('laravel-jumbotron-images::jumbotronImages.edit', compact('jumbotronImage')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/***************************************************************************/ |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Update the specified resource in storage. |
117
|
|
|
* |
118
|
|
|
* @param \Illuminate\Http\Request $request |
119
|
|
|
* @param int $jumbotronImageId |
120
|
|
|
* @return \Illuminate\Http\Response |
121
|
|
|
*/ |
122
|
1 |
|
public function update(Request $request, $jumbotronImageId) |
123
|
|
|
{ |
124
|
1 |
|
$jumbotronImage = JumbotronImage::find($jumbotronImageId); |
125
|
|
|
|
126
|
|
|
// Set the default language to update the quote in English |
127
|
1 |
|
App::setLocale('en'); |
128
|
|
|
|
129
|
1 |
|
$this->saveOnDb($request, $jumbotronImage); |
130
|
|
|
|
131
|
1 |
|
return redirect()->route('jumbotron-images.index') |
132
|
1 |
|
->with('success', 'Jumbotron image updated succesfully'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/***************************************************************************/ |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remove the specified resource from storage. |
139
|
|
|
* |
140
|
|
|
* @param int $jumbotronImageId |
141
|
|
|
* @return \Illuminate\Http\Response |
142
|
|
|
*/ |
143
|
1 |
|
public function destroy($jumbotronImageId) |
144
|
|
|
{ |
145
|
1 |
|
$jumbotronImage = JumbotronImage::find($jumbotronImageId); |
146
|
1 |
|
$jumbotronImage->delete(); |
147
|
|
|
|
148
|
1 |
|
return redirect()->route('jumbotron-images.index') |
149
|
1 |
|
->with('success', 'Jumbotron image deleted succesfully'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/***************************************************************************/ |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Save the record on DB. |
156
|
|
|
* @param \Illuminate\Http\Request $request |
157
|
|
|
* @param \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage $jumbotronImage |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
2 |
|
public function saveOnDb($request, $jumbotronImage) |
161
|
|
|
{ |
162
|
2 |
|
$jumbotronImage->translateOrNew('en')->title = $request->get('title'); |
163
|
2 |
|
$jumbotronImage->translateOrNew('en')->body = $request->get('body'); |
164
|
2 |
|
$jumbotronImage->translateOrNew('en')->button_text = $request->get('button_text'); |
165
|
|
|
//$jumbotronImage->image_file_name = $request->get('image_file_name'); |
166
|
2 |
|
$jumbotronImage->button_url = $request->get('button_url'); |
167
|
|
|
|
168
|
|
|
// Teacher profile picture upload |
169
|
2 |
|
if ($request->file('image_file_name')) { |
170
|
|
|
$imageFile = $request->file('image_file_name'); |
171
|
|
|
$imageName = $imageFile->hashName(); |
172
|
|
|
$imageSubdir = 'jumbotron_images'; |
173
|
|
|
$imageWidth = '1067'; |
174
|
|
|
$thumbWidth = '690'; |
175
|
|
|
|
176
|
|
|
$this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth); |
|
|
|
|
177
|
|
|
$jumbotronImage->image_file_name = $imageName; |
178
|
|
|
} else { |
179
|
2 |
|
$jumbotronImage->image_file_name = $request->image_file_name; |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
$jumbotronImage->save(); |
183
|
2 |
|
} |
184
|
|
|
|
185
|
|
|
/***************************************************************************/ |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Upload image on server. |
189
|
|
|
* $imageFile - the file to upload |
190
|
|
|
* $imageSubdir is the subdir in /storage/app/public/images/.. |
191
|
|
|
* |
192
|
|
|
* @param \Illuminate\Http\UploadedFile[] $imageFile |
193
|
|
|
* @param string $imageName |
194
|
|
|
* @param string $imageSubdir |
195
|
|
|
* @param string $imageWidth |
196
|
|
|
* @param string $thumbWidth |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
public function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth) |
200
|
|
|
{ |
201
|
|
|
|
202
|
|
|
// Create dir if not exist (in /storage/app/public/images/..) |
203
|
|
|
if (! \Storage::disk('public')->has('images/'.$imageSubdir.'/')) { |
|
|
|
|
204
|
|
|
\Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$destinationPath = 'app/public/images/'.$imageSubdir.'/'; |
208
|
|
|
|
209
|
|
|
// Resize the image with Intervention - http://image.intervention.io/api/resize |
210
|
|
|
// - resize and store the image to a width of 300 and constrain aspect ratio (auto height) |
211
|
|
|
// - save file as jpg with medium quality |
212
|
|
|
$image = Image::make($imageFile->getRealPath()) |
213
|
|
|
->resize($imageWidth, null, |
|
|
|
|
214
|
|
|
function ($constraint) { |
215
|
|
|
$constraint->aspectRatio(); |
216
|
|
|
}) |
217
|
|
|
->save(storage_path($destinationPath.$imageName), 75); |
218
|
|
|
|
219
|
|
|
// Create the thumb |
220
|
|
|
$image->resize($thumbWidth, null, |
221
|
|
|
function ($constraint) { |
222
|
|
|
$constraint->aspectRatio(); |
223
|
|
|
}) |
224
|
|
|
->save(storage_path($destinationPath.'thumb_'.$imageName), 75); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/***************************************************************************/ |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Return and array with the jumbotron possible height options. |
231
|
|
|
* |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
1 |
|
public static function getJumbotronHeightArray() |
235
|
|
|
{ |
236
|
|
|
$ret = [ |
237
|
1 |
|
'is-small' => 'Small', |
238
|
|
|
'is-medium' => 'Medium', |
239
|
|
|
'is-large' => 'Large', |
240
|
|
|
'is-halfheight' => 'Halfheight', |
241
|
|
|
'is-fullheight' => 'Fullheight', |
242
|
|
|
]; |
243
|
1 |
|
return $ret; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/***************************************************************************/ |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Return and array with the jumbotron possible opacity options. |
250
|
|
|
* |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
1 |
|
public static function getCoverOpacityArray() |
254
|
|
|
{ |
255
|
|
|
$ret = [ |
256
|
1 |
|
'0' => 'none', |
257
|
|
|
'0.1' => '10%', |
258
|
|
|
'0.2' => '20%', |
259
|
|
|
'0.3' => '30%', |
260
|
|
|
'0.4' => '40%', |
261
|
|
|
'0.5' => '50%', |
262
|
|
|
]; |
263
|
1 |
|
return $ret; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/***************************************************************************/ |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|