diff --git a/app/Http/Controllers/admin/TourController.php b/app/Http/Controllers/admin/TourController.php index dce8cb3..d5330fd 100644 --- a/app/Http/Controllers/admin/TourController.php +++ b/app/Http/Controllers/admin/TourController.php @@ -4,9 +4,9 @@ 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\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; @@ -14,18 +14,37 @@ use Illuminate\Support\Str; class TourController extends Controller { + /** + * لیست تورها + */ public function index() { - $tours = Tour::latest()->paginate(10); - return view('admin.tours.index', compact('tours')); + // همه تورها نمایش داده میشوند + // اگر فقط تورهای تایید شده را میخواهی: + // ->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(); @@ -33,89 +52,238 @@ class TourController extends Controller try { $data = $request->validated(); - // ساخت slug اگر خالی بود + // ساخت slug در صورت خالی بودن if (empty($data['slug'])) { $data['slug'] = Str::slug($data['title']); } + /* + * وضعیت تور + * + * 1 = در انتظار + * 2 = تایید شده + * + * اگر میخواهی تور بلافاصله در لیست نمایش داده شود، + * مقدار 2 قرار میدهیم. + */ + $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]); + $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', 'تور با موفقیت ایجاد شد.'); + + return redirect() + ->route('admin.tours.index') + ->with('success', 'تور با موفقیت ایجاد شد.'); } catch (\Exception $exception) { - Log::error($exception); + DB::rollBack(); - return back()->withInput()->withErrors(['general' => 'خطایی در ثبت رخ داد.']); + + 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')); + + return view( + 'admin.tours.edit', + compact('tour', 'travelTypes') + ); } - public function update(TourStoreRequest $request, Tour $tour) - { + /** + * بروزرسانی تور + */ + public function update( + TourStoreRequest $request, + Tour $tour + ) { DB::beginTransaction(); try { + $data = $request->validated(); + // ساخت slug if (empty($data['slug'])) { $data['slug'] = Str::slug($data['title']); } + /* + * اگر status از فرم ارسال نشده باشد، + * وضعیت فعلی تور حفظ شود. + */ + if (!isset($data['status'])) { + $data['status'] = $tour->status ?? 2; + } + + // اگر تصویر جدید آپلود شده if ($request->hasFile('image')) { - // حذف عکس قبلی از حافظه - if ($tour->image && Storage::disk('public')->exists($tour->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'); + + $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', 'تور با موفقیت بهروزرسانی شد.'); + + return redirect() + ->route('admin.tours.index') + ->with( + 'success', + 'تور با موفقیت بهروزرسانی شد.' + ); } catch (\Exception $exception) { - Log::error($exception); + DB::rollBack(); - return back()->withInput()->withErrors(['general' => 'خطایی در بهروزرسانی رخ داد.']); + + Log::error($exception); + + return back() + ->withInput() + ->withErrors([ + 'general' => + 'خطایی در بهروزرسانی تور رخ داد.' + ]); } } + /** + * حذف تصویر تور + */ public function removeImage(Tour $tour) { - // حذف تصویر تکی (اختیاری — میتوانی منطق دلخواه بگذاری) - return back()->with('success', 'عملیات حذف تصویر انجام شد.'); + 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) { - $tour->delete(); + try { - return redirect()->route('admin.tours.index') - ->with('success', 'تور با موفقیت حذف شد.'); + // حذف تصویر + 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' => + 'حذف تور با خطا مواجه شد.' + ]); + } } } diff --git a/resources/views/tours/index.blade.php b/resources/views/tours/index.blade.php index e7922aa..ac9c7ce 100644 --- a/resources/views/tours/index.blade.php +++ b/resources/views/tours/index.blade.php @@ -3,11 +3,65 @@ @section('title', 'لیست تورها') @section('content') + + {{-- استایل دکمه علاقهمندی (مناسب برای کارتها) --}} + +
تور فعالی یافت نشد.
@@ -15,4 +69,26 @@تور فعالی یافت نشد.
--}} +{{-- @endforelse--}} +{{----}} +{{-- {{ $tour->description }}--}} +{{--
--}} + +{{-- @else--}} + +{{----}} +{{-- توضیحی برای این تور ثبت نشده است.--}} +{{--
--}} + +{{-- @endif--}} + +{{--