user(); $depCollections = $customer->getOrCreateAllDepartaimentsCollections(); $departaimants = $this->getUserDepartaimantsMaps($depCollections); return view('club.pages.departaimants.index', compact('departaimants')); } /** * Save Customer collection state * * @throws \RuntimeException If a collection type is missing. */ public function bourseSaveState( SaveCustomerCollectionRequest $request, DepartaimantCollectionType $collectiontype ): JsonResponse { /** @var \App\Models\Customer */ $customer = Auth::guard('club')->user(); /** @var \App\Models\DepartaimantsCollection $departaimantCollection */ $departaimantCollection = $customer->departaimantsCollections() ->where('type', $collectiontype->value())->firstOrFail(); $depStatus = DepartaimantStatus::make($request->integer('state')); /** @var string|null */ $depEnumClass = Arr::get($request->maps, $collectiontype->value(), null); if (! $depEnumClass) { throw new \RuntimeException('Missing ' . $collectiontype->value() . 'from request'); } /** @var \App\Enums\Departaimants\IDepartaimant */ $departaimant = forward_static_call( [$depEnumClass, 'make'], $request->input('id') ); $departaimantCollection->set($departaimant, $depStatus); $departaimantCollection->saveOrFail(); $depCollections = $customer->getOrCreateAllDepartaimentsCollections(); return response()->json([ 'status' => 'OK', 'map' => $this->getUserDepartaimantsMaps($depCollections), ]); } /** * Summary of bourseSearch * * @return \Illuminate\Http\JsonResponse * phpcs:disable Generic.Metrics.CyclomaticComplexity */ public function bourseSearch( SearchCustomerCollectionRequest $request, DepartaimantCollectionType $collectiontype ): JsonResponse { // @phpcs:enable $itemsPerPage = 15; $output = collect(); $state = DepartaimantStatus::make((int) $request->input('state')); $pseudo = $request->string('pseudo', ''); $departaimant = Utils::replaceSpecChar( str_replace(['-', ' ', '\''], '', strtolower($request->string('departaimant', ''))) ); if ($state->equals(DepartaimantStatus::gives)) { $status = DepartaimantStatus::wants; } elseif ($state->equals(DepartaimantStatus::wants)) { $status = DepartaimantStatus::gives; } else { return response()->json([ 'status' => 'KO', 'message' => 'Message cannot search with other status than wants or gives', ]); } /** @var \App\Models\Customer */ $customer = Auth::guard('club')->user(); $ids = collect(BlockList::query()->select('customer_id', 'blocked_id') ->whereHas('customer', fn (Builder $q) => $q->where('id', $customer->getKey())) ->orWhereHas('blocked', fn (Builder $q) => $q->where('id', $customer->getKey())) ->get()->toArray()); $ids = $ids->flatten()->unique(); $userDep = $customer->departaimantsCollections()->where('type', $collectiontype->value)->first(); $userDepStatus = []; foreach ($userDep->getAllDepartaimants() as $dep) { if ($dep->status->value === $state->value) { // * hack to prevent php to cast array key to int $userDepStatus['.' . $dep->departaimant->value] = $dep->departaimant; } } $req = DepartaimantsCollection::getDepartaimantsCollectionWithStatus($userDepStatus, $status, $collectiontype) ->with('customer'); $req->whereHas('customer', function (Builder $query) use ($pseudo, $ids) { // * Handle blocked ids if ($ids->count()) { $query->whereNotIn('id', $ids); } // * Filter with Pseudo if (strlen($pseudo)) { $query->where('username', 'LIKE', "%{$pseudo}%"); } }); $totalCount = $req->count(); $req->orderBy('updated_at', 'DESC'); $req->offset($itemsPerPage * ($request->input('page') - 1))->limit($itemsPerPage); /** @var \App\Models\DepartaimantsCollection $depCollection */ foreach ($req->get() as $depCollection) { foreach ($depCollection->getAllDepartaimantsWithStatus($collectiontype, $status) as $dep) { if ( $dep->status->equals($status) // * Filter results with Departaimant and (strlen($departaimant) ? strpos( strtolower(Utils::replaceSpecChar( str_replace(['-', ' ', '\''], '', $dep->departaimant->value() . $dep->departaimant->label()) )), $departaimant ) !== false : true) // * Filter results with customer Departaimants and array_key_exists('.' . $dep->departaimant->value(), $userDepStatus) ) { $output->add([ 'departaimantCode' => $dep->departaimant->value(), 'departaimant' => $dep->departaimant->label(), 'username' => $depCollection->customer->username, 'hashcode' => $depCollection->customer->hashcode, 'date' => (new NiceDate())->get(null, '', $depCollection->updated_at, []), ]); } } // end foreach } // end foreach // Paginate. $depCollectionPaginate = $this->paginateManually( $totalCount, $output->sortBy('date')->values(), $itemsPerPage, $request->input('page') ); return response()->json([ 'status' => 'OK', 'data' => $depCollectionPaginate, ]); } /** * Undocumented function */ private function getUserDepartaimantsMaps(Collection $departaimantsCollection): array { $datas = $departaimantsCollection->mapWithKeys(function ($collection) { $datas = []; foreach ($collection->getAllDepartaimants() as $departaimantItem) { $datas[$collection->type->value][] = [ 'id' => $departaimantItem->departaimant->value(), 'name' => $departaimantItem->departaimant->label(), 'state' => $departaimantItem->status->value(), ]; } return $datas; })->toArray(); return $datas; } /** * Paginate a collection. */ private function paginateManually( int $totalCount, Collection $results, int $pageSize, int $page = 1 ): LengthAwarePaginator { return $this->paginator($results, $totalCount, $pageSize, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => 'page', ]); } /** * Create a new length-aware paginator instance. */ protected function paginator( Collection $items, int $total, int $perPage, int $currentPage, array $options ): LengthAwarePaginator { return Container::getInstance()->makeWith(LengthAwarePaginator::class, [ 'items' => $items, 'total' => $total, 'perPage' => $perPage, 'currentPage' => $currentPage, 'options' => $options, ]); } }