326 lines
9.8 KiB
PHP
326 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\FlightStoreRequest;
|
|
use App\Http\Requests\Admin\FlightUpdateRequest;
|
|
use App\Models\ActivityLog;
|
|
use App\Models\Airline;
|
|
use App\Models\Airport;
|
|
use App\Models\File;
|
|
use App\Models\Flight;
|
|
use Exception;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
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([
|
|
'departureAirport.city',
|
|
'arrivalAirport.city',
|
|
'airline',
|
|
])
|
|
->when($request->filled('search'), function (Builder $query) use ($request) {
|
|
|
|
$search = $request->input('search');
|
|
|
|
$query->where(function (Builder $query) use ($search) {
|
|
|
|
$query->where('title', 'LIKE', "%{$search}%")
|
|
->orWhere('summary', 'LIKE', "%{$search}%")
|
|
->orWhereHas('departureAirport.city', function (Builder $query) use ($search) {
|
|
$query->where('name', 'LIKE', "%{$search}%");
|
|
})
|
|
->orWhereHas('arrivalAirport.city', function (Builder $query) use ($search) {
|
|
$query->where('name', 'LIKE', "%{$search}%");
|
|
})
|
|
->orWhereHas('airline', function (Builder $query) use ($search) {
|
|
$query->where('name', 'LIKE', "%{$search}%");
|
|
});
|
|
|
|
});
|
|
})
|
|
->when($request->filled('flight_type'), function (Builder $query) use ($request) {
|
|
|
|
$query->where(
|
|
'flight_type',
|
|
$request->input('flight_type')
|
|
);
|
|
|
|
})
|
|
->when($request->filled('status'), function (Builder $query) use ($request) {
|
|
|
|
$query->where(
|
|
'status',
|
|
$request->input('status')
|
|
);
|
|
|
|
})
|
|
->when($request->filled('sort'), function (Builder $query) use ($request) {
|
|
|
|
switch ($request->input('sort')) {
|
|
|
|
case 'oldest':
|
|
$query->orderBy('created_at');
|
|
break;
|
|
|
|
case 'price_asc':
|
|
$query->orderBy('price');
|
|
break;
|
|
|
|
case 'price_desc':
|
|
$query->orderByDesc('price');
|
|
break;
|
|
|
|
case 'departure_asc':
|
|
$query->orderBy('departure_time');
|
|
break;
|
|
|
|
case 'departure_desc':
|
|
$query->orderByDesc('departure_time');
|
|
break;
|
|
|
|
case 'newest':
|
|
default:
|
|
$query->orderByDesc('created_at');
|
|
break;
|
|
}
|
|
}, function (Builder $query) {
|
|
|
|
$query->orderByDesc('created_at');
|
|
|
|
})
|
|
->paginate(15)
|
|
->withQueryString();
|
|
|
|
return view('admin.flights.index', compact('flights'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$airlines = Airline::query()
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
$airports = Airport::query()
|
|
->with('city')
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
return view('admin.flights.create', compact(
|
|
'airlines',
|
|
'airports'
|
|
));
|
|
}
|
|
|
|
|
|
public function edit(Flight $flight)
|
|
{
|
|
$airlines = Airline::query()
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
$airports = Airport::query()
|
|
->with('city')
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
$flight->load('file');
|
|
|
|
$createdLog = ActivityLog::query()
|
|
->where('model_type', Flight::class)
|
|
->where('model_id', $flight->id)
|
|
->where('action', 'created')
|
|
->with('admin')
|
|
->first();
|
|
|
|
$lastUpdatedLog = ActivityLog::query()
|
|
->where('model_type', Flight::class)
|
|
->where('model_id', $flight->id)
|
|
->where('action', 'updated')
|
|
->with('admin')
|
|
->latest('created_at')
|
|
->first();
|
|
|
|
return view('admin.flights.edit', compact(
|
|
'flight',
|
|
'airlines',
|
|
'airports',
|
|
'createdLog',
|
|
'lastUpdatedLog'
|
|
));
|
|
}
|
|
|
|
public function store(FlightStoreRequest $request)
|
|
{
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
$image = $request->file('image');
|
|
|
|
$imageName = 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(),
|
|
]);
|
|
|
|
$flight = Flight::create([
|
|
'file_id' => $file->id,
|
|
'arrival_aiport_id' => $request->arrival_aiport_id,
|
|
'departure_airport_id' => $request->departure_airport_id,
|
|
'airline_id' => $request->airline_id,
|
|
'title' => $request->title,
|
|
'summary' => $request->input('summary') ?? '',
|
|
'flight_type' => $request->flight_type,
|
|
'departure_time' => $request->departure_time,
|
|
'price' => $request->price,
|
|
'capacity' => 0,
|
|
'views' => 0,
|
|
'status' => $request->status,
|
|
]);
|
|
|
|
// ثبت لاگ ایجاد پرواز
|
|
logActivity($flight, 'created');
|
|
|
|
DB::commit();
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error($exception);
|
|
|
|
return back()
|
|
->withErrors([
|
|
'general' => $exception->getMessage(),
|
|
])
|
|
->withInput();
|
|
}
|
|
|
|
return redirect()
|
|
->route('admin.flights.index')
|
|
->with('success', 'پرواز با موفقیت ایجاد شد.');
|
|
}
|
|
|
|
public function update(FlightUpdateRequest $request, Flight $flight)
|
|
{
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
$oldFile = $flight->file;
|
|
|
|
$flight->update([
|
|
'arrival_aiport_id' => $request->arrival_aiport_id,
|
|
'departure_airport_id' => $request->departure_airport_id,
|
|
'airline_id' => $request->airline_id,
|
|
'title' => $request->title,
|
|
'summary' => $request->input('summary') ?? '',
|
|
'flight_type' => $request->flight_type,
|
|
'departure_time' => $request->departure_time,
|
|
'price' => $request->price,
|
|
'capacity' => 0,
|
|
'status' => $request->status,
|
|
]);
|
|
|
|
// ثبت لاگ ویرایش پرواز (باید بلافاصله بعد از update صدا زده شود)
|
|
logActivity($flight, 'updated');
|
|
|
|
|
|
if ($request->hasFile('image')) {
|
|
|
|
$image = $request->file('image');
|
|
|
|
$path = $image->store('flights', 'public');
|
|
|
|
$newFile = File::create([
|
|
'name' => pathinfo(
|
|
$image->hashName(),
|
|
PATHINFO_FILENAME
|
|
),
|
|
'extension' => $image->getClientOriginalExtension(),
|
|
'size' => $image->getSize(),
|
|
'original_name' => $image->getClientOriginalName(),
|
|
'path' => $path,
|
|
]);
|
|
|
|
$flight->update([
|
|
'file_id' => $newFile->id,
|
|
]);
|
|
|
|
// ثبت لاگ تغییر عکس (بهصورت جدا، چون update دومیه)
|
|
logActivity($flight, 'updated');
|
|
|
|
if ($oldFile) {
|
|
|
|
if (Storage::disk('public')->exists($oldFile->path)) {
|
|
|
|
Storage::disk('public')->delete(
|
|
$oldFile->path
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error($exception);
|
|
|
|
return back()
|
|
->withErrors([
|
|
'general' => 'خطایی در ویرایش پرواز رخ داده است.'
|
|
])
|
|
->withInput();
|
|
}
|
|
|
|
|
|
return redirect()
|
|
->route('admin.flights.index')
|
|
->with('success', 'پرواز با موفقیت ویرایش شد.');
|
|
}
|
|
public function destroy(Flight $flight)
|
|
{
|
|
if ($flight->bookings()->exists()) {
|
|
|
|
return back()->withErrors([
|
|
'error' => 'این پرواز دارای رزرو است و قابل حذف نیست.'
|
|
]);
|
|
}
|
|
|
|
$flight->delete();
|
|
|
|
return redirect()
|
|
->route('admin.flights.index')
|
|
->with('success', 'پرواز با موفقیت حذف شد.');
|
|
}
|
|
|
|
|
|
}
|