Merge branch 'fix-orders-issues' into 'develop'
fix: change product order See merge request ldc-web/projets/boutique-cnc-sable!88
This commit is contained in:
commit
90af1eb37e
4 changed files with 92 additions and 41 deletions
|
|
@ -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'));
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,14 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Bo;
|
||||
|
||||
use App\Models\Product;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateProductOrderRequest extends FormRequest
|
||||
{
|
||||
private $product;
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
|
|
@ -19,6 +18,11 @@ class UpdateProductOrderRequest extends FormRequest
|
|||
return auth()->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')
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class RecalculateProductsOrder extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
DB::transaction(function () {
|
||||
$index = 1;
|
||||
DB::table('products')
|
||||
->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
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
@method('PATCH')
|
||||
<div class="mb-3">
|
||||
<label for="order-{{ $model->id }}" class="form-label">{{ __('Nouvelle position') }}</label>
|
||||
<input type="number" class="form-input" id="order-{{ $model->id }}" placeholder="1" name="order" min="1">
|
||||
<input type="number" class="form-input" id="order-{{ $model->id }}" placeholder="1" name="order" min="1" value="{{ $value ?? '' }}">
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">{{ _('Sauvegarder') }}</button>
|
||||
</form>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue