122 lines
3.7 KiB
PHP
122 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\TourStoreRequest;
|
|
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;
|
|
use Illuminate\Support\Str;
|
|
|
|
class TourController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$tours = Tour::latest()->paginate(10);
|
|
return view('admin.tours.index', compact('tours'));
|
|
}
|
|
|
|
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']);
|
|
}
|
|
|
|
$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) {
|
|
Log::error($exception);
|
|
DB::rollBack();
|
|
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();
|
|
|
|
if (empty($data['slug'])) {
|
|
$data['slug'] = Str::slug($data['title']);
|
|
}
|
|
|
|
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) {
|
|
Log::error($exception);
|
|
DB::rollBack();
|
|
return back()->withInput()->withErrors(['general' => 'خطایی در بهروزرسانی رخ داد.']);
|
|
}
|
|
}
|
|
|
|
public function removeImage(Tour $tour)
|
|
{
|
|
// حذف تصویر تکی (اختیاری — میتوانی منطق دلخواه بگذاری)
|
|
return back()->with('success', 'عملیات حذف تصویر انجام شد.');
|
|
}
|
|
|
|
public function destroy(Tour $tour)
|
|
{
|
|
$tour->delete();
|
|
|
|
return redirect()->route('admin.tours.index')
|
|
->with('success', 'تور با موفقیت حذف شد.');
|
|
}
|
|
}
|