v1.2.2
- Subcategories can now be reordered within their category — up/down arrow buttons next to "Edytuj" in Admin > Kategorie. The order set there is used everywhere a subcategory list is shown (pickers, admin listings, etc.).
This commit is contained in:
@@ -316,10 +316,55 @@ class Panel extends Component
|
||||
return;
|
||||
}
|
||||
|
||||
Category::query()->find($categoryId)?->subcategories()->create(['name' => $name]);
|
||||
$category = Category::query()->find($categoryId);
|
||||
|
||||
if (! $category) {
|
||||
return;
|
||||
}
|
||||
|
||||
$nextPosition = ((int) $category->subcategories()->max('sort_order')) + 1;
|
||||
$category->subcategories()->create(['name' => $name, 'sort_order' => $nextPosition]);
|
||||
$this->newSubNames[$categoryId] = '';
|
||||
}
|
||||
|
||||
public function moveSubcategoryUp(int $subcategoryId): void
|
||||
{
|
||||
$this->swapSubcategoryOrder($subcategoryId, -1);
|
||||
}
|
||||
|
||||
public function moveSubcategoryDown(int $subcategoryId): void
|
||||
{
|
||||
$this->swapSubcategoryOrder($subcategoryId, 1);
|
||||
}
|
||||
|
||||
protected function swapSubcategoryOrder(int $subcategoryId, int $direction): void
|
||||
{
|
||||
$sub = Subcategory::query()->find($subcategoryId);
|
||||
|
||||
if (! $sub) {
|
||||
return;
|
||||
}
|
||||
|
||||
$siblings = Subcategory::query()
|
||||
->where('category_id', $sub->category_id)
|
||||
->orderBy('sort_order')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
$idx = $siblings->search(fn (Subcategory $s) => $s->id === $sub->id);
|
||||
$swapIdx = $idx + $direction;
|
||||
|
||||
if ($idx === false || $swapIdx < 0 || $swapIdx >= $siblings->count()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$other = $siblings[$swapIdx];
|
||||
[$order, $otherOrder] = [$sub->sort_order, $other->sort_order];
|
||||
|
||||
$sub->update(['sort_order' => $otherOrder]);
|
||||
$other->update(['sort_order' => $order]);
|
||||
}
|
||||
|
||||
public function openSubcategoryEditForm(int $subcategoryId): void
|
||||
{
|
||||
$sub = Subcategory::query()->with('customFields')->findOrFail($subcategoryId);
|
||||
|
||||
Reference in New Issue
Block a user