Completed
Push — master ( 05ded5...0674f6 )
by Davide
05:57 queued 16s
created

CardController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelCards\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\LaravelCards\Models\Card;
10
use DavideCasiraghi\LaravelCards\Facades\LaravelCards;
11
12
class CardController
13
{
14
    /**
15
     * Display the specified resource.
16
     *
17
     * @param  \Illuminate\Http\Request  $request
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function index(Request $request)
21
    {
22
        $searchKeywords = $request->input('keywords');
23
        //$searchCategory = $request->input('category_id');
24
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
25
26
        if ($searchKeywords) {
27
            $cards = Card::
28
                                select('card_translations.card_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale')
29
                                ->join('card_translations', 'cards.id', '=', 'card_translations.card_id')
30
                                ->orderBy('title')
31
                                ->where('title', 'like', '%'.$searchKeywords.'%')
32
                                ->where('locale', 'en')
33
                                ->paginate(20);
34
        } else {
35
            $cards = Card::
36
                                select('card_translations.card_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale')
37
                                ->join('card_translations', 'cards.id', '=', 'card_translations.card_id')
38
                                ->where('locale', 'en')
39
                                ->orderBy('title')
40
                                ->paginate(20);
41
        }
42
43
        return view('laravel-cards::cards.index', compact('cards'))
44
                             ->with('i', (request()->input('page', 1) - 1) * 20)
45
                             ->with('searchKeywords', $searchKeywords)
46
                             ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
47
    }
48
49
    /***************************************************************************/
50
51
    /**
52
     * Show the form for creating a new resource.
53
     *
54
     * @return \Illuminate\Http\Response
55
     */
56
    public function create()
57
    {
58
        return view('laravel-cards::cards.create')
59
                    ->with('buttonColorArray', $this->getButtonColorArray());
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
    public function store(Request $request)
71
    {
72
        $card = new Card();
73
74
        // Set the default language to edit the quote in English
75
        App::setLocale('en');
76
77
        $this->saveOnDb($request, $card);
78
79
        return redirect()->route('laravel-cards.index')
80
                            ->with('success', 'Card image added succesfully');
81
    }
82
83
    /***************************************************************************/
84
85
    /**
86
     * Display the specified resource.
87
     *
88
     * @param  int $cardId
89
     * @return \Illuminate\Http\Response
90
     */
91
    public function show($cardId = null)
92
    {
93
        //$card = Card::find($cardId);
94
        $card = LaravelCards::getCard($cardId);
0 ignored issues
show
Bug introduced by
The method getCard() does not exist on DavideCasiraghi\LaravelCards\Facades\LaravelCards. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

94
        /** @scrutinizer ignore-call */ 
95
        $card = LaravelCards::getCard($cardId);
Loading history...
95
        $cardParameters = ($card) ? (LaravelCards::getParametersArray($card)) : null;
0 ignored issues
show
Bug introduced by
The method getParametersArray() does not exist on DavideCasiraghi\LaravelCards\Facades\LaravelCards. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

95
        $cardParameters = ($card) ? (LaravelCards::/** @scrutinizer ignore-call */ getParametersArray($card)) : null;
Loading history...
96
        
97
        return view('laravel-cards::cards.show', compact('card'))
98
                ->with('cardParameters', $cardParameters);
99
    }
100
101
    /***************************************************************************/
102
103
    /**
104
     * Show the form for editing the specified resource.
105
     *
106
     * @param  int $cardId
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function edit($cardId = null)
110
    {
111
        $card = Card::find($cardId);
112
113
        return view('laravel-cards::cards.edit', compact('card'))
114
                    ->with('buttonColorArray', $this->getButtonColorArray());
115
    }
116
117
    /***************************************************************************/
118
119
    /**
120
     * Update the specified resource in storage.
121
     *
122
     * @param  \Illuminate\Http\Request  $request
123
     * @param  int  $cardId
124
     * @return \Illuminate\Http\Response
125
     */
126
    public function update(Request $request, $cardId)
127
    {
128
        $card = Card::find($cardId);
129
130
        // Set the default language to update the quote in English
131
        App::setLocale('en');
132
133
        $this->saveOnDb($request, $card);
134
135
        return redirect()->route('laravel-cards.index')
136
                            ->with('success', 'Card image updated succesfully');
137
    }
138
139
    /***************************************************************************/
140
141
    /**
142
     * Remove the specified resource from storage.
143
     *
144
     * @param  int  $cardId
145
     * @return \Illuminate\Http\Response
146
     */
147
    public function destroy($cardId)
148
    {
149
        $card = Card::find($cardId);
150
        $card->delete();
151
152
        return redirect()->route('laravel-cards.index')
153
                            ->with('success', 'Card image deleted succesfully');
154
    }
155
156
    /***************************************************************************/
157
158
    /**
159
     * Save the record on DB.
160
     * @param  \Illuminate\Http\Request  $request
161
     * @param  \DavideCasiraghi\LaravelCards\Models\Card  $card
162
     * @return void
163
     */
164
    public function saveOnDb($request, $card)
165
    {
166
        $card->translateOrNew('en')->heading = $request->get('heading');
167
        $card->translateOrNew('en')->title = $request->get('title');
168
        $card->translateOrNew('en')->body = $request->get('body');
169
        $card->translateOrNew('en')->button_text = $request->get('button_text');
170
        $card->translateOrNew('en')->image_alt = $request->get('image_alt');
171
        
172
        $card->img_alignment = $request->get('img_alignment');
173
        $card->img_col_size = $request->get('img_col_size');
174
        $card->img_col_size = $request->get('img_col_size');
175
        $card->bkg_color = $request->get('bkg_color');        
176
        $card->button_url = $request->get('button_url');
177
        $card->button_color = $request->get('button_color');
178
        $card->button_corners = $request->get('button_corners');
179
        $card->button_icon = $request->get('button_icon');
180
        $card->container_wrap = ($request->container_wrap == 'on') ? 1 : 0;
181
        
182
        // Card image upload
183
        if ($request->file('image_file_name')) {
184
            $imageFile = $request->file('image_file_name');
185
            $imageName = $imageFile->hashName();
186
            $imageSubdir = 'cards';
187
            $imageWidth = '1067';
188
            $thumbWidth = '690';
189
190
            $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\LaravelC...::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

190
            $this->uploadImageOnServer(/** @scrutinizer ignore-type */ $imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
Loading history...
191
            $card->image_file_name = $imageName;
192
        } else {
193
            $card->image_file_name = $request->image_file_name;
194
        }
195
196
        $card->save();
197
    }
198
199
    /***************************************************************************/
200
201
    /**
202
     * Upload image on server.
203
     * $imageFile - the file to upload
204
     * $imageSubdir is the subdir in /storage/app/public/images/..
205
     *
206
     * @param  array $imageFile
207
     * @param  string $imageName
208
     * @param  string $imageSubdir
209
     * @param  string $imageWidth
210
     * @param  string $thumbWidth
211
     * @return void
212
     */
213
    public static function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth)
214
    {
215
216
        // Create dir if not exist (in /storage/app/public/images/..)
217
        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

217
        if (! \Storage::disk('public')->/** @scrutinizer ignore-call */ has('images/'.$imageSubdir.'/')) {
Loading history...
218
            \Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
219
        }
220
221
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
222
223
        // Resize the image with Intervention - http://image.intervention.io/api/resize
224
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
225
        // - save file as jpg with medium quality
226
        $image = Image::make($imageFile->getRealPath())
227
                                ->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

227
                                ->resize(/** @scrutinizer ignore-type */ $imageWidth, null,
Loading history...
228
                                    function ($constraint) {
229
                                        $constraint->aspectRatio();
230
                                    })
231
                                ->save(storage_path($destinationPath.$imageName), 75);
232
233
        // Create the thumb
234
        $image->resize($thumbWidth, null,
235
                    function ($constraint) {
236
                        $constraint->aspectRatio();
237
                    })
238
                ->save(storage_path($destinationPath.'thumb_'.$imageName), 75);
239
    }
240
241
    /***************************************************************************/
242
243
    /**
244
     * Return and array with the button possible color options.
245
     *
246
     * @return array
247
     */
248
    public static function getButtonColorArray()
249
    {
250
        $ret = [
251
             'press-red' => 'Red',
252
             'press-pink' => 'Pink',
253
             'press-purple' => 'Purple',
254
             'press-deeppurple' => 'Deep purple',
255
             'press-indigo' => 'Indigo',
256
             'press-blue' => 'Blue',
257
             'press-lightblue' => 'Light blue',
258
             'press-cyan' => 'Cyan',
259
             'press-teal' => 'Teal',
260
             'press-green' => 'Green',
261
             'press-lightgreen' => 'Light green',
262
             'press-lime' => 'Lime',
263
             'press-yellow' => 'Yellow',
264
             'press-amber' => 'Amber',
265
             'press-orange' => 'Orange',
266
             'press-deeporange' => 'Deeporange',
267
             'press-brown' => 'Brown',
268
             'press-grey' => 'Grey',
269
             'press-bluegrey' => 'Blue grey',
270
             'press-black' => 'Black',
271
         ];
272
273
        return $ret;
274
    }
275
276
277
}
278