105 lines
3.9 KiB
PHP
105 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Achievement;
|
|
use App\Models\Aircraft;
|
|
use App\Models\Alliance;
|
|
use App\Models\Continent;
|
|
use App\Models\Country;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class AchievementController extends Controller
|
|
{
|
|
public function index(User $user)
|
|
{
|
|
$achievements = Achievement::with(['category', 'difficulty'])
|
|
->get()
|
|
->groupBy(fn(Achievement $a) => $a->category->name)
|
|
->map(fn($group) => $group->sortBy('sort_order')->values());
|
|
|
|
$userAchievements = $user->achievements()
|
|
->with('achievement')
|
|
->orderBy('achievement_id')
|
|
->get()
|
|
->keyBy('achievement_id');
|
|
|
|
return Inertia::render('UserAchievements', [
|
|
'user' => $user,
|
|
'canEdit' => auth()->id() === $user->id,
|
|
'isFollowing' => auth()->check() && auth()->user()->isFollowing($user),
|
|
'achievements' => $achievements,
|
|
'userAchievements' => $userAchievements,
|
|
'loggedInUser' => auth()->user(),
|
|
]);
|
|
}
|
|
|
|
function getRegionsByCountryCode(string $countryCode)
|
|
{
|
|
return Country::whereCode($countryCode)
|
|
->first()
|
|
->regions()
|
|
->orderBy('name')
|
|
->get()
|
|
->toArray();
|
|
}
|
|
|
|
public function specific(User $user, Achievement $achievement)
|
|
{
|
|
$regions = match($achievement->internal_name){
|
|
'fun_challenges.australian_states' => $this->getRegionsByCountryCode('AU'),
|
|
'fun_challenges.chinese_provinces' => $this->getRegionsByCountryCode('CN'),
|
|
'fun_challenges.canadian_provinces' => $this->getRegionsByCountryCode('CA'),
|
|
'fun_challenges.brazilian_states' => $this->getRegionsByCountryCode('BR'),
|
|
'fun_challenges.us_states' => $this->getRegionsByCountryCode('US'),
|
|
default => [],
|
|
};
|
|
|
|
$allianceInternalName = match($achievement->internal_name){
|
|
'airlines_alliances.all_star_alliance' => 'star_alliance',
|
|
'airlines_alliances.all_oneworld' => 'oneworld',
|
|
'airlines_alliances.all_skyteam' => 'skyteam',
|
|
'airlines_alliances.all_vanilla_alliance' => 'vanilla_alliance',
|
|
default => null,
|
|
};
|
|
|
|
$continents = match($achievement->internal_name){
|
|
'countries_continents.all_continent_pairs_one_way', 'countries_continents.all_continent_pairs_both_ways' => Continent::all()->toArray(),
|
|
default => [],
|
|
};
|
|
|
|
$alliance = null;
|
|
$airlines = [];
|
|
|
|
if ($allianceInternalName) {
|
|
$alliance = Alliance::where('internal_name', $allianceInternalName)
|
|
->with('airlines')
|
|
->firstOrFail();
|
|
$airlines = $alliance->airlines()->with('country')->orderBy('name')->get();
|
|
}
|
|
|
|
$aircraftFamilies = match($achievement->internal_name){
|
|
'aircraft.all_boeing_7x7' => Aircraft::BOEING_FAMILIES,
|
|
'aircraft.all_airbus_a3xx' => Aircraft::AIRBUS_FAMILIES,
|
|
default => [],
|
|
};
|
|
|
|
return Inertia::render('Profile/UserAchievement', [
|
|
'user' => $user,
|
|
'achievement' => $achievement,
|
|
'loggedInUser' => auth()->user(),
|
|
'userAchievement' => $user->achievements()->where('achievement_id', $achievement->id)->first(),
|
|
'isFollowing' => auth()->check() && auth()->user()->isFollowing($user),
|
|
'flight_api_url' => FlightProfileController::getUserFlightApiURL($user),
|
|
'regions' => $regions,
|
|
'alliance' => $alliance,
|
|
'airlines' => $airlines,
|
|
'continents' => $continents,
|
|
'aircraft_families' => $aircraftFamilies,
|
|
]);
|
|
}
|
|
|
|
}
|