TestimonialController::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 9
c 2
b 0
f 2
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.9666
nc 2
nop 2
cc 2
crap 2
1
<?php
2
3
namespace DavideCasiraghi\LaravelTestimonials\Http\Controllers;
4
5
use DavideCasiraghi\LaravelFormPartials\Facades\LaravelFormPartials;
6
use DavideCasiraghi\LaravelTestimonials\Facades\LaravelTestimonials;
7
use DavideCasiraghi\LaravelTestimonials\Models\Testimonial;
8
use DavideCasiraghi\LaravelTestimonials\Models\TestimonialGroup;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\App;
11
use Intervention\Image\ImageManagerStatic as Image;
12
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
13
use Validator;
14
15
class TestimonialController extends Controller
16
{
17
    /* Restrict the access to this resource just to logged in users */
18 11
    public function __construct()
19
    {
20 11
        $this->middleware('auth', ['except' => ['show']]);
21 11
    }
22
23
    /**
24
     * Display the specified resource.
25
     *
26
     * @param  \Illuminate\Http\Request  $request
27
     * @return \Illuminate\Http\Response
28
     */
29 5
    public function index(Request $request)
30
    {
31 5
        $searchKeywords = $request->input('keywords');
32
        //$searchCategory = $request->input('category_id');
33 5
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
34
35 5
        if ($searchKeywords) {
36
            $testimonials = Testimonial::
37
                        select('testimonial_translations.testimonial_id AS id', 'name', 'body', 'image_file_name', 'locale')
38
                        ->join('testimonial_translations', 'testimonials.id', '=', 'testimonial_translations.testimonial_id')
39
                        ->orderBy('name')
40
                        ->where('name', 'like', '%'.$searchKeywords.'%')
41
                        ->where('locale', 'en')
42
                        ->paginate(20);
43
        } else {
44
            $testimonials = Testimonial::
45 5
                        select('testimonial_translations.testimonial_id AS id', 'name', 'body', 'image_file_name', 'locale')
46 5
                        ->join('testimonial_translations', 'testimonials.id', '=', 'testimonial_translations.testimonial_id')
47 5
                        ->where('locale', 'en')
48 5
                        ->orderBy('name')
49 5
                        ->paginate(20);
50
        }
51
52 5
        return view('laravel-testimonials::testimonials.index', compact('testimonials'))
53 5
                     ->with('i', (request()->input('page', 1) - 1) * 20)
54 5
                     ->with('searchKeywords', $searchKeywords)
55 5
                     ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
56
    }
57
58
    /***************************************************************************/
59
60
    /**
61
     * Show the form for creating a new resource.
62
     *
63
     * @return \Illuminate\Http\Response
64
     */
65 1
    public function create()
66
    {
67 1
        return view('laravel-testimonials::testimonials.create')
68 1
                    ->with('testimonialGroupsArray', $this->getTestimonialGroupsArray());
69
    }
70
71
    /***************************************************************************/
72
73
    /**
74
     * Store a newly created resource in storage.
75
     *
76
     * @param  \Illuminate\Http\Request  $request
77
     * @return \Illuminate\Http\Response
78
     */
79 2
    public function store(Request $request)
80
    {
81
        // Validate form datas
82 2
        $validator = Validator::make($request->all(), [
83 2
            'name' => 'required',
84
        ]);
85 2
        if ($validator->fails()) {
86 1
            return back()->withErrors($validator)->withInput();
87
        }
88
89 1
        $testimonial = new Testimonial();
90
91
        // Set the default language to edit the quote in English
92 1
        App::setLocale('en');
93
94 1
        $this->saveOnDb($request, $testimonial);
95
96 1
        return redirect()->route('testimonials.index')
97 1
                            ->with('success', 'Testimonial added succesfully');
98
    }
99
100
    /***************************************************************************/
101
102
    /**
103
     * Display the specified resource.
104
     *
105
     * @param  int $testimonialId
106
     * @return \Illuminate\Http\Response
107
     */
108 1
    public function show($testimonialId = null)
109
    {
110
        //$testimonial = Testimonial::find($testimonialId);
111 1
        $testimonial = Laraveltestimonials::getTestimonial($testimonialId);
0 ignored issues
show
Bug introduced by
The method getTestimonial() does not exist on DavideCasiraghi\LaravelT...des\LaravelTestimonials. 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

111
        /** @scrutinizer ignore-call */ 
112
        $testimonial = Laraveltestimonials::getTestimonial($testimonialId);
Loading history...
112 1
        $testimonialParameters = ($testimonial) ? (Laraveltestimonials::getParametersArray($testimonial)) : null;
0 ignored issues
show
Bug introduced by
The method getParametersArray() does not exist on DavideCasiraghi\LaravelT...des\LaravelTestimonials. 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

112
        $testimonialParameters = ($testimonial) ? (Laraveltestimonials::/** @scrutinizer ignore-call */ getParametersArray($testimonial)) : null;
Loading history...
113
114 1
        return view('laravel-testimonials::testimonials.show', compact('testimonial'))
115 1
                ->with('testimonialParameters', $testimonialParameters);
116
    }
117
118
    /***************************************************************************/
119
120
    /**
121
     * Show the form for editing the specified resource.
122
     *
123
     * @param  int $testimonialId
124
     * @return \Illuminate\Http\Response
125
     */
126 1
    public function edit($testimonialId = null)
127
    {
128 1
        $testimonial = Testimonial::find($testimonialId);
129
130 1
        return view('laravel-testimonials::testimonials.edit', compact('testimonial'))
131 1
                    ->with('testimonialGroupsArray', $this->getTestimonialGroupsArray());
132
    }
133
134
    /***************************************************************************/
135
136
    /**
137
     * Update the specified resource in storage.
138
     *
139
     * @param  \Illuminate\Http\Request  $request
140
     * @param  int  $testimonialId
141
     * @return \Illuminate\Http\Response
142
     */
143 2
    public function update(Request $request, $testimonialId)
144
    {
145
        // Validate form datas
146 2
        $validator = Validator::make($request->all(), [
147 2
            'name' => 'required',
148
        ]);
149 2
        if ($validator->fails()) {
150 1
            return back()->withErrors($validator)->withInput();
151
        }
152
153 1
        $testimonial = Testimonial::find($testimonialId);
154
155
        // Set the default language to update the quote in English
156 1
        App::setLocale('en');
157
158 1
        $this->saveOnDb($request, $testimonial);
159
160 1
        return redirect()->route('testimonials.index')
161 1
                            ->with('success', 'Testimonial updated succesfully');
162
    }
163
164
    /***************************************************************************/
165
166
    /**
167
     * Remove the specified resource from storage.
168
     *
169
     * @param  int  $testimonialId
170
     * @return \Illuminate\Http\Response
171
     */
172 1
    public function destroy($testimonialId)
173
    {
174 1
        $testimonial = Testimonial::find($testimonialId);
175 1
        $testimonial->delete();
176
177 1
        return redirect()->route('testimonials.index')
178 1
                            ->with('success', 'Testimonial deleted succesfully');
179
    }
180
181
    /***************************************************************************/
182
183
    /**
184
     * Save the record on DB.
185
     * @param  \Illuminate\Http\Request  $request
186
     * @param  \DavideCasiraghi\Laraveltestimonials\Models\Testimonial  $testimonial
187
     * @return void
188
     */
189 2
    public function saveOnDb($request, $testimonial)
190
    {
191 2
        $testimonial->translateOrNew('en')->name = $request->get('name');
192 2
        $testimonial->translateOrNew('en')->body = $request->get('body');
193 2
        $testimonial->translateOrNew('en')->profession = $request->get('profession');
194
195 2
        $testimonial->testimonials_group = $request->get('testimonials_group');
196 2
        $testimonial->gender = $request->get('gender');
197 2
        $testimonial->image_file_name = $request->get('image_file_name');
198
199
        // Testimonial image upload
200 2
        $imageSubdir = 'testimonials';
201 2
        $imageWidth = '300';
202 2
        $thumbWidth = '300';
203 2
        $testimonial->image_file_name = LaravelFormPartials::uploadImageOnServer($request->file('image_file_name'), $request->image_file_name, $imageSubdir, $imageWidth, $thumbWidth);
204
205 2
        $testimonial->save();
206 2
    }
207
208
    /***************************************************************************/
209
210
    /**
211
     * Return and array with the testimonial groups.
212
     *
213
     * @return array
214
     */
215 2
    public static function getTestimonialGroupsArray()
216
    {
217 2
        $ret = TestimonialGroup::listsTranslations('title')->orderBy('title')->pluck('title', 'id');
218
219 2
        return $ret;
220
    }
221
}
222