Passed
Push — master ( fcb56b...035d46 )
by Davide
03:33
created

JumbotronImageTranslationController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 13
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelJumbotronImages\Http\Controllers;
4
5
use Validator;
6
use Illuminate\Http\Request;
7
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
8
use DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImageTranslation;
9
10
class JumbotronImageTranslationController
11
{
12
    /***************************************************************************/
13
14
    /**
15
     * Show the form for creating a new resource.
16
     * @param int $jumbotronImageId
17
     * @param string $languageCode
18
     * @return \Illuminate\Http\Response
19
     */
20 1
    public function create($jumbotronImageId, $languageCode)
21
    {
22 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
23
24 1
        return view('laravel-jumbotron-images::jumbotronImagesTranslations.create')
25 1
                ->with('jumbotronImageId', $jumbotronImageId)
26 1
                ->with('languageCode', $languageCode)
27 1
                ->with('selectedLocaleName', $selectedLocaleName);
28
    }
29
30
    /***************************************************************************/
31
32
    /**
33
     * Show the form for editing the specified resource.
34
     *
35
     * @param int $jumbotronImageTranslationId
36
     * @param string $languageCode
37
     * @return \Illuminate\Http\Response
38
     */
39 1
    public function edit($jumbotronImageTranslationId, $languageCode)
40
    {
41 1
        $jumbotronImageTranslation = JumbotronImageTranslation::where('jumbotron_image_id', $jumbotronImageTranslationId)
42 1
                        ->where('locale', $languageCode)
43 1
                        ->first();
44
45 1
        $selectedLocaleName = $this->getSelectedLocaleName($languageCode);
46
47 1
        return view('laravel-jumbotron-images::jumbotronImagesTranslations.edit', compact('jumbotronImageTranslation'))
48 1
                    ->with('jumbotronImageTranslationId', $jumbotronImageTranslationId)
49 1
                    ->with('languageCode', $languageCode)
50 1
                    ->with('selectedLocaleName', $selectedLocaleName);
51
    }
52
53
    /***************************************************************************/
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param  \Illuminate\Http\Request  $request
59
     * @return \Illuminate\Http\Response
60
     */
61 1
    public function store(Request $request)
62
    {
63
64
        // Validate form datas
65
        /*$validator = Validator::make($request->all(), [
66
                'text' => 'required',
67
            ]);
68
        if ($validator->fails()) {
69
            return back()->withErrors($validator)->withInput();
70
        }*/
71
72 1
        $jumbotronImageTranslation = new JumbotronImageTranslation();
73
74 1
        $this->saveOnDb($request, $jumbotronImageTranslation, 'save');
75
76 1
        return redirect()->route('jumbotron-images.index')
77 1
                            ->with('success', 'Jumbotron Image translation added succesfully');
78
    }
79
80
    /***************************************************************************/
81
82
    /**
83
     * Update the specified resource in storage.
84
     *
85
     * @param  \Illuminate\Http\Request  $request
86
     * @param  int  $jumbotronImageTranslationId
87
     * @return \Illuminate\Http\Response
88
     */
89 1
    public function update(Request $request, $jumbotronImageTranslationId)
90
    {
91
        /*request()->validate([
92
            'text' => 'required',
93
        ]);*/
94
        
95
96 1
        $jumbotronImageTranslation = JumbotronImageTranslation::find($jumbotronImageTranslationId);
97
        //dd($jumbotronImageTranslation);
98 1
        $this->saveOnDb($request, $jumbotronImageTranslation, 'update');
99
        
100 1
        return redirect()->route('jumbotron-images.index')
101 1
                            ->with('success', 'Jumbotron Image translation added succesfully');
102
    }
103
104
    /***************************************************************************/
105
106
    /**
107
     * Save the record on DB.
108
     * @param  \Illuminate\Http\Request  $request
109
     * @param  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImageTranslation  $jumbotronImageTranslation
110
     * @return void
111
     */
112 2
    public function saveOnDb($request, $jumbotronImageTranslation, $saveOrUpdate)
113
    {
114 2
        $jumbotronImageTranslation->title = $request->get('title');
115 2
        $jumbotronImageTranslation->body = $request->get('body');
116 2
        $jumbotronImageTranslation->button_text = $request->get('button_text');
117
        
118 2
        switch ($saveOrUpdate) {
119 2
            case 'save':
120 1
                $jumbotronImageTranslation->jumbotron_image_id = $request->get('jumbotron_image_id');
121 1
                $jumbotronImageTranslation->locale = $request->get('language_code');
122 1
                $jumbotronImageTranslation->save();
123 1
                break;
124 1
            case 'update':
125 1
                $jumbotronImageTranslation->update();
126 1
                break;
127
        }
128
        
129 2
    }
130
131
    /***************************************************************************/
132
133
    /**
134
     * Get the language name from language code.
135
     *
136
     * @param  string $languageCode
137
     * @return string
138
     */
139 2
    public function getSelectedLocaleName($languageCode)
140
    {
141 2
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
142 2
        $ret = $countriesAvailableForTranslations[$languageCode]['name'];
143
144 2
        return $ret;
145
    }
146
147
    /***************************************************************************/
148
149
    /**
150
     * Remove the specified resource from storage.
151
     *
152
     * @param  int  $jumbotronImageTranslationId
153
     */
154 1
    public function destroy($jumbotronImageTranslationId)
155
    {
156 1
        $jumbotronImageTranslation = JumbotronImageTranslation::find($jumbotronImageTranslationId);
157 1
        $jumbotronImageTranslation->delete();
158
159 1
        return redirect()->route('jumbotron-images.index')
160 1
                            ->with('success', 'Jumbotron Image translation deleted succesfully');
161
    }
162
}
163