1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Models\Continent; |
6
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Models\Country; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Validator; |
9
|
|
|
|
10
|
|
|
class ContinentController extends Controller |
11
|
|
|
{ |
12
|
|
|
/* Restrict the access to this resource just to logged in users */ |
13
|
11 |
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
//$this->middleware('admin'); |
16
|
11 |
|
$this->middleware('admin', ['except' => ['updateContinentsDropdown']]); |
17
|
11 |
|
} |
18
|
|
|
|
19
|
|
|
/***************************************************************************/ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Display a listing of the resource. |
23
|
|
|
* |
24
|
|
|
* @return \Illuminate\View\View |
25
|
|
|
*/ |
26
|
1 |
|
public function index() |
27
|
|
|
{ |
28
|
1 |
|
$continents = Continent::orderBy('name')->paginate(10); |
29
|
|
|
|
30
|
1 |
|
return view('laravel-events-calendar::continents.index', compact('continents')) |
31
|
1 |
|
->with('i', (request()->input('page', 1) - 1) * 10); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/***************************************************************************/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Show the form for creating a new resource. |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\View\View |
40
|
|
|
*/ |
41
|
1 |
|
public function create() |
42
|
|
|
{ |
43
|
1 |
|
return view('laravel-events-calendar::continents.create'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/***************************************************************************/ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Store a newly created resource in storage. |
50
|
|
|
* |
51
|
|
|
* @param \Illuminate\Http\Request $request |
52
|
|
|
* @return \Illuminate\Http\RedirectResponse |
53
|
|
|
*/ |
54
|
2 |
|
public function store(Request $request) |
55
|
|
|
{ |
56
|
|
|
|
57
|
|
|
// Validate form datas |
58
|
2 |
|
$validator = Validator::make($request->all(), [ |
59
|
2 |
|
'name' => 'required', |
60
|
|
|
'code' => 'required', |
61
|
|
|
]); |
62
|
2 |
|
if ($validator->fails()) { |
63
|
1 |
|
return back()->withErrors($validator)->withInput(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$continent = new Continent(); |
67
|
1 |
|
$continent->name = $request->get('name'); |
68
|
1 |
|
$continent->code = $request->get('code'); |
69
|
|
|
|
70
|
1 |
|
$continent->save(); |
71
|
|
|
|
72
|
1 |
|
return redirect()->route('continents.index') |
73
|
1 |
|
->with('success', __('laravel-events-calendar::messages.continent_added_successfully')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/***************************************************************************/ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Display the specified resource. |
80
|
|
|
* |
81
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Continent $continent |
82
|
|
|
* @return \Illuminate\View\View |
83
|
|
|
*/ |
84
|
1 |
|
public function show(Continent $continent) |
85
|
|
|
{ |
86
|
1 |
|
return view('laravel-events-calendar::continents.show', compact('continent')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/***************************************************************************/ |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Show the form for editing the specified resource. |
93
|
|
|
* |
94
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Continent $continent |
95
|
|
|
* @return \Illuminate\View\View |
96
|
|
|
*/ |
97
|
1 |
|
public function edit(Continent $continent) |
98
|
|
|
{ |
99
|
1 |
|
return view('laravel-events-calendar::continents.edit', compact('continent')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/***************************************************************************/ |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Update the specified resource in storage. |
106
|
|
|
* |
107
|
|
|
* @param \Illuminate\Http\Request $request |
108
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Continent $continent |
109
|
|
|
* @return \Illuminate\Http\RedirectResponse |
110
|
|
|
*/ |
111
|
2 |
|
public function update(Request $request, Continent $continent) |
112
|
|
|
{ |
113
|
2 |
|
request()->validate([ |
114
|
2 |
|
'name' => 'required', |
115
|
|
|
'code' => 'required', |
116
|
|
|
]); |
117
|
|
|
|
118
|
1 |
|
$continent->update($request->all()); |
119
|
|
|
|
120
|
1 |
|
return redirect()->route('continents.index') |
121
|
1 |
|
->with('success', __('laravel-events-calendar::messages.continent_updated_successfully')); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/***************************************************************************/ |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Remove the specified resource from storage. |
128
|
|
|
* |
129
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Continent $continent |
130
|
|
|
* @return \Illuminate\Http\RedirectResponse |
131
|
|
|
*/ |
132
|
1 |
|
public function destroy(Continent $continent) |
133
|
|
|
{ |
134
|
1 |
|
$continent->delete(); |
135
|
|
|
|
136
|
1 |
|
return redirect()->route('continents.index') |
137
|
1 |
|
->with('success', __('laravel-events-calendar::messages.continent_deleted_successfully')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/***************************************************************************/ |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Return the contient id of the select country |
144
|
|
|
* after a country get selected. |
145
|
|
|
* |
146
|
|
|
* @param \Illuminate\Http\Request $request |
147
|
|
|
* @return int $ret |
148
|
|
|
*/ |
149
|
1 |
|
public static function updateContinentsDropdown(Request $request): int |
150
|
|
|
{ |
151
|
1 |
|
$selectedCountry = Country::find($request->get('country_id')); |
152
|
1 |
|
$ret = $selectedCountry->continent_id; |
153
|
|
|
|
154
|
1 |
|
return $ret; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|