davide-casiraghi /
laravel-events-calendar
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers; |
||||
| 4 | |||||
| 5 | use DavideCasiraghi\LaravelEventsCalendar\Models\Country; |
||||
| 6 | use DavideCasiraghi\LaravelEventsCalendar\Models\Region; |
||||
| 7 | use Illuminate\Http\Request; |
||||
| 8 | use Illuminate\Support\Facades\DB; |
||||
| 9 | use Illuminate\Support\Str; |
||||
| 10 | use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
||||
| 11 | use Validator; |
||||
| 12 | |||||
| 13 | class RegionController extends Controller |
||||
| 14 | { |
||||
| 15 | /* Restrict the access to this resource just to logged in users except show view */ |
||||
| 16 | 13 | public function __construct() |
|||
| 17 | { |
||||
| 18 | 13 | $this->middleware('admin', ['except' => ['show', 'updateRegionsDropdown']]); |
|||
| 19 | 13 | } |
|||
| 20 | |||||
| 21 | /***************************************************************************/ |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * Display a listing of the resource. |
||||
| 25 | * |
||||
| 26 | * @return \Illuminate\View\View |
||||
| 27 | */ |
||||
| 28 | 5 | public function index(Request $request) |
|||
| 29 | { |
||||
| 30 | 5 | $countries = Country::getCountries(); |
|||
| 31 | //$countries = Country::getActiveCountries(); |
||||
| 32 | |||||
| 33 | 5 | $searchKeywords = $request->input('keywords'); |
|||
| 34 | 5 | $searchCountry = $request->input('country_id'); |
|||
| 35 | |||||
| 36 | 5 | if ($searchKeywords || $searchCountry) { |
|||
| 37 | //$regions = DB::table('regions') |
||||
| 38 | $regions = Region:: |
||||
| 39 | select('region_translations.region_id AS id', 'name', 'timezone', 'locale', 'country_id') |
||||
| 40 | ->join('region_translations', 'regions.id', '=', 'region_translations.region_id') |
||||
| 41 | ->where('locale', 'en') |
||||
| 42 | ->when($searchKeywords, function ($query, $searchKeywords) { |
||||
| 43 | return $query->where('name', $searchKeywords)->orWhere('name', 'like', '%'.$searchKeywords.'%'); |
||||
| 44 | }) |
||||
| 45 | ->when($searchCountry, function ($query, $searchCountry) { |
||||
| 46 | return $query->where('country_id', '=', $searchCountry); |
||||
| 47 | }) |
||||
| 48 | ->orderBy('name') |
||||
| 49 | ->paginate(20); |
||||
| 50 | } else { |
||||
| 51 | $regions = Region:: |
||||
| 52 | 5 | select('region_translations.region_id AS id', 'name', 'timezone', 'locale', 'country_id') |
|||
| 53 | 5 | ->join('region_translations', 'regions.id', '=', 'region_translations.region_id') |
|||
| 54 | 5 | ->where('locale', 'en') |
|||
| 55 | 5 | ->orderBy('name') |
|||
| 56 | 5 | ->paginate(20); |
|||
| 57 | } |
||||
| 58 | |||||
| 59 | // Countries available for translations |
||||
| 60 | 5 | $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales(); |
|||
| 61 | |||||
| 62 | 5 | return view('laravel-events-calendar::regions.index', compact('regions')) |
|||
| 63 | 5 | ->with('i', (request()->input('page', 1) - 1) * 20) |
|||
| 64 | 5 | ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations) |
|||
| 65 | 5 | ->with('searchKeywords', $searchKeywords) |
|||
| 66 | 5 | ->with('searchCountry', $searchCountry) |
|||
| 67 | 5 | ->with('countries', $countries); |
|||
| 68 | } |
||||
| 69 | |||||
| 70 | /***************************************************************************/ |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * Show the form for creating a new resource. |
||||
| 74 | * |
||||
| 75 | * @return \Illuminate\View\View |
||||
| 76 | */ |
||||
| 77 | 1 | public function create() |
|||
| 78 | { |
||||
| 79 | 1 | $countries = Country::getCountries(); |
|||
| 80 | |||||
| 81 | 1 | return view('laravel-events-calendar::regions.create') |
|||
| 82 | 1 | ->with('countries', $countries); |
|||
| 83 | } |
||||
| 84 | |||||
| 85 | /***************************************************************************/ |
||||
| 86 | |||||
| 87 | /** |
||||
| 88 | * Store a newly created resource in storage. |
||||
| 89 | * |
||||
| 90 | * @param \Illuminate\Http\Request $request |
||||
| 91 | * @return \Illuminate\Http\RedirectResponse |
||||
| 92 | */ |
||||
| 93 | 2 | public function store(Request $request) |
|||
| 94 | { |
||||
| 95 | // Validate form datas |
||||
| 96 | 2 | $validator = Validator::make($request->all(), [ |
|||
| 97 | 2 | 'name' => 'required', |
|||
| 98 | 'country_id' => 'required', |
||||
| 99 | 'timezone' => 'required', |
||||
| 100 | ]); |
||||
| 101 | 2 | if ($validator->fails()) { |
|||
| 102 | 1 | return back()->withErrors($validator)->withInput(); |
|||
| 103 | } |
||||
| 104 | |||||
| 105 | 1 | $region = new Region(); |
|||
| 106 | |||||
| 107 | 1 | $this->saveOnDb($request, $region); |
|||
| 108 | |||||
| 109 | 1 | return redirect()->route('regions.index') |
|||
| 110 | 1 | ->with('success', __('laravel-events-calendar::messages.region_added_successfully')); |
|||
| 111 | } |
||||
| 112 | |||||
| 113 | /***************************************************************************/ |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * Display the specified resource. |
||||
| 117 | * |
||||
| 118 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
||||
| 119 | * @return \Illuminate\View\View |
||||
| 120 | */ |
||||
| 121 | 1 | public function show(Region $region) |
|||
| 122 | { |
||||
| 123 | 1 | return view('laravel-events-calendar::regions.show', compact('region')); |
|||
| 124 | } |
||||
| 125 | |||||
| 126 | /***************************************************************************/ |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * Show the form for editing the specified resource. |
||||
| 130 | * |
||||
| 131 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
||||
| 132 | * @return \Illuminate\View\View |
||||
| 133 | */ |
||||
| 134 | 1 | public function edit(Region $region) |
|||
| 135 | { |
||||
| 136 | 1 | $countries = Country::getCountries(); |
|||
| 137 | |||||
| 138 | 1 | return view('laravel-events-calendar::regions.edit', compact('region')) |
|||
| 139 | 1 | ->with('countries', $countries); |
|||
| 140 | } |
||||
| 141 | |||||
| 142 | /***************************************************************************/ |
||||
| 143 | |||||
| 144 | /** |
||||
| 145 | * Update the specified resource in storage. |
||||
| 146 | * |
||||
| 147 | * @param \Illuminate\Http\Request $request |
||||
| 148 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
||||
| 149 | * @return \Illuminate\Http\RedirectResponse |
||||
| 150 | */ |
||||
| 151 | 2 | public function update(Request $request, Region $region) |
|||
| 152 | { |
||||
| 153 | 2 | request()->validate([ |
|||
| 154 | 2 | 'name' => 'required', |
|||
| 155 | 'country_id' => 'required', |
||||
| 156 | 'timezone' => 'required', |
||||
| 157 | ]); |
||||
| 158 | |||||
| 159 | 1 | $this->saveOnDb($request, $region); |
|||
| 160 | |||||
| 161 | 1 | return redirect()->route('regions.index') |
|||
| 162 | 1 | ->with('success', __('laravel-events-calendar::messages.region_updated_successfully')); |
|||
| 163 | } |
||||
| 164 | |||||
| 165 | /***************************************************************************/ |
||||
| 166 | |||||
| 167 | /** |
||||
| 168 | * Remove the specified resource from storage. |
||||
| 169 | * |
||||
| 170 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
||||
| 171 | * @return \Illuminate\Http\RedirectResponse |
||||
| 172 | */ |
||||
| 173 | 1 | public function destroy(Region $region) |
|||
| 174 | { |
||||
| 175 | 1 | $region->delete(); |
|||
| 176 | |||||
| 177 | 1 | return redirect()->route('regions.index') |
|||
| 178 | 1 | ->with('success', __('laravel-events-calendar::messages.region_deleted_successfully')); |
|||
| 179 | } |
||||
| 180 | |||||
| 181 | /***************************************************************************/ |
||||
| 182 | |||||
| 183 | /** |
||||
| 184 | * Return the single event region datas by cat id. |
||||
| 185 | * |
||||
| 186 | * @param int $cat_id |
||||
| 187 | * @return \DavideCasiraghi\LaravelEventsCalendar\Models\Region |
||||
| 188 | */ |
||||
| 189 | /*public function eventregiondata($cat_id) |
||||
| 190 | { |
||||
| 191 | $ret = DB::table('regions')->where('id', $cat_id)->first(); |
||||
| 192 | //dump($ret); |
||||
| 193 | |||||
| 194 | return $ret; |
||||
| 195 | }*/ |
||||
| 196 | |||||
| 197 | /***************************************************************************/ |
||||
| 198 | |||||
| 199 | /** |
||||
| 200 | * Save/Update the record on DB. |
||||
| 201 | * |
||||
| 202 | * @param \Illuminate\Http\Request $request |
||||
| 203 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Region $region |
||||
| 204 | * @return void |
||||
| 205 | */ |
||||
| 206 | 2 | public function saveOnDb(Request $request, Region $region): void |
|||
| 207 | { |
||||
| 208 | 2 | $region->name = $request->get('name'); |
|||
|
0 ignored issues
–
show
|
|||||
| 209 | 2 | $region->country_id = $request->get('country_id'); |
|||
| 210 | 2 | $region->timezone = $request->get('timezone'); |
|||
| 211 | 2 | $region->slug = Str::slug($region->name, '-'); |
|||
|
0 ignored issues
–
show
|
|||||
| 212 | |||||
| 213 | 2 | $region->save(); |
|||
| 214 | 2 | } |
|||
| 215 | |||||
| 216 | /***************************************************************************/ |
||||
| 217 | |||||
| 218 | /** |
||||
| 219 | * Return and HTML with the updated regions dropdown for the homepage |
||||
| 220 | * after a country get selected. |
||||
| 221 | * |
||||
| 222 | * @param \Illuminate\Http\Request $request |
||||
| 223 | * @return string $ret |
||||
| 224 | */ |
||||
| 225 | 1 | public static function updateRegionsDropdown(Request $request) |
|||
| 226 | { |
||||
| 227 | /*$regions = Region::join('region_translations', 'regions.id', '=', 'region_translations.region_id') |
||||
| 228 | ->where('locale', 'en') |
||||
| 229 | ->where('country_id', $request->input('country_id')) |
||||
| 230 | ->orderBy('name') |
||||
| 231 | ->pluck('name','region_translations.region_id AS id'); */ |
||||
| 232 | |||||
| 233 | $regions = Region:: |
||||
| 234 | 1 | select('name', 'region_translations.region_id AS id') |
|||
| 235 | 1 | ->join('region_translations', 'regions.id', '=', 'region_translations.region_id') |
|||
| 236 | 1 | ->where('locale', 'en') |
|||
| 237 | 1 | ->where('country_id', $request->input('country_id')) |
|||
| 238 | 1 | ->orderBy('name') |
|||
| 239 | 1 | ->get(); |
|||
| 240 | |||||
| 241 | // GENERATE the HTML to return |
||||
| 242 | 1 | $ret = "<select name='region_id' id='region_id' class='selectpicker' title='".__('homepage-serach.select_a_region')."'>"; |
|||
|
0 ignored issues
–
show
Are you sure
__('homepage-serach.select_a_region') of type array|string can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 243 | 1 | foreach ($regions as $key => $region) { |
|||
| 244 | 1 | $ret .= "<option value='".$region->id."'>".$region->name.'</option>'; |
|||
| 245 | } |
||||
| 246 | 1 | $ret .= '</select>'; |
|||
| 247 | |||||
| 248 | 1 | return response($ret, 200)->header('Content-Type', 'text/html'); |
|||
| 249 | } |
||||
| 250 | |||||
| 251 | /***************************************************************************/ |
||||
| 252 | } |
||||
| 253 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.