registry categories and entries movement added

minor entries filters update
master
Константин 2023-08-15 23:23:24 +03:00
parent 2b01d85371
commit 626d742cef
6 changed files with 53 additions and 20 deletions

View File

@ -31,9 +31,12 @@ class CategoriesController extends Controller {
}
public function move(Request $request, $id) {
public function move(Request $request, $id): JsonResponse {
$model = $this->model->byUuid($id)->firstOrFail();
return $model;
$registry = Registry::byUuid($request->get('registry'))->first();
$parent = Category::byUuid($request->get('category'))->first();
$model->move($request->get('ord'), $parent, $registry);
return fractal($model, new CategoryTransformer())->respond();
}

View File

@ -5,7 +5,6 @@ namespace App\Http\Controllers\Api\Registries;
use App\Http\Controllers\Controller;
use App\Models\Registries\Category;
use App\Models\Registries\Entry;
use App\Models\Registries\Registry;
use App\Services\Filters\FiltersService;
use App\Transformers\Registries\EntryTransformer;
use Illuminate\Http\JsonResponse;
@ -32,12 +31,15 @@ class EntriesController extends Controller {
return fractal($model, new EntryTransformer())->respond();
}
public function move(Request $request, $id) {
public function move(Request $request, $id): JsonResponse {
$model = $this->model->byUuid($id)->firstOrFail();
$category = Category::byUuid($request->get('category'))->first();
$model->update(['category_id' => $category->id ?? 0]);
return fractal($model, new EntryTransformer())->respond();
}
public function store(Request $request): void {
}

View File

@ -48,4 +48,33 @@ class Category extends Model {
public function move($ord, ?Category $parent = null, ?Registry $registry = null) {
$prevParent = $this->parent;
if (($parent->id ?? 0) === ($prevParent->id ?? 0)) {
($ord > $this->ord) ? $this->moveSet('backward', $this->ord, $ord, $parent) : $this->moveSet('forward', $ord, $this->ord, $parent);
} else $this->moveSet('forward', $ord, null, $parent);
$this->update(['parent_id' => $parent->id ?? 0, 'ord' => $ord]);
$this->trimIndexes([$prevParent->id ?? 0, $parent->id ?? 0]);
}
public function moveSet($dir = 'forward', $ordFrom = null, $ordTo = null, ?Category $parent = null) {
$query = $parent ? $parent->children() : $this->registry->rootCategories();
if ($ordFrom !== null) $query->where('ord', '>=', $ordFrom);
if ($ordTo !== null) $query->where('ord', '<=', $ordTo);
$query->get()->each(function($category) use($dir) {
$category->update(['ord' => ($dir === 'forward') ? ($category->ord + 1) : ($category->ord - 1)]);
});
}
public function trimIndexes($parentIds) {
collect(is_array($parentIds) ? $parentIds : [$parentIds])->unique()->each(function($parentId) {
$this->registry->categories()->where(['parent_id' => $parentId])->orderBy('ord')->orderBy('id')->get()->each(function($category, $index) {
if ($category->ord !== $index) $category->update(['ord' => $index]);
});
});
}
public function getMaxOrd(): int {
$res = $this->parent ? $this->parent->children()->max('ord') : $this->registry->rootCategories()->max('ord');
return ($res !== null) ? ($res + 1) : 0;
}
}

View File

@ -6,7 +6,6 @@ use App\Models\Asset;
use App\Support\HasObjectsTrait;
use App\Support\RelationValuesTrait;
use App\Support\UuidScopeTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
@ -127,6 +126,4 @@ class Entry extends Model {
});
}
}

View File

@ -31,18 +31,17 @@ class EntryFilters extends FiltersService {
[
'name' => 'common',
'title' => 'Общие параметры',
'fields' => $this->nativeFields($filters)
'fields' => $this->nativeFields($filters, $registry)
]
];
if ($types) $groups[] = ['name' => 'properties', 'title' => 'Дополнительные характеристики', 'fields' => $this->objectFields($filters)];
//if ($v = $registry->options['properties'] ?? null) $groups[] = ['name' => 'properties', 'title' => 'Особые характеристики', 'fields' => $this->propertiesFields($v, $filters)];
$query = Entry::query();
$this->applyFilters($query, $filters->put('fake', 'zzz'));
return ['groups' => ['data' => $groups], 'total' => $query->count()];
}
public function nativeFields(Collection $filters): array {
return [
public function nativeFields(Collection $filters, Registry $registry): array {
$fields = [
[
'name' => 'registry',
'title' => 'Реестр',
@ -56,15 +55,17 @@ class EntryFilters extends FiltersService {
'type' => FieldType::RELATION,
'hidden' => true,
'value' => $filters->get('types') ? fractal(ObjectType::whereIn('uuid', $filters->get('types'))->get(), new ObjectTypeTransformer()) : null
],
[
'name' => 'state',
'title' => 'Статус',
'type' => FieldType::RELATION,
'represented' => $this->getRelationItems(EntryState::TITLES),
'value' => $this->getRelationValue($filters->get('state'), EntryState::TITLES)
],
]
];
if ($registry->options['states'] ?? null) $fields[] = [
'name' => 'state',
'title' => 'Статус',
'type' => FieldType::RELATION,
'represented' => $this->getRelationItems(EntryState::TITLES),
'value' => $this->getRelationValue($filters->get('state'), EntryState::TITLES)
];
return $fields;
}
public function objectFields(Collection $filters): array {

View File

@ -40,6 +40,7 @@ class CategoryForms extends FormsService {
$parent = Category::byUuid($data['parent'] ?? null)->first();
$data['parent_id'] = $parent->id ?? 0;
$model = $registry->categories()->create($data);
$model->update(['ord' => $model->getMaxOrd()]);
return fractal($model, new CategoryTransformer())->respond();
}