diff --git a/app/Http/Controllers/Moons/MoonLedgerController.php b/app/Http/Controllers/Moons/MoonLedgerController.php index b2a4e063b..60513377b 100644 --- a/app/Http/Controllers/Moons/MoonLedgerController.php +++ b/app/Http/Controllers/Moons/MoonLedgerController.php @@ -16,6 +16,7 @@ use Seat\Eseye\Eseye; use Seat\Eseye\Exceptions\RequestFailedException; use App\Library\Esi\Esi; use App\Library\Structures\StructureHelper; +use App\Library\Lookups\NewLookupHelper; //App Models use App\Models\Esi\EsiToken; @@ -34,22 +35,85 @@ class MoonLedgerController extends Controller public function displaySelection() { //Declare variables $structures = array(); + $esiHelper = new Esi; + $lookup = new NewLookupHelper; + //Check for the esi scope + if(!$esiHelper->HaveEsiScope(auth()->user()->getId(), 'esi-industry.read_corporation_mining.v1')) { + return redirect('/dashboard')->with('error', 'Need to add scopes for esi-industry.read_corporation_mining.v1'); + } else { + if(!$esiHelper->HaveEsiScope(auth()->user()->getId(), 'esi-universe.read_structures.v1')) { + return redirect('/dashboard')->with('error', 'Need to add scope for esi-universe.read_structures.v1'); + } + } + //Get the refresh token if scope checks have passed + $refreshToken = $esiHelper->GetRefreshToken(auth()->user()->getId()); + //Setup the esi container + $esi = $esiHelper->SetupEsiAuthentication($refreshToken); + + //Get the character data from the lookup table if possible or esi + $character = $lookup->LookupCharacter(auth()->user()->getId(), null); + + //Try to get the mining observers for the corporation from esi + try { + $response = $esi->invoke('get', '/corporation/{corporation_id}/mining/observers/', [ + 'corporation_id' => $character->corporation_id, + ]); + } catch(RequestFailedException $e) { + //If an exception has occurred for some reason redirect back to the dashboard with an error message + return redirect('/dashboard')->with('error', 'Failed to get mining structures.'); + } + + foreach($response as $resp) { + //Try to get the structure information from esi + try { + $structureInfo = $esi->invoke('get', '/universe/structures/{structure_id}/', [ + 'structure_id' => $resp->observer_id, + ]); + } catch(RequestFailedException $e) { + //If an exception has occurred, then do nothing + } + + //Setup the temporary array structure + $tempStructure = [ + $resp->observer_id => $sturcutreInfo->name, + ]; + + //Push the data onto the permanent array + array_push($structures, $tempStructure); + } return view('moons.ledger.displayselect')->with('structures', $structures); } public function displayLedger(Request $request) { - //Validate the request - $this->validate($request, [ - 'id' => 'required', - ]); - //Declare variables $esiHelper = new Esi; + $lookup = new NewLookupHelper; - //Create the authentication container for ESI, and check for the correct scopes - $config = config('esi'); + //Check for the esi scope + if(!$esiHelper->HaveEsiScope(auth()->user()->getId(), 'esi-industry.read_corporation_mining.v1')) { + //If the scope check fails, return with a redirect and error message + return redirect('/dashboar')->with('error', 'Could not find the scope for esi-industry.read_corporation_mining.v1'); + } + + $refreshToken = $esiHelper->GetRefreshToken(auth()->user()->getId()); + $esi = $esiHelper->SetupEsiAuthentication($refreshToken); + + //Get the character data from the lookup table if possible or esi + $character = $lookup->LookupCharacter(auth()->user()->getId(), null); + + //Try to get the mining ledger for the corporation observer + try { + $ledger = $esi->invoke('get', '/corporation/{corporation_id}/mining/observers/{observer_id}/', [ + 'corporation_id' => $character->corporation_id, + 'observer_id' => $request->structure, + ]); + } catch(RequestFailedException $e) { + return redirect('/dashboard')->with('error', 'Failed to get the mining ledger.'); + } + + } } diff --git a/app/Library/Lookups/NewLookupHelper.php b/app/Library/Lookups/NewLookupHelper.php index 9f311fa27..77f138163 100644 --- a/app/Library/Lookups/NewLookupHelper.php +++ b/app/Library/Lookups/NewLookupHelper.php @@ -32,6 +32,78 @@ class NewLookupHelper { $this->esi = new Eseye(); } + public function ItemIdToName($itemId) { + //Check if the item is stored in our own database first + $item = $this->LookupItem($itemId); + + if($item != null) { + return $item->name; + } else { + try { + $response = $this->esi->('get', '/universe/types/{type_id}/', [ + 'type_id' => $itemId, + ]); + } catch(RequestFailedException $e) { + Log::warning('Failed to get item information from /universe/types/{type_id}/ in NewLookupHelper.'); + return null; + } + + if(isset($response->description)) { + $this->StoreItem($response); + + return $response->name; + } else { + return null; + } + } + } + + private function LookupItem($itemId) { + $item = Item::where([ + 'type_id' => $itemId, + ])->first(); + + return $item; + } + + private function StoreItem($item) { + $newItem = new Item; + if(isset($item->capacity)) { + $newItem->capacity = $item->capacity; + } + $newItem->description = $item->description; + if(isset($item->graphic_id)) { + $newItem->graphic_id = $item->graphic_id; + } + $newItem->group_id = $item->group_id; + if(isset($item->icon_id)) { + $newItem->icon_id = $item->icon_id; + } + if(isset($item->market_group_id) { + $newItem->market_group_id = $item->market_group_id; + } + if(isset($item->mass)) { + $newItem->mass = $item->mass; + } + $newItem->name = $item->name; + if(isset($item->packaged_volume)) { + $newItem->packaged_volume = $item->packaged_volume; + } + if(isset($item->portion_size)) { + $newItem->portion_size = $item->portion_size; + } + $newItem->published = $item->published; + if(isset($item->radius)) { + $newItem->radius = $item->radius; + } + $newItem->type_id = $item->type_id; + if(isset($item->volume)) { + $newItem->volume = $item->volume; + } + $newItem-save(); + } + + public function SystemNameToId($system) { //Check if the solar system is stored in our own database first $solarSystem = $this->LookupSolarSystem($system); diff --git a/app/Models/Lookups/Item.php b/app/Models/Lookups/Item.php new file mode 100644 index 000000000..c45e6c2c7 --- /dev/null +++ b/app/Models/Lookups/Item.php @@ -0,0 +1,40 @@ +double('capacity', 20, 2)->nullable(); + $table->string('description'); + $table->unsignedBigInteger('graphic_id')->nullable(); + $table->unsignedBigInteger('group_id'); + $table->unsignedBigInteger('icon_id')->nullable(); + $table->unsignedBigInteger('market_group_id')->nullable(); + $table->double('mass', 20, 2)->nullable(); + $table->string('name'); + $table->double('packaged_volume', 20, 2)->nullable(); + $table->unsignedBigInteger('portion_size')->nullable(); + $table->boolean('published'); + $table->double('radius', 20, 2)->nullable(); + $table->unsignedBigInteger('type_id')->unique(); + $table->double('volume', 20, 2)->nullable(); + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('item_lookup'); + } +} diff --git a/resources/views/moons/ledger/displayselect.blade.php b/resources/views/moons/ledger/displayselect.blade.php index 7cf47edd1..af8e35156 100644 --- a/resources/views/moons/ledger/displayselect.blade.php +++ b/resources/views/moons/ledger/displayselect.blade.php @@ -1,4 +1,19 @@ @extends('layouts.b4') @section('content') - +
+
+
+

Select the Structure To View the Ledger For

+
+
+ {!! Form::open(['action' => 'Moons\MoonLedgerController@displayLedger', 'method' => 'POST']) !!} +
+ {{ Form::label('structure', 'Structure') }} + {{ Form::select('structure', $structures, ['class' => 'form-control']) }} +
+ {{ Form::submit('Submit', ['class' => 'btn btn-primary']) }} + {!! Form::close() !!} +
+
+
@endsection \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 8476c5f14..b024d978f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -122,6 +122,12 @@ Route::group(['middleware' => ['auth']], function(){ Route::get('/moons/admin/journal', 'Moons\MoonsAdminController@showJournalEntries'); Route::post('/moons/admin/display', 'Moons\MoonsAdminController@storeMoonRemoval'); + /** + * Moon Ledger Controller display pages + */ + //Route::post('/moons/ledger/display/', 'Moons\MoonLedgerController@displayLedger'); + //Route::get('/moons/ledger/display/select', 'Moons\MoonLedgerController@displaySelection'); + /** * Scopes Controller display pages */