392 lines
11 KiB
PHP
392 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Flight\FlightStoreRequest;
|
|
use App\Http\Requests\Admin\Flight\FlightUpdateRequest;
|
|
use App\Models\Airline;
|
|
use App\Models\Airport;
|
|
use App\Models\City;
|
|
use App\Models\Country;
|
|
use App\Models\File;
|
|
use App\Models\Flight;
|
|
use App\Models\FlightImage;
|
|
use Hekmatinasser\Verta\Verta;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class FlightController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$flights = Flight::query()
|
|
->with([
|
|
'airline',
|
|
'origin_airport.city',
|
|
'airport.city',
|
|
'coverImage.file',
|
|
])
|
|
->when($request->filled('search'), function ($query) use ($request) {
|
|
$search = $request->input('search');
|
|
|
|
$query->whereAny([
|
|
'flight_number',
|
|
''
|
|
], 'like', "%{$search}%");
|
|
})
|
|
->applySort($request->input('sort'))
|
|
->paginate(10)
|
|
->withQueryString();
|
|
|
|
return view('admin.flights.index', compact('flights'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$title = 'ایجاد پرواز';
|
|
|
|
$airlines = Airline::query()
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
$countries = Country::query()
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
$iranCountry = Country::query()
|
|
->where('name', 'iran')
|
|
->first();
|
|
|
|
return view('admin.flights.create', compact(
|
|
'title',
|
|
'airlines',
|
|
'countries',
|
|
'iranCountry'
|
|
));
|
|
|
|
}
|
|
|
|
public function citiesByCountry(Country $country): JsonResponse
|
|
{
|
|
$cities = City::query()
|
|
->where('county_id', $country->id)
|
|
->orderBy('name')
|
|
->get([
|
|
'id',
|
|
'name',
|
|
]);
|
|
|
|
return response()->json($cities);
|
|
}
|
|
|
|
public function airportsByCity(City $city): JsonResponse
|
|
{
|
|
$airports = Airport::query()
|
|
->where('city_id', $city->id)
|
|
->orderBy('name')
|
|
->get([
|
|
'id',
|
|
'name',
|
|
]);
|
|
|
|
return response()->json($airports);
|
|
}
|
|
|
|
public function store(FlightStoreRequest $request)
|
|
{
|
|
$data = $request->validated();
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$departureTime = Verta::parse(
|
|
$data['departure_time']
|
|
)->datetime();
|
|
|
|
$arrivalTime = Verta::parse(
|
|
$data['arrival_time']
|
|
)->datetime();
|
|
|
|
$flight = Flight::create([
|
|
'airline_id' => $data['airline_id'],
|
|
'origin_airport_id' => $data['origin_airport_id'],
|
|
'destination_airport_id' => $data['destination_airport_id'],
|
|
|
|
'flight_number' => $data['flight_number'],
|
|
'trip_type' => $data['trip_type'],
|
|
'flight_type' => $data['flight_type'],
|
|
'flight_scope' => $data['flight_scope'],
|
|
|
|
'departure_time' => $departureTime,
|
|
'arrival_time' => $arrivalTime,
|
|
|
|
'price' => $data['price'],
|
|
'capacity' => $data['capacity'],
|
|
'status' => $data['status'],
|
|
]);
|
|
|
|
$isDefault = true;
|
|
|
|
if (!empty($data['images'])) {
|
|
foreach ($data['images'] as $image) {
|
|
$imageName = $flight->id . '_' . time() . rand(11111, 99999) . '.' . $image->extension();
|
|
|
|
$path = $image->storeAs('flight_images', $imageName, 'public');
|
|
|
|
$file = File::create([
|
|
'name' => $imageName,
|
|
'size' => $image->getSize(),
|
|
'original_name' => $image->getClientOriginalName(),
|
|
'path' => $path,
|
|
'extension' => $image->extension(),
|
|
]);
|
|
|
|
FlightImage::create([
|
|
'flight_id' => $flight->id,
|
|
'file_id' => $file->id,
|
|
'is_default' => $isDefault,
|
|
]);
|
|
|
|
$isDefault = false;
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $exception) {
|
|
DB::rollBack();
|
|
|
|
Log::error($exception);
|
|
|
|
return back()
|
|
->withInput()
|
|
->with('error', 'ثبت پرواز با خطا مواجه شد.');
|
|
}
|
|
|
|
return redirect()
|
|
->route('admin.flights.index')
|
|
->with('success', 'پرواز با موفقیت ثبت شد.');
|
|
}
|
|
|
|
public function edit(Flight $flight)
|
|
{
|
|
$title = 'ویرایش پرواز';
|
|
|
|
$flight->load(['origin_airport.city.country', 'airport.city.country']);
|
|
|
|
$airlines = Airline::query()->orderBy('name')->get();
|
|
$countries = Country::query()->orderBy('name')->get();
|
|
|
|
$iranCountry = Country::query()->where('name', 'iran')->first();
|
|
|
|
$originCountryId = $flight->origin_airport?->city?->country?->id;
|
|
$destinationCountryId = $flight->airport?->city?->country?->id;
|
|
|
|
return view('admin.flights.edit', compact(
|
|
'title',
|
|
'flight',
|
|
'airlines',
|
|
'countries',
|
|
'iranCountry',
|
|
'originCountryId',
|
|
'destinationCountryId'
|
|
));
|
|
}
|
|
|
|
public function destroyImage(Flight $flight, FlightImage $image)
|
|
{
|
|
$image = $flight->images()
|
|
->with('file')
|
|
->findOrFail($image->id);
|
|
|
|
$file = $image->file;
|
|
|
|
DB::transaction(function () use ($flight, $image) {
|
|
$image->delete();
|
|
|
|
$hasDefaultImage = $flight->images()
|
|
->where('is_default', true)
|
|
->exists();
|
|
|
|
if (! $hasDefaultImage) {
|
|
$newDefaultImage = $flight->images()
|
|
->orderBy('sort_order')
|
|
->orderBy('id')
|
|
->first();
|
|
|
|
$newDefaultImage?->update([
|
|
'is_default' => true,
|
|
]);
|
|
}
|
|
});
|
|
|
|
deleteFile($file);
|
|
|
|
return redirect()
|
|
->route('admin.flights.edit', $flight)
|
|
->with('success', 'تصویر پرواز با موفقیت حذف شد.');
|
|
}
|
|
|
|
public function update(FlightUpdateRequest $request, Flight $flight)
|
|
{
|
|
$data = $request->validated();
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$departureTime = Verta::parse(
|
|
$data['departure_time']
|
|
)->datetime();
|
|
|
|
$arrivalTime = Verta::parse(
|
|
$data['arrival_time']
|
|
)->datetime();
|
|
|
|
$flight->update([
|
|
'airline_id' => $data['airline_id'],
|
|
'origin_airport_id' => $data['origin_airport_id'],
|
|
'destination_airport_id' => $data['destination_airport_id'],
|
|
|
|
'flight_number' => $data['flight_number'],
|
|
'trip_type' => $data['trip_type'],
|
|
'flight_type' => $data['flight_type'],
|
|
'flight_scope' => $data['flight_scope'],
|
|
|
|
'departure_time' => $departureTime,
|
|
'arrival_time' => $arrivalTime,
|
|
|
|
'price' => $data['price'],
|
|
'capacity' => $data['capacity'],
|
|
'status' => $data['status'],
|
|
]);
|
|
if (!empty($data['images'])) {
|
|
$hasDefault = $flight->images()
|
|
->where('is_default', true)
|
|
->exists();
|
|
|
|
$nextSort = (int)$flight->images()
|
|
->max('sort_order');
|
|
|
|
foreach ($data['images'] as $index => $image) {
|
|
$imageName = $flight->id . '_' . time() . rand(11111, 99999) . '.' . $image->extension();
|
|
|
|
$path = $image->storeAs(
|
|
'flight_images',
|
|
$imageName,
|
|
'public'
|
|
);
|
|
|
|
$file = File::create([
|
|
'name' => $imageName,
|
|
'size' => $image->getSize(),
|
|
'original_name' => $image->getClientOriginalName(),
|
|
'path' => $path,
|
|
'extension' => $image->extension(),
|
|
]);
|
|
|
|
FlightImage::create([
|
|
'flight_id' => $flight->id,
|
|
'file_id' => $file->id,
|
|
'sort_order' => $nextSort + $index + 1,
|
|
'is_default' => !$hasDefault && $index === 0,
|
|
]);
|
|
}
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error($exception);
|
|
|
|
return back()
|
|
->withInput()
|
|
->with('error', 'ویرایش پرواز با خطا مواجه شد.');
|
|
}
|
|
|
|
return redirect()
|
|
->route('admin.flights.index')
|
|
->with('success', 'پرواز با موفقیت ویرایش شد.');
|
|
}
|
|
|
|
|
|
public function destroy(Flight $flight)
|
|
{
|
|
$flight->delete();
|
|
|
|
return redirect()
|
|
->route('admin.flights.index')
|
|
->with('success', 'پرواز با موفقیت به سطل زباله منتقل شد.');
|
|
}
|
|
|
|
public function trash()
|
|
{
|
|
$title = 'پروازهای حذف شده';
|
|
|
|
$flights = Flight::onlyTrashed()
|
|
->with([
|
|
'airline',
|
|
'origin_airport.city',
|
|
'airport.city',
|
|
'coverImage.file',
|
|
])
|
|
->orderByDesc('deleted_at')
|
|
->paginate(4);
|
|
|
|
return view('admin.flights.trash', compact('title', 'flights'));
|
|
}
|
|
|
|
public function restore($id)
|
|
{
|
|
$flight = Flight::onlyTrashed()
|
|
->findOrFail($id);
|
|
|
|
$flight->restore();
|
|
|
|
return redirect()
|
|
->route('admin.flights.trash.index')
|
|
->with('success', 'پرواز با موفقیت بازگردانی شد.');
|
|
}
|
|
|
|
public function forceDelete($id)
|
|
{
|
|
$flight = Flight::onlyTrashed()
|
|
->with('images.file')
|
|
->findOrFail($id);
|
|
|
|
try {
|
|
DB::transaction(function () use ($flight) {
|
|
foreach ($flight->images as $image) {
|
|
$file = $image->file;
|
|
|
|
if ($file) {
|
|
Storage::disk('public')->delete($file->path);
|
|
|
|
$file->delete();
|
|
}
|
|
|
|
$image->delete();
|
|
}
|
|
|
|
$flight->forceDelete();
|
|
});
|
|
|
|
return redirect()
|
|
->route('admin.flights.trash.index')
|
|
->with('success', 'پرواز و تمام اطلاعات مرتبط با موفقیت حذف دائمی شدند.');
|
|
|
|
} catch (\Throwable $exception) {
|
|
report($exception);
|
|
|
|
return back()
|
|
->with('error', 'حذف دائمی پرواز انجام نشد.');
|
|
}
|
|
}
|
|
}
|