diff --git a/app/Http/Controllers/Api/Registries/CategoriesController.php b/app/Http/Controllers/Api/Registries/CategoriesController.php index c02ff12..af04a78 100644 --- a/app/Http/Controllers/Api/Registries/CategoriesController.php +++ b/app/Http/Controllers/Api/Registries/CategoriesController.php @@ -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(); } diff --git a/app/Http/Controllers/Api/Registries/EntriesController.php b/app/Http/Controllers/Api/Registries/EntriesController.php index b031c0a..d8e0448 100644 --- a/app/Http/Controllers/Api/Registries/EntriesController.php +++ b/app/Http/Controllers/Api/Registries/EntriesController.php @@ -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 { } diff --git a/app/Models/Registries/Category.php b/app/Models/Registries/Category.php index 698e292..274b54d 100644 --- a/app/Models/Registries/Category.php +++ b/app/Models/Registries/Category.php @@ -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; + } + + } diff --git a/app/Models/Registries/Entry.php b/app/Models/Registries/Entry.php index 8c8c542..6af622d 100644 --- a/app/Models/Registries/Entry.php +++ b/app/Models/Registries/Entry.php @@ -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 { }); } - - } diff --git a/app/Services/Filters/Registries/EntryFilters.php b/app/Services/Filters/Registries/EntryFilters.php index 25db78b..92202e0 100644 --- a/app/Services/Filters/Registries/EntryFilters.php +++ b/app/Services/Filters/Registries/EntryFilters.php @@ -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 { diff --git a/app/Services/Forms/Registries/CategoryForms.php b/app/Services/Forms/Registries/CategoryForms.php index 53914dc..3c9e7ba 100644 --- a/app/Services/Forms/Registries/CategoryForms.php +++ b/app/Services/Forms/Registries/CategoryForms.php @@ -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(); }