Completed
Push — master ( fc061e...4c13a1 )
by Davide
11:37
created

ResponsiveQuoteTranslationController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

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