Skip to content

Commit

Permalink
Merge branch 'dev' into updateReadme
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio authored Jun 30, 2024
2 parents 26efe20 + cbccec0 commit c7ee27e
Show file tree
Hide file tree
Showing 24 changed files with 307 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('user_fields', function (Blueprint $table) {
$table->boolean('internal')->after('private')->default(false);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user_fields', function (Blueprint $table) {
$table->dropColumn('internal');
});
}
};
14 changes: 14 additions & 0 deletions app/Events/NewsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Events;

use App\Contracts\Event;
use App\Models\News;

class NewsUpdated extends Event
{
public function __construct(
public News $news
) {
}
}
2 changes: 1 addition & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function render($request, Throwable $exception)
*/
private function handleApiError($request, Throwable $exception)
{
Log::error('API Error', $exception->getTrace());
Log::error('API Error: '.$exception->getMessage(), $exception->getTrace());

if ($exception instanceof AbstractHttpException) {
return $exception->getResponse();
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public function news(Request $request): View
$attrs['user_id'] = Auth::user()->id;

$this->newsSvc->addNews($attrs);
} elseif ($request->isMethod('patch')) {
$attrs = $request->post();
$attrs['user_id'] = Auth::user()->id;

$this->newsSvc->updateNews($attrs);
} elseif ($request->isMethod('delete')) {
$id = $request->input('news_id');
$this->newsSvc->deleteNews($id);
Expand Down
17 changes: 16 additions & 1 deletion app/Http/Controllers/Admin/UserFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
public function index(Request $request): View
{
$this->userFieldRepo->pushCriteria(new RequestCriteria($request));
$fields = $this->userFieldRepo->all();
$fields = $this->userFieldRepo->where('internal', false)->get();

return view('admin.userfields.index', ['fields' => $fields]);
}
Expand Down Expand Up @@ -99,6 +99,11 @@ public function edit(int $id): RedirectResponse|View
return redirect(route('admin.userfields.index'));
}

if ($field->internal) {
Flash::error('You cannot edit an internal user field');
return redirect(route('admin.userfields.index'));
}

return view('admin.userfields.edit', ['field' => $field]);
}

Expand All @@ -121,6 +126,11 @@ public function update(int $id, Request $request): RedirectResponse
return redirect(route('admin.userfields.index'));
}

if ($field->internal) {
Flash::error('You cannot edit an internal user field');
return redirect(route('admin.userfields.index'));
}

$this->userFieldRepo->update($request->all(), $id);

Flash::success('Field updated successfully.');
Expand All @@ -142,6 +152,11 @@ public function destroy(int $id): RedirectResponse
return redirect(route('admin.userfields.index'));
}

if ($field->internal) {
Flash::error('You cannot delete an internal user field');
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'));
Expand Down
10 changes: 10 additions & 0 deletions app/Http/Controllers/Api/MaintenanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api;

use App\Console\Cron;
use App\Console\Kernel;
use App\Contracts\Controller;
use App\Exceptions\CronInvalid;
use Illuminate\Http\JsonResponse;
Expand All @@ -25,6 +26,15 @@ public function cron(Request $request, string $id): JsonResponse
throw new CronInvalid();
}

// Create a console kernel instance
$consoleKernel = app()->make(Kernel::class);

// Run a null artisan thing just so Laravel internals can be setup properly
$status = $consoleKernel->handle(
new \Symfony\Component\Console\Input\ArgvInput(),
new \Symfony\Component\Console\Output\NullOutput()
);

$cron = app(Cron::class);
$run = $cron->run();

Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function showRegistrationForm(Request $request): View
}

$airlines = $this->airlineRepo->selectBoxList();
$userFields = UserField::where(['show_on_registration' => true, 'active' => true])->get();
$userFields = UserField::where(['show_on_registration' => true, 'active' => true, 'internal' => false])->get();

