290 lines
7.1 KiB
PHP
290 lines
7.1 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']);
|
|
}
|
|
|
|
/*
|
|
* وضعیت تور
|
|
*
|
|
* 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
|
|
]);
|
|
}
|
|
|
|
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']);
|
|
}
|
|
|
|
/*
|
|
* اگر status از فرم ارسال نشده باشد،
|
|
* وضعیت فعلی تور حفظ شود.
|
|
*/
|
|
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' =>
|
|
'حذف تور با خطا مواجه شد.'
|
|
]);
|
|
}
|
|
}
|
|
}
|