kithkin/app/Http/Controllers/ProfileController.php

173 lines
5.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProfileUpdateRequest;
use App\Models\UserAddress;
use App\Models\Location;
use App\Services\Location\Geocoder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;
class ProfileController extends Controller
{
/**
* profile index test
*/
public function index()
{
return $this->frame('profile.partials.addresses-form');
}
/**
* display user's profile forms
*/
public function edit(Request $request): View
{
$user = $request->user();
// try primary of each kind; fall back to the first of that kind
$home = $user->addresses()->where('kind', 'home')->where('is_primary', 1)->first()
?? $user->addresses()->where('kind', 'home')->first();
$billing = $user->addresses()->where('kind', 'billing')->where('is_primary', 1)->first()
?? $user->addresses()->where('kind', 'billing')->first();
return view('profile.edit', compact('user', 'home', 'billing'));
}
/**
* Update the user's profile information.
*/
public function update(ProfileUpdateRequest $request): RedirectResponse
{
$request->user()->fill($request->validated());
if ($request->user()->isDirty('email')) {
$request->user()->email_verified_at = null;
}
$request->user()->save();
return Redirect::route('profile.edit')->with('status', 'profile-updated');
}
/**
* save or update the user's addresses (/profile/addresses)
*/
public function saveAddresses(Request $request, Geocoder $geocoder): RedirectResponse
{
$user = $request->user();
$data = $request->validate([
'home' => 'array',
'home.label' => 'nullable|string|max:100',
'home.line1' => 'nullable|string|max:255',
'home.line2' => 'nullable|string|max:255',
'home.city' => 'nullable|string|max:120',
'home.state' => 'nullable|string|max:64',
'home.postal' => 'nullable|string|max:32',
'home.country' => 'nullable|string|max:64',
'home.phone' => 'nullable|string|max:32',
'billing' => 'array',
'billing.label' => 'nullable|string|max:100',
'billing.line1' => 'nullable|string|max:255',
'billing.line2' => 'nullable|string|max:255',
'billing.city' => 'nullable|string|max:120',
'billing.state' => 'nullable|string|max:64',
'billing.postal' => 'nullable|string|max:32',
'billing.country' => 'nullable|string|max:64',
'billing.phone' => 'nullable|string|max:32',
]);
DB::transaction(function () use ($user, $data, $geocoder) {
$save = function (string $kind, array $payload) use ($user, $geocoder) {
// short-circuit if nothing filled
$filled = collect($payload)->only(['line1','line2','city','state','postal','country','phone'])
->filter(fn ($v) => filled($v));
if ($filled->isEmpty()) {
return;
}
// upsert a single primary address of this kind
$addr = UserAddress::updateOrCreate(
['user_id' => $user->id, 'kind' => $kind, 'is_primary' => 1],
[
'label' => $payload['label'] ?? ucfirst($kind),
'line1' => $payload['line1'] ?? null,
'line2' => $payload['line2'] ?? null,
'city' => $payload['city'] ?? null,
'state' => $payload['state'] ?? null,
'postal' => $payload['postal'] ?? null,
'country' => $payload['country'] ?? null,
'phone' => $payload['phone'] ?? null,
]
);
// build a singleLine string to geocode
$singleLine = collect([
$addr->line1, $addr->line2, $addr->city, $addr->state, $addr->postal, $addr->country
])->filter()->implode(', ');
if ($singleLine !== '') {
// geocode and link to locations
$norm = $geocoder->forward($singleLine);
if ($norm) {
$loc = Location::findOrCreateNormalized($norm, $singleLine);
$addr->location_id = $loc->id;
$addr->save();
}
}
};
if (isset($data['home'])) {
$save('home', $data['home']);
}
if (isset($data['billing'])) {
$save('billing', $data['billing']);
}
});
return Redirect::route('profile.edit')->with('status', 'addresses-updated');
}
/**
* Delete the user's account.
*/
public function destroy(Request $request): RedirectResponse
{
$request->validateWithBag('userDeletion', [
'password' => ['required', 'current_password'],
]);
$user = $request->user();
Auth::logout();
$user->delete();
$request->session()->invalidate();
$request->session()->regenerateToken();
return Redirect::to('/');
}
/**
* content frame handler
*/
private function frame(?string $view = null, array $data = [])
{
return view('profile.index', [
'view' => $view,
'data' => $data,
]);
}
}