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/ContentCalendarController.php

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\ContentCalendarRequest;
use App\Models\Backend\BackendUser;
use App\Models\Backend\ContentCalendar;
use App\Models\Backend\ContentCalendarTranslation;
use App\Models\Backend\Ref;
use Illuminate\Http\Request;

class ContentCalendarController extends Controller
{
    public function index(Request $request)
    {
        $search = $request->get('search');
        $categoryFilter = $request->get('category');
        $statusFilter = $request->get('status');
        $typeFilter = $request->get('type');
        $yearFilter = $request->get('year');

        $calendars = ContentCalendar::with('translations')
            ->when($search, function ($q) use ($search) {
                $q->where(function ($sub) use ($search) {
                    $sub->where('calendar_category', 'like', "%{$search}%")
                        ->orWhere('calendar_status', 'like', "%{$search}%")
                        ->orWhere('calendar_type', 'like', "%{$search}%")
                        ->orWhereHas('translations', function ($t) use ($search) {
                            $t->where('calendar_translation_title', 'like', "%{$search}%")
                                ->orWhere('calendar_translation_content', 'like', "%{$search}%")
                                ->orWhere('calendar_translation_location', 'like', "%{$search}%");
                        });
                });
            })
            ->when($categoryFilter, fn($q) => $q->where('calendar_category', $categoryFilter))
            ->when($statusFilter, fn($q) => $q->where('calendar_status', $statusFilter))
            ->when($typeFilter, fn($q) => $q->where('calendar_type', $typeFilter))
            ->when($yearFilter, function ($q) use ($yearFilter) {
                $q->whereYear('calendar_date_start', $yearFilter)
                    ->orWhereYear('calendar_date_end', $yearFilter);
            })
            ->orderBy('calendar_date_start', 'desc')
            ->paginate(10)
            ->onEachSide(1)
            ->withQueryString();

        $categories = Ref::where('cat', 'CALENDAR_CATEGORY')->orderBy('sort')->pluck('descr', 'code');
        $statuses = Ref::where('cat', 'CALENDAR_STATUS')->orderBy('sort')->pluck('descr', 'code');
        $types = Ref::where('cat', 'CALENDAR_TYPE')->orderBy('sort')->pluck('descr', 'code');
        $displays = Ref::where('cat', 'CALENDAR_DISPLAY')->orderBy('sort')->pluck('descr', 'code');
        $languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');
        $years = ContentCalendar::selectRaw('YEAR(calendar_date_start) as year')
            ->whereNotNull('calendar_date_start')
            ->distinct()
            ->orderBy('year', 'desc')
            ->pluck('year');

        return view('backend.module.contentCalendar.index', [
            'calendars' => $calendars,
            'categories' => $categories,
            'statuses' => $statuses,
            'types' => $types,
            'displays' => $displays,
            'languages' => $languages,
            'years' => $years,
            'search' => $search,
            'categoryFilter' => $categoryFilter,
            'statusFilter' => $statusFilter,
            'typeFilter' => $typeFilter,
            'yearFilter' => $yearFilter,
        ]);
    }

    public function create()
    {
        $categories = Ref::where('cat', 'CALENDAR_CATEGORY')->orderBy('sort')->pluck('descr', 'code');
        $statuses = Ref::where('cat', 'CALENDAR_STATUS')->orderBy('sort')->pluck('descr', 'code');
        $types = Ref::where('cat', 'CALENDAR_TYPE')->orderBy('sort')->pluck('descr', 'code');
        $displays = Ref::where('cat', 'CALENDAR_DISPLAY')->orderBy('sort')->pluck('descr', 'code');
        $languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');

        return view('backend.module.contentCalendar.create', [
            'categories' => $categories,
            'statuses' => $statuses,
            'types' => $types,
            'displays' => $displays,
            'languages' => $languages,
        ]);
    }

    public function store(ContentCalendarRequest $request)
    {
        $calendar = ContentCalendar::create([
            'calendar_date_start' => $request->calendar_date_start,
            'calendar_date_end' => $request->calendar_date_end,
            'calendar_time_start' => $request->calendar_time_start,
            'calendar_time_end' => $request->calendar_time_end,
            'calendar_category' => $request->calendar_category,
            'calendar_display' => $request->calendar_display,
            'calendar_status' => $request->calendar_status,
            'calendar_type' => $request->calendar_type,
            'calendar_owner_id' => $request->calendar_owner_id,
            'total_participant' => $request->total_participant,
            'created_by' => auth()->id(),
            'updated_by' => auth()->id(),
        ]);

        if ($request->has('translations')) {
            foreach ($request->translations as $lang => $translation) {
                if (!empty($translation['title']) || !empty($translation['content']) || !empty($translation['location'])) {
                    ContentCalendarTranslation::create([
                        'calendar_translation_parent_id' => $calendar->calendar_id,
                        'calendar_translation_title' => $translation['title'] ?? '',
                        'calendar_translation_content' => $translation['content'] ?? '',
                        'calendar_translation_location' => $translation['location'] ?? '',
                        'calendar_translation_main' => $translation['main'] ?? 0,
                        'calendar_translation_language' => $lang,
                        'created_by' => auth()->id(),
                        'updated_by' => auth()->id(),
                    ]);
                }
            }
        }

        flash()->success('Calendar event created successfully!');

        return redirect()->route('content-calendar.index');
    }

    public function edit($id)
    {
        $calendar = ContentCalendar::with('translations')->findOrFail($id);
        $categories = Ref::where('cat', 'CALENDAR_CATEGORY')->orderBy('sort')->pluck('descr', 'code');
        $statuses = Ref::where('cat', 'CALENDAR_STATUS')->orderBy('sort')->pluck('descr', 'code');
        $types = Ref::where('cat', 'CALENDAR_TYPE')->orderBy('sort')->pluck('descr', 'code');
        $displays = Ref::where('cat', 'CALENDAR_DISPLAY')->orderBy('sort')->pluck('descr', 'code');
        $languages = Ref::where('cat', 'LANGUAGE')->orderBy('sort')->pluck('descr', 'code');
        $owners = BackendUser::orderBy('first_name')->orderBy('last_name')->get(['id', 'first_name', 'last_name', 'username']);

        return view('backend.module.contentCalendar.edit', [
            'calendar' => $calendar,
            'categories' => $categories,
            'statuses' => $statuses,
            'types' => $types,
            'displays' => $displays,
            'languages' => $languages,
            'owners' => $owners,
        ]);
    }

    public function update(ContentCalendarRequest $request, $id)
    {
        $calendar = ContentCalendar::findOrFail($id);

        $calendar->update([
            'calendar_date_start' => $request->calendar_date_start,
            'calendar_date_end' => $request->calendar_date_end,
            'calendar_time_start' => $request->calendar_time_start,
            'calendar_time_end' => $request->calendar_time_end,
            'calendar_category' => $request->calendar_category,
            'calendar_display' => $request->calendar_display,
            'calendar_status' => $request->calendar_status,
            'calendar_type' => $request->calendar_type,
            'calendar_owner_id' => $request->calendar_owner_id ?? $calendar->calendar_owner_id,
            'total_participant' => $request->total_participant,
            'updated_by' => auth()->id(),
        ]);

        $calendar->translations()->delete();

        if ($request->filled('translations')) {
            foreach ($request->translations as $lang => $translation) {
                if (!empty($translation['title']) || !empty($translation['content']) || !empty($translation['location'])) {
                    ContentCalendarTranslation::create([
                        'calendar_translation_parent_id' => $calendar->calendar_id,
                        'calendar_translation_title' => $translation['title'] ?? '',
                        'calendar_translation_content' => $translation['content'] ?? '',
                        'calendar_translation_location' => $translation['location'] ?? '',
                        'calendar_translation_main' => $translation['main'] ?? 0,
                        'calendar_translation_language' => $lang,
                        'created_by' => auth()->id(),
                        'updated_by' => auth()->id(),
                    ]);
                }
            }
        }

        flash()->success('Calendar event updated successfully!');

        return redirect()->route('content-calendar.index');
    }

    public function destroy($id)
    {
        $calendar = ContentCalendar::findOrFail($id);
        $calendar->translations()->delete();
        $calendar->delete();

        flash()->success('Calendar event deleted successfully.');

        return redirect()->route('content-calendar.index');
    }
}