|
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 CardTranslationController |
|
11
|
|
|
{ |
|
12
|
|
|
/***************************************************************************/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Show the form for creating a new resource. |
|
16
|
|
|
* @param int $cardId |
|
17
|
|
|
* @param string $languageCode |
|
18
|
|
|
* @return \Illuminate\Http\Response |
|
19
|
|
|
*/ |
|
20
|
1 |
|
public function create($cardId, $languageCode) |
|
21
|
|
|
{ |
|
22
|
1 |
|
$selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
|
23
|
|
|
|
|
24
|
1 |
|
return view('laravel-cards::cardsTranslations.create') |
|
25
|
1 |
|
->with('cardId', $cardId) |
|
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 $cardTranslationId |
|
36
|
|
|
* @param string $languageCode |
|
37
|
|
|
* @return \Illuminate\Http\Response |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function edit($cardId, $languageCode) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$cardTranslation = CardTranslation::where('card_id', $cardId) |
|
42
|
1 |
|
->where('locale', $languageCode) |
|
43
|
1 |
|
->first(); |
|
44
|
|
|
|
|
45
|
1 |
|
$selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
|
46
|
|
|
|
|
47
|
1 |
|
return view('laravel-cards::cardsTranslations.edit', compact('cardTranslation')) |
|
48
|
1 |
|
->with('cardId', $cardId) |
|
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 |
|
$cardTranslation = new CardTranslation(); |
|
73
|
|
|
|
|
74
|
1 |
|
$this->saveOnDb($request, $cardTranslation, 'save'); |
|
75
|
|
|
|
|
76
|
1 |
|
return redirect()->route('laravel-cards.index') |
|
77
|
1 |
|
->with('success', 'Card 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 $cardTranslationId |
|
87
|
|
|
* @return \Illuminate\Http\Response |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function update(Request $request, $cardTranslationId) |
|
90
|
|
|
{ |
|
91
|
|
|
/*request()->validate([ |
|
92
|
|
|
'text' => 'required', |
|
93
|
|
|
]);*/ |
|
94
|
|
|
|
|
95
|
1 |
|
$cardTranslation = CardTranslation::find($cardTranslationId); |
|
96
|
|
|
//dd($cardTranslation); |
|
97
|
1 |
|
$this->saveOnDb($request, $cardTranslation, 'update'); |
|
98
|
|
|
|
|
99
|
1 |
|
return redirect()->route('laravel-cards.index') |
|
100
|
1 |
|
->with('success', 'Card 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 $cardTranslation |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function saveOnDb($request, $cardTranslation, $saveOrUpdate) |
|
112
|
|
|
{ |
|
113
|
2 |
|
$cardTranslation->title = $request->get('title'); |
|
114
|
2 |
|
$cardTranslation->body = $request->get('body'); |
|
115
|
2 |
|
$cardTranslation->button_text = $request->get('button_text'); |
|
116
|
|
|
|
|
117
|
2 |
|
switch ($saveOrUpdate) { |
|
118
|
2 |
|
case 'save': |
|
119
|
1 |
|
$cardTranslation->card_id = $request->get('card_id'); |
|
120
|
1 |
|
$cardTranslation->locale = $request->get('language_code'); |
|
121
|
1 |
|
$cardTranslation->save(); |
|
122
|
1 |
|
break; |
|
123
|
1 |
|
case 'update': |
|
124
|
1 |
|
$cardTranslation->update(); |
|
125
|
1 |
|
break; |
|
126
|
|
|
} |
|
127
|
2 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/***************************************************************************/ |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get the language name from language code. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $languageCode |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
2 |
|
public function getSelectedLocaleName($languageCode) |
|
138
|
|
|
{ |
|
139
|
2 |
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
|
140
|
2 |
|
$ret = $countriesAvailableForTranslations[$languageCode]['name']; |
|
141
|
|
|
|
|
142
|
2 |
|
return $ret; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/***************************************************************************/ |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Remove the specified resource from storage. |
|
149
|
|
|
* |
|
150
|
|
|
* @param int $cardTranslationId |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function destroy($cardTranslationId) |
|
153
|
|
|
{ |
|
154
|
1 |
|
$cardTranslation = CardTranslation::find($cardTranslationId); |
|
155
|
1 |
|
$cardTranslation->delete(); |
|
156
|
|
|
|
|
157
|
1 |
|
return redirect()->route('laravel-cards.index') |
|
158
|
1 |
|
->with('success', 'Card translation deleted succesfully'); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|