TestimonialGroupController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

107
        /** @scrutinizer ignore-call */ 
108
        $testimonialGroup = Laraveltestimonials::getTestimonialGroup($testimonialGroupId);
Loading history...
108 1
        $testimonialGroupParameters = ($testimonialGroup) ? (Laraveltestimonials::getParametersArray($testimonialGroup)) : 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

108
        $testimonialGroupParameters = ($testimonialGroup) ? (Laraveltestimonials::/** @scrutinizer ignore-call */ getParametersArray($testimonialGroup)) : null;
Loading history...
109 1
        $testimonials = Laraveltestimonials::getTestimonialsByGroup($testimonialGroupId);
0 ignored issues
show
Bug introduced by
The method getTestimonialsByGroup() 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

109
        /** @scrutinizer ignore-call */ 
110
        $testimonials = Laraveltestimonials::getTestimonialsByGroup($testimonialGroupId);
Loading history...
110
        //dd($testimonialGroup);
111 1
        return view('laravel-testimonials::testimonialGroups.show', compact('testimonialGroup'))
112 1
                ->with('testimonialGroup', $testimonialGroup)
113 1
                ->with('testimonialGroupParameters', $testimonialGroupParameters)
114 1
                ->with('testimonials', $testimonials);
115
    }
116
117
    /***************************************************************************/
118
119
    /**
120
     * Show the form for editing the specified resource.
121
     *
122
     * @param  int $testimonialId
123
     * @return \Illuminate\Http\Response
124
     */
125 1
    public function edit($testimonialGroupId = null)
126
    {
127 1
        $testimonialGroup = TestimonialGroup::find($testimonialGroupId);
128
129 1
        return view('laravel-testimonials::testimonialGroups.edit', compact('testimonialGroup'));
130
    }
131
132
    /***************************************************************************/
133
134
    /**
135
     * Update the specified resource in storage.
136
     *
137
     * @param  \Illuminate\Http\Request  $request
138
     * @param  int  $testimonialGroupId
139
     * @return \Illuminate\Http\Response
140
     */
141 2
    public function update(Request $request, $testimonialGroupId)
142
    {
143
        // Validate form datas
144 2
        $validator = Validator::make($request->all(), [
145 2
            'title' => 'required',
146
        ]);
147 2
        if ($validator->fails()) {
148 1
            return back()->withErrors($validator)->withInput();
149
        }
150
151 1
        $testimonialGroup = TestimonialGroup::find($testimonialGroupId);
152
153
        // Set the default language to update the quote in English
154 1
        App::setLocale('en');
155
156 1
        $this->saveOnDb($request, $testimonialGroup);
157
158 1
        return redirect()->route('testimonialGroups.index')
159 1
                            ->with('success', 'Testimonial group updated succesfully');
160
    }
161
162
    /***************************************************************************/
163
164
    /**
165
     * Remove the specified resource from storage.
166
     *
167
     * @param  int  $testimonialGroupId
168
     * @return \Illuminate\Http\Response
169
     */
170 1
    public function destroy($testimonialGroupId)
171
    {
172 1
        $testimonialGroup = TestimonialGroup::find($testimonialGroupId);
173 1
        $testimonialGroup->delete();
174
175 1
        return redirect()->route('testimonialGroups.index')
176 1
                            ->with('success', 'Testimonial group deleted succesfully');
177
    }
178
179
    /***************************************************************************/
180
181
    /**
182
     * Save the record on DB.
183
     * @param  \Illuminate\Http\Request  $request
184
     * @param  \DavideCasiraghi\Laraveltestimonials\Models\TestimonialGroup  $testimonial
185
     * @return void
186
     */
187 2
    public function saveOnDb($request, $testimonialGroup)
188
    {
189 2
        $testimonialGroup->translateOrNew('en')->title = $request->get('title');
190 2
        $testimonialGroup->quotes_color = $request->get('quotes_color');
191 2
        $testimonialGroup->max_characters = $request->get('max_characters');
192 2
        $testimonialGroup->show_title = filter_var($request->show_title, FILTER_VALIDATE_BOOLEAN);
193 2
        $testimonialGroup->title_alignment = $request->get('title_alignment');
194
195 2
        $testimonialGroup->save();
196 2
    }
197
}
198