1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Models\Region; |
10
|
|
|
use DavideCasiraghi\LaravelEventsCalendar\Models\Country; |
11
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
12
|
|
|
|
13
|
|
|
class RegionController extends Controller |
14
|
|
|
{ |
15
|
|
|
/* Restrict the access to this resource just to logged in users except show view */ |
16
|
9 |
|
public function __construct() |
17
|
|
|
{ |
18
|
9 |
|
$this->middleware('admin', ['except' => ['show']]); |
19
|
9 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Display a listing of the resource. |
23
|
|
|
* |
24
|
|
|
* @return \Illuminate\Http\Response |
25
|
|
|
*/ |
26
|
3 |
|
public function index(Request $request) |
27
|
|
|
{ |
28
|
3 |
|
$countries = Country::getCountries(); |
29
|
|
|
|
30
|
3 |
|
$searchKeywords = $request->input('keywords'); |
31
|
3 |
|
if ($searchKeywords) { |
32
|
|
|
$regions = Region:: |
33
|
|
|
select('region_translations.region_id AS id', 'name', 'timezone', 'locale', 'country_id') |
34
|
|
|
->join('region_translations', 'regions.id', '=', 'region_translations.region_id') |
35
|
|
|
->where('name', 'like', '%'.$searchKeywords.'%') |
36
|
|
|
->where('locale', 'en') |
37
|
|
|
->orderBy('name') |
38
|
|
|
->paginate(20); |
39
|
|
|
} else { |
40
|
|
|
$regions = Region:: |
41
|
3 |
|
select('region_translations.region_id AS id', 'name', 'timezone', 'locale', 'country_id') |
42
|
3 |
|
->join('region_translations', 'regions.id', '=', 'region_translations.region_id') |
43
|
3 |
|
->where('locale', 'en') |
44
|
3 |
|
->orderBy('name') |
45
|
3 |
|
->paginate(20); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Countries available for translations |
49
|
3 |
|
$countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
50
|
|
|
|
51
|
3 |
|
return view('laravel-events-calendar::regions.index', compact('regions')) |
52
|
3 |
|
->with('i', (request()->input('page', 1) - 1) * 20) |
53
|
3 |
|
->with('countriesAvailableForTranslations', $countriesAvailableForTranslations) |
54
|
3 |
|
->with('searchKeywords', $searchKeywords) |
55
|
3 |
|
->with('countries', $countries); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Show the form for creating a new resource. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Http\Response |
62
|
|
|
*/ |
63
|
1 |
|
public function create() |
64
|
|
|
{ |
65
|
1 |
|
$countries = Country::getCountries(); |
66
|
|
|
|
67
|
1 |
|
return view('laravel-events-calendar::regions.create') |
68
|
1 |
|
->with('countries', $countries); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Store a newly created resource in storage. |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Http\Request $request |
75
|
|
|
* @return \Illuminate\Http\Response |
76
|
|
|
*/ |
77
|
2 |
|
public function store(Request $request) |
78
|
|
|
{ |
79
|
|
|
// Validate form datas |
80
|
2 |
|
$validator = Validator::make($request->all(), [ |
81
|
2 |
|
'name' => 'required', |
82
|
|
|
'country_id' => 'required', |
83
|
|
|
'timezone' => 'required', |
84
|
|
|
]); |
85
|
2 |
|
if ($validator->fails()) { |
86
|
1 |
|
return back()->withErrors($validator)->withInput(); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$region = new Region(); |
90
|
|
|
|
91
|
1 |
|
$this->saveOnDb($request, $region); |
92
|
|
|
|
93
|
1 |
|
return redirect()->route('regions.index') |
94
|
1 |
|
->with('success', __('laravel-events-calendar::messages.region_added_successfully')); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Display the specified resource. |
99
|
|
|
* |
100
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
101
|
|
|
* @return \Illuminate\Http\Response |
102
|
|
|
*/ |
103
|
1 |
|
public function show(Region $region) |
104
|
|
|
{ |
105
|
1 |
|
return view('laravel-events-calendar::regions.show', compact('region')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Show the form for editing the specified resource. |
110
|
|
|
* |
111
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
112
|
|
|
* @return \Illuminate\Http\Response |
113
|
|
|
*/ |
114
|
1 |
|
public function edit(Region $region) |
115
|
|
|
{ |
116
|
1 |
|
$countries = Country::getCountries(); |
117
|
|
|
|
118
|
1 |
|
return view('laravel-events-calendar::regions.edit', compact('region')) |
119
|
1 |
|
->with('countries', $countries); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Update the specified resource in storage. |
124
|
|
|
* |
125
|
|
|
* @param \Illuminate\Http\Request $request |
126
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
127
|
|
|
* @return \Illuminate\Http\Response |
128
|
|
|
*/ |
129
|
2 |
|
public function update(Request $request, Region $region) |
130
|
|
|
{ |
131
|
2 |
|
request()->validate([ |
132
|
2 |
|
'name' => 'required', |
133
|
|
|
'country_id' => 'required', |
134
|
|
|
'timezone' => 'required', |
135
|
|
|
]); |
136
|
|
|
|
137
|
1 |
|
$this->saveOnDb($request, $region); |
138
|
|
|
|
139
|
1 |
|
return redirect()->route('regions.index') |
140
|
1 |
|
->with('success', __('laravel-events-calendar::messages.region_updated_successfully')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Remove the specified resource from storage. |
145
|
|
|
* |
146
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
147
|
|
|
* @return \Illuminate\Http\Response |
148
|
|
|
*/ |
149
|
1 |
|
public function destroy(Region $region) |
150
|
|
|
{ |
151
|
1 |
|
$region->delete(); |
152
|
|
|
|
153
|
1 |
|
return redirect()->route('regions.index') |
154
|
1 |
|
->with('success', __('laravel-events-calendar::messages.region_deleted_successfully')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// ********************************************************************** |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return the single event region datas by cat id. |
161
|
|
|
* |
162
|
|
|
* @param int $cat_id |
163
|
|
|
* @return \DavideCasiraghi\LaravelEventsCalendar\Models\Region |
164
|
|
|
*/ |
165
|
|
|
/*public function eventregiondata($cat_id) |
166
|
|
|
{ |
167
|
|
|
$ret = DB::table('regions')->where('id', $cat_id)->first(); |
168
|
|
|
//dump($ret); |
169
|
|
|
|
170
|
|
|
return $ret; |
171
|
|
|
}*/ |
172
|
|
|
|
173
|
|
|
// ********************************************************************** |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Save/Update the record on DB. |
177
|
|
|
* |
178
|
|
|
* @param \Illuminate\Http\Request $request |
179
|
|
|
* @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
2 |
|
public function saveOnDb($request, $region) |
183
|
|
|
{ |
184
|
2 |
|
$region->name = $request->get('name'); |
|
|
|
|
185
|
2 |
|
$region->country_id = $request->get('country_id'); |
186
|
2 |
|
$region->timezone = $request->get('timezone'); |
187
|
2 |
|
$region->slug = Str::slug($region->name, '-'); |
|
|
|
|
188
|
|
|
|
189
|
2 |
|
$region->save(); |
190
|
2 |
|
} |
191
|
|
|
} |
192
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.