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

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\Backend\ContentArticle;
use App\Models\Backend\ContentCalendar;
use App\Models\Backend\ContentDirectoryDepartment;
use App\Models\Backend\ContentDirectoryStaff;
use App\Models\Backend\ContentDownload;
use App\Models\Backend\ContentFeedbackMain;
use App\Models\Backend\ContentImage;
use App\Models\Backend\ContentPublicHoliday;
use App\Models\Backend\ContentSlider;
use App\Models\Backend\ContentVideo;
use App\Models\Backend\BackendUser;
use App\Models\Backend\FrontendUser;
use App\Models\Backend\Ref;
use Illuminate\Http\Request;

class GlobalSearchController extends Controller
{
    protected int $limit = 5;

    public function search(Request $request)
    {
        $q = $request->get('q');

        if (!$q || strlen(trim($q)) < 2) {
            return response()->json([]);
        }

        $q = trim($q);
        $results = [];

        $this->searchArticles($q, $results);
        $this->searchDownloads($q, $results);
        $this->searchImages($q, $results);
        $this->searchVideos($q, $results);
        $this->searchSliders($q, $results);
        $this->searchPublicHolidays($q, $results);
        $this->searchCalendars($q, $results);
        $this->searchDepartments($q, $results);
        $this->searchStaff($q, $results);
        $this->searchFeedback($q, $results);
        $this->searchBackendUsers($q, $results);
        $this->searchFrontendUsers($q, $results);
        $this->searchRefs($q, $results);

        return response()->json($results);
    }

    protected function add(&$results, string $label, string $type, string $url): void
    {
        $results[] = [
            'label' => $label,
            'type'  => $type,
            'url'   => $url,
        ];
    }

    protected function searchArticles(string $q, array &$results): void
    {
        $items = ContentArticle::whereHas('translations', fn($b) => $b->where('article_translation_title', 'LIKE', "%{$q}%"))
            ->orWhere('article_code', 'LIKE', "%{$q}%")
            ->with('translations')
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                ($m->article_code ? "[{$m->article_code}] " : '') . ($m->translations->first()?->article_translation_title ?? $m->article_code),
                'Articles',
                route('content-article.edit', $m->article_id)
            );
        }
    }

    protected function searchDownloads(string $q, array &$results): void
    {
        $items = ContentDownload::whereNull('download_parent_id')
            ->where(function ($b) use ($q) {
                $b->where('download_title', 'LIKE', "%{$q}%")
                  ->orWhere('download_category', 'LIKE', "%{$q}%");
            })
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                $m->download_title,
                'Downloads',
                route('content-download.edit', $m->download_id)
            );
        }
    }

    protected function searchImages(string $q, array &$results): void
    {
        $items = ContentImage::whereNull('image_parent_id')
            ->where(function ($b) use ($q) {
                $b->where('image_title', 'LIKE', "%{$q}%")
                  ->orWhere('image_cat', 'LIKE', "%{$q}%");
            })
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                $m->image_title,
                'Images',
                route('content-image.edit', $m->image_id)
            );
        }
    }

    protected function searchVideos(string $q, array &$results): void
    {
        $items = ContentVideo::whereHas('translations', fn($b) => $b->where('video_translation_title', 'LIKE', "%{$q}%"))
            ->orWhere('video_code', 'LIKE', "%{$q}%")
            ->with('translations')
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                ($m->video_code ? "[{$m->video_code}] " : '') . ($m->translations->first()?->video_translation_title ?? $m->video_code),
                'Videos',
                route('content-video.edit', $m->video_id)
            );
        }
    }

    protected function searchSliders(string $q, array &$results): void
    {
        $items = ContentSlider::whereHas('translations', fn($b) => $b->where('slider_translation_title', 'LIKE', "%{$q}%"))
            ->with('translations')
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                $m->translations->first()?->slider_translation_title ?? "Slider #{$m->slider_id}",
                'Sliders',
                route('content-slider.edit', $m->slider_id)
            );
        }
    }

    protected function searchPublicHolidays(string $q, array &$results): void
    {
        $items = ContentPublicHoliday::where('public_holiday_title', 'LIKE', "%{$q}%")
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                $m->public_holiday_title,
                'Holidays',
                route('content-public-holiday.edit', $m->public_holiday_id)
            );
        }
    }

    protected function searchCalendars(string $q, array &$results): void
    {
        $items = ContentCalendar::whereHas('translations', fn($b) => $b->where('calendar_translation_title', 'LIKE', "%{$q}%"))
            ->with('translations')
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                $m->translations->first()?->calendar_translation_title ?? "Calendar #{$m->calendar_id}",
                'Calendar',
                route('content-calendar.edit', $m->calendar_id)
            );
        }
    }

    protected function searchDepartments(string $q, array &$results): void
    {
        $items = ContentDirectoryDepartment::where(function ($b) use ($q) {
                $b->where('directory_department_name', 'LIKE', "%{$q}%")
                  ->orWhere('directory_department_name_en', 'LIKE', "%{$q}%")
                  ->orWhere('directory_department_code', 'LIKE', "%{$q}%");
            })
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                ($m->directory_department_code ? "[{$m->directory_department_code}] " : '') . ($m->directory_department_name ?: $m->directory_department_name_en),
                'Departments',
                route('content-directory-department.edit', $m->directory_department_id)
            );
        }
    }

    protected function searchStaff(string $q, array &$results): void
    {
        $items = ContentDirectoryStaff::where(function ($b) use ($q) {
                $b->where('directory_staff_name', 'LIKE', "%{$q}%")
                  ->orWhere('directory_staff_email', 'LIKE', "%{$q}%")
                  ->orWhere('directory_staff_position', 'LIKE', "%{$q}%");
            })
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                $m->directory_staff_name . ($m->directory_staff_position ? " — {$m->directory_staff_position}" : ''),
                'Staff',
                route('content-directory-staff.edit', $m->directory_staff_id)
            );
        }
    }

    protected function searchFeedback(string $q, array &$results): void
    {
        $items = ContentFeedbackMain::where(function ($b) use ($q) {
                $b->where('ticket_no', 'LIKE', "%{$q}%")
                  ->orWhere('name', 'LIKE', "%{$q}%")
                  ->orWhere('email', 'LIKE', "%{$q}%")
                  ->orWhere('subject', 'LIKE', "%{$q}%");
            })
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                "[{$m->ticket_no}] {$m->name}" . ($m->subject ? " — {$m->subject}" : ''),
                'Feedback',
                route('content-feedback.edit', $m->id)
            );
        }
    }

    protected function searchBackendUsers(string $q, array &$results): void
    {
        $items = BackendUser::where(function ($b) use ($q) {
                $b->where('username', 'LIKE', "%{$q}%")
                  ->orWhere('email', 'LIKE', "%{$q}%")
                  ->orWhere('first_name', 'LIKE', "%{$q}%")
                  ->orWhere('last_name', 'LIKE', "%{$q}%");
            })
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                ($m->first_name || $m->last_name ? trim($m->first_name . ' ' . $m->last_name) . " ({$m->username})" : $m->username),
                'Backend Users',
                route('backend-user.edit', $m->id)
            );
        }
    }

    protected function searchFrontendUsers(string $q, array &$results): void
    {
        $items = FrontendUser::where(function ($b) use ($q) {
                $b->where('username', 'LIKE', "%{$q}%")
                  ->orWhere('email', 'LIKE', "%{$q}%")
                  ->orWhere('first_name', 'LIKE', "%{$q}%")
                  ->orWhere('last_name', 'LIKE', "%{$q}%");
            })
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                ($m->first_name || $m->last_name ? trim($m->first_name . ' ' . $m->last_name) . " ({$m->username})" : $m->username),
                'Frontend Users',
                route('frontend-user.edit', $m->id)
            );
        }
    }

    protected function searchRefs(string $q, array &$results): void
    {
        $items = Ref::where(function ($b) use ($q) {
                $b->where('cat', 'LIKE', "%{$q}%")
                  ->orWhere('code', 'LIKE', "%{$q}%")
                  ->orWhere('descr', 'LIKE', "%{$q}%")
                  ->orWhere('descr_en', 'LIKE', "%{$q}%");
            })
            ->limit($this->limit)
            ->get();
        foreach ($items as $m) {
            $this->add(
                $results,
                "[{$m->cat}] {$m->code} — {$m->descr}",
                'References',
                route('ref.edit', $m->id)
            );
        }
    }
}