Issues (8)

src/Http/Controllers/JumbotronImageController.php (3 issues)

1
<?php
2
3
namespace DavideCasiraghi\LaravelJumbotronImages\Http\Controllers;
4
5
use DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\App;
8
use Intervention\Image\ImageManagerStatic as Image;
9
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
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('buttonColorArray', $this->getButtonColorArray())
60 1
                    ->with('coverOpacityArray', $this->getCoverOpacityArray())
61 1
                    ->with('textWidthArray', $this->getTextWidthArray())
62 1
                    ->with('textVerticalAlignmentArray', $this->getTextVerticalAlignmentArray())
63 1
                    ->with('textHorizontalAlignmentArray', $this->getTextHorizontalAlignmentArray())
64 1
                    ->with('textShadowArray', $this->getTextShadowArray());
65
    }
66
67
    /***************************************************************************/
68
69
    /**
70
     * Store a newly created resource in storage.
71
     *
72
     * @param  \Illuminate\Http\Request  $request
73
     * @return \Illuminate\Http\Response
74
     */
75 1
    public function store(Request $request)
76
    {
77 1
        $jumbotronImage = new JumbotronImage();
78
79
        // Set the default language to edit the quote in English
80 1
        App::setLocale('en');
81
82 1
        $this->saveOnDb($request, $jumbotronImage);
83
84 1
        return redirect()->route('jumbotron-images.index')
85 1
                            ->with('success', 'Jumbotron image added succesfully');
86
    }
87
88
    /***************************************************************************/
89
90
    /**
91
     * Display the specified resource.
92
     *
93
     * @param  int $jumbotronImageId
94
     * @return \Illuminate\Http\Response
95
     */
96 1
    public function show($jumbotronImageId = null)
97
    {
98 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
99
100 1
        return view('laravel-jumbotron-images::jumbotronImages.show', compact('jumbotronImage'));
101
    }
102
103
    /***************************************************************************/
104
105
    /**
106
     * Show the form for editing the specified resource.
107
     *
108
     * @param  int $jumbotronImageId
109
     * @return \Illuminate\Http\Response
110
     */
111 1
    public function edit($jumbotronImageId = null)
112
    {
113 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
114
115 1
        return view('laravel-jumbotron-images::jumbotronImages.edit', compact('jumbotronImage'))
116 1
                    ->with('jumbotronHeightArray', $this->getJumbotronHeightArray())
117 1
                    ->with('buttonColorArray', $this->getButtonColorArray())
118 1
                    ->with('coverOpacityArray', $this->getCoverOpacityArray())
119 1
                    ->with('textWidthArray', $this->getTextWidthArray())
120 1
                    ->with('textVerticalAlignmentArray', $this->getTextVerticalAlignmentArray())
121 1
                    ->with('textHorizontalAlignmentArray', $this->getTextHorizontalAlignmentArray())
122 1
                    ->with('textShadowArray', $this->getTextShadowArray());
123
    }
124
125
    /***************************************************************************/
126
127
    /**
128
     * Update the specified resource in storage.
129
     *
130
     * @param  \Illuminate\Http\Request  $request
131
     * @param  int  $jumbotronImageId
132
     * @return \Illuminate\Http\Response
133
     */
134 1
    public function update(Request $request, $jumbotronImageId)
135
    {
136 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
137
138
        // Set the default language to update the quote in English
139 1
        App::setLocale('en');
140
141 1
        $this->saveOnDb($request, $jumbotronImage);
142
143 1
        return redirect()->route('jumbotron-images.index')
144 1
                            ->with('success', 'Jumbotron image updated succesfully');
145
    }
146
147
    /***************************************************************************/
148
149
    /**
150
     * Remove the specified resource from storage.
151
     *
152
     * @param  int  $jumbotronImageId
153
     * @return \Illuminate\Http\Response
154
     */
155 1
    public function destroy($jumbotronImageId)
156
    {
157 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
158 1
        $jumbotronImage->delete();
159
160 1
        return redirect()->route('jumbotron-images.index')
161 1
                            ->with('success', 'Jumbotron image deleted succesfully');
162
    }
163
164
    /***************************************************************************/
165
166
    /**
167
     * Save the record on DB.
168
     * @param  \Illuminate\Http\Request  $request
169
     * @param  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage  $jumbotronImage
170
     * @return void
171
     */
