phpvms/app/Http/Controllers/Frontend/ProfileController.php

273 lines
8.0 KiB
PHP
Raw Normal View History

<?php
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;
2018-02-21 12:33:09 +08:00
use App\Support\Countries;
use App\Support\Timezonelist;
use App\Support\Utils;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
2018-08-27 00:40:04 +08:00
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
2018-08-27 00:40:04 +08:00
use Intervention\Image\Facades\Image;
use Laracasts\Flash\Flash;
use Nwidart\Modules\Facades\Module;
2018-01-12 11:35:03 +08:00
class ProfileController extends Controller
{
2018-08-27 00:40:04 +08:00
private $airlineRepo;
private $airportRepo;
private $userRepo;
2018-02-21 12:33:09 +08:00
/**
* ProfileController constructor.
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo
* @param UserRepository $userRepo
2018-02-21 12:33:09 +08:00
*/
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
UserRepository $userRepo
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->userRepo = $userRepo;
}
/**
* Return whether the vmsACARS module is enabled or not
*/
private function acarsEnabled(): bool
{
// Is the ACARS module enabled?
$acars_enabled = false;
$acars = Module::find('VMSACARS');
if ($acars) {
$acars_enabled = $acars->isEnabled();
}
return $acars_enabled;
}
2018-02-21 12:33:09 +08:00
/**
* @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,
]);
}
2018-02-21 12:33:09 +08:00
/**
* @param $id
2018-08-27 00:40:04 +08:00
*
* @return mixed
2018-02-21 12:33:09 +08:00
*/
public function show($id)
{
$user = User::with(['fields', 'fields.field'])
->where('id', $id)
->first();
if (empty($user)) {
Flash::error('User not found!');
return redirect(route('frontend.dashboard.index'));
}
$airports = $this->airportRepo->all();
2020-08-14 05:44:41 +08:00
$userFields = $this->userRepo->getUserFields($user, true);
return view('profile.index', [
2020-08-14 05:44:41 +08:00
'user' => $user,
'userFields' => $userFields,
'airports' => $airports,
]);
}
/**
* Show the edit for form the user's profile
2019-09-14 06:20:50 +08:00
*
2018-02-21 12:33:09 +08:00
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @throws \Exception
*
2018-02-21 12:33:09 +08:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
*/
public function edit(Request $request)
{
/** @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'));
2020-08-14 05:44:41 +08:00
$userFields = $this->userRepo->getUserFields($user, false);
return view('profile.edit', [
'user' => $user,
'airlines' => $airlines,
'airports' => $airports,
'countries' => Countries::getSelectList(),
'timezones' => Timezonelist::toArray(),
'userFields' => $userFields,
]);
}
2018-02-21 12:33:09 +08:00
/**
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return mixed
2018-02-21 12:33:09 +08:00
*/
public function update(Request $request)
{
$id = Auth::user()->id;
$user = $this->userRepo->findWithoutFail($id);
$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);
Log::info($validator->errors()->toArray());
return redirect(route('frontend.profile.edit', $id))
->withErrors($validator)
->withInput();
}
$req_data = $request->all();
if (!$request->filled('password')) {
unset($req_data['password']);
} else {
$req_data['password'] = Hash::make($req_data['password']);
}
if (isset($req_data['avatar']) !== null) {
Storage::delete($user->avatar);
}
if ($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
$file_name = $user->ident.'.'.$avatar->getClientOriginalExtension();
$path = "avatars/{$file_name}";
// Create the avatar, resizing it and keeping the aspect ratio.
// https://stackoverflow.com/a/26892028
$w = config('phpvms.avatar.width');
$h = config('phpvms.avatar.height');
$canvas = Image::canvas($w, $h);
$image = Image::make($avatar)->resize($w, $h, static function ($constraint) {
$constraint->aspectRatio();
});
$canvas->insert($image);
$canvas->save(public_path('uploads/avatars/'.$file_name));
$req_data['avatar'] = $path;
}
2018-01-01 03:08:41 +08:00
$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'));
}
2018-01-01 03:08:41 +08:00
/**
* Regenerate the user's API key
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @param Request $request
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2018-01-01 03:08:41 +08:00
*/
public function regen_apikey(Request $request)
{
$user = User::find(Auth::user()->id);
Log::info('Regenerating API key "'.$user->ident.'"');
2018-01-01 03:08:41 +08:00
$user->api_key = Utils::generateApiKey();
$user->save();
flash('New API key generated!')->success();
2018-01-01 03:08:41 +08:00
return redirect(route('frontend.profile.index'));
}
/**
* Generate the ACARS config and send it to download
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function acars(Request $request)
{
$user = Auth::user();
$config = view('system.acars.config', ['user' => $user])->render();
return response($config)->withHeaders([
'Content-Type' => 'text/xml',
'Content-Length' => strlen($config),
'Cache-Control' => 'no-store, no-cache',
'Content-Disposition' => 'attachment; filename="settings.xml',
]);
}
}