CardController::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 2
b 0
f 1
nc 1
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
namespace DavideCasiraghi\LaravelCards\Http\Controllers;
4
5
use DavideCasiraghi\LaravelCards\Facades\LaravelCards;
6
use DavideCasiraghi\LaravelCards\Models\Card;
7
use DavideCasiraghi\LaravelFormPartials\Facades\LaravelFormPartials;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\App;
10
use Intervention\Image\ImageManagerStatic as Image;
11
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
12
13
class CardController extends Controller
14
{
15
    /* Restrict the access to this resource just to logged in users */
16 8
    public function __construct()
17
    {
18 8
        $this->middleware('auth', ['except' => ['show']]);
19 8
    }
20
21
    /**
22
     * Display the specified resource.
23
     *
24
     * @param  \Illuminate\Http\Request  $request
25
     * @return \Illuminate\Http\Response
26
     */
27 2
    public function index(Request $request)
28
    {
29 2
        $searchKeywords = $request->input('keywords');
30
        //$searchCategory = $request->input('category_id');
31 2
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
32
33 2
        if ($searchKeywords) {
34
            $cards = Card::
35
                        select('card_translations.card_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale')
36
                        ->join('card_translations', 'cards.id', '=', 'card_translations.card_id')
37
                        ->orderBy('title')
38
                        ->where('title', 'like', '%'.$searchKeywords.'%')
39
                        ->where('locale', 'en')
40
                        ->paginate(20);
41
        } else {
42
            $cards = Card::
43 2
                        select('card_translations.card_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale')
44 2
                        ->join('card_translations', 'cards.id', '=', 'card_translations.card_id')
45 2
                        ->where('locale', 'en')
46 2
                        ->orderBy('title')
47 2
                        ->paginate(20);
48
        }
49
50 2
        return view('laravel-cards::cards.index', compact('cards'))
51 2
                     ->with('i', (request()->input('page', 1) - 1) * 20)
52 2
                     ->with('searchKeywords', $searchKeywords)
53 2
                     ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
54
    }
55
56
    /***************************************************************************/
57
58
    /**
59
     * Show the form for creating a new resource.
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63 1
    public function create()
64
    {
65 1
        return view('laravel-cards::cards.create')
66 1
                    ->with('buttonColorArray', $this->getButtonColorArray());
67
    }
68
69
    /***************************************************************************/
70
71
    /**
72
     * Store a newly created resource in storage.
73
     *
74
     * @param  \Illuminate\Http\Request  $request
75
     * @return \Illuminate\Http\Response
76
     */
77 1
    public function store(Request $request)
78
    {
79 1
        $card = new Card();
80
81
        // Set the default language to edit the quote in English
82 1
        App::setLocale('en');
83
84 1
        $this->saveOnDb($request, $card);
85
86 1
        return redirect()->route('laravel-cards.index')
87 1
                            ->with('success', 'Card image added succesfully');
88
    }
89
90
    /***************************************************************************/
91
92
    /**
93
     * Display the specified resource.
94
     *
95
     * @param  int $cardId
96
     * @return \Illuminate\Http\Response
97
     */
98 1
    public function show($cardId = null)
99
    {
100
        //$card = Card::find($cardId);
101 1
        $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

101
        /** @scrutinizer ignore-call */ 
102
        $card = LaravelCards::getCard($cardId);
Loading history...
102 1
        $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

102
        $cardParameters = ($card) ? (LaravelCards::/** @scrutinizer ignore-call */ getParametersArray($card)) : null;
Loading history...
103
104 1
        return view('laravel-cards::cards.show', compact('card'))
105 1
                ->with('cardParameters', $cardParameters);
106
    }
107
108
    /***************************************************************************/
109
110
    /**
111
     * Show the form for editing the specified resource.
112
     *
113
     * @param  int $cardId
114
     * @return \Illuminate\Http\Response
115
     */
116 1
    public function edit($cardId = null)
117
    {
118 1
        $card = Card::find($cardId);
119
120 1
        return view('laravel-cards::cards.edit', compact('card'))
121 1
                    ->with('buttonColorArray', $this->getButtonColorArray());
122
    }
123
124
    /***************************************************************************/
125
126
    /**
127
     * Update the specified resource in storage.
128
     *
129
     * @param  \Illuminate\Http\Request  $request
130
     * @param  int  $cardId
131
     * @return \Illuminate\Http\Response
132
     */
133 1
    public function update(Request $request, $cardId)
134
    {
135 1
        $card = Card::find($cardId);
136
137
        // Set the default language to update the quote in English
138 1
        App::setLocale('en');
139
140 1
        $this->saveOnDb($request, $card);
141
142 1
        return redirect()->route('laravel-cards.index')
143 1
                            ->with('success', 'Card image updated succesfully');
144
    }
145
146
    /***************************************************************************/
147
148
    /**
149
     * Remove the specified resource from storage.
150
     *
151
     * @param  int  $cardId
152
     * @return \Illuminate\Http\Response
153
     */
154 1
    public function destroy($cardId)
155
    {
156 1
        $card = Card::find($cardId);
157 1
        $card->delete();
158
159 1
        return redirect()->route('laravel-cards.index')
160 1
                            ->with('success', 'Card image deleted succesfully');
161
    }
162
163
    /***************************************************************************/
164
165
    /**
166
     * Save the record on DB.
167
     * @param  \Illuminate\Http\Request  $request
168
     * @param  \DavideCasiraghi\LaravelCards\Models\Card  $card
169
     * @return void
170
     */
171 2
    public function saveOnDb($request, $card)
172
    {
173 2
        $card->translateOrNew('en')->heading = $request->get('heading');
174 2
        $card->translateOrNew('en')->title = $request->get('title');
175 2
        $card->translateOrNew('en')->body = $request->get('body');
176 2
        $card->translateOrNew('en')->button_text = $request->get('button_text');
177 2
        $card->translateOrNew('en')->image_alt = $request->get('image_alt');
178
179 2
        $card->img_alignment = $request->get('img_alignment');
180 2
        $card->img_col_size = $request->get('img_col_size');
181 2
        $card->img_col_size = $request->get('img_col_size');
182 2
        $card->bkg_color = $request->get('bkg_color');
183 2
        $card->button_url = $request->get('button_url');
184 2
        $card->button_color = $request->get('button_color');
185 2
        $card->button_corners = $request->get('button_corners');
186 2
        $card->button_icon = $request->get('button_icon');
187 2
        $card->container_wrap = ($request->container_wrap == 'on') ? 1 : 0;
188
189
        // Card image upload
190 2
        $imageSubdir = 'cards';
191 2
        $imageWidth = '1067';
192 2
        $thumbWidth = '690';
193 2
        $card->image_file_name = LaravelFormPartials::uploadImageOnServer($request->file('image_file_name'), $request->image_file_name, $imageSubdir, $imageWidth, $thumbWidth);
194
195 2
        $card->save();
196 2
    }
197
198
    /***************************************************************************/
199
200
    /**
201
     * Return and array with the button possible color options.
202
     *
203
     * @return array
204
     */
205 2
    public static function getButtonColorArray()
206
    {
207
        $ret = [
208 2
            'press-red' => 'Red',
209
            'press-pink' => 'Pink',
210
            'press-purple' => 'Purple',
211
            'press-deeppurple' => 'Deep purple',
212
            'press-indigo' => 'Indigo',
213
            'press-blue' => 'Blue',
214
            'press-lightblue' => 'Light blue',
215
            'press-cyan' => 'Cyan',
216
            'press-teal' => 'Teal',
217
            'press-green' => 'Green',
218
            'press-lightgreen' => 'Light green',
219
            'press-lime' => 'Lime',
220
            'press-yellow' => 'Yellow',
221
            'press-amber' => 'Amber',
222
            'press-orange' => 'Orange',
223
            'press-deeporange' => 'Deeporange',
224
            'press-brown' => 'Brown',
225
            'press-grey' => 'Grey',
226
            'press-bluegrey' => 'Blue grey',
227
            'press-black' => 'Black',
228
        ];
229
230 2
        return $ret;
231
    }
232
}
233