Positions Updated

This commit is contained in:
Devansh 2022-02-02 12:09:29 +05:30
parent c9a4a6c4f1
commit 5317fedd21
3 changed files with 44 additions and 29 deletions

View File

@ -55,11 +55,29 @@
<script type="text/x-template" id="product-image-item-template">
<label class="image-item" v-bind:class="{ 'has-image': imageData.length > 0 }">
<input type="hidden" :name="'images[' + image.id + ']'" v-if="! new_image"/>
<input
type="hidden"
:name="'images[files][' + image.id + ']'"
v-if="! new_image"/>
<input type="file" v-validate="'mimes:image/*'" accept="image/*" :name="'images[]'" ref="imageInput" :id="_uid" @change="addImageView($event)" multiple="multiple"/>
<input
type="hidden"
:name="'images[positions][' + image.id + ']'"/>
<img class="preview" :src="imageData" v-if="imageData.length > 0">
<input
:id="_uid"
ref="imageInput"
type="file"
:name="'images[files][]'"
accept="image/*"
multiple="multiple"
v-validate="'mimes:image/*'"
@change="addImageView($event)"/>
<img
class="preview"
:src="imageData"
v-if="imageData.length > 0">
<label class="remove-image" @click="removeImage()">
{{ __('admin::app.catalog.products.remove-image-btn-title') }}

View File

@ -73,7 +73,8 @@ class ProductForm extends FormRequest
$this->rules = array_merge($product->getTypeInstance()->getTypeValidationRules(), [
'sku' => ['required', 'unique:products,sku,' . $this->id, new Slug],
'url_key' => ['required', new ProductCategoryUniqueSlug('product_flat', $this->id)],
'images.*' => ['nullable', 'mimes:bmp,jpeg,jpg,png,webp'],
'images.files.*' => ['nullable', 'mimes:bmp,jpeg,jpg,png,webp'],
'images.positions.*' => ['nullable', 'integer'],
'videos.*' => ['nullable', 'mimetypes:application/octet-stream,video/mp4,video/webm,video/quicktime', 'max:' . $maxVideoFileSize],
'special_price_from' => ['nullable', 'date'],
'special_price_to' => ['nullable', 'date', 'after_or_equal:special_price_from'],

View File

@ -80,28 +80,41 @@ class ProductImageRepository extends Repository
{
$previousImageIds = $product->images()->pluck('id');
if ($images) {
foreach ($images as $imageId => $image) {
if (
isset($images['files']) && $images['files']
&& isset($images['positions']) && $images['positions']
) {
/**
* Filter out new images because position already setuped by index.
*/
$imagePositions = collect($images['positions'])->keys()->filter(function ($imagePosition) {
return is_numeric($imagePosition);
});
foreach ($images['files'] as $indexOrImageId => $image) {
if ($image instanceof UploadedFile) {
$this->create([
'path' => $image->store($this->getProductDirectory($product)),
'product_id' => $product->id,
'position' => $indexOrImageId,
]);
} else {
if (is_numeric($index = $previousImageIds->search($imageId))) {
$this->update([
'position' => $imagePositions->search($indexOrImageId),
], $indexOrImageId);
if (is_numeric($index = $previousImageIds->search($indexOrImageId))) {
$previousImageIds->forget($index);
}
$this->updateImagePosition($imageId);
}
}
}
foreach ($previousImageIds as $imageId) {
if ($image = $this->find($imageId)) {
foreach ($previousImageIds as $indexOrImageId) {
if ($image = $this->find($indexOrImageId)) {
Storage::delete($image->path);
$this->delete($imageId);
$this->delete($indexOrImageId);
}
}
}
@ -124,21 +137,4 @@ class ProductImageRepository extends Repository
$this->upload($product, $variant['images'] ?? null);
}
}
/**
* Update image position.
*
* @param int $imageId
* @return void
*/
public function updateImagePosition($imageId)
{
static $position = 0;
$this->update([
'position' => $position,
], $imageId);
++$position;
}
}