1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelCards\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use DavideCasiraghi\LaravelCards\Models\CardTranslation; |
8
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
9
|
|
|
|
10
|
|
|
class CardControllerTranslationController |
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
|
|
|
public function create($jumbotronImageId, $languageCode) |
21
|
|
|
{ |
22
|
|
|
$selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
23
|
|
|
|
24
|
|
|
return view('laravel-cards::cardsTranslations.create') |
25
|
|
|
->with('jumbotronImageId', $jumbotronImageId) |
26
|
|
|
->with('languageCode', $languageCode) |
27
|
|
|
->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
|
|
|
public function edit($jumbotronImageId, $languageCode) |
40
|
|
|
{ |
41
|
|
|
$jumbotronImageTranslation = CardTranslation::where('card_id', $jumbotronImageId) |
42
|
|
|
->where('locale', $languageCode) |
43
|
|
|
->first(); |
44
|
|
|
|
45
|
|
|
$selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
46
|
|
|
|
47
|
|
|
return view('laravel-cards::cardsTranslations.edit', compact('jumbotronImageTranslation')) |
48
|
|
|
->with('jumbotronImageId', $jumbotronImageId) |
49
|
|
|
->with('languageCode', $languageCode) |
50
|
|
|
->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
|
|
|
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
|
|
|
$jumbotronImageTranslation = new CardTranslation(); |
73
|
|
|
|
74
|
|
|
$this->saveOnDb($request, $jumbotronImageTranslation, 'save'); |
75
|
|
|
|
76
|
|
|
return redirect()->route('jumbotron-images.index') |
77
|
|
|
->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
|
|
|
public function update(Request $request, $jumbotronImageTranslationId) |
90
|
|
|
{ |
91
|
|
|
/*request()->validate([ |
92
|
|
|
'text' => 'required', |
93
|
|
|
]);*/ |
94
|
|
|
|
95
|
|
|
$jumbotronImageTranslation = CardTranslation::find($jumbotronImageTranslationId); |
96
|
|
|
//dd($jumbotronImageTranslation); |
97
|
|
|
$this->saveOnDb($request, $jumbotronImageTranslation, 'update'); |
98
|
|
|
|
99
|
|
|
return redirect()->route('jumbotron-images.index') |
100
|
|
|
->with('success', 'Jumbotron Image translation added succesfully'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/***************************************************************************/ |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Save the record on DB. |
107
|
|
|
* @param \Illuminate\Http\Request $request |
108
|
|
|
* @param \DavideCasiraghi\LaravelCards\Models\CardTranslation $jumbotronImageTranslation |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function saveOnDb($request, $jumbotronImageTranslation, $saveOrUpdate) |
112
|
|
|
{ |
113
|
|
|
$jumbotronImageTranslation->title = $request->get('title'); |
114
|
|
|
$jumbotronImageTranslation->body = $request->get('body'); |
115
|
|
|
$jumbotronImageTranslation->button_text = $request->get('button_text'); |
116
|
|
|
|
117
|
|
|
switch ($saveOrUpdate) { |
118
|
|
|
case 'save': |
119
|
|
|
$jumbotronImageTranslation->card_id = $request->get('card_id'); |
120
|
|
|
$jumbotronImageTranslation->locale = $request->get('language_code'); |
121
|
|
|
$jumbotronImageTranslation->save(); |
122
|
|
|
break; |
123
|
|
|
case 'update': |
124
|
|
|
$jumbotronImageTranslation->update(); |
125
|
|
|
break; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/***************************************************************************/ |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the language name from language code. |
133
|
|
|
* |
134
|
|
|
* @param string $languageCode |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getSelectedLocaleName($languageCode) |
138
|
|
|
{ |
139
|
|
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
140
|
|
|
$ret = $countriesAvailableForTranslations[$languageCode]['name']; |
141
|
|
|
|
142
|
|
|
return $ret; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/***************************************************************************/ |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Remove the specified resource from storage. |
149
|
|
|
* |
150
|
|
|
* @param int $jumbotronImageTranslationId |
151
|
|
|
*/ |
152
|
|
|
public function destroy($jumbotronImageTranslationId) |
153
|
|
|
{ |
154
|
|
|
$jumbotronImageTranslation = CardTranslation::find($jumbotronImageTranslationId); |
155
|
|
|
$jumbotronImageTranslation->delete(); |
156
|
|
|
|
157
|
|
|
return redirect()->route('jumbotron-images.index') |
158
|
|
|
->with('success', 'Jumbotron Image translation deleted succesfully'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|