2017-08-03 02:13:08 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
use Log;
|
|
|
|
use Hash;
|
|
|
|
use Flash;
|
|
|
|
use Validator;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Jackiedo\Timezonelist\Facades\Timezonelist;
|
|
|
|
|
2017-08-03 02:13:08 +08:00
|
|
|
use App\Models\User;
|
2018-01-01 03:08:41 +08:00
|
|
|
use App\Facades\Utils;
|
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;
|
2017-08-03 02:13:08 +08:00
|
|
|
use App\Http\Controllers\AppBaseController;
|
|
|
|
|
|
|
|
class ProfileController extends AppBaseController
|
|
|
|
{
|
2017-12-25 02:12:26 +08:00
|
|
|
private $airlineRepo,
|
|
|
|
$airportRepo,
|
|
|
|
$userRepo;
|
2017-08-03 02:13:08 +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
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2017-12-25 02:12:26 +08:00
|
|
|
$airports = $this->airportRepo->all();
|
2017-08-03 02:13:08 +08:00
|
|
|
|
|
|
|
return $this->view('profile.index', [
|
|
|
|
'user' => Auth::user(),
|
|
|
|
'airports' => $airports,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$user = User::where('id', $id)->first();
|
|
|
|
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();
|
2017-08-03 02:13:08 +08:00
|
|
|
|
|
|
|
return $this->view('profile.index', [
|
|
|
|
'user' => $user,
|
|
|
|
'airports' => $airports,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
/**
|
|
|
|
* Show the edit for form the user's profile
|
|
|
|
*/
|
|
|
|
public function edit(Request $request)
|
2017-08-03 02:13:08 +08:00
|
|
|
{
|
2017-12-25 02:12:26 +08:00
|
|
|
$user = User::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();
|
|
|
|
|
2018-01-10 09:08:16 +08:00
|
|
|
$countries = collect((new \League\ISO3166\ISO3166)->all())
|
|
|
|
->mapWithKeys(function($item, $key) {
|
|
|
|
return [strtolower($item['alpha2']) => $item['name']];
|
|
|
|
});
|
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
return $this->view('profile.edit', [
|
|
|
|
'user' => $user,
|
|
|
|
'airlines' => $airlines,
|
|
|
|
'airports' => $airports,
|
2018-01-10 09:08:16 +08:00
|
|
|
'countries' => $countries,
|
2017-12-25 02:12:26 +08:00
|
|
|
'timezones' => Timezonelist::toArray(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(Request $request)
|
|
|
|
{
|
|
|
|
$id = Auth::user()->id;
|
|
|
|
$user = $this->userRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
$validator = Validator::make($request->toArray(), [
|
|
|
|
'name' => 'required',
|
|
|
|
'email' => 'required|unique:users,email,'.$id,
|
|
|
|
'airline_id' => 'required',
|
|
|
|
'password' => 'confirmed'
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
Log::info('validator failed for user '.$user->pilot_id);
|
|
|
|
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-01-01 03:08:41 +08:00
|
|
|
$this->userRepo->update($req_data, $id);
|
2017-08-03 02:13:08 +08:00
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
Flash::success('Profile updated successfully!');
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function regen_apikey(Request $request)
|
|
|
|
{
|
|
|
|
$user = User::find(Auth::user()->id);
|
|
|
|
Log::info('Regenerating API key "'.$user->pilot_id.'"');
|
|
|
|
|
|
|
|
$user->api_key = Utils::generateApiKey();
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
flash('New API key generated!')->success();
|
|
|
|
return redirect(route('frontend.profile.index'));
|
|
|
|
}
|
2017-08-03 02:13:08 +08:00
|
|
|
}
|