diff --git a/app/Contracts/Migration.php b/app/Contracts/Migration.php index 5192b8fb..c5b8ef4c 100644 --- a/app/Contracts/Migration.php +++ b/app/Contracts/Migration.php @@ -5,6 +5,8 @@ namespace App\Contracts; use App\Support\Database; use DB; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Validator; +use Illuminate\Validation\ValidationException; /** * Class Migration @@ -60,4 +62,22 @@ abstract class Migration extends \Illuminate\Database\Migrations\Migration } } } + + /** + * Add an award from the migrations (for example, if you're adding an award module) + * + * @param array $award See \App\Models\Awardv + * + * @throws \Illuminate\Validation\ValidationException + */ + public function addAward(array $award) + { + $validator = Validator::make($award, \App\Models\Award::$rules); + if ($validator->fails()) { + throw new ValidationException($validator); + } + + $awardModel = new \App\Models\Award($award); + $awardModel->save(); + } } 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 16bbd12b..7a00e26b 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 @@ -3,12 +3,15 @@ use App\Contracts\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; +use Modules\Awards\Awards\PilotFlightAwards; class CreateAwardsTable extends Migration { /** * Run the migrations. * + * @throws \Illuminate\Validation\ValidationException + * * @return void */ public function up() @@ -23,7 +26,6 @@ class CreateAwardsTable extends Migration // EG, the airports has an internal expense for gate costs $table->string('ref_model')->nullable(); $table->text('ref_model_params')->nullable(); - //$table->string('ref_model_id', 36)->nullable(); $table->timestamps(); @@ -38,6 +40,18 @@ class CreateAwardsTable extends Migration $table->index(['user_id', 'award_id']); }); + + /** + * Add a default, sample award + */ + $award = [ + 'name' => 'Pilot 50 flights', + 'description' => 'When a pilot has 50 flights, give this award', + 'ref_model' => PilotFlightAwards::class, + 'ref_model_params' => 50, + ]; + + $this->addAward($award); } /** diff --git a/app/Database/seeds/dev/sample.yml b/app/Database/seeds/dev/sample.yml index c47a6418..b8da89f4 100644 --- a/app/Database/seeds/dev/sample.yml +++ b/app/Database/seeds/dev/sample.yml @@ -1,30 +1,10 @@ -#airlines: -# - id: 1 -# icao: VMS -# iata: VM -# name: phpvms airlines -# country: us -# active: 1 -# created_at: now -# updated_at: now - roles: id_column: name data: - name: fleet-only display_name: Edit Fleet -awards: - - id: 1 - name: Pilot 50 flights - description: When a pilot has 50 flights, give this award - image_url: - ref_model: Modules\Awards\Awards\PilotFlightAwards - ref_model_params: 50 - created_at: now - updated_at: now - news: - id: 1 user_id: 1 diff --git a/app/Database/seeds/dev/users.yml b/app/Database/seeds/dev/users.yml index 106b7030..092074d3 100644 --- a/app/Database/seeds/dev/users.yml +++ b/app/Database/seeds/dev/users.yml @@ -92,3 +92,10 @@ user_field_values: user_field_id: 2 user_id: 1 value: 'Nobody did' + +user_awards: + - id: 1 + user_id: 1 + award_id: 1 + created_at: now + updated_at: now diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 1d1d19d8..4be76a7c 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -8,6 +8,7 @@ use App\Http\Requests\UpdateUserRequest; use App\Models\Rank; use App\Models\Role; use App\Models\User; +use App\Models\UserAward; use App\Repositories\AirlineRepository; use App\Repositories\AirportRepository; use App\Repositories\PirepRepository; @@ -143,7 +144,7 @@ class UserController extends Controller public function edit($id) { $user = $this->userRepo - ->with(['fields', 'rank']) + ->with(['awards', 'fields', 'rank']) ->findWithoutFail($id); if (empty($user)) { @@ -265,6 +266,28 @@ class UserController extends Controller return redirect(route('admin.users.index')); } + /** + * Remove the award from a user + * + * @param \Illuminate\Http\Request $request + * @param mixed $id + * @param mixed $award_id + * + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy_user_award($id, $award_id, Request $request) + { + $userAward = UserAward::where(['user_id' => $id, 'award_id' => $award_id]); + if (empty($userAward)) { + Flash::error('The user award could not be found'); + return redirect()->back(); + } + + $userAward->delete(); + + return redirect()->back(); + } + /** * Regenerate the user's API key * diff --git a/app/Http/Controllers/Frontend/ProfileController.php b/app/Http/Controllers/Frontend/ProfileController.php index 0d3d15b8..92cf5865 100644 --- a/app/Http/Controllers/Frontend/ProfileController.php +++ b/app/Http/Controllers/Frontend/ProfileController.php @@ -61,27 +61,14 @@ class ProfileController extends Controller } /** + * Redirect to show() since only a single page gets shown and the template controls + * the other items that are/aren't shown + * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function index() { - /** @var User $user */ - $user = Auth::user(); - - if (setting('pilots.home_hubs_only')) { - $airports = $this->airportRepo->findWhere(['hub' => true]); - } else { - $airports = $this->airportRepo->all(); - } - - $userFields = $this->userRepo->getUserFields($user); - - return view('profile.index', [ - 'acars' => $this->acarsEnabled(), - 'user' => $user, - 'airports' => $airports, - 'userFields' => $userFields, - ]); + return $this->show(Auth::user()->id); } /** @@ -91,7 +78,8 @@ class ProfileController extends Controller */ public function show($id) { - $user = User::with(['fields', 'fields.field']) + /** @var \App\Models\User $user */ + $user = User::with(['awards', 'fields', 'fields.field']) ->where('id', $id) ->first(); diff --git a/app/Models/User.php b/app/Models/User.php index fbccafae..5ca493af 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -208,9 +208,12 @@ class User extends Authenticatable return $this->belongsTo(Airline::class, 'airline_id'); } + /** + * @return \App\Models\Award[]|mixed + */ public function awards() { - return $this->hasMany(UserAward::class, 'user_id'); + return $this->belongsToMany(Award::class, 'user_awards'); } /** diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 0954b7c8..4197b62b 100755 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -356,10 +356,17 @@ class RouteServiceProvider extends ServiceProvider Route::resource('subfleets', 'SubfleetController')->middleware('ability:admin,fleet'); - Route::resource('users', 'UserController')->middleware('ability:admin,users'); + /** + * USERS + */ + Route::delete('users/{id}/award/{award_id}', 'UserController@destroy_user_award') + ->name('users.destroy_user_award')->middleware('ability:admin,users'); + Route::get('users/{id}/regen_apikey', 'UserController@regen_apikey') ->name('users.regen_apikey')->middleware('ability:admin,users'); + Route::resource('users', 'UserController')->middleware('ability:admin,users'); + // defaults Route::get('', ['uses' => 'DashboardController@index']) ->middleware('update_pending', 'ability:admin,admin-access'); diff --git a/resources/lang/en/profile.php b/resources/lang/en/profile.php index df9fca29..53c11a61 100644 --- a/resources/lang/en/profile.php +++ b/resources/lang/en/profile.php @@ -4,7 +4,8 @@ return [ 'avatarresize' => 'This avatar will be resized to :width x :height pixels', 'newapikey' => 'New API Key', - 'yourprofile' => 'Your Profile', + 'your-profile' => 'Your Profile', + 'your-awards' => 'Your Awards', 'apikey' => 'API Key', 'apikey-show' => 'Show key', 'dontshare' => 'don\'t share this!', diff --git a/resources/lang/es/profile.php b/resources/lang/es/profile.php index c14db0f9..50392da6 100644 --- a/resources/lang/es/profile.php +++ b/resources/lang/es/profile.php @@ -4,7 +4,8 @@ return [ 'avatarresize' => 'Este avatar será redimensionado a :width x :height pixeles', 'newapikey' => 'Nueva clave API', - 'yourprofile' => 'Tu perfil', + 'your-profile' => 'Tu Perfil', + 'your-awards' => 'Tus Premios', 'apikey' => 'Clave API', 'apikey-show' => 'Mostrar clave', 'dontshare' => '¡No compartas esto!', diff --git a/resources/lang/it/profile.php b/resources/lang/it/profile.php index 341c503a..077cc3e3 100644 --- a/resources/lang/it/profile.php +++ b/resources/lang/it/profile.php @@ -3,7 +3,8 @@ return [ 'avatarresize' => 'Questo avatar sarà ridimensionato a :width x :height pixels', 'newapikey' => 'Nuova Chiave API', - 'yourprofile' => 'Il Tuo Profilo', + 'your-profile' => 'Il Tuo Profilo', + 'your-awards' => 'I Tuoi Premi', 'apikey' => 'Chiave API', 'apikey-show' => 'Mostra chiave', 'dontshare' => 'non condividerla!', diff --git a/resources/lang/pt-br/profile.php b/resources/lang/pt-br/profile.php index 8ca21424..16136ab4 100755 --- a/resources/lang/pt-br/profile.php +++ b/resources/lang/pt-br/profile.php @@ -4,7 +4,8 @@ return [ 'avatarresize' => 'Este avatar será redimensionado para :width x :height pixels', 'newapikey' => 'Nova Chave API', - 'yourprofile' => 'Seu Perfil', + 'your-profile' => 'Seu Perfil', + 'your-awards' => 'Seus Prêmios', 'apikey' => 'Chave API', 'apikey-show' => 'Mostrar chave', 'dontshare' => 'Não compartilhe isso!', diff --git a/resources/views/admin/users/awards.blade.php b/resources/views/admin/users/awards.blade.php new file mode 100644 index 00000000..5c9a58d0 --- /dev/null +++ b/resources/views/admin/users/awards.blade.php @@ -0,0 +1,23 @@ +@if($user->awards->count() > 0) +
| {{ $award->name }} | +{{ $award->description }} | ++ {{ Form::open(['url' => url('/admin/users/'.$user->id.'/award/'.$award->id), + 'method' => 'delete', 'class' => 'pjax_form form-inline']) }} + {{ Form::button('', ['type' => 'submit', + 'class' => 'btn btn-danger btn-small', + 'onclick' => "return confirm('Are you sure?')", + ]) }} + {{ Form::close() }} + | +
This user has no awards
+- {{ $user->airline->name }} -
-+ {{ $user->airline->name }} +
- @if($user->home_airport) - - @endif| @lang('common.email') |