2017-08-03 02:13:08 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Controller;
|
2017-08-03 02:13:08 +08:00
|
|
|
use App\Models\User;
|
2020-08-12 05:48:51 +08:00
|
|
|
use App\Models\UserField;
|
|
|
|
use App\Models\UserFieldValue;
|
2017-12-25 02:12:26 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
2017-08-03 02:13:08 +08:00
|
|
|
use App\Repositories\AirportRepository;
|
2017-12-25 02:12:26 +08:00
|
|
|
use App\Repositories\UserRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Support\Countries;
|
2021-06-05 04:57:27 +08:00
|
|
|
use App\Support\Discord;
|
2019-09-14 05:54:46 +08:00
|
|
|
use App\Support\Timezonelist;
|
2020-02-12 01:32:41 +08:00
|
|
|
use App\Support\Utils;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2019-09-14 05:54:46 +08:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-08-27 00:40:04 +08:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2019-09-14 05:54:46 +08:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2018-08-27 00:40:04 +08:00
|
|
|
use Intervention\Image\Facades\Image;
|
2019-09-14 05:54:46 +08:00
|
|
|
use Laracasts\Flash\Flash;
|
2020-02-08 02:29:43 +08:00
|
|
|
use Nwidart\Modules\Facades\Module;
|
2017-08-03 02:13:08 +08:00
|
|
|
|
2018-01-12 11:35:03 +08:00
|
|
|
class ProfileController extends Controller
|
2017-08-03 02:13:08 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
private $airlineRepo;
|
|
|
|
private $airportRepo;
|
|
|
|
private $userRepo;
|
2017-08-03 02:13:08 +08:00
|
|
|
|
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
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param UserRepository $userRepo
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-12-25 02:12:26 +08:00
|
|
|
public function __construct(
|
|
|
|
AirlineRepository $airlineRepo,
|
|
|
|
AirportRepository $airportRepo,
|
|
|
|
UserRepository $userRepo
|
|
|
|
) {
|
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->airportRepo = $airportRepo;
|
|
|
|
$this->userRepo = $userRepo;
|
2017-08-03 02:13:08 +08:00
|
|
|
}
|
|
|
|
|
2020-02-08 02:29:43 +08:00
|
|
|
/**
|
|
|
|
* Return whether the vmsACARS module is enabled or not
|
|
|
|
*/
|
|
|
|
private function acarsEnabled(): bool
|
|
|
|
{
|
|
|
|
// Is the ACARS module enabled?
|
|
|
|
$acars_enabled = false;
|
2021-01-26 05:04:43 +08:00
|
|
|
$acars = Module::find('VMSAcars');
|
2020-02-08 02:29:43 +08:00
|
|
|
if ($acars) {
|
|
|
|
$acars_enabled = $acars->isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $acars_enabled;
|
|
|
|
}
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
2020-10-12 04:06:09 +08:00
|
|
|
* Redirect to show() since only a single page gets shown and the template controls
|
|
|
|
* the other items that are/aren't shown
|
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
*/
|
2017-08-03 02:13:08 +08:00
|
|
|
public function index()
|
|
|
|
{
|
2020-10-12 04:06:09 +08:00
|
|
|
return $this->show(Auth::user()->id);
|
2017-08-03 02:13:08 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-02-21 00:36:26 +08:00
|
|
|
* @return mixed
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-08-03 02:13:08 +08:00
|
|
|
public function show($id)
|
|
|
|
{
|
2020-10-12 04:06:09 +08:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = User::with(['awards', 'fields', 'fields.field'])
|
2020-08-12 05:48:51 +08:00
|
|
|
->where('id', $id)
|
|
|
|
->first();
|
|
|
|
|
2017-08-03 02:13:08 +08:00
|
|
|
if (empty($user)) {
|
|
|
|
Flash::error('User not found!');
|
|
|
|
return redirect(route('frontend.dashboard.index'));
|
|
|
|
}
|
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
$airports = $this->airportRepo->all();
|
2020-08-14 05:44:41 +08:00
|
|
|
$userFields = $this->userRepo->getUserFields($user, true);
|
2017-08-03 02:13:08 +08:00
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('profile.index', [
|
2020-08-14 05:44:41 +08:00
|
|
|
'user' => $user,
|
|
|
|
'userFields' => $userFields,
|
|
|
|
'airports' => $airports,
|
2021-01-26 05:04:43 +08:00
|
|
|
'acars' => $this->acarsEnabled(),
|
2017-08-03 02:13:08 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2019-09-14 05:54:46 +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
|
2017-12-25 02:12:26 +08:00
|
|
|
*/
|
|
|
|
public function edit(Request $request)
|
2017-08-03 02:13:08 +08:00
|
|
|
{
|
2020-08-12 05:48:51 +08:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = User::with(['fields', 'fields.field'])
|
|
|
|
->where('id', Auth::user()->id)
|
|
|
|
->first();
|
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
if (empty($user)) {
|
|
|
|
Flash::error('User not found!');
|
|
|
|
return redirect(route('frontend.dashboard.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$airlines = $this->airlineRepo->selectBoxList();
|
2018-03-01 05:37:24 +08:00
|
|
|
$airports = $this->airportRepo->selectBoxList(false, setting('pilots.home_hubs_only'));
|
2021-01-28 21:31:29 +08:00
|
|
|
$userFields = $this->userRepo->getUserFields($user, true);
|
2017-12-25 02:12:26 +08:00
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('profile.edit', [
|
2020-08-12 05:48:51 +08:00
|
|
|
'user' => $user,
|
|
|
|
'airlines' => $airlines,
|
|
|
|
'airports' => $airports,
|
|
|
|
'countries' => Countries::getSelectList(),
|
|
|
|
'timezones' => Timezonelist::toArray(),
|
|
|
|
'userFields' => $userFields,
|
2017-12-25 02:12:26 +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
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2020-02-08 02:29:43 +08:00
|
|
|
* @return mixed
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-12-25 02:12:26 +08:00
|
|
|
public function update(Request $request)
|
|
|
|
{
|
|
|
|
$id = Auth::user()->id;
|
|
|
|
$user = $this->userRepo->findWithoutFail($id);
|
|
|
|
|
2020-08-12 05:48:51 +08:00
|
|
|
$rules = [
|
2018-03-20 09:50:40 +08:00
|
|
|
'name' => 'required',
|
|
|
|
'email' => 'required|unique:users,email,'.$id,
|
2017-12-25 02:12:26 +08:00
|
|
|
'airline_id' => 'required',
|
2018-05-01 09:23:51 +08:00
|
|
|
'password' => 'confirmed',
|
2018-05-01 09:29:10 +08:00
|
|
|
'avatar' => 'nullable|mimes:jpeg,png,jpg',
|
2020-08-12 05:48:51 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
$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);
|
2017-12-25 02:12:26 +08:00
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2019-07-17 01:54:14 +08:00
|
|
|
Log::info('validator failed for user '.$user->ident);
|
2017-12-25 02:12:26 +08:00
|
|
|
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']);
|
|
|
|
}
|
|
|
|
|
2018-05-01 09:29:10 +08:00
|
|
|
if (isset($req_data['avatar']) !== null) {
|
2018-05-01 09:23:51 +08:00
|
|
|
Storage::delete($user->avatar);
|
|
|
|
}
|
2020-08-12 05:48:51 +08:00
|
|
|
|
2021-06-05 04:57:27 +08:00
|
|
|
// Find out the user's private channel id
|
|
|
|
/*
|
|
|
|
// TODO: Uncomment when Discord API functionality is enabled
|
|
|
|
if ($request->filled('discord_id')) {
|
|
|
|
$discord_id = $request->post('discord_id');
|
|
|
|
if ($discord_id !== $user->discord_id) {
|
|
|
|
$req_data['discord_private_channel_id'] = Discord::getPrivateChannelId($discord_id);
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2018-05-01 09:23:51 +08:00
|
|
|
if ($request->hasFile('avatar')) {
|
|
|
|
$avatar = $request->file('avatar');
|
2019-07-17 01:54:14 +08:00
|
|
|
$file_name = $user->ident.'.'.$avatar->getClientOriginalExtension();
|
2018-05-01 23:32:48 +08:00
|
|
|
$path = "avatars/{$file_name}";
|
2019-05-13 20:52:07 +08:00
|
|
|
|
|
|
|
// 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);
|
2020-11-22 22:59:09 +08:00
|
|
|
Log::info('Uploading avatar into folder '.public_path('uploads/avatars'));
|
2019-05-13 20:52:07 +08:00
|
|
|
$canvas->save(public_path('uploads/avatars/'.$file_name));
|
|
|
|
|
2018-05-01 09:23:51 +08:00
|
|
|
$req_data['avatar'] = $path;
|
|
|
|
}
|
|
|
|
|
2018-01-01 03:08:41 +08:00
|
|
|
$this->userRepo->update($req_data, $id);
|
2017-08-03 02:13:08 +08:00
|
|
|
|
2020-08-12 05:48:51 +08:00
|
|
|
// 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)]);
|
|
|
|
}
|
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
Flash::success('Profile updated successfully!');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
return redirect(route('frontend.profile.index'));
|
2017-08-03 02:13:08 +08:00
|
|
|
}
|
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);
|
2019-07-17 01:54:14 +08:00
|
|
|
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-03-20 09:50:40 +08:00
|
|
|
|
2018-01-01 03:08:41 +08:00
|
|
|
return redirect(route('frontend.profile.index'));
|
|
|
|
}
|
2020-02-08 02:29:43 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
]);
|
|
|
|
}
|
2017-08-03 02:13:08 +08:00
|
|
|
}
|