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 CountryController extends Controller |
||
11 | { |
||
12 | /* Restrict the access to this resource just to logged in users */ |
||
13 | 12 | public function __construct() |
|
14 | { |
||
15 | //$this->middleware('admin'); |
||
16 | 12 | $this->middleware('admin', ['except' => ['updateCountriesDropdown']]); |
|
17 | 12 | } |
|
18 | |||
19 | /***************************************************************************/ |
||
20 | |||
21 | /** |
||
22 | * Display a listing of the resource. |
||
23 | * @param \Illuminate\Http\Request $request |
||
24 | * @return \Illuminate\View\View |
||
25 | */ |
||
26 | 2 | public function index(Request $request) |
|
27 | { |
||
28 | 2 | $continents = Continent::getContinents(); |
|
29 | |||
30 | 2 | $searchKeywords = $request->input('keywords'); |
|
31 | 2 | if ($searchKeywords) { |
|
32 | 1 | $countries = Country::orderBy('name') |
|
33 | 1 | ->where('name', 'like', '%'.$request->input('keywords').'%') |
|
34 | 1 | ->paginate(20); |
|
35 | } else { |
||
36 | 1 | $countries = Country::orderBy('name') |
|
37 | 1 | ->paginate(20); |
|
38 | } |
||
39 | |||
40 | 2 | return view('laravel-events-calendar::countries.index', compact('countries')) |
|
41 | 2 | ->with('i', (request()->input('page', 1) - 1) * 20) |
|
42 | 2 | ->with('continents', $continents) |
|
43 | 2 | ->with('searchKeywords', $searchKeywords); |
|
44 | } |
||
45 | |||
46 | /***************************************************************************/ |
||
47 | |||
48 | /** |
||
49 | * Show the form for creating a new resource. |
||
50 | * |
||
51 | * @return \Illuminate\View\View |
||
52 | */ |
||
53 | 1 | public function create() |
|
54 | { |
||
55 | 1 | $continents = Continent::getContinents(); |
|
56 | |||
57 | 1 | return view('laravel-events-calendar::countries.create') |
|
58 | 1 | ->with('continents', $continents); |
|
59 | } |
||
60 | |||
61 | /***************************************************************************/ |
||
62 | |||
63 | /** |
||
64 | * Store a newly created resource in storage. |
||
65 | * |
||
66 | * @param \Illuminate\Http\Request $request |
||
67 | * @return \Illuminate\Http\RedirectResponse |
||
68 | */ |
||
69 | 2 | public function store(Request $request) |
|
70 | { |
||
71 | |||
72 | // Validate form datas |
||
73 | 2 | $validator = Validator::make($request->all(), [ |
|
74 | 2 | 'name' => 'required', |
|
75 | 'code' => 'required', |
||
76 | 'continent_id' => 'required', |
||
77 | ]); |
||
78 | 2 | if ($validator->fails()) { |
|
79 | 1 | return back()->withErrors($validator)->withInput(); |
|
80 | } |
||
81 | |||
82 | 1 | $country = new Country(); |
|
83 | 1 | $country->name = $request->get('name'); |
|
84 | 1 | $country->code = $request->get('code'); |
|
85 | 1 | $country->continent_id = $request->get('continent_id'); |
|
86 | |||
87 | 1 | $country->save(); |
|
88 | |||
89 | 1 | return redirect()->route('countries.index') |
|
90 | 1 | ->with('success', __('laravel-events-calendar::messages.country_added_successfully')); |
|
91 | } |
||
92 | |||
93 | /***************************************************************************/ |
||
94 | |||
95 | /** |
||
96 | * Display the specified resource. |
||
97 | * |
||
98 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Country $country |
||
99 | * @return \Illuminate\View\View |
||
100 | */ |
||
101 | 1 | public function show(Country $country) |
|
102 | { |
||
103 | 1 | return view('laravel-events-calendar::countries.show', compact('country')); |
|
104 | } |
||
105 | |||
106 | /***************************************************************************/ |
||
107 | |||
108 | /** |
||
109 | * Show the form for editing the specified resource. |
||
110 | * |
||
111 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Country $country |
||
112 | * @return \Illuminate\View\View |
||
113 | */ |
||
114 | 1 | public function edit(Country $country) |
|
115 | { |
||
116 | 1 | $continents = Continent::getContinents(); |
|
117 | |||
118 | 1 | return view('laravel-events-calendar::countries.edit', compact('country'))->with('continents', $continents); |
|
119 | } |
||
120 | |||
121 | /***************************************************************************/ |
||
122 | |||
123 | /** |
||
124 | * Update the specified resource in storage. |
||
125 | * |
||
126 | * @param \Illuminate\Http\Request $request |
||
127 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Country $country |
||
128 | * @return \Illuminate\Http\RedirectResponse |
||
129 | */ |
||
130 | 2 | public function update(Request $request, Country $country) |
|
131 | { |
||
132 | 2 | request()->validate([ |
|
133 | 2 | 'name' => 'required', |
|
134 | 'code' => 'required', |
||
135 | 'continent_id' => 'required', |
||
136 | ]); |
||
137 | |||
138 | 1 | $country->update($request->all()); |
|
139 | |||
140 | 1 | return redirect()->route('countries.index') |
|
141 | 1 | ->with('success', __('laravel-events-calendar::messages.country_updated_successfully')); |
|
142 | } |
||
143 | |||
144 | /***************************************************************************/ |
||
145 | |||
146 | /** |
||
147 | * Remove the specified resource from storage. |
||
148 | * |
||
149 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Country $country |
||
150 | * @return \Illuminate\Http\RedirectResponse |
||
151 | */ |
||
152 | 1 | public function destroy(Country $country) |
|
153 | { |
||
154 | 1 | $country->delete(); |
|
155 | |||
156 | 1 | return redirect()->route('countries.index') |
|
157 | 1 | ->with('success', __('laravel-events-calendar::messages.country_deleted_successfully')); |
|
158 | } |
||
159 | |||
160 | /***************************************************************************/ |
||
161 | |||
162 | /** |
||
163 | * Return and HTML with the updated countries dropdown for the homepage |
||
164 | * after a continent get selected. |
||
165 | * |
||
166 | * @param \Illuminate\Http\Request $request |
||
167 | * @return string $ret |
||
168 | */ |
||
169 | 1 | public static function updateCountriesDropdown(Request $request) |
|
170 | { |
||
171 | 1 | $countries = Country::getActiveCountriesByContinent($request->get('continent_id')); |
|
172 | |||
173 | // GENERATE the HTML to return |
||
174 | 1 | $ret = "<select name='country_id' id='country_id' class='selectpicker' title='".__('homepage-serach.select_a_country')."'>"; |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
175 | 1 | foreach ($countries as $key => $country) { |
|
176 | 1 | $ret .= "<option value='".$country->id."'>".$country->name.'</option>'; |
|
177 | } |
||
178 | 1 | $ret .= '</select>'; |
|
179 | |||
180 | 1 | return response($ret, 200)->header('Content-Type', 'text/html'); |
|
181 | } |
||
182 | } |
||
183 |