Forráskód Böngészése

End ulad file image the input gallery and feature

Azam Rezayi 4 éve
szülő
commit
14ce15e6bd

+ 26 - 15
packages/product/src/Http/Controllers/ProductController.php

@@ -75,15 +75,16 @@ class ProductController extends Controller
 // *********************************************************save products ******************
     public function store(ProductRequest $request)
     {
+//       dd($request->all());
         $request->merge([
             'price' => preg_replace('/[^0-9]+/', '', $request->price),
             'sale_price' => preg_replace('/[^0-9]+/', '', $request->sale_price),
         ]);
-//        dd($request->all());
         if (!$request->has('draft_id')) {
             return abort(403, 'دسترسی به این صفحه ممکن نیست ؛ لطفا مجددا تلاش کنید.');
         }
         $draft = Draft::findOrFail($request->draft_id);
+//        $info = $request->only(['featureDescription', 'featureCaption']);
         $data = [
             'title' => $request->title,
             'slug' => $request->slug,
@@ -94,7 +95,9 @@ class ProductController extends Controller
             'type' => $request->type,
             'status' => $request->status,
             'discription' => $request->discription,
-            'creator_id' => auth()->user()->id
+            'creator_id' => auth()->user()->id,
+//            'descriptionImg' => $request->featureDescription,
+//                'caption' => $request->featureCaption,
         ];
 //        if( $draft->attachments->count() == 0 ){
 //            return redirect()->back()
@@ -105,16 +108,11 @@ class ProductController extends Controller
         $product = Product::create($data);
         $product->categories()->sync($request->categories);
 
-       Upload::where('parent_type', 'Packages\Product')->where('uploadable_id', $draft->id)
+       Upload::where('parent_type', 'Packages\Product')->where('uploadable_id', $draft->id)->where('uploadable_id', $draft->id)
             ->update(['parent_type' => 'image\Product', 'uploadable_id' => $product->id]);
         $draft->delete();
+//        $uploaded = Upload::create();
 
-        if ($request->has('gallery_image')) {
-            $file = $request->only('gallery_image', 'captionGallery', 'descriptionGallery');
-            $type = 'gallery_image';
-            $diskName = 'product';
-            $this->uploadGallery($file, $diskName, $product, $type);
-        }
 
         $msg = 'ذخیره محصول با موفقیت انجام شد ';
         return redirect(route('products.edit', $product->id))->with('success', $msg);
@@ -178,8 +176,7 @@ class ProductController extends Controller
     public function uploadFeature(Request $request)
     {
         if ($request->has('featured_image')) {
-
-            $info = $request->only(['featureDescription', 'featureCaption']);
+           $info = $request->only(['featureDescription', 'featureCaption']);
             $file = $request->file('featured_image');
             $type = 'featured_image';
             $diskName = 'product';
@@ -206,9 +203,10 @@ class ProductController extends Controller
                 'descriptionImg' => $info['featureDescription'],
                 'caption' => $info['featureCaption'],
             ];
+            $uploaded = Upload::create($uploadData);
         }
 
-      $uploaded = Upload::create($uploadData);
+      dd($uploadData);
 
 //            $uploaded = $product->uploads()->create($uploadData);
         }
@@ -249,9 +247,15 @@ class ProductController extends Controller
     }
 
 //    ****************************uploadGallery********************
-    public function uploadGallery($files, $diskName, $product, $type)
+    public function uploadGallery(Request $request)
     {
-        foreach ($files['gallery_image'] as $key => $file) {
+
+        if ($request->has('gallery_image')) {
+            $files = $request->only('gallery_image', 'captionGallery', 'descriptionGallery');
+            $type = 'gallery_image';
+            $diskName = 'product';
+//           dd($files);
+      foreach ($files['gallery_image'] as $key => $file) {
             $fileExtension = $file->getClientOriginalExtension();
             $fileMimeType = $file->getMimeType();
             $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate()->format('m');
@@ -261,19 +265,26 @@ class ProductController extends Controller
             if (File::exists($filePath)) {
                 $fileName = time() . '_' . $fileName;
             }
+          $draft = Draft::orderBy('id', 'Desc')->get()->first();
             $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
+
             $uploadData = [
                 'name' => $fileName,
                 'path' => $upload,
                 'mime_type' => $fileMimeType,
                 'extension' => $fileExtension,
                 'parent_type' => 'packages\product',
+                'uploadable_type' => 'Packages\Product\Models\Product',
+                'uploadable_id' => $draft->id,
                 'type' => $type,
                 'descriptionImg' => $files['descriptionGallery'][$key],
                 'caption' => $files['captionGallery'][$key],
             ];
-            $uploaded = $product->uploads()->create($uploadData);
+//           dd($uploadData);
+            $uploaded = Upload::create($uploadData);
+
         }
+      }
     }
 
     public function updateGallery($files, $diskName, $product, $type)

+ 100 - 16
packages/product/src/js/product.js

@@ -1,11 +1,11 @@
 
 //**************************show images gallery**********************************************
-$(document).ready(function(){
-    $('.upload-widget').on('click', '.deleteGallary', function(e){
-        e.preventDefault();
-        $(this).closest(".DeleteImages").remove();
-    });
-});
+// $(document).ready(function(){
+//     $('.upload-widget').on('click', '.deleteGallary', function(e){
+//         e.preventDefault();
+//         $(this).closest(".DeleteImages").remove();
+//     });
+// });
 var FileUploader = (function() {
     function FileUploader(options) {
         options = options || {};
@@ -85,9 +85,6 @@ var FileUploader = (function() {
                             '</span>' +
                             '</div>';
                     }
-
-
-
                     self.output.insertBefore(div, null);
                 }
             })(file)
@@ -96,7 +93,6 @@ var FileUploader = (function() {
     }
     return FileUploader;
 })();
-
     if( $('.GalleryImageChoice').length > 0 ){
         var GalleryImageChoice = new FileUploader({className: '.GalleryImageChoice'});
         GalleryImageChoice.init();
@@ -304,19 +300,17 @@ $(document).ready(function () {
         };
     }
 });
-//    ********************File Attachment*******************
-
-
+//    ********************File Attachment featured_image*******************
 $(document).ready(function(){
     $(".attachment_upload").on('change',function(){
         var loc = window.location;
         var base_url = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "");
-        var uploader = $('.attachment_upload[name="featured_image"]');
+        var uploader = $('input[type="file"]');
          var data = new FormData();
         uploader.each(function(){
             data.append('featured_image', $(this).prop('files')[0]);
-            data.append('featureDescription', $(this).closest('.form-group').find('[name=featureDescription]'));
-            data.append('featureCaption', $(this).closest('.form-group').find('[name=featureCaption]'));
+           data.append('featureDescription', $(this).closest('.form-group').find('[name=featureDescription]').val());
+            data.append('featureCaption', $(this).closest('.form-group').find('[name=featureCaption]').val());
         })
         data.append('_token', $('[name="csrf-token"]').attr('content'));
         $.ajax({
@@ -332,3 +326,93 @@ $(document).ready(function(){
     });
 });
 //****************
+$(document).ready(function(){
+    $(".attachment_upload_gallery").on('change',function(){
+        var loc = window.location;
+        var base_url = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "");
+ for( let i=0; i<$(this).prop('files').length; i++ ){
+        var uploader = $('.attachment_upload_gallery[name="gallery_image[]"]');
+        var data = new FormData();
+        uploader.each(function(){
+            data.append('gallery_image[]', $(this).prop('files')[i]);
+            data.append('descriptionGallery[]', $(this).closest('.form-group').find('[name=descriptionGallery[]]'));
+            data.append('captionGallery[]', $(this).closest('.form-group').find('[name=captionGallery[]]'));
+        })
+        data.append('_token', $('[name="csrf-token"]').attr('content'));
+        $.ajax({
+            url:  base_url + "/panel/product_uploads/GalleryImage",
+            method: 'POST',
+            data: data,
+            processData: false,
+            contentType: false,
+            success: function(data) {
+
+            }
+        });
+     };
+});
+});
+//delete
+// $('#previewImages').on('click', '#delete', function(){
+//     var loc = window.location;
+//     var base_url = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "");
+//     var target = $(this).closest('.attachment_file_upload ');
+//     Swal.fire({
+//         title: 'آیا اطمینان دارید؟',
+//         text: 'در صورت تایید فایل آپلود شده حذف خواهد شد.',
+//         type: 'warning',
+//         customClass: {
+//             icon: 'swal2-arabic-question-mark'
+//         },
+//         showCancelButton: true,
+//         confirmButtonColor: '#e3342f',
+//         cancelButtonColor: '#38c172',
+//         cancelButtonText: 'انصراف',
+//         confirmButtonText: 'تایید و حذف'
+//     }).then((result) => {
+//         if(result.value){
+//             Swal.fire({
+//                 title: 'در حال اجرای درخواست',
+//                 type: 'info',
+//                 allowEscapeKey: false,
+//                 allowOutsideClick: false,
+//                 onOpen: () => {
+//                     Swal.showLoading();
+//                 }
+//             });
+//
+//             $.ajax({
+//                 type: 'post',
+//                 url: base_url + "/panel/uploads/{upload}",
+//                 dataType: 'json',
+//                 data: data,
+//                 success: function(response){
+//                     target.remove();
+//                     Swal.fire({
+//                         title: 'عملیات موفق',
+//                         text: 'فایل با موفقیت حذف شد.',
+//                         type: 'info'
+//                     });
+//                 },
+//                 error: function(response){
+//                     if( response.status == 404 ){
+//                         var response_text = $.parseJSON( response.responseText )
+//                         Swal.fire({
+//                             title: 'خطا در حذف فایل',
+//                             html: response_text.errors.file[0],
+//                             type: 'error',
+//                             customClass: {
+//                                 icon: 'swal2-arabic-question-mark'
+//                             },
+//                             confirmButtonText: 'تایید'
+//                         })
+//                     }
+//                 }
+//             });
+//         }
+//     });
+// });
+//***************************************
+
+
+

+ 3 - 8
packages/product/src/routes/web.php

@@ -1,18 +1,13 @@
 <?php
-
-
-
 Route::group([
     'namespace' => 'Packages\Product\Http\Controllers',
     'middleware' => ['web', 'auth', 'verified'],
     'prefix' => 'panel'
-], function() {
-
-    Route::resource('products','ProductController');
-
-
+], function () {
+    Route::resource('products', 'ProductController');
     Route::get('uploads/{upload}', 'ProductController@destroyFile')->name('uploads.destroyFile');
     Route::post('product_uploads/FeaturedImage', 'ProductController@uploadFeature')->name('FeaturedImage');
+    Route::post('product_uploads/GalleryImage', 'ProductController@uploadGallery')->name('GalleryImage');
 
 });
 

+ 6 - 4
packages/product/src/views/product/create.blade.php

@@ -140,6 +140,7 @@
                                              </span>
                                         @enderror
                                     </div>
+                                    <div class="">
                                     <div class="form-group col-md-8 ">
                                         <span class="btn btn-primary btn-file " >
                                             <input type="file" id="uploadImages" name="featured_image" accept="image/*" class="form-control photo  browse attachment_upload @error('featured_image') is-invalid  @enderror"  value="{{old('featured_image')}}" autocomplete="featured_image" autofocus>
@@ -148,7 +149,7 @@
                                         <div class="hide-element" id="previewImages">
                                             <div class="media">
                                                 <div class="media-left ">
-                                                    <a href="{{route('uploads.destroyFile', $uploads->id)}}" role="button" class="btn btn-danger pull-right" id="delete">
+                                                    <a href="" role="button" class="btn btn-danger pull-right" id="delete">
                                                         <i class="icon fa fa-close"></i>
                                                     </a>
                                                     <img class="media-object img-thumbnail" src="" alt="" id="0" title="" data-toggle="modal" data-target="#individualImagePreview" style="width: 250px; height: 150px;" >
@@ -156,11 +157,11 @@
                                                 <div class="media-body">
                                                     <p>
                                                         <label for="featureDescription">توضیحات: </label>
-                                                        <input type="text" class="form-control"  value="" name="featureDescription">
+                                                        <input type="text" class="form-control"  value="" name="featureDescription" id="featureDescription">
                                                     </p>
                                                     <p>
                                                         <label for="featureCaption">عنوان: </label>
-                                                        <input type="text" class="form-control" value="" name="featureCaption">
+                                                        <input type="text" class="form-control" value="" name="featureCaption" id="#featureCaption">
                                                     </p>
                                                 </div>
                                             </div>
@@ -176,7 +177,7 @@
                                         <div class="upload-widget GalleryImageChoice ">
                                             <div class="select">
                                                 <span class="btn btn-primary btn-file">
-                                                    <input class="files" type="file" name="gallery_image[]" value="{{old('gallery_image[]')}}" multiple>
+                                                    <input class="files attachment_upload_gallery" type="file" name="gallery_image[]" value="{{old('gallery_image[]')}}" multiple>
                                                     <label for="title">
                                                          {{ __('product.gallery_image') }}
                                                     </label>
@@ -196,6 +197,7 @@
                                             <a href="{{ route('products.index') }}" type="button" class="btn btn-warning" > انصراف</a>
                                         </div>
                                     </div>
+                                    </div>
                                 </form>
                             </div>
                             <div class="card-footer">

+ 3 - 3
packages/product/src/views/product/edit.blade.php

@@ -163,13 +163,13 @@
                                     <div class="form-group col-md-8">
                                         @component('product::components.featuredImage')
                                             @slot('ImageSrc')
-                                                {{\Illuminate\Support\Facades\Storage::disk('product')->url($product->featured_image->path)}}
+{{--                                                {{\Illuminate\Support\Facades\Storage::disk('product')->url($product->featured_image->path)}}--}}
                                             @endslot
                                             @slot('featureDescription')
-                                                {{$product->featured_image->descriptionImg}}
+{{--                                                {{$product->featured_image->descriptionImg}}--}}
                                             @endslot
                                             @slot('featureCaption')
-                                                {{$product->featured_image->caption}}
+{{--                                                {{$product->featured_image->caption}}--}}
                                             @endslot
                                         @endcomponent
                                     </div>