|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2016 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 | +namespace Pterodactyl\Http\Controllers\Admin; |
| 25 | + |
| 26 | +use Alert; |
| 27 | +use DB; |
| 28 | +use Log; |
| 29 | +use Validator; |
| 30 | + |
| 31 | +use Pterodactyl\Models; |
| 32 | +use Pterodactyl\Repositories\ServiceRepository; |
| 33 | +use Pterodactyl\Exceptions\DisplayException; |
| 34 | +use Pterodactyl\Exceptions\DisplayValidationException; |
| 35 | + |
| 36 | +use Pterodactyl\Http\Controllers\Controller; |
| 37 | +use Illuminate\Http\Request; |
| 38 | + |
| 39 | +class ServiceController extends Controller |
| 40 | +{ |
| 41 | + |
| 42 | + public function __construct() |
| 43 | + { |
| 44 | + // |
| 45 | + } |
| 46 | + |
| 47 | + public function getIndex(Request $request) |
| 48 | + { |
| 49 | + return view('admin.services.index', [ |
| 50 | + 'services' => Models\Service::all() |
| 51 | + ]); |
| 52 | + } |
| 53 | + |
| 54 | + public function getNew(Request $request) |
| 55 | + { |
| 56 | + // |
| 57 | + } |
| 58 | + |
| 59 | + public function postNew(Request $request) |
| 60 | + { |
| 61 | + // |
| 62 | + } |
| 63 | + |
| 64 | + public function getService(Request $request, $service) |
| 65 | + { |
| 66 | + return view('admin.services.view', [ |
| 67 | + 'service' => Models\Service::findOrFail($service), |
| 68 | + 'options' => Models\ServiceOptions::select( |
| 69 | + 'service_options.*', |
| 70 | + DB::raw('(SELECT COUNT(*) FROM servers WHERE servers.option = service_options.id) as c_servers') |
| 71 | + )->where('parent_service', $service)->get() |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + public function postService(Request $request, $service) |
| 76 | + { |
| 77 | + try { |
| 78 | + $repo = new ServiceRepository\Service; |
| 79 | + $repo->update($service, $request->except([ |
| 80 | + '_token' |
| 81 | + ])); |
| 82 | + Alert::success('Successfully updated this service.')->flash(); |
| 83 | + } catch (DisplayValidationException $ex) { |
| 84 | + return redirect()->route('admin.services.service', $service)->withErrors(json_decode($ex->getMessage()))->withInput(); |
| 85 | + } catch (DisplayException $ex) { |
| 86 | + Alert::danger($ex->getMessage())->flash(); |
| 87 | + } catch (\Exception $ex) { |
| 88 | + Log::error($ex); |
| 89 | + Alert::danger('An error occurred while attempting to update this service.')->flash(); |
| 90 | + } |
| 91 | + return redirect()->route('admin.services.service', $service)->withInput(); |
| 92 | + } |
| 93 | + |
| 94 | + public function getOption(Request $request, $option) |
| 95 | + { |
| 96 | + $opt = Models\ServiceOptions::findOrFail($option); |
| 97 | + return view('admin.services.options.view', [ |
| 98 | + 'service' => Models\Service::findOrFail($opt->parent_service), |
| 99 | + 'option' => $opt, |
| 100 | + 'variables' => Models\ServiceVariables::where('option_id', $option)->get(), |
| 101 | + 'servers' => Models\Server::select('servers.*', 'users.email as a_ownerEmail') |
| 102 | + ->join('users', 'users.id', '=', 'servers.owner') |
| 103 | + ->where('option', $option) |
| 104 | + ->paginate(10) |
| 105 | + ]); |
| 106 | + } |
| 107 | + |
| 108 | + public function postOption(Request $request, $option) |
| 109 | + { |
| 110 | + // editing option |
| 111 | + } |
| 112 | + |
| 113 | + public function postOptionVariable(Request $request, $option, $variable) |
| 114 | + { |
| 115 | + if ($variable === 'new') { |
| 116 | + // adding new variable |
| 117 | + } else { |
| 118 | + try { |
| 119 | + $repo = new ServiceRepository\Variable; |
| 120 | + |
| 121 | + // Because of the way old() works on the display side we prefix all of the variables with thier ID |
| 122 | + // We need to remove that prefix here since the repo doesn't want it. |
| 123 | + $data = []; |
| 124 | + foreach($request->except(['_token']) as $id => $val) { |
| 125 | + $data[str_replace($variable.'_', '', $id)] = $val; |
| 126 | + } |
| 127 | + $repo->update($variable, $data); |
| 128 | + Alert::success('Successfully updated variable.')->flash(); |
| 129 | + } catch (DisplayValidationException $ex) { |
| 130 | + $data = []; |
| 131 | + foreach(json_decode($ex->getMessage(), true) as $id => $val) { |
| 132 | + $data[$variable.'_'.$id] = $val; |
| 133 | + } |
| 134 | + return redirect()->route('admin.services.option', $option)->withErrors((object) $data)->withInput(); |
| 135 | + } catch (DisplayException $ex) { |
| 136 | + Alert::danger($ex->getMessage())->flash(); |
| 137 | + } catch (\Exception $ex) { |
| 138 | + Log::error($ex); |
| 139 | + Alert::danger('An error occurred while attempting to update this service.')->flash(); |
| 140 | + } |
| 141 | + return redirect()->route('admin.services.option', $option)->withInput(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments