%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /www/loslex/test/app/Livewire/
Upload File :
Create Path :
Current File : /www/loslex/test/app/Livewire/ContestList.php

<?php

namespace App\Livewire;

use Livewire\Component;
use Livewire\Attributes\Session;
use App\Enums\ContestPublicationStatusEnum;
use App\Enums\RangeRegionEnum;
use App\Models\Contest;
use App\Models\ContestCategory;
use App\Models\ContestLevel;
use App\Models\Range;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;

class ContestList extends Component
{
    public $contests;

    #filters
    #[Session]
    public $fcHideHistory = true;
    #[Session]
    public $fcRegistration = false;
    #[Session]
    public $fcLevel;
    #[Session]
    public $fcRegion;
    #[Session]
    public $fcCategory;
    #[Session]
    public $fcRange;
    #[Session]
    public $fcName;
    #[Session]
    public $openFilters = false;

    public $filterCount;

    private function countFilters() {
        $cnt = 0;
        if (is_array($this->fcLevel)) { $cnt += count($this->fcLevel); }
        if (is_array($this->fcRegion)) { $cnt += count($this->fcRegion); }
        if (is_array($this->fcCategory)) { $cnt += count($this->fcCategory); }
        if (is_array($this->fcRange)) { $cnt += count($this->fcRange); }
        if (!empty($this->fcName)) { $cnt += 1; }

        $this->filterCount = $cnt;
    }

    private function decodeValue($val)
    {
        if (is_array($val)) {
            return $val;
        }

        if (is_array($json = json_decode($val))) {
            return $json;
        }

        if (is_array($arr = explode(',', $val))) {
            return $arr;
        }
    }

    public function mount()
    {
        global $request;
        $keys = ['rng','reg','lvl'];

        if (count(array_intersect($keys, $request->keys())) > 0) {
            $this->reset();

            foreach ($request->query() as $key => $val) {
                $decoded = $this->decodeValue($val);

                if (is_array($decoded)) {
                    switch ($key) {
                        case 'rng':
                            $this->fcRange = $decoded;
                            $this->openFilters = true;
                            break;

                        case 'reg':
                            $this->fcRegion = $decoded;
                            $this->openFilters = true;
                            break;

                        case 'lvl':
                            $this->fcLevel = $decoded;
                            $this->openFilters = true;
                            break;

                        default: break;
                    }
                }
            }

            redirect($request->url());
        }
    }

    public function clearFilter()
    {
        $this->reset();
        $this->dispatch('resetSelects');
    }

    public function render()
    {
        $this->countFilters();

        $this->contests = Contest::orderBy('date')->with(['range','organizer_group','contest_level','contest_category','registrations'])
                            //liveWire filters
                            ->when($this->fcHideHistory, function (Builder $query) { $dayshift = Carbon::today()->addDays(-3); $query->where('date', '>=', $dayshift); })
                            ->when($this->fcRegistration, function (Builder $query) { $now = Carbon::now(); $query->where([['registration_start', '<', $now],['registration_end', '>', $now]]); })
                            ->when($this->fcLevel, function (Builder $query, array $ids) { $query->whereIn('contest_level_id', $ids); })
                            ->when($this->fcRegion, function (Builder $query, array $ids) {
                                $rangeids = Range::select('id')->whereIn('region', $ids)->get();
                                $query->whereIn('range_id', $rangeids);
                            })
                            ->when($this->fcCategory, function (Builder $query, array $categories) { $query->whereIn('contest_category_id', $categories); })
                            ->when($this->fcRange, function (Builder $query, array $ranges) { $query->whereIn('range_id', $ranges); })
                            ->when($this->fcName, function (Builder $query, string $title) { $query->where('contestname', 'LIKE', "%{$title}%"); })
                            ->get();

        if (!Auth::user()?->is_admin){
            $this->contests = $this->contests->reject(function ($value) {
                if ($value->published == ContestPublicationStatusEnum::PUBLISHED) { return false; }        // not removing from list if user is admin
                if (Auth::user()?->is_organizer_member($value->organizer_group_id)) { return false; }      // not removing if user is organizer of the contest
                if ($value->published == ContestPublicationStatusEnum::HIDDEN                              // not removing if contest is hidden but user is registered (by admin/organizer)
                    && $value->registrations->where('user_id', Auth::user()?->id)->count() > 0) { return false; }
                return true;
            });
        }

        return view('livewire.contest-list', [
            'contests'      => $this->contests,
            'contestlevels' => ContestLevel::where('is_active', 1)->get(),
            'regions'       => RangeRegionEnum::cases(),
            'categories'    => ContestCategory::where('is_active', 1)->get(),
            'ranges'        => Range::where('is_active', 1)->get(),
        ]);
    }
}

Zerion Mini Shell 1.0