get('search'); $sliders = ContentSlider::with('translations') ->when($search, function ($q) use ($search) { $q->where(function ($sub) use ($search) { $sub->where('slider_type', 'like', "%{$search}%") ->orWhere('slider_dimension', 'like', "%{$search}%") ->orWhere('slider_status', 'like', "%{$search}%") ->orWhereHas('translations', function ($t) use ($search) { $t->where('slider_translation_title', 'like', "%{$search}%"); }); }); }) ->orderBy('slider_sort') ->orderBy('created_at', 'desc') ->paginate(10)->onEachSide(1)->withQueryString(); $statuses = Ref::where('cat', 'SLIDER_STATUS')->orderBy('sort')->pluck('descr_en', 'code'); $portals = FrontendSite::orderBy('name')->pluck('name', 'slug'); return view('backend.module.contentSlider.index', [ 'sliders' => $sliders, 'statuses' => $statuses, 'portals' => $portals, 'search' => $search, ]); } public function create() { $statuses = Ref::where('cat', 'SLIDER_STATUS')->orderBy('sort')->pluck('descr_en', 'code'); $languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr_en', 'code'); $types = Ref::where('cat', 'SLIDER_TYPE')->orderBy('sort')->pluck('descr_en', 'code'); $dimensions = Ref::where('cat', 'SLIDER_DIMENSION')->orderBy('sort')->pluck('descr_en', 'code'); $urlTypes = Ref::where('cat', 'SLIDER_URL_TYPE')->orderBy('sort')->pluck('descr_en', 'code'); $portals = FrontendSite::where('status', true)->orderBy('name')->pluck('name', 'slug'); $defaultSiteSlug = FrontendSite::where('is_default', true)->value('slug'); return view('backend.module.contentSlider.create', [ 'statuses' => $statuses, 'languages' => $languages, 'types' => $types, 'dimensions' => $dimensions, 'urlTypes' => $urlTypes, 'portals' => $portals, 'defaultSiteSlug' => $defaultSiteSlug, ]); } public function store(ContentSliderRequest $request) { $urlUploadPath = null; if ($request->hasFile('slider_url_upload')) { $file = $request->file('slider_url_upload'); $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $filename = $name.'_'.now()->format('Ymd').'_'.substr(uniqid(), -5).'.'.$file->getClientOriginalExtension(); $urlUploadPath = $file->storeAs('content-slider', $filename, 'public'); } $slider = ContentSlider::create([ 'slider_portal' => $request->slider_portal, 'slider_status' => $request->slider_status, 'slider_sort' => $request->slider_sort ?? 0, 'slider_url_type' => $request->slider_url_type, 'slider_url' => $request->slider_url, 'slider_url_upload' => $urlUploadPath, 'slider_type' => $request->slider_type, 'slider_dimension' => $request->slider_dimension, 'slider_transition' => $request->slider_transition, 'slider_start_date' => $request->slider_start_date ?: null, 'slider_start_time' => $request->slider_start_time ?: null, 'slider_end_date' => $request->slider_end_date ?: null, 'slider_end_time' => $request->slider_end_time ?: null, 'created_by' => auth()->id(), 'updated_by' => auth()->id(), ]); if ($request->has('translations')) { foreach ($request->translations as $lang => $translation) { if (! empty($translation['title'])) { $imgPath = null; if (isset($translation['img']) && $translation['img'] instanceof UploadedFile) { $file = $translation['img']; $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $filename = $name.'_'.now()->format('Ymd').'_'.substr(uniqid(), -5).'.'.$file->getClientOriginalExtension(); $imgPath = $file->storeAs('content-slider', $filename, 'public'); } ContentSliderTranslation::create([ 'slider_translation_parent_id' => $slider->slider_id, 'slider_translation_title' => $translation['title'], 'slider_translation_img' => $imgPath, 'slider_translation_main' => $translation['main'] ?? 0, 'slider_translation_language' => $lang, 'created_by' => auth()->id(), 'updated_by' => auth()->id(), ]); } } } flash()->success('Slider created successfully!'); return redirect()->route('content-slider.index'); } public function edit($id) { $slider = ContentSlider::with('translations')->findOrFail($id); $statuses = Ref::where('cat', 'SLIDER_STATUS')->orderBy('sort')->pluck('descr_en', 'code'); $languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr_en', 'code'); $types = Ref::where('cat', 'SLIDER_TYPE')->orderBy('sort')->pluck('descr_en', 'code'); $dimensions = Ref::where('cat', 'SLIDER_DIMENSION')->orderBy('sort')->pluck('descr_en', 'code'); $urlTypes = Ref::where('cat', 'SLIDER_URL_TYPE')->orderBy('sort')->pluck('descr_en', 'code'); $portals = FrontendSite::where('status', true)->orderBy('name')->pluck('name', 'slug'); return view('backend.module.contentSlider.edit', [ 'slider' => $slider, 'statuses' => $statuses, 'languages' => $languages, 'types' => $types, 'dimensions' => $dimensions, 'urlTypes' => $urlTypes, 'portals' => $portals, ]); } public function update(ContentSliderRequest $request, $id) { $slider = ContentSlider::findOrFail($id); $urlUploadPath = $slider->slider_url_upload; if ($request->hasFile('slider_url_upload')) { if ($urlUploadPath && \Storage::disk('public')->exists($urlUploadPath)) { \Storage::disk('public')->delete($urlUploadPath); } $file = $request->file('slider_url_upload'); $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $filename = $name.'_'.now()->format('Ymd').'_'.substr(uniqid(), -5).'.'.$file->getClientOriginalExtension(); $urlUploadPath = $file->storeAs('content-slider', $filename, 'public'); } $slider->update([ 'slider_portal' => $request->slider_portal, 'slider_status' => $request->slider_status, 'slider_sort' => $request->slider_sort ?? $slider->slider_sort, 'slider_url_type' => $request->slider_url_type, 'slider_url' => $request->slider_url, 'slider_url_upload' => $urlUploadPath, 'slider_type' => $request->slider_type, 'slider_dimension' => $request->slider_dimension, 'slider_transition' => $request->slider_transition, 'slider_start_date' => $request->slider_start_date ?: null, 'slider_start_time' => $request->slider_start_time ?: null, 'slider_end_date' => $request->slider_end_date ?: null, 'slider_end_time' => $request->slider_end_time ?: null, 'updated_by' => auth()->id(), ]); // Capture existing images before delete $oldImgs = $slider->translations->pluck('slider_translation_img', 'slider_translation_language'); $slider->translations()->delete(); if ($request->filled('translations')) { foreach ($request->translations as $lang => $translation) { if (! empty($translation['title'])) { $imgPath = $oldImgs[$lang] ?? null; if (isset($translation['img']) && $translation['img'] instanceof UploadedFile) { // Delete old if exists if ($imgPath && \Storage::disk('public')->exists($imgPath)) { \Storage::disk('public')->delete($imgPath); } $file = $translation['img']; $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); $filename = $name.'_'.now()->format('Ymd').'_'.substr(uniqid(), -5).'.'.$file->getClientOriginalExtension(); $imgPath = $file->storeAs('content-slider', $filename, 'public'); } ContentSliderTranslation::create([ 'slider_translation_parent_id' => $slider->slider_id, 'slider_translation_title' => $translation['title'], 'slider_translation_img' => $imgPath, 'slider_translation_main' => $translation['main'] ?? 0, 'slider_translation_language' => $lang, 'created_by' => auth()->id(), 'updated_by' => auth()->id(), ]); } } } flash()->success('Slider updated successfully!'); return redirect()->route('content-slider.index'); } public function destroy($id) { $slider = ContentSlider::findOrFail($id); foreach ($slider->translations as $translation) { if ($translation->slider_translation_img && \Storage::disk('public')->exists($translation->slider_translation_img)) { \Storage::disk('public')->delete($translation->slider_translation_img); } } if ($slider->slider_url_upload && \Storage::disk('public')->exists($slider->slider_url_upload)) { \Storage::disk('public')->delete($slider->slider_url_upload); } $slider->translations()->delete(); $slider->delete(); flash()->success('Slider deleted successfully.'); return redirect()->route('content-slider.index'); } public function reorder(Request $request) { $ids = $request->input('ids', []); $page = (int) $request->input('page', 1); $perPage = 10; $offset = ($page - 1) * $perPage; foreach ($ids as $index => $id) { ContentSlider::where('slider_id', $id)->update(['slider_sort' => $offset + $index + 1]); } return response()->json(['success' => true]); } }