Browse Source

Add selecte to array

Azam Rezayi 4 years ago
parent
commit
0d7c644124

+ 42 - 50
packages/product/src/Http/Controllers/ProductController.php

@@ -94,19 +94,19 @@ class ProductController extends Controller
     public function productStepVariable(Request $request, $id)
     public function productStepVariable(Request $request, $id)
     {
     {
         $attributes = Attribute::orderBy('id')->where('use_in_veriation', 1)->get();
         $attributes = Attribute::orderBy('id')->where('use_in_veriation', 1)->get();
-//        $attributes = Attribute::query()->where('id', $request->id)->where('use_in_veriation', 1)->first();
+
+        $attributables = \DB::table('attributables')->where('val_attribute', true)->get();
+//        dd($attributables->val_attribute);
         $product = $this->productChecker($id);
         $product = $this->productChecker($id);
         if ($product != false){
         if ($product != false){
             $msg = 'ذخیره محصول با موفقیت انجام شد ';
             $msg = 'ذخیره محصول با موفقیت انجام شد ';
-            return view('product::product.variableProduct', compact('product', 'attributes'))->with('success', $msg);
+            return view('product::product.variableProduct', compact('product', 'attributes', 'attributables'))->with('success', $msg);
         }else{
         }else{
             return abort(419);
             return abort(419);
         }
         }
     }
     }
-
     public function productStepThree(Request $request)
     public function productStepThree(Request $request)
     {
     {
-//        dd($request->all());
         $product = $this->productChecker($request->product_id);
         $product = $this->productChecker($request->product_id);
         if ($product != false){
         if ($product != false){
             $request->validate([
             $request->validate([
@@ -141,7 +141,6 @@ class ProductController extends Controller
             return abort(419);
             return abort(419);
         }
         }
     }
     }
-
     public function productChecker($id)
     public function productChecker($id)
     {
     {
         $product = Product::where('id', $id)->where('creator_id', Auth::user()->id)->first();
         $product = Product::where('id', $id)->where('creator_id', Auth::user()->id)->first();
@@ -153,7 +152,6 @@ class ProductController extends Controller
     }
     }
     // ******************************************************edit page product*********************************************
     // ******************************************************edit page product*********************************************
     public function edit(Product $product)
     public function edit(Product $product)
-
     {
     {
         $categories = Category::orderBy('id', 'DESC')->get();
         $categories = Category::orderBy('id', 'DESC')->get();
         return view('product::product.edit', compact('product', 'categories'));
         return view('product::product.edit', compact('product', 'categories'));
@@ -178,27 +176,11 @@ class ProductController extends Controller
             'discription' => $request->discription,
             'discription' => $request->discription,
             'creator_id' => auth()->user()->id,
             'creator_id' => auth()->user()->id,
         ];
         ];
-
-
-        if ($request->has('gallery_image')) {
-            $file = $request->only('gallery_image', 'captionGallery', 'descriptionGallery');
-            $type = 'gallery_image';
-            $diskName = 'product';
-            $this->updateGallery($file, $diskName, $product, $type);
-        }
-        if ($request->has('featured_image')) {
-            $info = $request->only(['featureDescription', 'featureCaption']);
-            $file = $request->file('featured_image');
-            $type = 'featured_image';
-            $diskName = 'product';
-            $this->updateFeature($file, $diskName, $product, $type, $info);
-        }
         $product->update($data);
         $product->update($data);
         $product->categories()->sync($request->categories);
         $product->categories()->sync($request->categories);
         $msg = 'ویرایش محصول با موفقیت انجام شد ';
         $msg = 'ویرایش محصول با موفقیت انجام شد ';
         return redirect(route('products.index'))->with('success', $msg);
         return redirect(route('products.index'))->with('success', $msg);
     }
     }
-
     //***************************** delete products*****************************************
     //***************************** delete products*****************************************
     public function destroy(Product $product)
     public function destroy(Product $product)
     {
     {
@@ -207,7 +189,6 @@ class ProductController extends Controller
             return back();
             return back();
         }
         }
     }
     }
-
 //************************delete image in the product**********************
 //************************delete image in the product**********************
     public function destroyFile(Upload $upload)
     public function destroyFile(Upload $upload)
     {
     {
@@ -216,39 +197,50 @@ class ProductController extends Controller
         session()->flash('image_deleted', $msg);
         session()->flash('image_deleted', $msg);
         return redirect()->back();
         return redirect()->back();
     }
     }
