diff --git a/app/Http/Controllers/Bo/ProductsController.php b/app/Http/Controllers/Bo/ProductsController.php index bbd8a5d7..fee2b36b 100644 --- a/app/Http/Controllers/Bo/ProductsController.php +++ b/app/Http/Controllers/Bo/ProductsController.php @@ -145,9 +145,9 @@ class ProductsController extends Controller $weight_variable = $request->weight_variable; $status = $request->status; $orderable_start = $request->orderable_start_date === null ? null - : $request->orderable_start_date . " " . $request->orderable_start_time; + : $request->orderable_start_date . " " . $request->orderable_start_time; $orderable_end = $request->orderable_end_date === null ? null - : $request->orderable_end_date . " " . $request->orderable_end_time; + : $request->orderable_end_date . " " . $request->orderable_end_time; $star = $request->star; $slug = Str::slug($name); $rangeId = $request->range_id; @@ -584,24 +584,24 @@ class ProductsController extends Controller if (File::exists(storage_path($incoImage))) { $imagePath = storage_path($incoImage); // Redimensionne. - // Create mask. - // Resize original image into mask. - $this->resizeImg( - $imagePath, - config('theme.product_image_large_maxwidth'), - config('theme.product_image_large_maxheight'), - File::name($incoImage) . "_large.png" - ); + // Create mask. + // Resize original image into mask. + $this->resizeImg( + $imagePath, + config('theme.product_image_large_maxwidth'), + config('theme.product_image_large_maxheight'), + File::name($incoImage) . "_large.png" + ); - $this->resizeImg( - $imagePath, - config('theme.product_image_maxwidth'), - config('theme.product_image_maxheight'), - File::name($incoImage) . "_small.png" - ); + $this->resizeImg( + $imagePath, + config('theme.product_image_maxwidth'), + config('theme.product_image_maxheight'), + File::name($incoImage) . "_small.png" + ); - $preparedImages[] = "inco/images/" . File::name($incoImage) . "_large.png"; + $preparedImages[] = "inco/images/" . File::name($incoImage) . "_large.png"; }//end if }//end foreach return $preparedImages; @@ -871,31 +871,40 @@ class ProductsController extends Controller } /** - * Update product order. + * Swap product order with the product at the target order. * * @param UpdateProductOrderRequest $request * @param Product $product - * @return void + * @return \Illuminate\Http\RedirectResponse */ public function changeSelectedOrder(UpdateProductOrderRequest $request, Product $product) { - // Laravel 8 style ... validated() never returns a string. - $validated = $request->validated(); - $newOrder = $validated['order']; + $newOrder = (int) $request->getOrder(); + $oldOrder = (int) $product->order; - return DB::transaction(function () use ($product, $newOrder) { - // Update all products who will be moved with this order update. - // Includes soft deleted to avoid problems if we restore them. - $request = Product::withTrashed() + if ($oldOrder === $newOrder) { + return redirect()->back(); + } + + return DB::transaction(function () use ($product, $oldOrder, $newOrder) { + $targetProduct = Product::withTrashed() + ->where('order', $newOrder) ->where('id', '!=', $product->id) - ->whereBetween('order', [min($product->order, $newOrder), max($product->order, $newOrder)]); - $product->order < $newOrder ? $request->decrement('order', 1) : $request->increment('order', 1); + ->first(); - // Update the product. - $product->order = $newOrder; - if (!$product->saveOrFail()) { - return back()->with('error', trans('crud.messages.order_not_changed')); - }; + if ($targetProduct) { + $targetProduct->order = -1; + $targetProduct->saveOrFail(); + + $product->order = $newOrder; + $product->saveOrFail(); + + $targetProduct->order = $oldOrder; + $targetProduct->saveOrFail(); + } else { + $product->order = $newOrder; + $product->saveOrFail(); + } return redirect()->back()->with('success', trans('crud.messages.order_changed')); }); diff --git a/app/Http/Requests/Bo/UpdateProductOrderRequest.php b/app/Http/Requests/Bo/UpdateProductOrderRequest.php index cc01750b..98613707 100644 --- a/app/Http/Requests/Bo/UpdateProductOrderRequest.php +++ b/app/Http/Requests/Bo/UpdateProductOrderRequest.php @@ -1,14 +1,13 @@ check(); } + public function getOrder(): int + { + return (int) $this->input('order'); + } + /** * Prepare the data for validation. * @@ -26,11 +30,8 @@ class UpdateProductOrderRequest extends FormRequest */ protected function prepareForValidation(): void { - $this->product = $this->route('product'); - $max = Product::query()->max('order'); - $this->merge([ - 'order' => min($this->input('order'), $max) + 'order' => $this->input('order') ]); } diff --git a/database/migrations/2026_10_07_100650_recalculate_products_order.php b/database/migrations/2026_10_07_100650_recalculate_products_order.php new file mode 100644 index 00000000..f92fc87d --- /dev/null +++ b/database/migrations/2026_10_07_100650_recalculate_products_order.php @@ -0,0 +1,41 @@ +select('id') + ->orderBy('order', 'asc') + ->orderBy('id', 'asc') + ->cursor() + ->each(function ($product) use (&$index) { + DB::table('products') + ->where('id', $product->id) + ->update(['order' => $index++]); + }); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // DO nothing + } +} diff --git a/resources/views/bo/modules/change-model-order-select.blade.php b/resources/views/bo/modules/change-model-order-select.blade.php index acfd6a0d..fce70985 100644 --- a/resources/views/bo/modules/change-model-order-select.blade.php +++ b/resources/views/bo/modules/change-model-order-select.blade.php @@ -12,7 +12,7 @@ @method('PATCH')
- +