parent
2b01d85371
commit
626d742cef
|
|
@ -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();
|
$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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ namespace App\Http\Controllers\Api\Registries;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Models\Registries\Category;
|
use App\Models\Registries\Category;
|
||||||
use App\Models\Registries\Entry;
|
use App\Models\Registries\Entry;
|
||||||
use App\Models\Registries\Registry;
|
|
||||||
use App\Services\Filters\FiltersService;
|
use App\Services\Filters\FiltersService;
|
||||||
use App\Transformers\Registries\EntryTransformer;
|
use App\Transformers\Registries\EntryTransformer;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
@ -32,12 +31,15 @@ class EntriesController extends Controller {
|
||||||
return fractal($model, new EntryTransformer())->respond();
|
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 {
|
public function store(Request $request): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ use App\Models\Asset;
|
||||||
use App\Support\HasObjectsTrait;
|
use App\Support\HasObjectsTrait;
|
||||||
use App\Support\RelationValuesTrait;
|
use App\Support\RelationValuesTrait;
|
||||||
use App\Support\UuidScopeTrait;
|
use App\Support\UuidScopeTrait;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||||
|
|
@ -127,6 +126,4 @@ class Entry extends Model {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,18 +31,17 @@ class EntryFilters extends FiltersService {
|
||||||
[
|
[
|
||||||
'name' => 'common',
|
'name' => 'common',
|
||||||
'title' => 'Общие параметры',
|
'title' => 'Общие параметры',
|
||||||
'fields' => $this->nativeFields($filters)
|
'fields' => $this->nativeFields($filters, $registry)
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
if ($types) $groups[] = ['name' => 'properties', 'title' => 'Дополнительные характеристики', 'fields' => $this->objectFields($filters)];
|
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();
|
$query = Entry::query();
|
||||||
$this->applyFilters($query, $filters->put('fake', 'zzz'));
|
$this->applyFilters($query, $filters->put('fake', 'zzz'));
|
||||||
return ['groups' => ['data' => $groups], 'total' => $query->count()];
|
return ['groups' => ['data' => $groups], 'total' => $query->count()];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function nativeFields(Collection $filters): array {
|
public function nativeFields(Collection $filters, Registry $registry): array {
|
||||||
return [
|
$fields = [
|
||||||
[
|
[
|
||||||
'name' => 'registry',
|
'name' => 'registry',
|
||||||
'title' => 'Реестр',
|
'title' => 'Реестр',
|
||||||
|
|
@ -56,15 +55,17 @@ class EntryFilters extends FiltersService {
|
||||||
'type' => FieldType::RELATION,
|
'type' => FieldType::RELATION,
|
||||||
'hidden' => true,
|
'hidden' => true,
|
||||||
'value' => $filters->get('types') ? fractal(ObjectType::whereIn('uuid', $filters->get('types'))->get(), new ObjectTypeTransformer()) : null
|
'value' => $filters->get('types') ? fractal(ObjectType::whereIn('uuid', $filters->get('types'))->get(), new ObjectTypeTransformer()) : null
|
||||||
],
|
]
|
||||||
[
|
];
|
||||||
|
if ($registry->options['states'] ?? null) $fields[] = [
|
||||||
'name' => 'state',
|
'name' => 'state',
|
||||||
'title' => 'Статус',
|
'title' => 'Статус',
|
||||||
'type' => FieldType::RELATION,
|
'type' => FieldType::RELATION,
|
||||||
'represented' => $this->getRelationItems(EntryState::TITLES),
|
'represented' => $this->getRelationItems(EntryState::TITLES),
|
||||||
'value' => $this->getRelationValue($filters->get('state'), EntryState::TITLES)
|
'value' => $this->getRelationValue($filters->get('state'), EntryState::TITLES)
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
return $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function objectFields(Collection $filters): array {
|
public function objectFields(Collection $filters): array {
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ class CategoryForms extends FormsService {
|
||||||
$parent = Category::byUuid($data['parent'] ?? null)->first();
|
$parent = Category::byUuid($data['parent'] ?? null)->first();
|
||||||
$data['parent_id'] = $parent->id ?? 0;
|
$data['parent_id'] = $parent->id ?? 0;
|
||||||
$model = $registry->categories()->create($data);
|
$model = $registry->categories()->create($data);
|
||||||
|
$model->update(['ord' => $model->getMaxOrd()]);
|
||||||
return fractal($model, new CategoryTransformer())->respond();
|
return fractal($model, new CategoryTransformer())->respond();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue