Your IP : 216.73.216.79


Current Path : /var/www/html/reddsis/app/Http/Controllers/Backend/
Upload File :
Current File : /var/www/html/reddsis/app/Http/Controllers/Backend/ContentPhotoListController.php

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\ContentPhotoListRequest;
use App\Models\Backend\ContentPhotoGallery;
use App\Models\Backend\ContentPhotoList;
use App\Models\Backend\Ref;
use Illuminate\Http\Request;

class ContentPhotoListController extends Controller
{
    public function index(Request $request)
    {
        $search = $request->get('search');
        $galleryId = $request->get('gallery_id');

        $photos = ContentPhotoList::where(function ($q) {
            $q->whereNull('photo_main')->orWhere('photo_main', '1');
        })->with('gallery')
            ->when($galleryId, function ($q) use ($galleryId) {
                $q->where('photo_gallery_id', $galleryId);
            })
            ->when($search, function ($q) use ($search) {
                $q->where(function ($sub) use ($search) {
                    $sub->where('photo_descr', 'like', "%{$search}%");
                });
            })
            ->orderBy('photo_sort')
            ->orderBy('created_at', 'desc')
            ->paginate(10)->onEachSide(1)->withQueryString();

        $galleries = ContentPhotoGallery::with('translations')->orderBy('gallery_sort')->get();
        $statuses = Ref::where('cat', 'PHOTO_STATUS')->orderBy('sort')->pluck('descr', 'code');

        return view('backend.module.contentPhotoList.index', [
            'photos' => $photos,
            'galleries' => $galleries,
            'statuses' => $statuses,
            'search' => $search,
            'galleryId' => $galleryId,
        ]);
    }

    public function create()
    {
        $galleries = ContentPhotoGallery::with('translations')->orderBy('gallery_sort')->get();
        $languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');

        return view('backend.module.contentPhotoList.create', [
            'galleries' => $galleries,
            'languages' => $languages,
        ]);
    }

    public function store(ContentPhotoListRequest $request)
    {
        $filePath = null;
        if ($request->hasFile('photo_url')) {
            $file = $request->file('photo_url');
            $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
            $filename = $name . '_' . now()->format('Ymd') . '_' . substr(uniqid(), -5) . '.' . $file->getClientOriginalExtension();
            $filePath = $file->storeAs('content-photo-list', $filename, 'public');
        }

        $mainLang = null;
        $translations = $request->translations ?? [];
        foreach ($translations as $code => $trans) {
            if (!empty($trans['main'])) {
                $mainLang = $code;
                break;
            }
        }
        if (!$mainLang) {
            $mainLang = array_key_first($translations);
        }

        $photo = ContentPhotoList::create([
            'photo_gallery_id' => $request->photo_gallery_id,
            'photo_main' => '1',
            'photo_parent_id' => null,
            'photo_language' => $mainLang,
            'photo_sort' => $request->photo_sort ?? 0,
            'photo_url' => $filePath,
            'photo_descr' => $translations[$mainLang]['descr'] ?? '',
            'created_by' => auth()->id(),
            'updated_by' => auth()->id(),
        ]);

        foreach ($translations as $code => $trans) {
            if ($code === $mainLang) continue;
            if (!empty($trans['descr'])) {
                ContentPhotoList::create([
                    'photo_gallery_id' => $request->photo_gallery_id,
                    'photo_main' => '0',
                    'photo_parent_id' => $photo->photo_id,
                    'photo_language' => $code,
                    'photo_sort' => 0,
                    'photo_url' => $filePath,
                    'photo_descr' => $trans['descr'] ?? '',
                    'created_by' => auth()->id(),
                    'updated_by' => auth()->id(),
                ]);
            }
        }

        flash()->success('Photo created successfully!');
        if ($request->filled('_redirect')) {
            return redirect()->to($request->_redirect);
        }
        return redirect()->route('content-photo-list.index');
    }

    public function edit($id)
    {
        $photo = ContentPhotoList::findOrFail($id);
        $galleries = ContentPhotoGallery::with('translations')->orderBy('gallery_sort')->get();
        $languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');

        $translations = collect();
        if ($photo->photo_main === '1' || $photo->photo_main === 1) {
            $translations = ContentPhotoList::where('photo_parent_id', $photo->photo_id)->get();
        }

        return view('backend.module.contentPhotoList.edit', [
            'photo' => $photo,
            'galleries' => $galleries,
            'languages' => $languages,
            'translations' => $translations,
        ]);
    }

