Completed
Push — master ( e53778...db0b20 )
by Davide
03:26
created

JumbotronImageController::getButtonColorArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

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

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

223
        if (! \Storage::disk('public')->/** @scrutinizer ignore-call */ has('images/'.$imageSubdir.'/')) {
Loading history...
224
            \Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
225
        }
226
227
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
228
229
        // Resize the image with Intervention - http://image.intervention.io/api/resize
230
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
231
        // - save file as jpg with medium quality
232
        $image = Image::make($imageFile->getRealPath())
233
                                ->resize($imageWidth, null,
0 ignored issues
show
Bug introduced by
$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

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