Add/edit the award class in Admin #155

This commit is contained in:
Nabeel Shahzad
2018-03-17 12:17:38 -05:00
parent a21e2dd412
commit e9baf4acb5
14 changed files with 195 additions and 86 deletions

View File

@@ -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'];

View File

@@ -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();

View File

@@ -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

View File

@@ -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'],
]);
}

View File

@@ -25,6 +25,6 @@ class UpdateAwardRequest extends FormRequest
*/
public function rules()
{
return Fare::$rules;
return Award::$rules;
}
}

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -3,9 +3,10 @@
@section('content')
<div class="card border-blue-bottom">
<div class="content">
{!! Form::open(['route' => 'admin.awards.store']) !!}
{{ Form::open(['route' => 'admin.awards.store']) }}
@include('admin.awards.fields')
{!! Form::close() !!}
{{ Form::close() }}
</div>
</div>
@endsection
@include('admin.awards.scripts')

View File

@@ -3,9 +3,10 @@
@section('content')
<div class="card border-blue-bottom">
<div class="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() }}
</div>
</div>
@endsection
@include('admin.awards.scripts')

View File

@@ -1,46 +1,78 @@
<div class="row">
<div class="col-md-12">
<div class="callout callout-success">
Awards that can be granted to pilots.
<div class="col-sm-12">
@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
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('name', 'Name:') !!}&nbsp;<span class="required">*</span>
<div class="callout callout-info">
<i class="icon fa fa-info">&nbsp;&nbsp;</i>
This will be the title of the award
</div>
<br />
{!! Form::text('name', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('name') }}</p>
</div>
<div class="form-group col-sm-6">
{!! Form::label('image', 'Image:') !!}
<div class="callout callout-info">
<i class="icon fa fa-info">&nbsp;&nbsp;</i>
This is the image of the award. Be creative!
</div>
{!! Form::text('image_url', null, [
'class' => 'form-control',
'placeholder' => 'Enter the url of the image location'
]) !!}
<p class="text-danger">{{ $errors->first('image_url') }}</p>
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('title', 'Title:') !!}&nbsp;<span class="required">*</span>
<div class="callout callout-info">
<i class="icon fa fa-info">&nbsp;&nbsp;</i>
This will be the title of the award
<div class="form-group col-sm-6">
{!! Form::label('description', 'Description:') !!}&nbsp
<div class="callout callout-info">
<i class="icon fa fa-info">&nbsp;&nbsp;</i>
This is the description of the award.
</div>
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
<p class="text-danger">{{ $errors->first('description') }}</p>
</div>
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group col-sm-6">
<div>
{{ Form::label('ref_class', 'Award Class:') }}
{{ Form::select('ref_class', $award_classes, null , [
'class' => 'form-control select2',
'id' => 'award_class_select',
]) }}
<p class="text-danger">{{ $errors->first('ref_class') }}</p>
</div>
<div>
{{ Form::label('ref_class_params', 'Award Class parameters') }}
{{ Form::text('ref_class_params', null, ['class' => 'form-control']) }}
<p class="text-danger">{{ $errors->first('ref_class_params') }}</p>
<div class="form-group col-sm-6">
{!! Form::label('image', 'Image:') !!}
<div class="callout callout-info">
<i class="icon fa fa-info">&nbsp;&nbsp;</i>
This is the image of the award. Be creative!
</div>
{!! Form::text('image', null, ['class' => 'form-control', 'placeholder' => 'Enter the url of the image location']) !!}
</div>
<p id="ref_class_param_description">
<div class="form-group col-sm-6">
{!! Form::label('description', 'Description:') !!}&nbsp
<div class="callout callout-info">
<i class="icon fa fa-info">&nbsp;&nbsp;</i>
This is the description of the award.
</div>
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
</div>
</p>
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
<div class="pull-right">
{!! Form::button('Save', ['type' => 'submit', 'class' => 'btn btn-success']) !!}
<a href="{!! route('admin.awards.index') !!}" class="btn btn-warn">Cancel</a>
</div>
</div>
<div class="row">
<!-- Submit Field -->
<div class="form-group col-sm-12">
<div class="pull-right">
{!! Form::button('Save', ['type' => 'submit', 'class' => 'btn btn-success']) !!}
<a href="{!! route('admin.awards.index') !!}" class="btn btn-warn">Cancel</a>
</div>
</div>
</div>

View File

@@ -0,0 +1,31 @@
@section('scripts')
<script>
@if(isset($award_descriptions))
const award_descriptions = {!! json_encode($award_descriptions) !!};
@else
const award_descriptions = {};
@endif
const changeParamDescription = (award_class) => {
const descrip = award_descriptions[award_class];
console.log('Found description: ', descrip);
$("p#ref_class_param_description").text(descrip);
};
$(document).ready(() => {
const select_id = "select#award_class_select";
console.log('award descriptions', award_descriptions);
$(select_id).change((e) => {
const award_class = $(select_id + " option:selected").val();
console.log('award class selected: ', award_class);
changeParamDescription(award_class);
});
// on load
const award_class = $(select_id + " option:selected").val();
changeParamDescription(award_class);
});
</script>
@endsection

View File

@@ -1,9 +0,0 @@
@extends('admin.app')
@section('content')
<div class="card border-blue-bottom">
<div class="content">
@include('admin.awards.show_fields')
</div>
</div>
@endsection

View File

@@ -1,17 +0,0 @@
<!-- Title Field -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
<p>{!! $award->title !!}</p>
</div>
<!-- Description Field -->
<div class="form-group">
{!! Form::label('Description', 'Description:') !!}
<p>{!! $award->description !!}</p>
</div>
<!-- Image Field -->
<div class="form-group">
{!! Form::label('image', 'Image:') !!}
<p><img src="{!! $award->image !!}" alt="No Image Available" /></p>
</div>

View File

@@ -1,21 +1,38 @@
<table class="table table-hover table-responsive" id="awards-table">
<thead>
<th>Title</th>
<th class="text-center">Description</th>
<th class="text-center">Image</th>
<th>Name</th>
<th>Description</th>
<th>Image</th>
<th class="text-right">Action</th>
</thead>
<tbody>
@foreach($awards as $award)
<tr>
<td>{!! $award->title !!}</td>
<td class="text-center">{!! $award->description !!}</td>
<td class="text-center"><img src="{!! $award->image !!}" name="{!! $award->title !!}" alt="No Image Available" /></td>
<td>
<a href="{{ route('admin.awards.edit', [$award->id]) }}">
{{ $award->name }}</a>
</td>
<td>{{ $award->description }}</td>
<td>
@if($award->image)
<img src="{{ $award->image }}" name="{{ $award->name }}" alt="No Image Available" />
@else
-
@endif
</td>
<td class="text-right">
{!! Form::open(['route' => ['admin.awards.destroy', $award->id], 'method' => 'delete']) !!}
<a href="{!! route('admin.awards.edit', [$award->id]) !!}" class='btn btn-sm btn-success btn-icon'><i class="fa fa-pencil-square-o"></i></a>
{!! Form::button('<i class="fa fa-times"></i>', ['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']) }}
<a href="{{ route('admin.awards.edit', [$award->id]) }}" class='btn btn-sm btn-success btn-icon'>
<i class="fas fa-pencil-alt"></i></a>
{{ Form::button('<i class="fa fa-times"></i>', [
'type' => 'submit',
'class' => 'btn btn-sm btn-danger btn-icon',
'onclick' => "return confirm('Are you sure you want to delete this award?')"
]) }}
{{ Form::close() }}
</td>
</tr>
@endforeach