Quellcode durchsuchen

Edit filde feacher images in the pages product edit

Azam Rezayi vor 4 Jahren
Ursprung
Commit
d77babdf32

+ 1 - 0
database/migrations/2020_07_05_045648_create_uploads_table.php

@@ -19,6 +19,7 @@ class CreateUploadsTable extends Migration
             $table->string('path');
             $table->string('extension');
             $table->string('mime_type');
+            $table->string('parent_type');
             $table->string('descriptionImg')->nullable();
             $table->string('caption')->nullable();
             $table->enum('type',['featured_image','gallery_image']);

+ 11 - 8
packages/product/src/Http/Controllers/ProductController.php

@@ -12,7 +12,7 @@ use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
 use Packages\Product\Models\Attachment;
 use Packages\Product\Models\Draft;
-use Packages\Product\Traits\Attachments;
+use Packages\Product\Traits\Uploads;
 use Packages\Product\Http\Requests\ProductRequest;
 use Packages\Product\Http\Requests\ProductRequestEdit;
 use Packages\Product\Models\Product;
@@ -100,12 +100,12 @@ class ProductController extends Controller
 //                ->withErrors( ['error_message' => 'حتما باید یک فایل آپلود شود.'] )
 //                ->withInput( $request->all() );
 //        }
-//        dd($draft->attachments);
+//        dd($draft->attachments->count() == 0);
         $product = Product::create($data);
         $product->categories()->sync($request->categories);
 
-        Attachment::where('parent_type', 'Packages\Product')->where('parent_id', $draft->id)
-            ->update(['parent_type' => 'Packages\Product', 'parent_id' => $product->id]);
+       Upload::where('parent_type', 'Packages\Product')
+            ->update(['parent_type' => 'image\Product']);
         $draft->delete();
 
         if ($request->has('gallery_image')) {
@@ -151,8 +151,8 @@ class ProductController extends Controller
             'discription' => $request->discription,
             'creator_id' => auth()->user()->id,
         ];
-        $product->update($data);
-        $product->categories()->sync($request->categories);
+
+
         if ($request->has('gallery_image')) {
             $file = $request->only('gallery_image', 'captionGallery', 'descriptionGallery');
             $type = 'gallery_image';
@@ -164,9 +164,10 @@ class ProductController extends Controller
             $file = $request->file('featured_image');
             $type = 'featured_image';
             $diskName = 'product';
-            $this->uploadFeature($file, $diskName, $product, $type, $info);
+            $this->updateFeature($file, $diskName, $product, $type, $info);
         }
-
+        $product->update($data);
+        $product->categories()->sync($request->categories);
         $msg = 'ویرایش محصول با موفقیت انجام شد ';
         return redirect(route('products.index'))->with('success', $msg);
     }
@@ -199,6 +200,7 @@ class ProductController extends Controller
             'path' => $upload,
             'mime_type' => $fileMimeType,
             'extension' => $fileExtension,
+            'parent_type' => 'packages\product',
             'type' => $type,
             'descriptionImg' => $info['featureDescription'],
             'caption' => $info['featureCaption'],
@@ -261,6 +263,7 @@ class ProductController extends Controller
                 'path' => $upload,
                 'mime_type' => $fileMimeType,
                 'extension' => $fileExtension,
+                'parent_type' => 'packages\product',
                 'type' => $type,
                 'descriptionImg' => $files['descriptionGallery'][$key],
                 'caption' => $files['captionGallery'][$key],

+ 1 - 0
packages/product/src/Http/Requests/ProductRequest.php

@@ -35,6 +35,7 @@ class ProductRequest extends FormRequest
             'featureDescription' => ['nullable'],
             'featureCaption' => ['nullable'],
             'descriptionImg' => ['nullable'],
+            'parent_type' =>['nullable'],
             'caption' => ['nullable'],
             'gallery_image' => ['nullable'],
             'featured_image' => ['required', 'image', 'mimes:jpeg,jpg,png,gif', 'max:8000'],

+ 2 - 2
packages/product/src/Models/Draft.php

@@ -1,10 +1,10 @@
 <?php
 
 namespace Packages\Product\Models;
-use Packages\Product\Traits\Attachments;
+use Packages\Product\Traits\Uploads;
 use Illuminate\Database\Eloquent\Model;
 
 class Draft extends Model
 {
-   use Attachments;
+   use Uploads;
 }

+ 2 - 2
packages/product/src/Models/Product.php

@@ -3,7 +3,7 @@
 namespace Packages\Product\Models;
 use App\Models\Category;
 use App\Models\Upload;
-use Packages\Product\Traits\Attachments;
+use Packages\Product\Traits\Uploads;
 use Illuminate\Database\Eloquent\Model;
 use App\User;
 use Illuminate\Database\Eloquent\SoftDeletes;
