120 lines
4.3 KiB
PHP
120 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Back;
|
|
|
|
use App\Http\Requests\Back\ModelListRequest;
|
|
use App\Http\Requests\Back\UploadFileRequest;
|
|
use Illuminate\Support\Str;
|
|
use Uwil\LaravelCommon\Controllers\Controller;
|
|
use Uwil\LaravelCommon\Helpers\CollectionHelper;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
|
|
class InputsController extends Controller
|
|
{
|
|
/**
|
|
* Get a paginate collection from specific model.
|
|
*
|
|
* @param \App\Http\Requests\Back\ModelListRequest $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function modelList(ModelListRequest $request): \Illuminate\Http\JsonResponse
|
|
{
|
|
// Validated data from request.
|
|
$validated = $request->validated();
|
|
|
|
// Get target model.
|
|
$table = $validated['model'];
|
|
|
|
// Get the class of the target model and verify its existence.
|
|
$class = '\\App\\Models\\' . Str::studly(Str::singular($table));
|
|
$modelClass = class_exists($class) ? $class : null;
|
|
if (!$modelClass) {
|
|
return response()->json(['error' => 'Invalid model class'], 404);
|
|
}
|
|
|
|
// Check if there is a field defined.
|
|
if (!array_key_exists('field', $validated) || empty($validated['field'])) {
|
|
// Get a new instance of the model.
|
|
$model = new $modelClass();
|
|
|
|
// List of priority fields.
|
|
$possibleFields = ['title', 'name', 'label'];
|
|
|
|
// Check the presence of priority field in fillable fields.
|
|
$field = collect($possibleFields)->first(fn($f) => in_array($f, $model->getFillable()));
|
|
|
|
// Fallback on model key name.
|
|
if (!$field) {
|
|
$field = $model->getKeyName();
|
|
}
|
|
} else {
|
|
$field = $validated['field'];
|
|
}
|
|
|
|
// Filter all models in the list containing the searched value if any,
|
|
// except specific id if any, and sort them.
|
|
$search = $validated['search'];
|
|
$exclude = $validated['exclude'];
|
|
$items = $modelClass::getModelsCached()
|
|
->when(!empty($exclude), function ($query) use ($exclude) {
|
|
return $query->reject(fn($item) => $item->id == $exclude);
|
|
})
|
|
->when(!empty($search), function ($query) use ($field, $search) {
|
|
return $query->filter(function ($item) use ($field, $search) {
|
|
$value = data_get($item, $field);
|
|
if (!is_string($value)) {
|
|
return false;
|
|
}
|
|
return Str::contains(Str::lower($value), Str::lower($search));
|
|
});
|
|
})
|
|
->sortBy(function ($item) use ($field) {
|
|
return data_get($item, $field);
|
|
})
|
|
->values();
|
|
|
|
// Set a LengthAwarePagination collection.
|
|
$page = $validated['page'];
|
|
$pageSize = $validated['pageSize'];
|
|
$itemsPaginate = CollectionHelper::paginateManually($items, $pageSize, $page);
|
|
|
|
// Get the selected value.
|
|
$selectedId = $validated['selectedId'];
|
|
$selectedValue = $modelClass::getModelsCached()->firstWhere('id', $selectedId);
|
|
|
|
// Return the pagination with the target field.
|
|
return response()->json([
|
|
'items' => $itemsPaginate,
|
|
'field' => $field,
|
|
'model' => $selectedValue ?? null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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}"]);
|
|
}
|
|
}
|