    public function update(ContentPhotoListRequest $request, $id)
    {
        $photo = ContentPhotoList::findOrFail($id);

        $filePath = $photo->photo_url;
        if ($request->hasFile('photo_url')) {
            if ($filePath && \Storage::disk('public')->exists($filePath)) {
                \Storage::disk('public')->delete($filePath);
            }
            $file = $request->file('photo_url');
            $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
            $filename = $name . '_' . now()->format('Ymd') . '_' . substr(uniqid(), -5) . '.' . $file->getClientOriginalExtension();
            $filePath = $file->storeAs('content-photo-list', $filename, 'public');
        }

        $mainLang = null;
        $translations = $request->translations ?? [];
        foreach ($translations as $code => $trans) {
            if (!empty($trans['main'])) {
                $mainLang = $code;
                break;
            }
        }
        if (!$mainLang) {
            $mainLang = array_key_first($translations);
        }

        $photo->update([
            'photo_gallery_id' => $request->photo_gallery_id,
            'photo_language' => $mainLang,
            'photo_sort' => $request->photo_sort ?? 0,
            'photo_url' => $filePath,
            'photo_descr' => $translations[$mainLang]['descr'] ?? '',
            'updated_by' => auth()->id(),
        ]);

        ContentPhotoList::where('photo_parent_id', $photo->photo_id)->delete();

        foreach ($translations as $code => $trans) {
            if ($code === $mainLang) continue;
            if (!empty($trans['descr'])) {
                ContentPhotoList::create([
                    'photo_gallery_id' => $request->photo_gallery_id,
                    'photo_main' => '0',
                    'photo_parent_id' => $photo->photo_id,
                    'photo_language' => $code,
                    'photo_sort' => 0,
                    'photo_url' => $filePath,
                    'photo_descr' => $trans['descr'] ?? '',
                    'created_by' => auth()->id(),
                    'updated_by' => auth()->id(),
                ]);
            }
        }

        flash()->success('Photo updated successfully!');
        return redirect()->route('content-photo-list.index', ['gallery_id' => $photo->photo_gallery_id]);
    }

    public function destroy($id)
    {
        $photo = ContentPhotoList::findOrFail($id);

        if ($photo->photo_main === '1' || $photo->photo_main === 1) {
            $translations = ContentPhotoList::where('photo_parent_id', $photo->photo_id)->get();
            foreach ($translations as $t) {
                if ($t->photo_url && \Storage::disk('public')->exists($t->photo_url)) {
                    \Storage::disk('public')->delete($t->photo_url);
                }
                $t->delete();
            }
        }

        if ($photo->photo_url && \Storage::disk('public')->exists($photo->photo_url)) {
            \Storage::disk('public')->delete($photo->photo_url);
        }

        $photo->delete();

        flash()->success('Photo deleted successfully.');
        return redirect()->back();
    }

    public function bulkStore(Request $request)
    {
        $request->validate([
            'photo_gallery_id' => 'required|integer|exists:content_photo_gallery,gallery_id',
            'files' => 'required|array',
            'files.*' => 'required|image|mimes:jpg,jpeg,png,webp|max:5120',
            'translations' => 'nullable|array',
            'translations.*.descr' => 'nullable|string',
            'translations.*.main' => 'nullable|boolean',
        ]);

        $galleryId = $request->photo_gallery_id;
        $uploaded = 0;
        $translations = $request->translations ?? [];

        $mainLang = null;
        foreach ($translations as $code => $trans) {
            if (!empty($trans['main'])) {
                $mainLang = $code;
                break;
            }
        }
        if (!$mainLang) {
            $mainLang = array_key_first($translations);
        }

        if ($request->hasFile('files')) {
            foreach ($request->file('files') as $file) {
                $name = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
                $filename = $name . '_' . now()->format('Ymd') . '_' . substr(uniqid(), -5) . '.' . $file->getClientOriginalExtension();
                $filePath = $file->storeAs('content-photo-list', $filename, 'public');

                $photo = ContentPhotoList::create([
                    'photo_gallery_id' => $galleryId,
                    'photo_main' => '1',
                    'photo_parent_id' => null,
                    'photo_language' => $mainLang,
                    'photo_sort' => 0,
                    'photo_url' => $filePath,
                    'photo_descr' => $translations[$mainLang]['descr'] ?? $name,
                    'created_by' => auth()->id(),
                    'updated_by' => auth()->id(),
                ]);

                foreach ($translations as $code => $trans) {
                    if ($code === $mainLang) continue;
                    if (!empty($trans['descr'])) {
                        ContentPhotoList::create([
                            'photo_gallery_id' => $galleryId,
                            'photo_main' => '0',
                            'photo_parent_id' => $photo->photo_id,
                            'photo_language' => $code,
                            'photo_sort' => 0,
                            'photo_url' => $filePath,
                            'photo_descr' => $trans['descr'] ?? '',
                            'created_by' => auth()->id(),
                            'updated_by' => auth()->id(),
                        ]);
                    }
                }

                $uploaded++;
            }
        }

        if ($request->filled('_redirect')) {
            flash()->success("{$uploaded} photos uploaded successfully.");
            return redirect()->to($request->_redirect);
        }

        return response()->json([
            'success' => true,
            'message' => "{$uploaded} photos uploaded successfully.",
        ]);
    }
    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) {
            \App\Models\Backend\ContentPhotoList::where("id", $id)->update(["photo_sort" => $offset + $index + 1]);
        }
        return response()->json(["success" => true]);
    }

}