validated(); // Get the class of the target model and verify its existence. $modelClass = $this->getClassFromTableName($validated['model']); if (!$modelClass) { return response()->json(['error' => 'Invalid model class'], 404); } // Determine the field to display. $field = $validated['field'] ?? null; if (!$field) { $model = new $modelClass(); $field = collect(['title', 'name', 'label']) ->first(fn($f) => in_array($f, $model->getFillable(), true)) ?? $model->getKeyName(); } // Fetch all cached models once. $allItems = $modelClass::getModelsCached(); // Apply search and exclude filters $items = $allItems ->when(!empty($validated['exclude']), function ($collection) use ($validated) { return $collection->reject(fn($item) => $item->id == $validated['exclude']); }) ->when(!empty($validated['search']), function ($collection) use ($validated, $field) { return $collection->filter(fn($item) => is_string($value = data_get($item, $field)) && Str::contains(Str::lower($value), Str::lower($validated['search']))); }) ->sortBy(fn($item) => data_get($item, $field)) ->values(); // Paginate manually. $itemsPaginate = CollectionHelper::paginateManually( $items, $validated['pageSize'], $validated['page'] ); // Get selected model. $selectedValue = $allItems->firstWhere('id', $validated['selectedId']); return response()->json([ 'items' => $itemsPaginate, 'field' => $field, 'model' => $selectedValue, ]); } /** * Get access models. * * @param \App\Http\Requests\Back\ModelAccessRequest $request * @return \Illuminate\Http\JsonResponse */ public function modelAccess(ModelAccessRequest $request): \Illuminate\Http\JsonResponse { $validated = $request->validated(); $modelClass = $this->getClassFromTableName($validated['modelName']); if (!$modelClass) { return response()->json(['error' => 'Invalid model class'], 404); } $url = (method_exists($modelClass, 'getAccessModel')) ? $modelClass::getAccessModel($validated['selectedEntry']) : static::getAccessModel($validated['selectedEntry'], $validated['modelName']); $token = Crypt::encryptString(json_encode([ 'modelName' => $validated['modelName'], 'modelId' => $validated['selectedEntry'], ])); return response()->json([ 'url' => $url, 'token' => $token, ]); } /** * Get the url to access a specific resource. * * @param integer $modelId Id of the target model. * @param integer $modelName Name of the target model. * @return string */ private function getAccessModel(int $modelId, string $modelName): string { $targetModel = $this->getClassFromTableName($modelName)::getModelsCached()->firstWhere('id', $modelId); if (!$targetModel) { return null; } return route("front.$modelName.show", $targetModel->slug); } /** * Retrieve some data from specific token. * * @param \App\Http\Requests\Back\RetrieveDataFromTokenRequest $request * @return \Illuminate\Http\JsonResponse */ public function retrieveDataFromToken(RetrieveDataFromTokenRequest $request): \Illuminate\Http\JsonResponse { $validated = $request->validated(); $payload = json_decode(Crypt::decryptString($validated['token']), true); $modelClass = $this->getClassFromTableName($payload['modelName']); $modelId = null; if ($modelClass && $modelClass::getModelsCached()->contains('id', $payload['modelId'])) { $modelId = $payload['modelId']; } return response()->json([ 'modelName' => $payload['modelName'], 'modelId' => $modelId, ]); } /** * Upload a file sent in a request. * Used for picture upload in CKEditor. * * @param \App\Http\Requests\Back\UploadFileRequest $request * @return \Illuminate\Http\JsonResponse */ public function uploadFile(UploadFileRequest $request): \Illuminate\Http\JsonResponse { // Get the class of the target model and verify its existence. $modelClass = "App\\Models\\$request->folder"; if (!class_exists($modelClass)) { return response()->json(['error' => 'Invalid model class'], 404); } // Get a new instance of the model. $model = new $modelClass(); /** @var \Illuminate\Http\UploadedFile $image Get the file from the request data. */ $image = $request->file('upload'); // Store the uploaded file. $path = FileHelper::storeFile($model, $image, true, private: false); // Return the path of the file. return response()->json(['url' => "/{$path}"]); } }