user(); /** @var \Illuminate\Database\Eloquent\Collection */ $depCollections = $customer->getOrCreateAllDepartaimentsCollections(); $departaimants = json_encode($this->getUserDepartaimantsMaps($depCollections)); return view('front.club.bourse', compact('departaimants')); } public function bourseSaveState(SaveCustomerCollectionRequest $request, DepartaimantCollectionType $type) { /** @var \App\Models\Customers */ $user = Auth::guard('customer')->user(); /** @var \App\Models\DepartaimantsCollection $departaimant */ $departaimant = $user->departaimantsCollections() ->where('type', $type->value)->firstOrFail(); $depStatus = DepartaimantStatus::make((int)$request->state); $departaimant->set( forward_static_call([$request->maps[$type->value], 'make'], $request->id), $depStatus ); $departaimant->saveOrFail(); return response()->json([ 'status' => 'OK', 'map' => $this->getUserDepartaimantsMaps($user->departaimantsCollections) ]); } public function bourseSearch(SearchCustomerCollectionRequest $request, DepartaimantCollectionType $type) { $output = collect(); $state = DepartaimantStatus::make((int)$request->state); $pseudo = $request->pseudo; $departaimant = str_replace(['-', ' ', '\''], '', strtolower($request->departaimant ?? '')); $departaimant = Utils::replaceSpecChar($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\Customers */ $customer = Auth::guard('customer')->user(); $ids = collect(BlockList::select('customer_id', 'blocked_id') ->where('customer_id', $customer->id) ->orWhere('blocked_id', $customer->id)->get()->toArray()); $ids = $ids->flatten()->unique()->all(); $userDep = $customer->departaimantsCollections()->where('type', $type->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, $type) ->with('customer'); $req->whereHas('customer', function ($query) use ($pseudo, $ids) { // handle blocked ids if (count($ids)) { $query->where('id', 'NOT IN', implode(',', $ids)); } // Filter with Pseudo if ($pseudo !== null) { $query->where('username', 'LIKE', "%$pseudo%"); } }); $req->orderBy('updated_at', 'DESC'); /** @var \Illuminate\Database\Eloquent\Collection $depsCollection */ $depsCollection = $req->simplePaginate(15); /** @var \App\Models\DepartaimantsCollection $depCollection */ foreach ($depsCollection as $depCollection) { foreach ($depCollection->getAllDepartaimantsWithStatus($type, $status) as $dep) { $dep = (object)$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 $output->sortBy('date'); return response()->json(['status' => 'OK', 'data' => $output->values(), 'page' => 1]); } /** * Undocumented function * * @param Collection $departaimantsCollection * @return array */ 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; } }