2017-08-03 02:13:08 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Facades\Utils;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Controller;
|
2017-08-03 02:13:08 +08:00
|
|
|
use App\Models\User;
|
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;
|
|
|
|
use Flash;
|
|
|
|
use Hash;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Jackiedo\Timezonelist\Facades\Timezonelist;
|
|
|
|
use Log;
|
|
|
|
use Validator;
|
2018-05-01 09:23:51 +08:00
|
|
|
use Intervention\Image\Facades\Image;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
2017-08-03 02:13:08 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class ProfileController
|
|
|
|
* @package App\Http\Controllers\Frontend
|
|
|
|
*/
|
2018-01-12 11:35:03 +08:00
|
|
|
class ProfileController extends Controller
|
2017-08-03 02:13:08 +08:00
|
|
|
{
|
2017-12-25 02:12:26 +08:00
|
|
|
private $airlineRepo,
|
|
|
|
$airportRepo,
|
|
|
|
$userRepo;
|
2017-08-03 02:13:08 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* ProfileController constructor.
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
if (setting('pilots.home_hubs_only')) {
|
2018-03-01 05:37:24 +08:00
|
|
|
$airports = $this->airportRepo->findWhere(['hub' => true]);
|
|
|
|
} else {
|
|
|
|
$airports = $this->airportRepo->all();
|
|
|
|
}
|
2017-08-03 02:13:08 +08:00
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('profile.index', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user' => Auth::user(),
|
2017-08-03 02:13:08 +08:00
|
|
|
'airports' => $airports,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
|
|
|
*/
|
2017-08-03 02:13:08 +08:00
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$user = User::where('id', $id)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
Flash::error('User not found!');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-08-03 02:13:08 +08:00
|
|
|
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
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('profile.index', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user' => $user,
|
2017-08-03 02:13:08 +08:00
|
|
|
'airports' => $airports,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
/**
|
|
|
|
* Show the edit for form the user's profile
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param Request $request
|
|
|
|
* @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
|
|
|
{
|
2017-12-25 02:12:26 +08:00
|
|
|
$user = User::where('id', Auth::user()->id)->first();
|
|
|
|
if (empty($user)) {
|
|
|
|
Flash::error('User not found!');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-12-25 02:12:26 +08:00
|
|
|
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'));
|
2017-12-25 02:12:26 +08:00
|
|
|
|
2018-03-12 07:00:42 +08:00
|
|
|
return view('profile.edit', [
|
2017-12-25 02:12:26 +08:00
|
|
|
'user' => $user,
|
|
|
|
'airlines' => $airlines,
|
|
|
|
'airports' => $airports,
|
2018-02-13 00:51:04 +08:00
|
|
|
'countries' => Countries::getSelectList(),
|
2017-12-25 02:12:26 +08:00
|
|
|
'timezones' => Timezonelist::toArray(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @return $this
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
|
|
|
*/
|
2017-12-25 02:12:26 +08:00
|
|
|
public function update(Request $request)
|
|
|
|
{
|
|
|
|
$id = Auth::user()->id;
|
|
|
|
$user = $this->userRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
$validator = Validator::make($request->toArray(), [
|
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',
|
2017-12-25 02:12:26 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
if ($request->hasFile('avatar')) {
|
|
|
|
$avatar = $request->file('avatar');
|
|
|
|
$file_name = $user->pilot_id . '.' .$avatar->getClientOriginalExtension();
|
2018-05-01 23:32:48 +08:00
|
|
|
$path = "avatars/{$file_name}";
|
2018-05-01 09:23:51 +08:00
|
|
|
Image::make($avatar)->resize(config('phpvms.avatar.width'), config('phpvms.avatar.height'))->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);
|
2017-08-03 02:13:08 +08:00
|
|
|
|
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-02-21 12:33:09 +08:00
|
|
|
* @param Request $request
|
|
|
|
* @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->pilot_id.'"');
|
|
|
|
|
|
|
|
$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'));
|
|
|
|
}
|
2017-08-03 02:13:08 +08:00
|
|
|
}
|