2017-08-03 04:12:26 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
2017-12-05 04:21:46 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Repositories\AirportRepository;
|
2017-12-21 09:12:39 +08:00
|
|
|
use App\Services\GeoService;
|
2017-12-05 00:59:25 +08:00
|
|
|
use Log;
|
|
|
|
|
2017-08-03 04:12:26 +08:00
|
|
|
use Illuminate\Http\Request;
|
2017-08-03 04:29:04 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2017-08-03 04:12:26 +08:00
|
|
|
|
2017-12-05 04:21:46 +08:00
|
|
|
use App\Http\Controllers\AppBaseController;
|
2017-12-13 01:49:35 +08:00
|
|
|
use App\Models\UserBid;
|
2017-12-05 04:21:46 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
2017-12-05 00:34:01 +08:00
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
2017-12-05 04:21:46 +08:00
|
|
|
|
2017-08-04 10:02:02 +08:00
|
|
|
use Mockery\Exception;
|
2017-12-05 00:59:25 +08:00
|
|
|
use Prettus\Repository\Exceptions\RepositoryException;
|
2017-08-03 04:12:26 +08:00
|
|
|
|
|
|
|
class FlightController extends AppBaseController
|
|
|
|
{
|
2017-12-21 09:12:39 +08:00
|
|
|
private $airlineRepo,
|
|
|
|
$airportRepo,
|
|
|
|
$flightRepo,
|
|
|
|
$geoSvc;
|
2017-12-05 04:21:46 +08:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
AirlineRepository $airlineRepo,
|
|
|
|
AirportRepository $airportRepo,
|
2017-12-21 09:12:39 +08:00
|
|
|
FlightRepository $flightRepo,
|
|
|
|
GeoService $geoSvc
|
2017-12-05 04:21:46 +08:00
|
|
|
) {
|
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->airportRepo = $airportRepo;
|
2017-08-03 04:12:26 +08:00
|
|
|
$this->flightRepo = $flightRepo;
|
2017-12-21 09:12:39 +08:00
|
|
|
$this->geoSvc = $geoSvc;
|
2017-08-03 04:12:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2017-08-03 04:29:04 +08:00
|
|
|
$where = ['active' => true];
|
2017-08-03 04:12:26 +08:00
|
|
|
|
2017-08-03 04:29:04 +08:00
|
|
|
// default restrictions on the flights shown. Handle search differently
|
|
|
|
if (config('phpvms.only_flights_from_current')) {
|
|
|
|
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:59:25 +08:00
|
|
|
try {
|
|
|
|
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
|
|
|
|
} catch (RepositoryException $e) {
|
|
|
|
Log::emergency($e);
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:34:01 +08:00
|
|
|
$flights = $this->flightRepo->paginate();
|
2017-08-03 04:29:04 +08:00
|
|
|
|
2017-12-13 01:49:35 +08:00
|
|
|
$saved_flights = UserBid::where('user_id', Auth::id())
|
2017-08-04 10:02:02 +08:00
|
|
|
->pluck('flight_id')->toArray();
|
|
|
|
|
2017-08-03 04:12:26 +08:00
|
|
|
return $this->view('flights.index', [
|
2017-12-05 04:21:46 +08:00
|
|
|
'airlines' => $this->airlineRepo->selectBoxList(true),
|
|
|
|
'airports' => $this->airportRepo->selectBoxList(true),
|
2017-08-03 04:12:26 +08:00
|
|
|
'flights' => $flights,
|
2017-08-04 10:02:02 +08:00
|
|
|
'saved' => $saved_flights,
|
2017-08-03 04:12:26 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:34:01 +08:00
|
|
|
/**
|
|
|
|
* Make a search request using the Repository search
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
|
|
* @throws \Prettus\Repository\Exceptions\RepositoryException
|
|
|
|
*/
|
2017-08-04 10:02:02 +08:00
|
|
|
public function search(Request $request)
|
2017-08-03 04:12:26 +08:00
|
|
|
{
|
2017-12-05 00:59:25 +08:00
|
|
|
$flights = $this->flightRepo->searchCriteria($request)->paginate();
|
2017-08-03 04:29:04 +08:00
|
|
|
|
2017-12-13 01:49:35 +08:00
|
|
|
$saved_flights = UserBid::where('user_id', Auth::id())
|
2017-08-04 10:02:02 +08:00
|
|
|
->pluck('flight_id')->toArray();
|
|
|
|
|
2017-08-03 04:29:04 +08:00
|
|
|
return $this->view('flights.index', [
|
2017-12-05 04:21:46 +08:00
|
|
|
'airlines' => $this->airlineRepo->selectBoxList(true),
|
|
|
|
'airports' => $this->airportRepo->selectBoxList(true),
|
2017-08-03 04:29:04 +08:00
|
|
|
'flights' => $flights,
|
2017-08-04 10:02:02 +08:00
|
|
|
'saved' => $saved_flights,
|
2017-08-03 04:29:04 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-08-04 10:02:02 +08:00
|
|
|
public function save(Request $request)
|
|
|
|
{
|
|
|
|
$user_id = Auth::id();
|
|
|
|
$flight_id = $request->input('flight_id');
|
2017-12-05 00:34:01 +08:00
|
|
|
$action = strtolower($request->input('action'));
|
2017-08-04 10:02:02 +08:00
|
|
|
|
|
|
|
$cols = ['user_id' => $user_id, 'flight_id' => $flight_id];
|
|
|
|
|
2017-12-05 00:34:01 +08:00
|
|
|
if($action === 'save') {
|
2017-12-13 01:49:35 +08:00
|
|
|
$uf = UserBid::create($cols);
|
2017-08-04 10:02:02 +08:00
|
|
|
$uf->save();
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'id' => $uf->id,
|
|
|
|
'message' => 'Saved!',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:34:01 +08:00
|
|
|
elseif ($action === 'remove') {
|
2017-08-04 10:02:02 +08:00
|
|
|
try {
|
2017-12-13 01:49:35 +08:00
|
|
|
$uf = UserBid::where($cols)->first();
|
2017-08-04 10:02:02 +08:00
|
|
|
$uf->delete();
|
|
|
|
} catch (Exception $e) { }
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'message' => 'Deleted!'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 09:12:39 +08:00
|
|
|
/**
|
|
|
|
* Show the flight information page
|
|
|
|
*/
|
2017-08-04 10:02:02 +08:00
|
|
|
public function show($id)
|
|
|
|
{
|
2017-12-21 09:12:39 +08:00
|
|
|
$flight = $this->flightRepo->find($id);
|
|
|
|
if (empty($flight)) {
|
|
|
|
Flash::error('Flight not found!');
|
|
|
|
return redirect(route('frontend.dashboard.index'));
|
|
|
|
}
|
2017-08-04 10:02:02 +08:00
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
$map_features = $this->geoSvc->flightGeoJson($flight);
|
2017-12-21 09:12:39 +08:00
|
|
|
|
|
|
|
return $this->view('flights.show', [
|
|
|
|
'flight' => $flight,
|
2017-12-25 07:38:38 +08:00
|
|
|
'map_features' => $map_features,
|
2017-12-21 09:12:39 +08:00
|
|
|
]);
|
2017-08-04 10:02:02 +08:00
|
|
|
}
|
2017-08-03 04:12:26 +08:00
|
|
|
}
|