2727use Log ;
2828use Alert ;
2929use Storage ;
30- use Pterodactyl \ Models ;
30+ use Javascript ;
3131use Illuminate \Http \Request ;
32+ use Pterodactyl \Models \Service ;
33+ use Pterodactyl \Models \ServiceOption ;
3234use Pterodactyl \Exceptions \DisplayException ;
3335use Pterodactyl \Http \Controllers \Controller ;
3436use Pterodactyl \Repositories \OptionRepository ;
37+ use Pterodactyl \Repositories \VariableRepository ;
3538use Pterodactyl \Exceptions \DisplayValidationException ;
3639
3740class OptionController extends Controller
3841{
42+ /**
43+ * Handles request to view page for adding new option.
44+ *
45+ * @param Request $request
46+ * @return \Illuminate\View\View
47+ */
48+ public function new (Request $ request )
49+ {
50+ $ services = Service::with ('options ' )->get ();
51+ Javascript::put (['services ' => $ services ->keyBy ('id ' )]);
52+
53+ return view ('admin.services.options.new ' , ['services ' => $ services ]);
54+ }
55+
56+ /**
57+ * Handles POST request to create a new option.
58+
59+ * @param Request $request
60+ * @return \Illuminate\Response\RedirectResponse
61+ */
62+ public function create (Request $ request )
63+ {
64+ $ repo = new OptionRepository ;
65+
66+ try {
67+ $ option = $ repo ->create ($ request ->intersect ([
68+ 'service_id ' , 'name ' , 'description ' , 'tag ' ,
69+ 'docker_image ' , 'startup ' , 'config_from ' , 'config_startup ' ,
70+ 'config_logs ' , 'config_files ' , 'config_stop '
71+ ]));
72+ Alert::success ('Successfully created new service option. ' )->flash ();
73+
74+ return redirect ()->route ('admin.services.option.view ' , $ option ->id );
75+ } catch (DisplayValidationException $ ex ) {
76+ return redirect ()->route ('admin.services.option.new ' )->withErrors (json_decode ($ ex ->getMessage ()))->withInput ();
77+ } catch (DisplayException $ ex ) {
78+ Alert::danger ($ ex ->getMessage ())->flash ();
79+ } catch (\Exception $ ex ) {
80+ Log::error ($ ex );
81+ Alert::danger ('An unhandled exception occurred while attempting to create this service. This error has been logged. ' )->flash ();
82+ }
83+
84+ return redirect ()->route ('admin.services.option.new ' )->withInput ();
85+ }
86+
3987 /**
4088 * Display option overview page.
4189 *
@@ -45,27 +93,89 @@ class OptionController extends Controller
4593 */
4694 public function viewConfiguration (Request $ request , $ id )
4795 {
48- return view ('admin.services.options.view ' , ['option ' => Models \ServiceOption::findOrFail ($ id )]);
96+ return view ('admin.services.options.view ' , ['option ' => ServiceOption::findOrFail ($ id )]);
97+ }
98+
99+ /**
100+ * Display variable overview page for a service option.
101+ *
102+ * @param Request $request
103+ * @param int $id
104+ * @return \Illuminate\View\View
105+ */
106+ public function viewVariables (Request $ request , $ id )
107+ {
108+ return view ('admin.services.options.variables ' , ['option ' => ServiceOption::with ('variables ' )->findOrFail ($ id )]);
49109 }
50110
111+ /**
112+ * Handles POST when editing a configration for a service option.
113+ *
114+ * @param Request $request
115+ * @return \Illuminate\Response\RedirectResponse
116+ */
51117 public function editConfiguration (Request $ request , $ id )
52118 {
53119 $ repo = new OptionRepository ;
54120
55121 try {
56- $ repo ->update ($ id , $ request ->intersect ([
57- 'name ' , 'description ' , 'tag ' , 'docker_image ' , 'startup ' ,
58- 'config_from ' , 'config_stop ' , 'config_logs ' , 'config_files ' , 'config_startup ' ,
59- ]));
122+ if ($ request ->input ('action ' ) !== 'delete ' ) {
123+ $ repo ->update ($ id , $ request ->intersect ([
124+ 'name ' , 'description ' , 'tag ' , 'docker_image ' , 'startup ' ,
125+ 'config_from ' , 'config_stop ' , 'config_logs ' , 'config_files ' , 'config_startup ' ,
126+ ]));
127+ Alert::success ('Service option configuration has been successfully updated. ' )->flash ();
128+ } else {
129+ $ option = ServiceOption::with ('service ' )->where ('id ' , $ id )->first ();
130+ $ repo ->delete ($ id );
131+ Alert::success ('Successfully deleted service option from the system. ' )->flash ();
60132
61- Alert::success ('Service option configuration has been successfully updated. ' )->flash ();
133+ return redirect ()->route ('admin.services.view ' , $ option ->service_id );
134+ }
62135 } catch (DisplayValidationException $ ex ) {
63136 return redirect ()->route ('admin.services.option.view ' , $ id )->withErrors (json_decode ($ ex ->getMessage ()));
137+ } catch (DisplayException $ ex ) {
138+ Alert::danger ($ ex ->getMessage ())->flash ();
64139 } catch (\Exception $ ex ) {
65140 Log::error ($ ex );
66- Alert::danger ('An unhandled exception occurred while attempting to update this service option . This error has been logged. ' )->flash ();
141+ Alert::danger ('An unhandled exception occurred while attempting to perform that action . This error has been logged. ' )->flash ();
67142 }
68143
69144 return redirect ()->route ('admin.services.option.view ' , $ id );
70145 }
146+
147+ /**
148+ * Handles POST when editing a configration for a service option.
149+ *
150+ * @param Request $request
151+ * @param int $option
152+ * @param int $variable
153+ * @return \Illuminate\Response\RedirectResponse
154+ */
155+ public function editVariable (Request $ request , $ option , $ variable )
156+ {
157+ $ repo = new VariableRepository ;
158+
159+ try {
160+ if ($ request ->input ('action ' ) !== 'delete ' ) {
161+ $ variable = $ repo ->update ($ variable , $ request ->only ([
162+ 'name ' , 'description ' , 'env_variable ' ,
163+ 'default_value ' , 'options ' , 'rules ' ,
164+ ]));
165+ Alert::success ("The service variable ' {$ variable ->name }' has been updated. " )->flash ();
166+ } else {
167+ $ repo ->delete ($ variable );
168+ Alert::success ("That service variable has been deleted. " )->flash ();
169+ }
170+ } catch (DisplayValidationException $ ex ) {
171+ return redirect ()->route ('admin.services.option.variables ' , $ option )->withErrors (json_decode ($ ex ->getMessage ()));
172+ } catch (DisplayException $ ex ) {
173+ Alert::danger ($ ex ->getMessage ())->flash ();
174+ } catch (\Exception $ ex ) {
175+ Log::error ($ ex );
176+ Alert::danger ('An unhandled exception was encountered while attempting to process that request. This error has been logged. ' )->flash ();
177+ }
178+
179+ return redirect ()->route ('admin.services.option.variables ' , $ option );
180+ }
71181}
0 commit comments