172 2
    public function saveOnDb($request, $jumbotronImage)
173
    {
174 2
        $jumbotronImage->translateOrNew('en')->title = $request->get('title');
175 2
        $jumbotronImage->translateOrNew('en')->body = $request->get('body');
176 2
        $jumbotronImage->translateOrNew('en')->button_text = $request->get('button_text');
177 2
        $jumbotronImage->button_url = $request->get('button_url');
178 2
        $jumbotronImage->jumbotron_height = $request->get('jumbotron_height');
179 2
        $jumbotronImage->cover_opacity = $request->get('cover_opacity');
180 2
        $jumbotronImage->scroll_down_arrow = ($request->scroll_down_arrow == 'on') ? 1 : 0;
181 2
        $jumbotronImage->background_color = $request->get('background_color');
182 2
        $jumbotronImage->button_color = $request->get('button_color');
183 2
        $jumbotronImage->parallax = ($request->parallax == 'on') ? 1 : 0;
184 2
        $jumbotronImage->white_moon = ($request->white_moon == 'on') ? 1 : 0;
185 2
        $jumbotronImage->text_width = $request->get('text_width');
186 2
        $jumbotronImage->text_vertical_alignment = $request->get('text_vertical_alignment');
187 2
        $jumbotronImage->text_horizontal_alignment = $request->get('text_horizontal_alignment');
188 2
        $jumbotronImage->text_shadow = $request->get('text_shadow');
189
190
        // Jumbotron image upload
191 2
        if ($request->file('image_file_name')) {
192
            $imageFile = $request->file('image_file_name');
193
            $imageName = $imageFile->hashName();
194
            $imageSubdir = 'jumbotron_images';
195
            $imageWidth = '1067';
196
            $thumbWidth = '690';
197
198
            $this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
0 ignored issues
show
It seems like $imageFile can also be of type Illuminate\Http\UploadedFile; however, parameter $imageFile of DavideCasiraghi\LaravelJ...::uploadImageOnServer() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
            $this->uploadImageOnServer(/** @scrutinizer ignore-type */ $imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
Loading history...
199
            $jumbotronImage->image_file_name = $imageName;
200
        } else {
201 2
            $jumbotronImage->image_file_name = $request->image_file_name;
202
        }
203
204 2
        $jumbotronImage->save();
205 2
    }
206
207
    /***************************************************************************/
208
209
    /**
210
     * Upload image on server.
211
     * $imageFile - the file to upload
212
     * $imageSubdir is the subdir in /storage/app/public/images/..
213
     *
214
     * @param  array $imageFile
215
     * @param  string $imageName
216
     * @param  string $imageSubdir
217
     * @param  string $imageWidth
218
     * @param  string $thumbWidth
219
     * @return void
220
     */
221 1
    public static function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth)