-
 //    ****************************uploadGallery********************
 //    ****************************uploadGallery********************
-
     public function uploadValAttribute(Request $request)
     public function uploadValAttribute(Request $request)
     {
     {
-//       $attributes = Attribute::orderBy('id')->where('use_in_veriation', 1)->get();
         $productID = $request->productID;
         $productID = $request->productID;
         $product = Product::where('id', $productID)->first();
         $product = Product::where('id', $productID)->first();
-        $info = json_decode($request->info);
-        $type = json_decode($request->type);
-        $attributables = $product->attributes()->where($info,'val_attribute');
-//        dd($attributables);
-        dd($type);
-        if (($attributables)){
-            foreach ($info as $item){
-                $data = [
-                    'val_attribute' => $item,
-                    'attribute_id' => $type,
-                    'attributable_id' => $productID,
-                    'attributable_type' => 'products',
-                    'created_at' => Carbon::now(),
-                    'updated_at' => Carbon::now(),
-                ];
+        $attributes = [];
+        foreach ( $request->val_attribute as $attr_id => $values ){
+            $attributes[$attr_id] = ['val_attribute' => $values];
 
 
-                \DB::table('attributables')->insert($data);
-            }
-            $operate['status'] = 200;
-            $operate['msg'] = 'با موفقیت ذخیره شد !';
-        }else{
-            $operate['status'] = 500;
-            $operate['msg'] = 'اطلاعات قبلا ذخیره شده';
-        }
-            return $operate;
         }
         }
+//        dd( $attributes );
+        $product->attributes()->sync( $attributes );
+ dd( $attributes );
+        foreach ( $request->val_attribute as $attr_id => $values ){
+            $values = array_values( $values );
+            echo $attr_id . ' ';
+            var_dump( $values );
+            echo "<hr>";
 
 
+        }
+        dd();
+        $product->attributes()->sync($request->val_attribute);
+        dd(1);
+
+//        if ((!$attributables->attribute_id='attribute_id' && !$attributables->attributable_id='attributable_id')){
+//            foreach ($info as $item){
+//                $data = [
+//                    'val_attribute' => $item,
+//                    'attribute_id' => $type,
+//                    'attributable_id' => $productID,
+//                    'attributable_type' => 'products',
+//                    'created_at' => Carbon::now(),
+//                    'updated_at' => Carbon::now(),
+//                ];
+//
+//                \DB::table('attributables')->insert($data);
+//            }
+//            $operate['status'] = 200;
+//            $operate['msg'] = 'با موفقیت ذخیره شد !';
+//        }else{
+//            $operate['status'] = 500;
+//            $operate['msg'] = 'اطلاعات قبلا ذخیره شده';
+//        }
+//            return $operate;
+//        }
+    }
 }
 }

+ 19 - 20
packages/product/src/js/product.js

