From e9baf4acb502bb43a8ac0b41d2b206e42d680ef2 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 17 Mar 2018 12:17:38 -0500 Subject: [PATCH] Add/edit the award class in Admin #155 --- app/Console/Commands/DevCommands.php | 4 +- .../2018_01_28_180522_create_awards_table.php | 2 +- app/Database/seeds/sample.yml | 10 ++ .../Controllers/Admin/AwardController.php | 42 +++++++- app/Http/Requests/UpdateAwardRequest.php | 2 +- app/Models/Award.php | 9 +- .../{AwardsService.php => AwardService.php} | 12 ++- resources/views/admin/awards/create.blade.php | 5 +- resources/views/admin/awards/edit.blade.php | 5 +- resources/views/admin/awards/fields.blade.php | 96 ++++++++++++------- .../views/admin/awards/scripts.blade.php | 31 ++++++ resources/views/admin/awards/show.blade.php | 9 -- .../views/admin/awards/show_fields.blade.php | 17 ---- resources/views/admin/awards/table.blade.php | 37 +++++-- 14 files changed, 195 insertions(+), 86 deletions(-) rename app/Services/{AwardsService.php => AwardService.php} (64%) create mode 100644 resources/views/admin/awards/scripts.blade.php delete mode 100755 resources/views/admin/awards/show.blade.php delete mode 100755 resources/views/admin/awards/show_fields.blade.php diff --git a/app/Console/Commands/DevCommands.php b/app/Console/Commands/DevCommands.php index 6539c9bb..c302d5af 100644 --- a/app/Console/Commands/DevCommands.php +++ b/app/Console/Commands/DevCommands.php @@ -7,7 +7,7 @@ use App\Models\Acars; use App\Models\Airline; use App\Models\Pirep; use App\Models\User; -use App\Services\AwardsService; +use App\Services\AwardService; use DB; use PDO; use Symfony\Component\Yaml\Yaml; @@ -51,7 +51,7 @@ class DevCommands extends BaseCommand */ protected function listAwardClasses() { - $awardSvc = app(AwardsService::class); + $awardSvc = app(AwardService::class); $awards = $awardSvc->findAllAwardClasses(); $headers = ['Award Name', 'Class']; diff --git a/app/Database/migrations/2018_01_28_180522_create_awards_table.php b/app/Database/migrations/2018_01_28_180522_create_awards_table.php index c35b2689..e8645762 100644 --- a/app/Database/migrations/2018_01_28_180522_create_awards_table.php +++ b/app/Database/migrations/2018_01_28_180522_create_awards_table.php @@ -15,7 +15,7 @@ class CreateAwardsTable extends Migration { Schema::create('awards', function (Blueprint $table) { $table->increments('id'); - $table->string('title', 50); + $table->string('name'); $table->text('description')->nullable(); $table->text('image_url')->nullable(); diff --git a/app/Database/seeds/sample.yml b/app/Database/seeds/sample.yml index 45870c04..8683cf29 100644 --- a/app/Database/seeds/sample.yml +++ b/app/Database/seeds/sample.yml @@ -93,6 +93,16 @@ ranks: auto_approve_manual: 1 auto_promote: 0 +awards: + - id: 1 + name: Pilot 50 flights + description: When a pilot has 50 flights, give this award + image_url: + ref_class: App\Awards\PilotFlightAwards + ref_class_params: 50 + created_at: now + updated_at: now + news: - id: 1 user_id: 1 diff --git a/app/Http/Controllers/Admin/AwardController.php b/app/Http/Controllers/Admin/AwardController.php index c7f6804f..bfda770a 100755 --- a/app/Http/Controllers/Admin/AwardController.php +++ b/app/Http/Controllers/Admin/AwardController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Requests\CreateAwardRequest; use App\Http\Requests\UpdateAwardRequest; use App\Repositories\AwardRepository; +use App\Services\AwardService; use Illuminate\Http\Request; use Flash; use Prettus\Repository\Criteria\RequestCriteria; @@ -13,11 +14,39 @@ use Response; class AwardController extends BaseController { /** @var AwardRepository */ - private $awardRepository; + private $awardRepository, + $awardSvc; - public function __construct(AwardRepository $awardRepo) + public function __construct( + AwardRepository $awardRepo, + AwardService $awardSvc + ) { $this->awardRepository = $awardRepo; + $this->awardSvc = $awardSvc; + } + + /** + * @return array + */ + protected function getAwardClassesAndDescriptions(): array + { + $awards = [ + '' => '', + ]; + + $descriptions = []; + + $award_classes = $this->awardSvc->findAllAwardClasses(); + foreach($award_classes as $class_ref => $award) { + $awards[$class_ref] = $award->name; + $descriptions[$class_ref] = $award->param_description; + } + + return [ + 'awards' => $awards, + 'descriptions' => $descriptions, + ]; } /** @@ -42,7 +71,11 @@ class AwardController extends BaseController */ public function create() { - return view('admin.awards.create'); + $class_refs = $this->getAwardClassesAndDescriptions(); + return view('admin.awards.create', [ + 'award_classes' => $class_refs['awards'], + 'award_descriptions' => $class_refs['descriptions'], + ]); } /** @@ -91,8 +124,11 @@ class AwardController extends BaseController return redirect(route('admin.awards.index')); } + $class_refs = $this->getAwardClassesAndDescriptions(); return view('admin.awards.edit', [ 'award' => $award, + 'award_classes' => $class_refs['awards'], + 'award_descriptions' => $class_refs['descriptions'], ]); } diff --git a/app/Http/Requests/UpdateAwardRequest.php b/app/Http/Requests/UpdateAwardRequest.php index 390076b8..7f695573 100755 --- a/app/Http/Requests/UpdateAwardRequest.php +++ b/app/Http/Requests/UpdateAwardRequest.php @@ -25,6 +25,6 @@ class UpdateAwardRequest extends FormRequest */ public function rules() { - return Fare::$rules; + return Award::$rules; } } diff --git a/app/Models/Award.php b/app/Models/Award.php index b0c0071f..b4402fc6 100755 --- a/app/Models/Award.php +++ b/app/Models/Award.php @@ -14,7 +14,7 @@ class Award extends BaseModel public $table = 'awards'; public $fillable = [ - 'title', + 'name', 'description', 'image_url', 'ref_class', @@ -22,9 +22,12 @@ class Award extends BaseModel ]; public static $rules = [ - 'title' => 'required', + 'name' => 'required', 'description' => 'nullable', 'image_url' => 'nullable', + + 'ref_class' => 'required', + 'ref_class_params' => 'nullable' ]; /** @@ -41,8 +44,6 @@ class Award extends BaseModel try { return new $this->ref_class($award, $user); - # return $klass; - # return $klass->find($this->ref_class_id); } catch (\Exception $e) { return null; } diff --git a/app/Services/AwardsService.php b/app/Services/AwardService.php similarity index 64% rename from app/Services/AwardsService.php rename to app/Services/AwardService.php index 0c877989..5694c838 100644 --- a/app/Services/AwardsService.php +++ b/app/Services/AwardService.php @@ -5,26 +5,32 @@ namespace App\Services; use App\Support\ClassLoader; use Module; -class AwardsService +class AwardService { /** * Find any of the award classes + * @return \App\Interfaces\AwardInterface[] */ public function findAllAwardClasses() { $awards = []; + $formatted_awards = []; # Find the awards in the app/Awards directory $classes = ClassLoader::getClassesInPath(app_path('/Awards')); $awards = array_merge($awards, $classes); - # Look throughout all the other modules + # Look throughout all the other modules, in the module/{MODULE}/Awards directory foreach (Module::all() as $module) { $path = $module->getExtraPath('Awards'); $classes = ClassLoader::getClassesInPath($path); $awards = array_merge($awards, $classes); } - return $awards; + foreach ($awards as $award) { + $formatted_awards[\get_class($award)] = $award; + } + + return $formatted_awards; } } diff --git a/resources/views/admin/awards/create.blade.php b/resources/views/admin/awards/create.blade.php index 6a6a8c2b..6879d717 100755 --- a/resources/views/admin/awards/create.blade.php +++ b/resources/views/admin/awards/create.blade.php @@ -3,9 +3,10 @@ @section('content')
- {!! Form::open(['route' => 'admin.awards.store']) !!} + {{ Form::open(['route' => 'admin.awards.store']) }} @include('admin.awards.fields') - {!! Form::close() !!} + {{ Form::close() }}
@endsection +@include('admin.awards.scripts') diff --git a/resources/views/admin/awards/edit.blade.php b/resources/views/admin/awards/edit.blade.php index 85058e75..733d54e6 100755 --- a/resources/views/admin/awards/edit.blade.php +++ b/resources/views/admin/awards/edit.blade.php @@ -3,9 +3,10 @@ @section('content')
- {!! Form::model($award, ['route' => ['admin.awards.update', $award->id], 'method' => 'patch']) !!} + {{ Form::model($award, ['route' => ['admin.awards.update', $award->id], 'method' => 'patch']) }} @include('admin.awards.fields') - {!! Form::close() !!} + {{ Form::close() }}
@endsection +@include('admin.awards.scripts') diff --git a/resources/views/admin/awards/fields.blade.php b/resources/views/admin/awards/fields.blade.php index 88bcfb86..67913b6e 100755 --- a/resources/views/admin/awards/fields.blade.php +++ b/resources/views/admin/awards/fields.blade.php @@ -1,46 +1,78 @@
-
-
- Awards that can be granted to pilots. +
+ @component('admin.components.info') + These are the awards that pilots can earn. Each award is assigned an + award class, which will be run whenever a pilot's stats are changed, + including after a PIREP is accepted. + @endcomponent +
+
+
+
+ {!! Form::label('name', 'Name:') !!} * +
+    + This will be the title of the award
-
+ {!! Form::text('name', null, ['class' => 'form-control']) !!} +