222
    {
223
224
        // Create dir if not exist (in /storage/app/public/images/..)
225 1
        if (! \Storage::disk('public')->has('images/'.$imageSubdir.'/')) {
0 ignored issues
show
The method has() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

225
        if (! \Storage::disk('public')->/** @scrutinizer ignore-call */ has('images/'.$imageSubdir.'/')) {
Loading history...
226 1
            \Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
227
        }
228
229 1
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
230
231
        // Resize the image with Intervention - http://image.intervention.io/api/resize
232
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
233
        // - save file as jpg with medium quality
234 1
        $image = Image::make($imageFile->getRealPath())
235 1
                                ->resize($imageWidth, null,
0 ignored issues
show
$imageWidth of type string is incompatible with the type integer expected by parameter $width of Intervention\Image\Image::resize(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

235
                                ->resize(/** @scrutinizer ignore-type */ $imageWidth, null,
Loading history...
236
                                    function ($constraint) {
237 1
                                        $constraint->aspectRatio();
238 1
                                    })
239 1
                                ->save(storage_path($destinationPath.$imageName), 75);
240
241
        // Create the thumb
242 1
        $image->resize($thumbWidth, null,
243
                    function ($constraint) {
244 1
                        $constraint->aspectRatio();
245 1
                    })
246 1
                ->save(storage_path($destinationPath.'thumb_'.$imageName), 75);
247 1
    }
248
249
    /***************************************************************************/
250
251
    /**
252
     * Return and array with the jumbotron possible height options.
253
     *
254
     * @return array
255
     */
256 2
    public static function getJumbotronHeightArray()
257
    {
258
        $ret = [
259 2
            'is-small' => 'Small',
260
            'is-medium' => 'Medium',
261
            'is-large' => 'Large',
262
            'is-halfheight' => 'Halfheight',
263
            'is-fullheight' => 'Fullheight',
264
        ];
265
266 2
        return $ret;
267
    }
268
269
    /***************************************************************************/
270
271
    /**
272
     * Return and array with the jumbotron possible opacity options.
273
     *
274
     * @return array
275
     */
276 2
    public static function getCoverOpacityArray()
277
    {
278
        $ret = [
279 2
            '0' => 'none',
280
            '0.1' => '10%',
281
            '0.2' => '20%',
282
            '0.3' => '30%',
283
            '0.4' => '40%',
284
            '0.5' => '50%',
285
        ];
286
287 2
        return $ret;
288
    }
289
290
    /***************************************************************************/
291
292
    /**
293
     * Return and array with the button possible color options.
294
     *
295
     * @return array
296
     */
297 2
    public static function getButtonColorArray()
298
    {
299
        $ret = [
300 2
            'press-red' => 'Red',
301
            'press-pink' => 'Pink',
302
            'press-purple' => 'Purple',
303
            'press-deeppurple' => 'Deep purple',
304
            'press-indigo' => 'Indigo',
305
            'press-blue' => 'Blue',
306
            'press-lightblue' => 'Light blue',
307
            'press-cyan' => 'Cyan',
308
            'press-teal' => 'Teal',
309
            'press-green' => 'Green',
310
            'press-lightgreen' => 'Light green',
311
            'press-lime' => 'Lime',
312
            'press-yellow' => 'Yellow',
313
            'press-amber' => 'Amber',
314
            'press-orange' => 'Orange',
315
            'press-deeporange' => 'Deeporange',
316
            'press-brown' => 'Brown',
317
            'press-grey' => 'Grey',
318
            'press-bluegrey' => 'Blue grey',
319
            'press-black' => 'Black',
320
        ];
321
322 2
        return $ret;
323
    }
324
325
    /***************************************************************************/
326
327
    /**
328
     * Return and array with the text possible width options.
329
     *
330
     * @return array
331
     */
332 2
    public static function getTextWidthArray()
333
    {
334
        $ret = [
335 2
            '100' => '100%',
336
            '90' => '90%',
337
            '80' => '80%',
338
            '70' => '70%',
339
            '60' => '60%',
340
            '50' => '50%',
341
            '40' => '40%',
342
            '30' => '30%',
343
            '20' => '20%',
344
        ];
345
346 2
        return $ret;
347
    }
348
349
    /***************************************************************************/
350
351
    /**
352
     * Return and array with the text possible vertical alignment options.
353
     *
354
     * @return array
355
     */
356 2
    public static function getTextVerticalAlignmentArray()
357
    {
358
        $ret = [
359 2
            'align-items: flex-start;' => 'Top',
360
            'align-items: center;' => 'Center',
361
            'align-items: flex-end;' => 'Bottom',
362
        ];
363
364 2
        return $ret;
365
    }
366
367
    /***************************************************************************/
368
369
    /**
370
     * Return and array with the text possible horizontal alignment options.
371
     *
372
     * @return array
373
     */
374 2
    public static function getTextHorizontalAlignmentArray()
375
    {
376
        $ret = [
377 2
            'left' => 'Left',
378
            'center' => 'Center',
379
            'right' => 'Right',
380
        ];
381
382 2
        return $ret;
383
    }
384
385
    /***************************************************************************/
386
387
    /**
388
     * Return and array with the text possible shadow options.
389
     *
390
     * @return array
391
     */
392 2
    public static function getTextShadowArray()
393
    {
394
        $ret = [
395 2
            '' => 'None',
396
            'text-shadow: 2px 1px 5px rgba(0,0,0,0.3);' => 'Small',
397
            'text-shadow: 2px 1px 1px rgba(0,0,0,0.3);' => 'Small Blurred',
398
            'text-shadow: 3px 2px 2px rgba(0,0,0,0.3);' => 'High',
399
        ];
400
401 2
        return $ret;
402
    }
403
}
404