Files

262 lines
6.2 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\TourStoreRequest;
use App\Models\Flight;
use App\Models\Tour;
use App\Models\TravelType;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class TourController extends Controller
{
public function index()
{
// ->where('status', 2)
$tours = Tour::latest()->paginate(20);
// آخرین پروازهای تایید شده
$flights = Flight::where('status', 2)
->latest()
->paginate(3);
return view('admin.tours.index', compact('tours', 'flights'));
}
/**
* فرم ایجاد تور
*/
public function create()
{
$travelTypes = TravelType::all();
return view('admin.tours.create', compact('travelTypes'));
}
/**
* ذخیره تور جدید
*/
public function store(TourStoreRequest $request)
{
DB::beginTransaction();
try {
$data = $request->validated();
// ساخت slug در صورت خالی بودن
if (empty($data['slug'])) {
$data['slug'] = Str::slug($data['title']);
}
$data['status'] = 2;
// ایجاد تور
$tour = Tour::create($data);
// آپلود تصویر
if ($request->hasFile('image')) {
$image = $request->file('image');
$newFileName =
time()
. '.tour-image'
. rand(11111, 99999)
. '.'
. $image->extension();
$path = $image->storeAs(
'tours',
$newFileName,
'public'
);
$tour->update([
'image' => $path
]);
}
DB::commit();
return redirect()
->route('admin.tours.index')
->with('success', 'تور با موفقیت ایجاد شد.');
} catch (\Exception $exception) {
DB::rollBack();
Log::error($exception);
return back()
->withInput()
->withErrors([
'general' => 'خطایی در ثبت تور رخ داد.'
]);
}
}
public function show(Tour $tour)
{
return view('admin.tours.show', compact('tour'));
}
public function edit(Tour $tour)
{
$travelTypes = TravelType::all();
return view(
'admin.tours.edit',
compact('tour', 'travelTypes')
);
}
public function update(
TourStoreRequest $request,
Tour $tour
) {
DB::beginTransaction();
try {
$data = $request->validated();
// ساخت slug
if (empty($data['slug'])) {
$data['slug'] = Str::slug($data['title']);
}
if (!isset($data['status'])) {
$data['status'] = $tour->status ?? 2;
}
// اگر تصویر جدید آپلود شده
if ($request->hasFile('image')) {
// حذف تصویر قبلی
if (
$tour->image &&
Storage::disk('public')->exists($tour->image)
) {
Storage::disk('public')->delete($tour->image);
}
$image = $request->file('image');
$newFileName =
time()
. '.tour-image'
. rand(11111, 99999)
. '.'
. $image->extension();
$path = $image->storeAs(
'tours',
$newFileName,
'public'
);
$data['image'] = $path;
}
// بروزرسانی
$tour->update($data);
DB::commit();
return redirect()
->route('admin.tours.index')
->with(
'success',
'تور با موفقیت به‌روزرسانی شد.'
);
} catch (\Exception $exception) {
DB::rollBack();
Log::error($exception);
return back()
->withInput()
->withErrors([
'general' =>
'خطایی در به‌روزرسانی تور رخ داد.'
]);
}
}
public function removeImage(Tour $tour)
{
try {
if (
$tour->image &&
Storage::disk('public')->exists($tour->image)
) {
Storage::disk('public')->delete($tour->image);
}
$tour->update([
'image' => null
]);
return back()->with(
'success',
'تصویر تور با موفقیت حذف شد.'
);
} catch (\Exception $exception) {
Log::error($exception);
return back()->withErrors([
'general' =>
'حذف تصویر با خطا مواجه شد.'
]);
}
}
public function destroy(Tour $tour)
{
try {
// حذف تصویر
if (
$tour->image &&
Storage::disk('public')->exists($tour->image)
) {
Storage::disk('public')->delete($tour->image);
}
// حذف تور
$tour->delete();
return redirect()
->route('admin.tours.index')
->with(
'success',
'تور با موفقیت حذف شد.'
);
} catch (\Exception $exception) {
Log::error($exception);
return back()->withErrors([
'general' =>
'حذف تور با خطا مواجه شد.'
]);
}
}
}