{{ $errors->first('name') }}

+
+ + +
+ {!! Form::label('image', 'Image:') !!} +
+    + This is the image of the award. Be creative! +
+ {!! Form::text('image_url', null, [ + 'class' => 'form-control', + 'placeholder' => 'Enter the url of the image location' + ]) !!} +

{{ $errors->first('image_url') }}

+
-
- {!! Form::label('title', 'Title:') !!} * -
-    - This will be the title of the award +
+ {!! Form::label('description', 'Description:') !!}  +
+    + This is the description of the award. +
+ {!! Form::textarea('description', null, ['class' => 'form-control']) !!} +

{{ $errors->first('description') }}

- {!! Form::text('title', null, ['class' => 'form-control']) !!} -
+
+
+ {{ Form::label('ref_class', 'Award Class:') }} + {{ Form::select('ref_class', $award_classes, null , [ + 'class' => 'form-control select2', + 'id' => 'award_class_select', + ]) }} +

{{ $errors->first('ref_class') }}

+
+
+ {{ Form::label('ref_class_params', 'Award Class parameters') }} + {{ Form::text('ref_class_params', null, ['class' => 'form-control']) }} +

{{ $errors->first('ref_class_params') }}

-
- {!! Form::label('image', 'Image:') !!} -
-    - This is the image of the award. Be creative! -
- {!! Form::text('image', null, ['class' => 'form-control', 'placeholder' => 'Enter the url of the image location']) !!} -
+

