1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelCards\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use DavideCasiraghi\LaravelCards\Models\CardTranslation; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Validator; |
8
|
|
|
|
9
|
|
|
class CardTranslationController extends Controller |
10
|
|
|
{ |
11
|
|
|
/***************************************************************************/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Show the form for creating a new resource. |
15
|
|
|
* @param int $cardId |
16
|
|
|
* @param string $languageCode |
17
|
|
|
* @return \Illuminate\Http\Response |
18
|
|
|
*/ |
19
|
1 |
|
public function create($cardId, $languageCode) |
20
|
|
|
{ |
21
|
1 |
|
$selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
22
|
|
|
|
23
|
1 |
|
return view('laravel-cards::cardsTranslations.create') |
24
|
1 |
|
->with('cardId', $cardId) |
25
|
1 |
|
->with('languageCode', $languageCode) |
26
|
1 |
|
->with('selectedLocaleName', $selectedLocaleName); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/***************************************************************************/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Show the form for editing the specified resource. |
33
|
|
|
* |
34
|
|
|
* @param int $cardTranslationId |
35
|
|
|
* @param string $languageCode |
36
|
|
|
* @return \Illuminate\Http\Response |
37
|
|
|
*/ |
38
|
1 |
|
public function edit($cardId, $languageCode) |
39
|
|
|
{ |
40
|
1 |
|
$cardTranslation = CardTranslation::where('card_id', $cardId) |
41
|
1 |
|
->where('locale', $languageCode) |
42
|
1 |
|
->first(); |
43
|
|
|
|
44
|
1 |
|
$selectedLocaleName = $this->getSelectedLocaleName($languageCode); |
45
|
|
|
|
46
|
1 |
|
return view('laravel-cards::cardsTranslations.edit', compact('cardTranslation')) |
47
|
1 |
|
->with('cardId', $cardId) |
48
|
1 |
|
->with('languageCode', $languageCode) |
49
|
1 |
|
->with('selectedLocaleName', $selectedLocaleName); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/***************************************************************************/ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Store a newly created resource in storage. |
56
|
|
|
* |
57
|
|
|
* @param \Illuminate\Http\Request $request |
58
|
|
|
* @return \Illuminate\Http\Response |
59
|
|
|
*/ |
60
|
1 |
|
public function store(Request $request) |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
// Validate form datas |
64
|
|
|
/*$validator = Validator::make($request->all(), [ |
65
|
|
|
'text' => 'required', |
66
|
|
|
]); |
67
|
|
|
if ($validator->fails()) { |
68
|
|
|
return back()->withErrors($validator)->withInput(); |
69
|
|
|
}*/ |
70
|
|
|
|
71
|
1 |
|
$cardTranslation = new CardTranslation(); |
72
|
|
|
|
73
|
1 |
|
$this->saveOnDb($request, $cardTranslation, 'save'); |
74
|
|
|
|
75
|
1 |
|
return redirect()->route('laravel-cards.index') |
76
|
1 |
|
->with('success', 'Card translation added succesfully'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/***************************************************************************/ |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Update the specified resource in storage. |
83
|
|
|
* |
84
|
|
|
* @param \Illuminate\Http\Request $request |
85
|
|
|
* @param int $cardTranslationId |
86
|
|
|
* @return \Illuminate\Http\Response |
87
|
|
|
*/ |
88
|
1 |
|
public function update(Request $request, $cardTranslationId) |
89
|
|
|
{ |
90
|
|
|
/*request()->validate([ |
91
|
|
|
'text' => 'required', |
92
|
|
|
]);*/ |
93
|
|
|
|
94
|
1 |
|
$cardTranslation = CardTranslation::find($cardTranslationId); |
95
|
|
|
//dd($cardTranslation); |
96
|
1 |
|
$this->saveOnDb($request, $cardTranslation, 'update'); |
97
|
|
|
|
98
|
1 |
|
return redirect()->route('laravel-cards.index') |
99
|
1 |
|
->with('success', 'Card translation added succesfully'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/***************************************************************************/ |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Save the record on DB. |
106
|
|
|
* @param \Illuminate\Http\Request $request |
107
|
|
|
* @param \DavideCasiraghi\LaravelCards\Models\CardTranslation $cardTranslation |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
2 |
|
public function saveOnDb($request, $cardTranslation, $saveOrUpdate) |
111
|
|
|
{ |
112
|
2 |
|
$cardTranslation->title = $request->get('title'); |
113
|
2 |
|
$cardTranslation->body = $request->get('body'); |
114
|
2 |
|
$cardTranslation->button_text = $request->get('button_text'); |
115
|
|
|
|
116
|
2 |
|
switch ($saveOrUpdate) { |
117
|
2 |
|
case 'save': |
118
|
1 |
|
$cardTranslation->card_id = $request->get('card_id'); |
119
|
1 |
|
$cardTranslation->locale = $request->get('language_code'); |
120
|
1 |
|
$cardTranslation->save(); |
121
|
1 |
|
break; |
122
|
1 |
|
case 'update': |
123
|
1 |
|
$cardTranslation->update(); |
124
|
1 |
|
break; |
125
|
|
|
} |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
/***************************************************************************/ |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Remove the specified resource from storage. |
132
|
|
|
* |
133
|
|
|
* @param int $cardTranslationId |
134
|
|
|
*/ |
135
|
1 |
|
public function destroy($cardTranslationId) |
136
|
|
|
{ |
137
|
1 |
|
$cardTranslation = CardTranslation::find($cardTranslationId); |
138
|
1 |
|
$cardTranslation->delete(); |
139
|
|
|
|
140
|
1 |
|
return redirect()->route('laravel-cards.index') |
141
|
1 |
|
->with('success', 'Card translation deleted succesfully'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|