@@ -12,7 +12,7 @@ use Cviebrock\EloquentSluggable\Sluggable;
 class Product extends Model
 {
     use Sluggable;
-    use Attachments;
+    use Uploads;
     use SoftDeletes;
 
    protected $fillable = ['title', 'discription', 'price', 'creator_id', 'type', 'status', 'sale_price', 'sku', 'ReleaseStatus'];

+ 0 - 11
packages/product/src/Traits/Attachments.php

@@ -1,11 +0,0 @@
-<?php
-
-namespace Packages\Product\Traits;
-
-use Packages\Product\Models\Attachment;
-
-trait Attachments{
-    public function attachments(){
-        return $this->morphMany(Attachment::class, 'parent');
-    }
-}

+ 12 - 0
packages/product/src/Traits/Uploads.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace Packages\Product\Traits;
+
+use App\Models\Upload;
+
+
+trait Uploads{
+    public function attachments(){
+        return $this->morphMany(Upload::class, 'parent');
+    }
+}

+ 62 - 0
packages/product/src/js/product.js

@@ -305,3 +305,65 @@ $(document).ready(function () {
     }
 });
 //    ********************File Attachment*******************
+// $('.attachment_upload').change(function(){
+//     var target = $(this).closest('.attachments');
+//
+//     for( let i=0; i<$(this).prop('files').length; i++ ){
+//         // let file_id = uniqid();
+//
+//
+//         var formData = new FormData();
+//         formData.append('file', $(this).prop('files')[i]);
+//         formData.append('file_type', 'attachment');
+//         formData.append('object_type', target.attr('data-object-type'));
+//         formData.append('object_id', target.attr('data-object-id'));
+//         let fail_message = '<span class="ltr">آپلود فایل ' + $(this).prop('files')[i].name + ' با خطا مواجه شد.</span>';
+//         $.ajax({
+//             url: ajax_upload_url,
+//             async: true,
+//             data: formData,
+//             processData: false,
+//             contentType: false,
+//             dataType: 'json',
+//             type: 'post',
+//             success: function(response){
+//                 if( response.status == 1 ){
+//                     $('#' + file_id ).find('a').attr('href', response.file_url );
+//                     $('#' + file_id ).find('.uploaded_file_path').val( response.file_path );
+//                     $('#' + file_id ).find('.progress').remove();
+//                 }
+//             },
+//             error: function(response){
+//                 if( response.status == 422 ){
+//                     var response_text = $.parseJSON( response.responseText );
+//                     var text = fail_message;
+//                     for( var j=0; j<response_text.errors.file.length; j++ ){
+//                         text += "<br />" + response_text.errors.file[j];
+//                     }
+//                     Swal.fire({
+//                         title: 'خطا در آپلود فایل',
+//                         html: text,
+//                         type: 'error',
+//                         customClass: {
+//                             icon: 'swal2-arabic-question-mark'
+//                         },
+//                         confirmButtonText: 'تایید'
+//                     })
+//                 }
+//             },
+//             xhr: function() {
+//                 var myXhr = $.ajaxSettings.xhr();
+//                 if(myXhr.upload){
+//                     myXhr.upload.addEventListener(
+//                         'progress',
+//                         function(e){
+//                             progress( e, file_id );
+//                         },
+//                         false);
+//                 }
+//                 return myXhr;
+//             }
+//         });
+//     }
+//     $(this).val('');
+// });

+ 3 - 5
packages/product/src/views/product/create.blade.php

@@ -140,9 +140,9 @@
                                              </span>
                                         @enderror
                                     </div>
-                                    <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 @error('featured_image') is-invalid  @enderror" value="{{old('featured_image')}}" autocomplete="featured_image" autofocus>
+                                    <div class="form-group col-md-8 ">
+                                        <span class="btn btn-primary btn-file attachment_upload" data-name="attachment">
+                                            <input type="file" id="uploadImages" name="featured_image" accept="image/*" class="form-control photo  browse  @error('featured_image') is-invalid  @enderror" data-name="attachment" value="{{old('featured_image')}}" autocomplete="featured_image" autofocus>
                                             <label for="title">{{ __('product.featured_image') }}</label> <i class="required">&nbsp;*</i>
                                         </span>
                                         <div class="hide-element" id="previewImages">
@@ -187,14 +187,12 @@
                                             </div>
                                         </div>
                                     </div>
-
                                     <div class="form-group mb-0">
                                         <div class="">
                                             <button type="submit" class="btn btn-primary" >
                                                 {{ __('ثبت محصول') }}
                                             </button>
                                             <a href="{{ route('products.index') }}" type="button" class="btn btn-warning" > انصراف</a>
-                                            <button  type="submit" class="btn btn-warning" id="DraftProduct"> انصراف</button>
                                         </div>
                                     </div>
                                 </form>