-

- {!! Form::label('description', 'Description:') !!}  -
-    - This is the description of the award. -
- {!! Form::textarea('description', null, ['class' => 'form-control']) !!} -
+

+
- -
-
- {!! Form::button('Save', ['type' => 'submit', 'class' => 'btn btn-success']) !!} - Cancel
+ +
+ +
+
+ {!! Form::button('Save', ['type' => 'submit', 'class' => 'btn btn-success']) !!} + Cancel +
+
diff --git a/resources/views/admin/awards/scripts.blade.php b/resources/views/admin/awards/scripts.blade.php new file mode 100644 index 00000000..737da463 --- /dev/null +++ b/resources/views/admin/awards/scripts.blade.php @@ -0,0 +1,31 @@ +@section('scripts') + + +@endsection diff --git a/resources/views/admin/awards/show.blade.php b/resources/views/admin/awards/show.blade.php deleted file mode 100755 index a2da501a..00000000 --- a/resources/views/admin/awards/show.blade.php +++ /dev/null @@ -1,9 +0,0 @@ -@extends('admin.app') - -@section('content') -
-
- @include('admin.awards.show_fields') -
-
-@endsection diff --git a/resources/views/admin/awards/show_fields.blade.php b/resources/views/admin/awards/show_fields.blade.php deleted file mode 100755 index bdf1eff6..00000000 --- a/resources/views/admin/awards/show_fields.blade.php +++ /dev/null @@ -1,17 +0,0 @@ - -
- {!! Form::label('title', 'Title:') !!} -

{!! $award->title !!}

-
- - -
- {!! Form::label('Description', 'Description:') !!} -

{!! $award->description !!}

-
- - -
- {!! Form::label('image', 'Image:') !!} -

No Image Available

-
\ No newline at end of file diff --git a/resources/views/admin/awards/table.blade.php b/resources/views/admin/awards/table.blade.php index 9b05a53d..3bc0bcc4 100755 --- a/resources/views/admin/awards/table.blade.php +++ b/resources/views/admin/awards/table.blade.php @@ -1,21 +1,38 @@ - - - + + + @foreach($awards as $award) - - - + + + @endforeach
TitleDescriptionImageNameDescriptionImage Action
{!! $award->title !!}{!! $award->description !!}No Image Available + + {{ $award->name }} + {{ $award->description }} + + @if($award->image) + No Image Available + @else + - + @endif + - {!! Form::open(['route' => ['admin.awards.destroy', $award->id], 'method' => 'delete']) !!} - - {!! Form::button('', ['type' => 'submit', 'class' => 'btn btn-sm btn-danger btn-icon', 'onclick' => "return confirm('Are you sure you want to delete this award?')"]) !!} - {!! Form::close() !!} + {{ Form::open(['route' => ['admin.awards.destroy', $award->id], 'method' => 'delete']) }} + + + + {{ Form::button('', [ + 'type' => 'submit', + 'class' => 'btn btn-sm btn-danger btn-icon', + 'onclick' => "return confirm('Are you sure you want to delete this award?')" + ]) }} + + {{ Form::close() }}