2016-01-11 00:22:21 +00:00
< ? php
2016-01-20 00:10:39 +00:00
/**
2016-01-20 21:05:16 +00:00
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright ( c ) 2015 - 2017 Dane Everitt < dane @ daneeveritt . com >.
2016-01-20 00:10:39 +00:00
*
2016-01-20 20:56:40 +00:00
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ), to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
2016-01-20 00:10:39 +00:00
*
2016-01-20 20:56:40 +00:00
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software .
2016-01-20 00:10:39 +00:00
*
2016-01-20 20:56:40 +00:00
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE .
2016-01-20 00:10:39 +00:00
*/
2016-12-07 22:46:38 +00:00
2016-01-11 00:22:21 +00:00
namespace Pterodactyl\Http\Controllers\Admin ;
2016-01-17 04:10:46 +00:00
use Alert ;
2016-01-11 00:22:21 +00:00
use Pterodactyl\Models ;
2016-12-07 22:46:38 +00:00
use Illuminate\Http\Request ;
use Pterodactyl\Exceptions\DisplayException ;
2016-01-11 00:22:21 +00:00
use Pterodactyl\Http\Controllers\Controller ;
2016-12-07 22:46:38 +00:00
use Pterodactyl\Repositories\LocationRepository ;
2016-01-17 03:57:28 +00:00
use Pterodactyl\Exceptions\DisplayValidationException ;
2016-01-11 00:22:21 +00:00
class LocationsController extends Controller
{
public function __construct ()
{
//
}
public function getIndex ( Request $request )
{
return view ( 'admin.locations.index' , [
2017-02-10 22:09:56 +00:00
'locations' => Models\Location :: withCount ( 'nodes' , 'servers' ) -> paginate ( 20 ),
2016-01-11 00:22:21 +00:00
]);
}
2016-01-17 03:29:35 +00:00
public function deleteLocation ( Request $request , $id )
2016-01-11 00:22:21 +00:00
{
2017-02-10 22:09:56 +00:00
$location = Models\Location :: withCount ( 'nodes' ) -> findOrFail ( $id );
2016-01-17 03:29:35 +00:00
2017-02-10 22:09:56 +00:00
if ( $location -> nodes_count > 0 ) {
2016-01-17 03:29:35 +00:00
return response () -> json ([
2017-02-10 22:09:56 +00:00
'error' => 'You cannot remove a location that is currently assigned to a node.' ,
2016-01-17 03:29:35 +00:00
], 422 );
}
2017-02-10 22:09:56 +00:00
$location -> delete ();
2016-12-07 22:46:38 +00:00
2016-01-17 03:29:35 +00:00
return response ( '' , 204 );
2016-01-11 00:22:21 +00:00
}
2016-01-17 03:57:28 +00:00
public function patchLocation ( Request $request , $id )
{
try {
$location = new LocationRepository ;
2017-02-03 21:50:28 +00:00
$location -> edit ( $id , $request -> only ([ 'long' , 'short' ]));
2016-12-07 22:46:38 +00:00
2016-01-17 03:57:28 +00:00
return response ( '' , 204 );
} catch ( DisplayValidationException $ex ) {
return response () -> json ([
2017-02-03 21:50:28 +00:00
'error' => 'There was a validation error while processing this request. Location descriptions must be between 1 and 255 characters, and the location code must be between 1 and 20 characters with no spaces or special characters.' ,
2016-01-17 03:57:28 +00:00
], 422 );
} catch ( \Exception $ex ) {
// This gets caught and processed into JSON anyways.
throw $ex ;
}
}
public function postLocation ( Request $request )
{
2016-01-17 04:10:46 +00:00
try {
$location = new LocationRepository ;
2017-02-09 22:43:54 +00:00
$location -> create ( $request -> only ([ 'long' , 'short' ]));
2016-01-17 04:10:46 +00:00
Alert :: success ( 'New location successfully added.' ) -> flash ();
2016-12-07 22:46:38 +00:00
2016-01-17 04:10:46 +00:00
return redirect () -> route ( 'admin.locations' );
} catch ( DisplayValidationException $ex ) {
return redirect () -> route ( 'admin.locations' ) -> withErrors ( json_decode ( $ex -> getMessage ())) -> withInput ();
} catch ( DisplayException $ex ) {
Alert :: danger ( $ex -> getMessage ()) -> flash ();
} catch ( \Exception $ex ) {
Log :: error ( $ex );
Alert :: danger ( 'An unhandled exception occured while attempting to add this location. Please try again.' ) -> flash ();
}
2016-12-07 22:46:38 +00:00
2016-01-17 04:10:46 +00:00
return redirect () -> route ( 'admin.locations' ) -> withInput ();
2016-01-17 03:57:28 +00:00
}
2016-01-11 00:22:21 +00:00
}