Custom user fields during registration and profile edit #711
This commit is contained in:
@@ -16,7 +16,7 @@ abstract class Listener
|
||||
public function subscribe(Dispatcher $events): void
|
||||
{
|
||||
foreach (static::$callbacks as $klass => $cb) {
|
||||
$events->listen($klass, get_class($this).'@'.$cb);
|
||||
$events->listen($klass, static::class.'@'.$cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ use Hashids\Hashids;
|
||||
$factory->define(App\Models\Airline::class, function (Faker $faker) {
|
||||
return [
|
||||
'id' => null,
|
||||
'icao' => function (array $apt) use ($faker) {
|
||||
'icao' => function (array $apt) {
|
||||
$hashids = new Hashids(microtime(), 5);
|
||||
$mt = str_replace('.', '', microtime(true));
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
use App\Contracts\Model;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateUserFields extends Migration
|
||||
{
|
||||
/**
|
||||
* Add two tables for holding user fields and the values
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
/*
|
||||
* Hold a master list of fields
|
||||
*/
|
||||
Schema::create('user_fields', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name', 200);
|
||||
$table->text('description')->nullable();
|
||||
$table->boolean('show_on_registration')->default(false)->nullable();
|
||||
$table->boolean('required')->default(false)->nullable();
|
||||
$table->boolean('private')->default(false)->nullable();
|
||||
$table->boolean('active')->default(true)->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
/*
|
||||
* The values for the actual fields
|
||||
*/
|
||||
Schema::create('user_field_values', function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->unsignedBigInteger('user_field_id');
|
||||
$table->string('user_id', Model::ID_MAX_LENGTH);
|
||||
$table->text('value')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['user_field_id', 'user_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -69,3 +69,26 @@ role_user:
|
||||
- user_id: 3
|
||||
role_id: 2
|
||||
user_type: App\Models\User
|
||||
|
||||
user_fields:
|
||||
- id: 1
|
||||
name: 'VATSIM ID'
|
||||
show_on_registration: true
|
||||
required: false
|
||||
private: false
|
||||
- id: 2
|
||||
name: 'Referral'
|
||||
description: 'Who referred you'
|
||||
show_on_registration: true
|
||||
required: false
|
||||
private: true
|
||||
|
||||
user_field_values:
|
||||
- id: 1
|
||||
user_field_id: 1
|
||||
user_id: 1
|
||||
value: 'my vatsim id'
|
||||
- id: 2
|
||||
user_field_id: 2
|
||||
user_id: 1
|
||||
value: 'Nobody did'
|
||||
|
||||
@@ -158,11 +158,12 @@ class UserController extends Controller
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$user = $this->userRepo->findWithoutFail($id);
|
||||
$user = $this->userRepo
|
||||
->with(['fields', 'rank'])
|
||||
->findWithoutFail($id);
|
||||
|
||||
if (empty($user)) {
|
||||
Flash::error('User not found');
|
||||
|
||||
return redirect(route('admin.users.index'));
|
||||
}
|
||||
|
||||
|
||||
154
app/Http/Controllers/Admin/UserFieldController.php
Normal file
154
app/Http/Controllers/Admin/UserFieldController.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Contracts\Controller;
|
||||
use App\Repositories\UserFieldRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Laracasts\Flash\Flash;
|
||||
use Prettus\Repository\Criteria\RequestCriteria;
|
||||
|
||||
class UserFieldController extends Controller
|
||||
{
|
||||
/** @var \App\Repositories\UserFieldRepository */
|
||||
private $userFieldRepo;
|
||||
|
||||
/**
|
||||
* @param UserFieldRepository $userFieldRepo
|
||||
*/
|
||||
public function __construct(UserFieldRepository $userFieldRepo)
|
||||
{
|
||||
$this->userFieldRepo = $userFieldRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the UserField.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->userFieldRepo->pushCriteria(new RequestCriteria($request));
|
||||
$fields = $this->userFieldRepo->all();
|
||||
|
||||
return view('admin.userfields.index', ['fields' => $fields]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new UserField.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('admin.userfields.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created UserField in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$this->userFieldRepo->create($request->all());
|
||||
|
||||
Flash::success('Field added successfully.');
|
||||
return redirect(route('admin.userfields.index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified UserField.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$field = $this->userFieldRepo->findWithoutFail($id);
|
||||
|
||||
if (empty($field)) {
|
||||
Flash::error('Flight field not found');
|
||||
return redirect(route('admin.userfields.index'));
|
||||
}
|
||||
|
||||
return view('admin.userfields.show', ['field' => $field]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified UserField.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$field = $this->userFieldRepo->findWithoutFail($id);
|
||||
|
||||
if (empty($field)) {
|
||||
Flash::error('Field not found');
|
||||
return redirect(route('admin.userfields.index'));
|
||||
}
|
||||
|
||||
return view('admin.userfields.edit', ['field' => $field]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified UserField in storage.
|
||||
*
|
||||
* @param $id
|
||||
* @param Request $request
|
||||
*
|
||||
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($id, Request $request)
|
||||
{
|
||||
$field = $this->userFieldRepo->findWithoutFail($id);
|
||||
|
||||
if (empty($field)) {
|
||||
Flash::error('UserField not found');
|
||||
return redirect(route('admin.userfields.index'));
|
||||
}
|
||||
|
||||
$this->userFieldRepo->update($request->all(), $id);
|
||||
|
||||
Flash::success('Field updated successfully.');
|
||||
return redirect(route('admin.userfields.index'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified UserField from storage.
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$field = $this->userFieldRepo->findWithoutFail($id);
|
||||
if (empty($field)) {
|
||||
Flash::error('Field not found');
|
||||
return redirect(route('admin.userfields.index'));
|
||||
}
|
||||
|
||||
if ($this->userFieldRepo->isInUse($id)) {
|
||||
Flash::error('This field cannot be deleted, it is in use. Deactivate it instead');
|
||||
return redirect(route('admin.userfields.index'));
|
||||
}
|
||||
|
||||
$this->userFieldRepo->delete($id);
|
||||
|
||||
Flash::success('Field deleted successfully.');
|
||||
return redirect(route('admin.userfields.index'));
|
||||
}
|
||||
}
|
||||
@@ -3,19 +3,20 @@
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Contracts\Controller;
|
||||
use App\Http\Requests\CreateUserRequest;
|
||||
use App\Models\Enums\UserState;
|
||||
use App\Models\User;
|
||||
use App\Models\UserField;
|
||||
use App\Models\UserFieldValue;
|
||||
use App\Repositories\AirlineRepository;
|
||||
use App\Repositories\AirportRepository;
|
||||
use App\Services\UserService;
|
||||
use App\Support\Countries;
|
||||
use App\Support\Timezonelist;
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Auth\RegistersUsers;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
@@ -61,12 +62,14 @@ class RegisterController extends Controller
|
||||
{
|
||||
$airports = $this->airportRepo->selectBoxList(false, setting('pilots.home_hubs_only'));
|
||||
$airlines = $this->airlineRepo->selectBoxList();
|
||||
$userFields = UserField::where(['show_on_registration' => true])->get();
|
||||
|
||||
return view('auth.register', [
|
||||
'airports' => $airports,
|
||||
'airlines' => $airlines,
|
||||
'countries' => Countries::getSelectList(),
|
||||
'timezones' => Timezonelist::toArray(),
|
||||
'airports' => $airports,
|
||||
'airlines' => $airlines,
|
||||
'countries' => Countries::getSelectList(),
|
||||
'timezones' => Timezonelist::toArray(),
|
||||
'userFields' => $userFields,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -88,6 +91,12 @@ class RegisterController extends Controller
|
||||
'toc_accepted' => 'accepted',
|
||||
];
|
||||
|
||||
// Dynamically add the required fields
|
||||
$userFields = UserField::where(['show_on_registration' => true, 'required' => true])->get();
|
||||
foreach ($userFields as $field) {
|
||||
$rules['field_'.$field->slug] = 'required';
|
||||
}
|
||||
|
||||
if (config('captcha.enabled')) {
|
||||
$rules['g-recaptcha-response'] = 'required|captcha';
|
||||
}
|
||||
@@ -119,6 +128,15 @@ class RegisterController extends Controller
|
||||
|
||||
Log::info('User registered: ', $user->toArray());
|
||||
|
||||
$userFields = UserField::all();
|
||||
foreach ($userFields as $field) {
|
||||
$field_name = 'field_'.$field->slug;
|
||||
UserFieldValue::updateOrCreate([
|
||||
'user_field_id' => $field->id,
|
||||
'user_id' => $user->id,
|
||||
], ['value' => $opts[$field_name]]);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
@@ -131,8 +149,10 @@ class RegisterController extends Controller
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function register(CreateUserRequest $request)
|
||||
public function register(Request $request)
|
||||
{
|
||||
$this->validator($request->all())->validate();
|
||||
|
||||
$user = $this->create($request->all());
|
||||
if ($user->state === UserState::PENDING) {
|
||||
return view('auth.pending');
|
||||
|
||||
@@ -4,6 +4,8 @@ namespace App\Http\Controllers\Frontend;
|
||||
|
||||
use App\Contracts\Controller;
|
||||
use App\Models\User;
|
||||
use App\Models\UserField;
|
||||
use App\Models\UserFieldValue;
|
||||
use App\Repositories\AirlineRepository;
|
||||
use App\Repositories\AirportRepository;
|
||||
use App\Repositories\UserRepository;
|
||||
@@ -63,16 +65,22 @@ class ProfileController extends Controller
|
||||
*/
|
||||
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' => Auth::user(),
|
||||
'airports' => $airports,
|
||||
'acars' => $this->acarsEnabled(),
|
||||
'user' => $user,
|
||||
'airports' => $airports,
|
||||
'userFields' => $userFields,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -83,10 +91,12 @@ class ProfileController extends Controller
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$user = User::where('id', $id)->first();
|
||||
$user = User::with(['fields', 'fields.field'])
|
||||
->where('id', $id)
|
||||
->first();
|
||||
|
||||
if (empty($user)) {
|
||||
Flash::error('User not found!');
|
||||
|
||||
return redirect(route('frontend.dashboard.index'));
|
||||
}
|
||||
|
||||
@@ -109,22 +119,27 @@ class ProfileController extends Controller
|
||||
*/
|
||||
public function edit(Request $request)
|
||||
{
|
||||
$user = User::where('id', Auth::user()->id)->first();
|
||||
/** @var \App\Models\User $user */
|
||||
$user = User::with(['fields', 'fields.field'])
|
||||
->where('id', Auth::user()->id)
|
||||
->first();
|
||||
|
||||
if (empty($user)) {
|
||||
Flash::error('User not found!');
|
||||
|
||||
return redirect(route('frontend.dashboard.index'));
|
||||
}
|
||||
|
||||
$airlines = $this->airlineRepo->selectBoxList();
|
||||
$airports = $this->airportRepo->selectBoxList(false, setting('pilots.home_hubs_only'));
|
||||
$userFields = $this->userRepo->getUserFields($user);
|
||||
|
||||
return view('profile.edit', [
|
||||
'user' => $user,
|
||||
'airlines' => $airlines,
|
||||
'airports' => $airports,
|
||||
'countries' => Countries::getSelectList(),
|
||||
'timezones' => Timezonelist::toArray(),
|
||||
'user' => $user,
|
||||
'airlines' => $airlines,
|
||||
'airports' => $airports,
|
||||
'countries' => Countries::getSelectList(),
|
||||
'timezones' => Timezonelist::toArray(),
|
||||
'userFields' => $userFields,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -140,13 +155,20 @@ class ProfileController extends Controller
|
||||
$id = Auth::user()->id;
|
||||
$user = $this->userRepo->findWithoutFail($id);
|
||||
|
||||
$validator = Validator::make($request->toArray(), [
|
||||
$rules = [
|
||||
'name' => 'required',
|
||||
'email' => 'required|unique:users,email,'.$id,
|
||||
'airline_id' => 'required',
|
||||
'password' => 'confirmed',
|
||||
'avatar' => 'nullable|mimes:jpeg,png,jpg',
|
||||
]);
|
||||
];
|
||||
|
||||
$userFields = UserField::where(['show_on_registration' => true, 'required' => true])->get();
|
||||
foreach ($userFields as $field) {
|
||||
$rules['field_'.$field->slug] = 'required';
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->toArray(), $rules);
|
||||
|
||||
if ($validator->fails()) {
|
||||
Log::info('validator failed for user '.$user->ident);
|
||||
@@ -167,6 +189,7 @@ class ProfileController extends Controller
|
||||
if (isset($req_data['avatar']) !== null) {
|
||||
Storage::delete($user->avatar);
|
||||
}
|
||||
|
||||
if ($request->hasFile('avatar')) {
|
||||
$avatar = $request->file('avatar');
|
||||
$file_name = $user->ident.'.'.$avatar->getClientOriginalExtension();
|
||||
@@ -190,6 +213,16 @@ class ProfileController extends Controller
|
||||
|
||||
$this->userRepo->update($req_data, $id);
|
||||
|
||||
// Save all of the user fields
|
||||
$userFields = UserField::all();
|
||||
foreach ($userFields as $field) {
|
||||
$field_name = 'field_'.$field->slug;
|
||||
UserFieldValue::updateOrCreate([
|
||||
'user_field_id' => $field->id,
|
||||
'user_id' => $id,
|
||||
], ['value' => $request->get($field_name)]);
|
||||
}
|
||||
|
||||
Flash::success('Profile updated successfully!');
|
||||
|
||||
return redirect(route('frontend.profile.index'));
|
||||
|
||||
@@ -9,14 +9,14 @@ use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property string $disk
|
||||
* @property string $path
|
||||
* @property bool $public
|
||||
* @property int $download_count
|
||||
* @property string $url
|
||||
* @property string $filename
|
||||
* @property string $name
|
||||
* @property string $description
|
||||
* @property string $disk
|
||||
* @property string $path
|
||||
* @property bool $public
|
||||
* @property int $download_count
|
||||
* @property string $url
|
||||
* @property string $filename
|
||||
*/
|
||||
class File extends Model
|
||||
{
|
||||
|
||||
@@ -14,11 +14,11 @@ use Carbon\Carbon;
|
||||
* Holds various journals, depending on the morphed_type and morphed_id columns
|
||||
*
|
||||
* @property mixed id
|
||||
* @property Money $balance
|
||||
* @property string $currency
|
||||
* @property Carbon $updated_at
|
||||
* @property Carbon $post_date
|
||||
* @property Carbon $created_at
|
||||
* @property Money $balance
|
||||
* @property string $currency
|
||||
* @property Carbon $updated_at
|
||||
* @property Carbon $post_date
|
||||
* @property Carbon $created_at
|
||||
* @property \App\Models\Enums\JournalType type
|
||||
* @property mixed morphed_type
|
||||
* @property mixed morphed_id
|
||||
|
||||
@@ -13,11 +13,11 @@ use Carbon\Carbon;
|
||||
/**
|
||||
* Class Ledger
|
||||
*
|
||||
* @property Money $balance
|
||||
* @property string $currency
|
||||
* @property Carbon $updated_at
|
||||
* @property Carbon $post_date
|
||||
* @property Carbon $created_at
|
||||
* @property Money $balance
|
||||
* @property string $currency
|
||||
* @property Carbon $updated_at
|
||||
* @property Carbon $post_date
|
||||
* @property Carbon $created_at
|
||||
*/
|
||||
class Ledger extends Model
|
||||
{
|
||||
|
||||
@@ -6,10 +6,10 @@ use App\Contracts\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* @property string $id The Simbrief OFP ID
|
||||
* @property int $user_id The user that generated this
|
||||
* @property string $flight_id Optional, if attached to a flight, removed if attached to PIREP
|
||||
* @property string $pirep_id Optional, if attached to a PIREP, removed if attached to flight
|
||||
* @property string $id The Simbrief OFP ID
|
||||
* @property int $user_id The user that generated this
|
||||
* @property string $flight_id Optional, if attached to a flight, removed if attached to PIREP
|
||||
* @property string $pirep_id Optional, if attached to a PIREP, removed if attached to flight
|
||||
* @property string $acars_xml
|
||||
* @property string $ofp_xml
|
||||
* @property string $ofp_html
|
||||
|
||||
@@ -10,32 +10,33 @@ use Illuminate\Notifications\Notifiable;
|
||||
use Laratrust\Traits\LaratrustUserTrait;
|
||||
|
||||
/**
|
||||
* @property int id
|
||||
* @property int pilot_id
|
||||
* @property int airline_id
|
||||
* @property string name
|
||||
* @property string name_private Only first name, rest are initials
|
||||
* @property string email
|
||||
* @property string password
|
||||
* @property string api_key
|
||||
* @property mixed timezone
|
||||
* @property string ident
|
||||
* @property string curr_airport_id
|
||||
* @property string home_airport_id
|
||||
* @property string avatar
|
||||
* @property Airline airline
|
||||
* @property Flight[] flights
|
||||
* @property int flight_time
|
||||
* @property int transfer_time
|
||||
* @property string remember_token
|
||||
* @property \Carbon\Carbon created_at
|
||||
* @property \Carbon\Carbon updated_at
|
||||
* @property Rank rank
|
||||
* @property Journal journal
|
||||
* @property int rank_id
|
||||
* @property int state
|
||||
* @property bool opt_in
|
||||
* @property string last_pirep_id
|
||||
* @property int id
|
||||
* @property int pilot_id
|
||||
* @property int airline_id
|
||||
* @property string name
|
||||
* @property string name_private Only first name, rest are initials
|
||||
* @property string email
|
||||
* @property string password
|
||||
* @property string api_key
|
||||
* @property mixed timezone
|
||||
* @property string ident
|
||||
* @property string curr_airport_id
|
||||
* @property string home_airport_id
|
||||
* @property string avatar
|
||||
* @property Airline airline
|
||||
* @property Flight[] flights
|
||||
* @property int flight_time
|
||||
* @property int transfer_time
|
||||
* @property string remember_token
|
||||
* @property \Carbon\Carbon created_at
|
||||
* @property \Carbon\Carbon updated_at
|
||||
* @property Rank rank
|
||||
* @property Journal journal
|
||||
* @property int rank_id
|
||||
* @property int state
|
||||
* @property bool opt_in
|
||||
* @property string last_pirep_id
|
||||
* @property UserFieldValue[] fields
|
||||
*
|
||||
* @mixin \Illuminate\Database\Eloquent\Builder
|
||||
* @mixin \Illuminate\Notifications\Notifiable
|
||||
@@ -212,6 +213,16 @@ class User extends Authenticatable
|
||||
return $this->hasMany(UserAward::class, 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* The bid rows
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function bids()
|
||||
{
|
||||
return $this->hasMany(Bid::class, 'user_id');
|
||||
}
|
||||
|
||||
public function home_airport()
|
||||
{
|
||||
return $this->belongsTo(Airport::class, 'home_airport_id');
|
||||
@@ -227,22 +238,9 @@ class User extends Authenticatable
|
||||
return $this->belongsTo(Pirep::class, 'last_pirep_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* These are the flights they've bid on
|
||||
*/
|
||||
// public function flights()
|
||||
// {
|
||||
// return $this->belongsToMany(Flight::class, 'bids');
|
||||
// }
|
||||
|
||||
/**
|
||||
* The bid rows
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function bids()
|
||||
public function fields()
|
||||
{
|
||||
return $this->hasMany(Bid::class, 'user_id');
|
||||
return $this->hasMany(UserFieldValue::class, 'user_id');
|
||||
}
|
||||
|
||||
public function pireps()
|
||||
|
||||
49
app/Models/UserField.php
Normal file
49
app/Models/UserField.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Contracts\Model;
|
||||
|
||||
/**
|
||||
* @property string name
|
||||
* @property string slug
|
||||
* @property string value Only set if "squashed"
|
||||
* @property bool show_on_registration
|
||||
* @property bool required
|
||||
* @property bool private
|
||||
*/
|
||||
class UserField extends Model
|
||||
{
|
||||
public $table = 'user_fields';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'show_on_registration', // Show on the registration form?
|
||||
'required', // Required to be filled out in registration?
|
||||
'private', // Whether this is shown on the user's public profile
|
||||
'active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'show_on_registration' => 'boolean',
|
||||
'required' => 'boolean',
|
||||
'private' => 'boolean',
|
||||
'active' => 'boolean',
|
||||
];
|
||||
|
||||
public static $rules = [
|
||||
'name' => 'required',
|
||||
'description' => 'nullable',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the slug so we can use it in forms
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSlugAttribute(): string
|
||||
{
|
||||
return str_slug($this->name, '_');
|
||||
}
|
||||
}
|
||||
37
app/Models/UserFieldValue.php
Normal file
37
app/Models/UserFieldValue.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Contracts\Model;
|
||||
|
||||
/**
|
||||
* @property string name
|
||||
* @property string value
|
||||
* @property UserField field
|
||||
* @property User user
|
||||
*/
|
||||
class UserFieldValue extends Model
|
||||
{
|
||||
public $table = 'user_field_values';
|
||||
|
||||
protected $fillable = [
|
||||
'user_field_id',
|
||||
'user_id',
|
||||
'value',
|
||||
];
|
||||
|
||||
public static $rules = [];
|
||||
|
||||
/**
|
||||
* Foreign Keys
|
||||
*/
|
||||
public function field()
|
||||
{
|
||||
return $this->belongsTo(UserField::class, 'user_field_id');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ class BaseNotification extends Notification implements ShouldQueue
|
||||
{
|
||||
// Look in the notifications.channels config and see where this particular
|
||||
// notification can go. Map it to $channels
|
||||
$klass = get_class($this);
|
||||
$klass = static::class;
|
||||
$notif_config = config('notifications.channels', []);
|
||||
if (!array_key_exists($klass, $notif_config)) {
|
||||
Log::error('Notification type '.$klass.' missing from notifications config, defaulting to mail');
|
||||
|
||||
@@ -262,10 +262,10 @@ class RouteServiceProvider extends ServiceProvider
|
||||
Route::resource('flightfields', 'FlightFieldController')
|
||||
->middleware('ability:admin,flights');
|
||||
|
||||
// pirep related routes
|
||||
Route::get('pireps/fares', 'PirepController@fares')
|
||||
->middleware('ability:admin,pireps');
|
||||
Route::resource('userfields', 'UserFieldController')->middleware('ability:admin,users');
|
||||
|
||||
// pirep related routes
|
||||
Route::get('pireps/fares', 'PirepController@fares')->middleware('ability:admin,pireps');
|
||||
Route::get('pireps/pending', 'PirepController@pending')
|
||||
->middleware('ability:admin,pireps');
|
||||
|
||||
|
||||
32
app/Repositories/UserFieldRepository.php
Normal file
32
app/Repositories/UserFieldRepository.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Contracts\Repository;
|
||||
use App\Models\UserField;
|
||||
use App\Models\UserFieldValue;
|
||||
|
||||
class UserFieldRepository extends Repository
|
||||
{
|
||||
protected $fieldSearchable = [
|
||||
'name' => 'like',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function model(): string
|
||||
{
|
||||
return UserField::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether or not this field is in use by a value
|
||||
*
|
||||
* @param $id
|
||||
*/
|
||||
public function isInUse($id): bool
|
||||
{
|
||||
return UserFieldValue::where(['user_field_id' => $id])->exists();
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,11 @@ namespace App\Repositories;
|
||||
use App\Contracts\Repository;
|
||||
use App\Models\Enums\UserState;
|
||||
use App\Models\User;
|
||||
use App\Models\UserField;
|
||||
use App\Repositories\Criteria\WhereCriteria;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class UserRepository
|
||||
*/
|
||||
class UserRepository extends Repository
|
||||
{
|
||||
protected $fieldSearchable = [
|
||||
@@ -29,6 +28,26 @@ class UserRepository extends Repository
|
||||
return User::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the fields which has the mapped values
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
*
|
||||
* @return \App\Models\UserField[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
|
||||
*/
|
||||
public function getUserFields(User $user): Collection
|
||||
{
|
||||
return (UserField::all())->map(function ($field, $_) use ($user) {
|
||||
foreach ($user->fields as $userFieldValue) {
|
||||
if ($userFieldValue->field->slug === $field->slug) {
|
||||
$field->value = $userFieldValue->value;
|
||||
}
|
||||
}
|
||||
|
||||
return $field;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Number of PIREPs that are pending
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user