Browse Source

Save migration draft and attachment

Azam Rezayi 4 years ago
parent
commit
3fdf4bc159

+ 25 - 2
packages/product/src/Http/Controllers/ProductController.php

@@ -2,6 +2,9 @@
 
 namespace Packages\Product\Http\Controllers;
 
+use Illuminate\Support\Facades\Auth;
+use Packages\Product\Models\Draft;
+use Packages\Product\Models\Attachment;
 use App\Http\Controllers\Controller;
 use Facade\Ignition\Support\Packagist\Package;
 use Illuminate\Database\Eloquent\Builder;
@@ -9,6 +12,7 @@ use Illuminate\Support\Facades\Facade;
 use Illuminate\Support\Facades\File;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
+use Packages\Product\Traits\Attachments;
 use Packages\Product\Http\Requests\ProductRequest;
 use Packages\Product\Http\Requests\ProductRequestEdit;
 use Packages\Product\Models\Product;
@@ -55,11 +59,30 @@ class ProductController extends Controller
     }
 
 //**************************************************** page create products *****************
-    public function create()
+    public function create(Request $request)
     {
         $categories = Category::orderBy('parent_id')->where('parent_id', 0)->get();
 
-        return view('product::product.create', compact('categories'));
+        if( !$request->has('draft_id') ){
+            $draft = new Draft();
+            $draft->object_type = 'Packages\Product';
+            $draft->save();
+            dd($draft->id);
+            return view('product::product.create', ['draft_id' => $draft->id] , compact('categories'));
+
+        }
+        else{
+            $draft = Draft::find( $request->draft_id );
+            if( is_null( $draft ) ){
+                return redirect()->route( 'product::product.create', compact('categories') );
+            }
+        }
+        $args = [
+
+            'draft'     => $draft
+        ];
+
+        return view('product::product.create', compact('categories', 'args'));
     }
 
 // *********************************************************save products ******************

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

@@ -6,5 +6,5 @@ use Illuminate\Database\Eloquent\Model;
 
 class Draft extends Model
 {
-    use Attachments;
+//   use Attachmments;
 }

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

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

+ 1 - 6
packages/product/src/routes/web.php

@@ -9,14 +9,9 @@ Route::group([
 ], function() {
 
     Route::resource('products','ProductController');
-;
-
-    Route::get('uploads/{upload}', 'ProductController@destroyFile')->name('uploads.destroyFile');
-
-
-
 
 
+    Route::get('uploads/{upload}', 'ProductController@destroyFile')->name('uploads.destroyFile');
 });
 
 

+ 1 - 63
packages/product/src/views/product/create.blade.php

@@ -25,6 +25,7 @@
                             <div class="card-body mx-lg-5 px-lg-3">
                                 <form method="POST" action="{{ route('products.store') }}" enctype="multipart/form-data" id="DraftProduct">
                                     @csrf
+{{--                                    <input type="hidden" name="draft_id" value="{{ $draft->id }}">--}}
                                     <div class="form-group">
                                          <label for="title">{{ __('product.title') }}</label><i class="required">&nbsp; *</i>
                                          <input id="title" type="text" class="form-control @error('title') is-invalid @enderror" name="title" value="{{ old('title') }}" autocomplete="title" autofocus>
@@ -206,70 +207,7 @@
     @endslot
 
     @slot('script')
-        <script>
-                    $("#DraftProduct").on('keypress', function(e) {
-                         // alert('ssalam');
-                        var getUrl = window.location;
-                        var base_url = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
-                            e.preventDefault();
 
-                                title = $("#title").val();
-                                price = $("#price").val();
-                                slug = $("#slug").val();
-                                type = $("#type").val();
-                                status = $("#status").val();
-                                ReleaseStatus = $("#ReleaseStatus").val();
-                                discription = $("#discription").val();
-                                categories = $("#categories").val();
-                               featured_image = $("#featured_image").val();
-
-                            if (title === '') {
-                                alert("لطفا پارامتر ارسالی را وارد کنید");
-                            }
-                            if (price === '') {
-                                alert("لطفا پارامتر ارسالی را وارد کنید");
-                            }
-                            if (slug === '') {
-                                alert("لطفا پارامتر ارسالی را وارد کنید");
-                            }
-                             if (type  === '') {
-                                alert("لطفا پارامتر ارسالی را وارد کنید");
-                            }
-                             if (status  ==='') {
-                                alert("لطفا پارامتر ارسالی را وارد کنید");
-                            }
-                             if (discription  === '') {
-                                alert("لطفا پارامتر ارسالی را وارد کنید");
-                            }
-                             if (categories  === '') {
-                                alert("لطفا پارامتر ارسالی را وارد کنید");
-                            }
-                             if (featured_image === '') {
-                                alert("لطفا پارامتر ارسالی را وارد کنید");
-                            }else{
-                                $.ajax({
-                                    method: "POST",
-                                    url: base_url + "/product/SaveFormProductByAjax",
-                                     data:{"title":title,"price":price,"slug":slug,"type":type,"status":status,"discription":discription,"categories":categories,"featured_image":featured_image},
-                                    // contentType: "application/json; charset=utf-8",
-                                    dataType: "json",
-                                    success: function(response) {
-                                        if (response.Ajax) {
-                                            alert(response.msg);
-                                        } else {
-                                            alert("این متن برای تست  می باشد");
-                                        }
-                                    },
-                                    error: function(response) {
-                                        alert("مشکلی در اجرای برنامه به وجود آمده ");
-                                    }
-                                });
-                            }
-                        timeout: 10000
-                        });
-
-
-        </script>
     @endslot
 
 @endcomponent