1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\PhpResponsiveRandomQuote\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
8
|
|
|
use DavideCasiraghi\PhpResponsiveRandomQuote\Models\QuoteTranslation; |
9
|
|
|
|
10
|
|
|
class ResponsiveQuoteTranslationController |
11
|
|
|
{ |
12
|
|
|
/***************************************************************************/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Show the form for creating a new resource. |
16
|
|
|
* @param int $quoteId |
17
|
|
|
* @param string $languageCode |
18
|
|
|
* @return \Illuminate\Http\Response |
19
|
|
|
*/ |
20
|
1 |
|
public function create($quoteId, $languageCode) |
21
|
|
|
{ |
22
|
1 |
|
$selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
23
|
|
|
|
24
|
1 |
|
return view('php-responsive-quote::quoteTranslations.create') |
25
|
1 |
|
->with('quoteId', $quoteId) |
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 $quoteId |
36
|
|
|
* @param string $languageCode |
37
|
|
|
* @return \Illuminate\Http\Response |
38
|
|
|
*/ |
39
|
1 |
|
public function edit($quoteId, $languageCode) |
40
|
|
|
{ |
41
|
1 |
|
$quoteTranslation = QuoteTranslation::where('quote_id', $quoteId) |
42
|
1 |
|
->where('locale', $languageCode) |
43
|
1 |
|
->first(); |
44
|
|
|
|
45
|
1 |
|
$selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
46
|
|
|
|
47
|
1 |
|
return view('php-responsive-quote::quoteTranslations.edit', compact('quoteTranslation')) |
48
|
1 |
|
->with('quoteId', $quoteId) |
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
|
1 |
|
$validator = Validator::make($request->all(), [ |
66
|
1 |
|
'text' => 'required', |
67
|
|
|
]); |
68
|
1 |
|
if ($validator->fails()) { |
69
|
|
|
return back()->withErrors($validator)->withInput(); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$quoteTranslation = new QuoteTranslation(); |
73
|
|
|
|
74
|
1 |
|
$this->saveOnDb($request, $quoteTranslation); |
75
|
|
|
|
76
|
1 |
|
return redirect()->route('php-responsive-quote.index') |
77
|
1 |
|
->with('success', 'Quote 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 $quoteTranslationId |
87
|
|
|
* @return \Illuminate\Http\Response |
88
|
|
|
*/ |
89
|
1 |
|
public function update(Request $request, $quoteTranslationId) |
90
|
|
|
{ |
91
|
1 |
|
request()->validate([ |
92
|
1 |
|
'text' => 'required', |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
$quoteTranslation = QuoteTranslation::find($quoteTranslationId); |
96
|
|
|
|
97
|
|
|
$this->saveOnDb($request, $quoteTranslation); |
98
|
|
|
|
99
|
|
|
return redirect()->route('php-responsive-quote.index') |
100
|
|
|
->with('success', 'Quote translation added succesfully'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/***************************************************************************/ |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Save the record on DB. |
107
|
|
|
* @param \Illuminate\Http\Request $request |
108
|
|
|
* @param \DavideCasiraghi\PhpResponsiveRandomQuote\Models\QuoteTranslation $quoteTranslation |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
1 |
|
public function saveOnDb($request, $quoteTranslation) |
112
|
|
|
{ |
113
|
1 |
|
$quoteTranslation->quote_id = $request->get('quote_id'); |
114
|
1 |
|
$quoteTranslation->locale = $request->get('language_code'); |
115
|
|
|
|
116
|
1 |
|
$quoteTranslation->text = $request->get('text'); |
117
|
1 |
|
$quoteTranslation->save(); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/***************************************************************************/ |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the language name from language code. |
124
|
|
|
* |
125
|
|
|
* @param string $languageCode |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
2 |
|
public function getSelectedLocaleName($languageCode) |
129
|
|
|
{ |
130
|
2 |
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
131
|
2 |
|
$ret = $countriesAvailableForTranslations[$languageCode]['name']; |
132
|
|
|
|
133
|
2 |
|
return $ret; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/***************************************************************************/ |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Remove the specified resource from storage. |
140
|
|
|
* |
141
|
|
|
* @param int $quoteTranslationId |
142
|
|
|
*/ |
143
|
1 |
|
public function destroy($quoteTranslationId) |
144
|
|
|
{ |
145
|
1 |
|
$quoteTranslation = QuoteTranslation::find($quoteTranslationId); |
146
|
1 |
|
$quoteTranslation->delete(); |
147
|
|
|
|
148
|
1 |
|
return redirect()->route('php-responsive-quote.index') |
149
|
1 |
|
->with('success', 'Quote translation deleted succesfully'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|