@@ -1,11 +1,9 @@
-
-
 // Add Attribute
 // Add Attribute
 $(document).on('click', '.attribute_creator', function () {
 $(document).on('click', '.attribute_creator', function () {
-    // const attr_id = $('#Show_attributeValue').val();
-    //  const attr_label = $('#Show_attributeValue').find('option:selected').text();
-    var attr = document.getElementById("Show_attributeValue").value;
-    if(attr == $('#Show_selectValue').html()){
+    const attr_id = $('#Show_attributeValue').val();
+    const attr_label = $('#Show_attributeValue').find('option:selected').text();
+    // var attr = document.getElementById("Show_attributeValue").value;
+    if(attr_label == $('#Show_selectValue').html()){
         create = false;
         create = false;
         swal({
         swal({
             title: "مقدار تکراری",
             title: "مقدار تکراری",
@@ -18,15 +16,15 @@ $(document).on('click', '.attribute_creator', function () {
     }else {
     }else {
         create = true;
         create = true;
     }
     }
-    document.getElementById("Show_selectValue").innerHTML = attr;
+    document.getElementById("Show_selectValue").innerHTML = attr_label;
     if (create == true){
     if (create == true){
-        let new_select = $('<div>').append( $(".added_attribute:first")
-            .clone());
+        let new_select = $('<div>').append( $(".added_attribute:first").clone() );
         new_select.find('span').remove();
         new_select.find('span').remove();
         new_select.find('select').removeClass();
         new_select.find('select').removeClass();
         new_select.find('select').addClass('newSelectItem');
         new_select.find('select').addClass('newSelectItem');
         $('div.added_attribute:last').after( new_select.html() );
         $('div.added_attribute:last').after( new_select.html() );
-        $('div.added_attribute:last').show();
+        $('div.added_attribute:last').show().find('select').attr('name', 'val_attribute[' + attr_id + '][]');
+        $('#saveItemAttribute').show();
         $(".added_attribute select").select2({
         $(".added_attribute select").select2({
             tags: true
             tags: true
         });
         });
@@ -38,8 +36,12 @@ $(document).on('click', '.attribute_creator', function () {
         var base_url = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "");
         var base_url = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "");
         var data = new FormData();
         var data = new FormData();
         data.append('_token', $('[name="csrf-token"]').attr('content'));
         data.append('_token', $('[name="csrf-token"]').attr('content'));
-        data.append('info', JSON.stringify($('.newSelectItem').val()));
-        data.append('type', JSON.stringify($('#Show_attributeValue').val()));
+        $('select.newSelectItem').each(function(){
+            let attribute_values = $(this).val();
+            for( let i=0; i<attribute_values.length; i++ ){
+                data.append( $(this).attr('name'), attribute_values[i]);
+            }
+        });
         data.append('productID',$('#productID').val());
         data.append('productID',$('#productID').val());
         $.ajax({
         $.ajax({
             url:  base_url + "/panel/attributes_uploads/val_attribute",
             url:  base_url + "/panel/attributes_uploads/val_attribute",
@@ -48,11 +50,11 @@ $(document).on('click', '.attribute_creator', function () {
             processData: false,
             processData: false,
             contentType: false,
             contentType: false,
             success: function(operate) {
             success: function(operate) {
-                if (operate.status == 200){
-                    swal(operate.msg);
-                }else {
-                    swal(operate.msg);
-                }
+                // if (operate.status == 200){
+                //     swal(operate.msg);
+                // }else {
+                //     swal(operate.msg);
+                // }
             }
             }
         });
         });
 
 
@@ -64,6 +66,3 @@ $(document).on('click', '.variable_creator', function () {
     $("div.added_variable:last").show();
     $("div.added_variable:last").show();
 });
 });
 
 
-
-
-

+ 3 - 0
packages/product/src/sass/product.scss

@@ -189,3 +189,6 @@ output span {
     margin: 30px 20px;
     margin: 30px 20px;
 
 
 }
 }
+.saveItemAttribute{
+    display: none;
+}

+ 7 - 3
packages/product/src/views/product/include/createAttribute.blade.php

@@ -31,6 +31,7 @@
                         <label id="Show_selectValue" class="attr"></label><per class="font-color">:</per>
                         <label id="Show_selectValue" class="attr"></label><per class="font-color">:</per>
                     </div>
                     </div>
                     <div class="form-group Attr_item">
                     <div class="form-group Attr_item">
+{{--                        <input type="hidden" name="attributeID" id="productID" value="{{ $attributes->id }}">--}}
 {{--                        <input name="val_attribute[]" id="" class="attr js-example-tokenizer saveItemAttribute" multiple="multiple">--}}
 {{--                        <input name="val_attribute[]" id="" class="attr js-example-tokenizer saveItemAttribute" multiple="multiple">--}}
                     <select name="val_attribute[]" id="" class="val_attributes_all attr js-example-tokenizer " multiple="multiple">
                     <select name="val_attribute[]" id="" class="val_attributes_all attr js-example-tokenizer " multiple="multiple">
                         <option value=""></option>
                         <option value=""></option>
@@ -38,12 +39,15 @@
                     <a href="" class="deleteGallery btn btn-danger ">
                     <a href="" class="deleteGallery btn btn-danger ">
                         <i class="icon fa fa-close AddPad"></i>
                         <i class="icon fa fa-close AddPad"></i>
                     </a>
                     </a>
-                    <button type="submit" class="saveItemAttribute btn btn-success ">
-                        اعمال
-                    </button>
                     </div>
                     </div>
+
                 </div>
                 </div>
             </div>
             </div>
+            <div>
+                <button type="submit" id="saveItemAttribute" class="saveItemAttribute btn btn-success float-left">
+                    اعمال
+                </button>
+            </div>
         </div>
         </div>
 
 
     </div>
     </div>

+ 12 - 49
packages/product/src/views/product/variableProduct.blade.php

@@ -33,58 +33,21 @@
                                 <div class="col-lg-12 added_variable">
                                 <div class="col-lg-12 added_variable">
                                     <div><label for="" class="label attribute">ویژگی ها</label></div>
                                     <div><label for="" class="label attribute">ویژگی ها</label></div>
                                     <div class="form-row attribute_style">
                                     <div class="form-row attribute_style">
-                                        <div class="form-group col-lg-3">
-                                            <label for="slug">  {{ __('product.slug') }}</label>
-                                            <input id="slug" type="text" class="form-control @error('slug') is-invalid @enderror" name="slug"  value="{{$product->slug}}" autocomplete="slug" autofocus>
-                                            @error('slug')
-                                                <span class="invalid-feedback" role="alert">
-                                                        <strong>{{ $message }}</strong>
-                                                </span>
-                                            @enderror
-                                        </div>
-                                        <div class="form-group col-lg-3">
-                                            <label for="slug">  {{ __('product.slug') }}</label>
-                                            <input id="slug" type="text" class="form-control @error('slug') is-invalid @enderror" name="slug"  value="{{$product->slug}}" autocomplete="slug" autofocus>
-                                            @error('slug')
+
+                                        @foreach ($attributables as $item)
+{{--                                            {{dd($attributables)}}--}}
+                                            <div class="form-group col-lg-3">
+                                                <label for="slug">{{$item->attribute_id}}</label>
+                                                <select id="slug" type="text" class="form-control @error('slug') is-invalid @enderror" name="slug"  value="{{$item->val_attribute}}" autocomplete="slug" autofocus>
+                                                     <option value="{{$item->val_attribute}}"></option>
+                                                </select>
+                                                @error('slug')
                                                 <span class="invalid-feedback" role="alert">
                                                 <span class="invalid-feedback" role="alert">
                                                         <strong>{{ $message }}</strong>
                                                         <strong>{{ $message }}</strong>
                                                 </span>
                                                 </span>
-                                            @enderror
-                                        </div>
-                                        <div class="form-group col-lg-3">
-                                            <label for="slug">  {{ __('product.slug') }}</label>
-                                            <input id="slug" type="text" class="form-control @error('slug') is-invalid @enderror" name="slug"  value="{{$product->slug}}" autocomplete="slug" autofocus>
-                                            @error('slug')
-                                                <span class="invalid-feedback" role="alert">
-                                                    <strong>{{ $message }}</strong>
-                                                </span>
-                                            @enderror
-                                        </div>
-                                        <div class="form-group col-lg-3">
-                                            <label for="ReleaseStatus">{{ __('product.Country') }}</label><i
-                                                class="required"> *</i>
-                                            <select id="ReleaseStatus" type="text"
-                                                    class="form-control @error('ReleaseStatus') is-invalid @enderror"
-                                                    name="ReleaseStatus" autocomplete="ReleaseStatus">
-                                                <option value="draft"
-                                                        @if($product->ReleaseStatus==='پیشنویس') selected='selected' @endif>
-                                                    پیشنویس
-                                                </option>
-                                                <option value="pending"
-                                                        @if($product->ReleaseStatus==='در انتظار انتشار') selected='selected' @endif>
-                                                    در انتظار انتشار
-                                                </option>
-                                                <option value="published"
-                                                        @if($product->ReleaseStatus==='منتشر شده') selected='selected' @endif>
-                                                    منتشر شده
-                                                </option>
-                                            </select>
-                                            @error('ReleaseStatus')
-                                            <span class="invalid-feedback" role="alert">
-                                                <strong>{{ $message }}</strong>
-                                            </span>
-                                            @enderror
-                                        </div>
+                                                @enderror
+                                            </div>
+                                        @endforeach
                                     </div>
                                     </div>
                                     <div class="attribute_style">
                                     <div class="attribute_style">
                                         @include('product::product.include.simpleProduct')
                                         @include('product::product.include.simpleProduct')