return view('auth.register', [
'airports' => [],
Expand Down Expand Up @@ -124,6 +124,7 @@ protected function validator(array $data): Validator
$userFields = UserField::where([
'show_on_registration' => true,
'required' => true,
'internal' => false,
'active' => true,
])->get();

Expand Down Expand Up @@ -215,7 +216,7 @@ protected function create(Request $request): User

Log::info('User registered: ', $user->toArray());

$userFields = UserField::where(['show_on_registration' => true, 'active' => true])->get();
$userFields = UserField::where(['show_on_registration' => true, 'active' => true, 'internal' => false])->get();
foreach ($userFields as $field) {
$field_name = 'field_'.$field->slug;
UserFieldValue::updateOrCreate([
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Frontend/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function search(Request $request): View
->when($filter_by_user, function ($query) use ($allowed_flights) {
return $query->whereIn('id', $allowed_flights);
})
->sortable('flight_number', 'route_code', 'route_leg')
->sortable('flight_number')->orderBy('route_code')->orderBy('route_leg')
->paginate();

$saved_flights = [];
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Frontend/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function update(Request $request): RedirectResponse
'avatar' => 'nullable|mimes:jpeg,png,jpg',
];

$userFields = UserField::where(['show_on_registration' => true, 'required' => true])->get();
$userFields = UserField::where(['show_on_registration' => true, 'required' => true, 'internal' => false])->get();
foreach ($userFields as $field) {
$rules['field_'.$field->slug] = 'required';
}
Expand Down Expand Up @@ -217,7 +217,7 @@ public function update(Request $request): RedirectResponse
}

// Save all of the user fields
$userFields = UserField::all();
$userFields = UserField::where('internal', false)->get();
foreach ($userFields as $field) {
$field_name = 'field_'.$field->slug;
UserFieldValue::updateOrCreate([
Expand Down
48 changes: 33 additions & 15 deletions app/Models/Enums/FlightType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,40 @@ class FlightType extends Enum
public const TECHNICAL_TEST = 'T';
public const MILITARY = 'W';
public const TECHNICAL_STOP = 'X';
public const SHUTTLE = 'S';
public const ADDTL_SHUTTLE = 'B';
public const CARGO_IN_CABIN = 'Q';
public const ADDTL_CARGO_IN_CABIN = 'R';
public const CHARTER_CARGO_IN_CABIN = 'L';
public const GENERAL_AVIATION = 'D';
public const AIR_TAXI = 'N';
public const COMPANY_SPECIFIC = 'Y';
public const OTHER = 'Z';

protected static array $labels = [
self::SCHED_PAX => 'flights.type.pass_scheduled',
self::SCHED_CARGO => 'flights.type.cargo_scheduled',
self::CHARTER_PAX_ONLY => 'flights.type.charter_pass_only',
self::ADDITIONAL_CARGO => 'flights.type.addtl_cargo_mail',
self::VIP => 'flights.type.special_vip',
self::ADDTL_PAX => 'flights.type.pass_addtl',
self::CHARTER_CARGO_MAIL => 'flights.type.charter_cargo',
self::AMBULANCE => 'flights.type.ambulance',
self::TRAINING => 'flights.type.training_flight',
self::MAIL_SERVICE => 'flights.type.mail_service',
self::CHARTER_SPECIAL => 'flights.type.charter_special',
self::POSITIONING => 'flights.type.positioning',
self::TECHNICAL_TEST => 'flights.type.technical_test',
self::MILITARY => 'flights.type.military',
self::TECHNICAL_STOP => 'flights.type.technical_stop',
self::SCHED_PAX => 'flights.type.pass_scheduled',
self::SCHED_CARGO => 'flights.type.cargo_scheduled',
self::CHARTER_PAX_ONLY => 'flights.type.charter_pass_only',
self::ADDITIONAL_CARGO => 'flights.type.addtl_cargo_mail',
self::VIP => 'flights.type.special_vip',
self::ADDTL_PAX => 'flights.type.pass_addtl',
self::CHARTER_CARGO_MAIL => 'flights.type.charter_cargo',
self::AMBULANCE => 'flights.type.ambulance',
self::TRAINING => 'flights.type.training_flight',
self::MAIL_SERVICE => 'flights.type.mail_service',
self::CHARTER_SPECIAL => 'flights.type.charter_special',
self::POSITIONING => 'flights.type.positioning',
self::TECHNICAL_TEST => 'flights.type.technical_test',
self::MILITARY => 'flights.type.military',
self::TECHNICAL_STOP => 'flights.type.technical_stop',
self::SHUTTLE => 'flights.type.shuttle',
self::ADDTL_SHUTTLE => 'flights.type.addtl_shuttle',
self::CARGO_IN_CABIN => 'flights.type.cargo_in_cabin',
self::ADDTL_CARGO_IN_CABIN => 'flights.type.addtl_cargo_in_cabin',
self::CHARTER_CARGO_IN_CABIN => 'flights.type.charter_cargo_in_cabin',
self::GENERAL_AVIATION => 'flights.type.general_aviation',
self::AIR_TAXI => 'flights.type.air_taxi',
self::COMPANY_SPECIFIC => 'flights.type.company_specific',
self::OTHER => 'flights.type.other',
];
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function airline(): BelongsTo

public function awards(): BelongsToMany
{
return $this->belongsToMany(Award::class, 'user_awards')->withTrashed();
return $this->belongsToMany(Award::class, 'user_awards')->withTimestamps()->withTrashed();
}

public function bids(): HasMany
Expand Down
2 changes: 2 additions & 0 deletions app/Models/UserField.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ class UserField extends Model
'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
'internal', // Whether this field is for internal use only (e.g. modules)
'active',
];

protected $casts = [
'show_on_registration' => 'boolean',
'required' => 'boolean',
'private' => 'boolean',
'internal' => 'boolean',
'active' => 'boolean',
];

Expand Down
4 changes: 4 additions & 0 deletions app/Notifications/Messages/Broadcast/PirepStatusChanged.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ public function createFields(Pirep $pirep): array
'Flight Time' => Time::minutesToTimeString($pirep->flight_time),
];

if ($pirep->landing_rate) {
$fields['Landing Rate'] = $pirep->landing_rate.'ft/min';
}

// Show the distance, but include the planned distance if it's been set
$fields['Distance'] = [];
if ($pirep->distance) {
Expand Down
20 changes: 20 additions & 0 deletions app/Notifications/NotificationEventsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Listener;
use App\Events\AwardAwarded;
use App\Events\NewsAdded;
use App\Events\NewsUpdated;
use App\Events\PirepAccepted;
use App\Events\PirepFiled;
use App\Events\PirepPrefiled;
Expand Down Expand Up @@ -33,6 +34,7 @@ class NotificationEventsHandler extends Listener
public static $callbacks = [
AwardAwarded::class => 'onAwardAwarded',
NewsAdded::class => 'onNewsAdded',
NewsUpdated::class => 'onNewsUpdated',
PirepPrefiled::class => 'onPirepPrefile',
PirepStatusChange::class => 'onPirepStatusChange',
PirepAccepted::class => 'onPirepAccepted',
Expand Down Expand Up @@ -268,6 +270,24 @@ public function onNewsAdded(NewsAdded $event): void
Notification::send([$event->news], new Messages\Broadcast\NewsAdded($event->news));
}

/**
* Notify all users of a news event, but only the users which have opted in
*
* @param \App\Events\NewsUpdated $event
*/
public function onNewsUpdated(NewsUpdated $event): void
{
Log::info('NotificationEvents::onNewsAdded');
if (setting('notifications.mail_news', true)) {
$this->notifyAllUsers(new Messages\NewsAdded($event->news));
}

/*
* Broadcast notifications
*/
Notification::send([$event->news], new Messages\Broadcast\NewsAdded($event->news));
}

/**
* Notify all users that user has awarded a new award
*
Expand Down
1 change: 1 addition & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ private function mapAdminRoutes()

Route::match([
'get',
'patch',
'post',
'delete',
], 'dashboard/news', ['uses' => 'DashboardController@news'])
Expand Down
17 changes: 11 additions & 6 deletions app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ public function model()
/**
* Get all of the fields which has the mapped values
*
* @param User $user
* @param bool $only_public_fields Only include the user's public fields
* @param User $user
* @param bool $only_public_fields Only include the user's public fields
* @param mixed $with_internal_fields
*
* @return \App\Models\UserField[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
*/
public function getUserFields(User $user, $only_public_fields = null): Collection
public function getUserFields(User $user, $only_public_fields = null, $with_internal_fields = false): Collection
{
$fields = UserField::when(!$with_internal_fields, function ($query) {
return $query->where('internal', false);
});

if (is_bool($only_public_fields)) {
$fields = UserField::where(['private' => !$only_public_fields])->get();
} else {
$fields = UserField::get();
$fields = $fields->where(['private' => !$only_public_fields]);
}

$fields = $fields->get();

return $fields->map(function ($field, $_) use ($user) {
foreach ($user->fields as $userFieldValue) {
if ($userFieldValue->field->slug === $field->slug) {
Expand Down
9 changes: 8 additions & 1 deletion app/Services/CronService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ public function getCronPath(): string
$php_exec .= ' -d register_argc_argv=On';
}

$command = base_path('bin/cron');

// If the server has proc_open then use the default laravel scheduler
if (function_exists('proc_open')) {
$command = base_path('artisan schedule:run');
}

$path = [
$php_exec,
base_path('bin/cron'),
$command,
];

return implode(' ', $path);
Expand Down
6 changes: 6 additions & 0 deletions app/Services/LegacyImporter/PirepImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public function run($start = 0)
$count++;
}

if ($pirep->user && $pirep->state === PirepState::ACCEPTED) {
$pirep->user->update([
'last_pirep_id' => $pirep->id,
]);
}

if (!$pirep->airline || !$pirep->airline->journal) {
continue;
}
Expand Down
Loading

0 comments on commit c7ee27e

Please sign in to comment.