| Current Path : /var/www/html/reddsis/app/Http/Controllers/Backend/ |
| Current File : /var/www/html/reddsis/app/Http/Controllers/Backend/ContentPhotoGalleryController.php |
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\ContentPhotoGalleryRequest;
use App\Models\Backend\ContentPhotoGallery;
use App\Models\Backend\ContentPhotoGalleryTranslation;
use App\Models\Backend\ContentPhotoList;
use App\Models\Backend\FrontendSite;
use App\Models\Backend\Ref;
use Illuminate\Http\Request;
class ContentPhotoGalleryController extends Controller
{
public function index(Request $request)
{
$search = $request->get('search');
$galleries = ContentPhotoGallery::with('translations')->withCount('photoLists')
->when($search, function ($q) use ($search) {
$q->where(function ($sub) use ($search) {
$sub->where('gallery_cat', 'like', "%{$search}%")
->orWhere('gallery_status', 'like', "%{$search}%")
->orWhere('gallery_code', 'like', "%{$search}%")
->orWhereHas('translations', function ($t) use ($search) {
$t->where('gallery_translation_title', 'like', "%{$search}%");
});
});
})
->orderBy('gallery_sort')
->orderBy('created_at', 'desc')
->paginate(10)->onEachSide(1)->withQueryString();
$categories = Ref::where('cat', 'GALLERY_CAT')->orderBy('sort')->pluck('descr', 'code');
$statuses = Ref::where('cat', 'GALLERY_STATUS')->orderBy('sort')->pluck('descr', 'code');
return view('backend.module.contentPhotoGallery.index', [
'galleries' => $galleries,
'categories' => $categories,
'statuses' => $statuses,
'search' => $search,
]);
}
public function create()
{
$categories = Ref::where('cat', 'GALLERY_CAT')->orderBy('sort')->pluck('descr', 'code');
$statuses = Ref::where('cat', 'GALLERY_STATUS')->orderBy('sort')->pluck('descr', 'code');
$languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');
$portals = FrontendSite::where('status', true)->orderBy('name')->pluck('name', 'slug');
return view('backend.module.contentPhotoGallery.create', [
'categories' => $categories,
'statuses' => $statuses,
'languages' => $languages,
'portals' => $portals,
]);
}
public function store(ContentPhotoGalleryRequest $request)
{
$thumbPath = null;
if ($request->hasFile('gallery_thumbnail')) {
$file = $request->file('gallery_thumbnail');
$name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$filename = $name . '_' . now()->format('Ymd') . '_' . substr(uniqid(), -5) . '.' . $file->getClientOriginalExtension();
$thumbPath = $file->storeAs('content-photo-gallery', $filename, 'public');
}
$gallery = ContentPhotoGallery::create([
'gallery_date' => $request->gallery_date,
'gallery_location' => $request->gallery_location,
'gallery_status' => $request->gallery_status,
'gallery_portal' => $request->gallery_portal,
'gallery_thumbnail' => $thumbPath,
'gallery_cat' => $request->gallery_cat,
'gallery_code' => $request->gallery_code,
'gallery_sort' => $request->gallery_sort ?? 0,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
if ($request->has('translations')) {
foreach ($request->translations as $lang => $translation) {
if (!empty($translation['title'])) {
ContentPhotoGalleryTranslation::create([
'gallery_translation_parent_id' => $gallery->gallery_id,
'gallery_translation_title' => $translation['title'],
'gallery_translation_descr' => $translation['descr'] ?? '',
'gallery_translation_main' => $translation['main'] ?? 0,
'gallery_translation_language' => $lang,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
}
}
}
flash()->success('Photo gallery created successfully!');
return redirect()->route('content-photo-gallery.index');
}
public function edit($id)
{
$gallery = ContentPhotoGallery::with('translations')->findOrFail($id);
$categories = Ref::where('cat', 'GALLERY_CAT')->orderBy('sort')->pluck('descr', 'code');
$statuses = Ref::where('cat', 'GALLERY_STATUS')->orderBy('sort')->pluck('descr', 'code');
$languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');
$portals = FrontendSite::where('status', true)->orderBy('name')->pluck('name', 'slug');
return view('backend.module.contentPhotoGallery.edit', [
'gallery' => $gallery,
'categories' => $categories,
'statuses' => $statuses,
'languages' => $languages,
'portals' => $portals,
]);
}
public function update(ContentPhotoGalleryRequest $request, $id)
{
$gallery = ContentPhotoGallery::findOrFail($id);
$thumbPath = $gallery->gallery_thumbnail;
if ($request->hasFile('gallery_thumbnail')) {
if ($thumbPath && \Storage::disk('public')->exists($thumbPath)) {
\Storage::disk('public')->delete($thumbPath);
}
$file = $request->file('gallery_thumbnail');
$name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$filename = $name . '_' . now()->format('Ymd') . '_' . substr(uniqid(), -5) . '.' . $file->getClientOriginalExtension();
$thumbPath = $file->storeAs('content-photo-gallery', $filename, 'public');
}
$gallery->update([
'gallery_date' => $request->gallery_date,
'gallery_location' => $request->gallery_location,
'gallery_status' => $request->gallery_status,
'gallery_portal' => $request->gallery_portal,
'gallery_thumbnail' => $thumbPath,
'gallery_cat' => $request->gallery_cat,
'gallery_code' => $request->gallery_code,
'gallery_sort' => $request->gallery_sort ?? 0,
'updated_by' => auth()->id(),
]);
$gallery->translations()->delete();
if ($request->filled('translations')) {
foreach ($request->translations as $lang => $translation) {
if (!empty($translation['title'])) {
ContentPhotoGalleryTranslation::create([
'gallery_translation_parent_id' => $gallery->gallery_id,
'gallery_translation_title' => $translation['title'],
'gallery_translation_descr' => $translation['descr'] ?? '',
'gallery_translation_main' => $translation['main'] ?? 0,
'gallery_translation_language' => $lang,
'created_by' => auth()->id(),
'updated_by' => auth()->id(),
]);
}
}
}
flash()->success('Photo gallery updated successfully!');
return redirect()->route('content-photo-gallery.index');
}
public function destroy($id)
{
$gallery = ContentPhotoGallery::findOrFail($id);
if ($gallery->gallery_thumbnail && \Storage::disk('public')->exists($gallery->gallery_thumbnail)) {
\Storage::disk('public')->delete($gallery->gallery_thumbnail);
}
$gallery->translations()->delete();
ContentPhotoList::where('photo_gallery_id', $gallery->gallery_id)->delete();
$gallery->delete();
flash()->success('Photo gallery deleted successfully.');
return redirect()->route('content-photo-gallery.index');
}
}