Completed
Push — master ( 324eee...5f32a2 )
by Davide
04:51
created

CardController::store()   A

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

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

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