|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Pterodactyl\Http\Controllers\Admin; |
| 26 | + |
| 27 | +use Log; |
| 28 | +use Alert; |
| 29 | +use Illuminate\Http\Request; |
| 30 | +use Pterodactyl\Models\Location; |
| 31 | +use Pterodactyl\Exceptions\DisplayException; |
| 32 | +use Pterodactyl\Http\Controllers\Controller; |
| 33 | +use Pterodactyl\Repositories\LocationRepository; |
| 34 | +use Pterodactyl\Exceptions\DisplayValidationException; |
| 35 | + |
| 36 | +class LocationController extends Controller |
| 37 | +{ |
| 38 | + /** |
| 39 | + * Return the location overview page. |
| 40 | + * |
| 41 | + * @param Request $request |
| 42 | + * @return \Illuminate\View\View |
| 43 | + */ |
| 44 | + public function index(Request $request) |
| 45 | + { |
| 46 | + return view('admin.locations.index', [ |
| 47 | + 'locations' => Location::withCount('nodes', 'servers')->get(), |
| 48 | + ]); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Return the location view page. |
| 53 | + * |
| 54 | + * @param Request $request |
| 55 | + * @param int $id |
| 56 | + * @return \Illuminate\View\View |
| 57 | + */ |
| 58 | + public function view(Request $request, $id) |
| 59 | + { |
| 60 | + return view('admin.locations.view', ['location' => Location::with('nodes.servers')->findOrFail($id)]); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Handle request to create new location. |
| 65 | + * |
| 66 | + * @param Request $request |
| 67 | + * @return \Illuminate\Response\RedirectResponse |
| 68 | + */ |
| 69 | + public function create(Request $request) |
| 70 | + { |
| 71 | + $repo = new LocationRepository; |
| 72 | + |
| 73 | + try { |
| 74 | + $location = $repo->create($request->intersect(['short', 'long'])); |
| 75 | + Alert::success('Location was created successfully.')->flash(); |
| 76 | + |
| 77 | + return redirect()->route('admin.locations.view', $location->id); |
| 78 | + } catch (DisplayValidationException $ex) { |
| 79 | + return redirect()->route('admin.locations')->withErrors(json_decode($ex->getMessage())); |
| 80 | + } catch (\Exception $ex) { |
| 81 | + Log::error($ex); |
| 82 | + Alert::error('An unhandled exception occurred while processing this request. This error has been logged.')->flash(); |
| 83 | + } |
| 84 | + |
| 85 | + return redirect()->route('admin.locations'); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Handle request to update or delete location. |
| 90 | + * |
| 91 | + * @param Request $request |
| 92 | + * @param int $id |
| 93 | + * @return \Illuminate\Response\RedirectResponse |
| 94 | + */ |
| 95 | + public function update(Request $request, $id) |
| 96 | + { |
| 97 | + $repo = new LocationRepository; |
| 98 | + |
| 99 | + try { |
| 100 | + if ($request->input('action') !== 'delete') { |
| 101 | + $location = $repo->update($id, $request->intersect(['short', 'long'])); |
| 102 | + Alert::success('Location was updated successfully.')->flash(); |
| 103 | + } else { |
| 104 | + $repo->delete($id); |
| 105 | + |
| 106 | + return redirect()->route('admin.locations'); |
| 107 | + } |
| 108 | + } catch (DisplayValidationException $ex) { |
| 109 | + return redirect()->route('admin.locations.view', $id)->withErrors(json_decode($ex->getMessage())); |
| 110 | + } catch (DisplayException $ex) { |
| 111 | + Alert::danger($ex->getMessage())->flash(); |
| 112 | + } catch (\Exception $ex) { |
| 113 | + Log::error($ex); |
| 114 | + Alert::error('An unhandled exception occurred while processing this request. This error has been logged.')->flash(); |
| 115 | + } |
| 116 | + |
| 117 | + return redirect()->route('admin.locations.view', $id); |
| 118 | + } |
| 119 | +} |
0 commit comments