From b34df98d317529ec22439221b92f0f6b5f6afd77 Mon Sep 17 00:00:00 2001 From: saparatayev Date: Wed, 28 Jul 2021 17:14:55 +0500 Subject: [PATCH] images processing when editing post and adding post --- plugins/tps/birzha/components/OfferForm.php | 35 +++++++++++++++---- .../components/offerForm/second_step_form.htm | 15 +++++--- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/plugins/tps/birzha/components/OfferForm.php b/plugins/tps/birzha/components/OfferForm.php index 149e4ae77..dd8c3605c 100644 --- a/plugins/tps/birzha/components/OfferForm.php +++ b/plugins/tps/birzha/components/OfferForm.php @@ -136,16 +136,20 @@ class OfferForm extends ComponentBase 'description_tm' => 'required', 'description_en' => 'required', 'description_ru' => 'required', - // 'ends_at' => 'required|date', 'payment_term_id' => 'required', 'packaging' => 'required', 'delivery_term_id' => 'required', 'currency_id' => 'required', 'measure_id' => 'required', - 'new_img' => 'required' + // 'new_img' => 'required' ]; $this->validateForm($data, $rules); + // validate if no old images and new images + if(!isset($data['new_img']) && !isset($data['old_img'])) { + throw new ValidationException(['no_images' => 'Хотя бы 1 фото должно быть']); + } + // seaparate validation for file type $rules = [ 'new_img.*' => 'mimes:jpg,png' @@ -162,10 +166,11 @@ class OfferForm extends ComponentBase $attachedProduct = $this->fillProduct($data,$attachedProduct); - // add images to completely new product - foreach($data['new_img'] as $key => $img) { - $attachedProduct->images = $img; - $attachedProduct->save(); + if(isset($data['new_img'])) { + foreach($data['new_img'] as $key => $img) { + $attachedProduct->images = $img; + $attachedProduct->save(); + } } $this->page['fee'] = Settings::getValue('fee'); @@ -199,6 +204,24 @@ class OfferForm extends ComponentBase } } + // after deleting a photo go the second form_step + public function onImageDelete() { + // dd(Input::get('product_image_id')); + $product = Product::find(Input::get('being_edited_product_id')); + + $product->images()->find(Input::get('product_image_id'))->delete(); + + $this->page['measures'] = Measure::all(); + $this->page['paymentTerms'] = Term::where('type','payment')->get(); + $this->page['deliveryTerms'] = Term::where('type','delivery')->get(); + $this->page['currencies'] = Currency::all(); + $this->page['product'] = $product; + + return [ + '#form-steps' => $this->renderPartial('@second_step_form') + ]; + } + protected function validateFileType($data, $rules) { $validator = Validator::make($data, $rules); diff --git a/plugins/tps/birzha/components/offerForm/second_step_form.htm b/plugins/tps/birzha/components/offerForm/second_step_form.htm index 30dc65278..43f438c4a 100644 --- a/plugins/tps/birzha/components/offerForm/second_step_form.htm +++ b/plugins/tps/birzha/components/offerForm/second_step_form.htm @@ -130,7 +130,7 @@ - +
@@ -140,11 +140,12 @@ {% if product.images[i] %}
photo - +
{% endif %}
+ {% if not product.images[i] %}
@@ -159,10 +160,13 @@ />
- {% if product.images[i] %} -
+ {% else %} + -
+ {% endif %}
@@ -186,3 +190,4 @@ +