Browse Source

End filde draft

Azam Rezayi 4 years ago
parent
commit
4205611a89

+ 1 - 14
app/Http/Controllers/Panel/Categories/CategoryController.php

@@ -42,21 +42,8 @@ class CategoryController extends Controller
      */
     public function create()
     {
-//        $categories = Category::orderBy('parent_id', 'DESC');
 
-//        return view('panel.categories.create',compact('categories'));
-//        $categories = Category::orderBy('parent_id')->get();
-//       dd($categories);
-//        return view('panel.categories.create',compact('categories'));
-
-
-//            $categories    = Category:: orderBy('parent_id')->get();
-//
-////        dd($categories);
-//        return view('panel.categories.create', compact('categories'));
-
-
-            $categories = Category::orderBy('parent_id')->where('parent_id', 0)->get();
+          $categories = Category::orderBy('parent_id')->where('parent_id', 0)->get();
 
         return view('panel.categories.create', compact('categories'));
     }

+ 0 - 9
app/Traits/Attachments.php

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

+ 41 - 72
packages/product/src/Http/Controllers/ProductController.php

@@ -3,8 +3,6 @@
 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;
@@ -12,6 +10,8 @@ use Illuminate\Support\Facades\Facade;
 use Illuminate\Support\Facades\File;
 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\Http\Requests\ProductRequest;
 use Packages\Product\Http\Requests\ProductRequestEdit;
@@ -34,17 +34,15 @@ class ProductController extends Controller
     public function index()
     {
         $categories = Category::orderBy('id', 'DESC')->get();
-
+        $draft = Draft::orderBy('id', 'DESC')->first();
         $products = Product::query()->orderBy('id', 'DESC');
 
         if (request()->has('productCategory') && request('productCategory') != '')
             $products = $products->whereHas('categories', function (Builder $query) {
                 $query->whereRaw('id = ?', [request('productCategory')]);
             });
-
         if (request()->has('productStatus') && request('productStatus') != '')
             $products = $products->whereRaw('status = ?', [request('productStatus')]);
-
         if (request()->has('productName') && request('productName') != '')
             $products = $products->whereRaw('title = ?', [request('productName')]);
         if (request()->has('creatorId') && request('creatorId') != '')
@@ -52,47 +50,42 @@ class ProductController extends Controller
                 $query->whereRaw('name= ?', [request('creatorId')]);
             });
         $products = $products->paginate();
-
-
-        return view('product::index', compact('products', 'categories'));
-
+        return view('product::index', compact('products', 'categories', 'draft'));
     }
 
 //**************************************************** page create products *****************
     public function create(Request $request)
     {
         $categories = Category::orderBy('parent_id')->where('parent_id', 0)->get();
-
         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'));
-
+            return redirect()->route('products.create',['draft_id' => $draft->id]);
         }
         else{
             $draft = Draft::find( $request->draft_id );
             if( is_null( $draft ) ){
-                return redirect()->route( 'product::product.create', compact('categories') );
+                return redirect()->route( 'products.create');
             }
         }
         $args = [
-
-            'draft'     => $draft
+            'draft'         => $draft
         ];
-
-        return view('product::product.create', compact('categories', 'args'));
+        return view( 'product::product.create',$args, compact('categories')) ;
     }
-
 // *********************************************************save products ******************
-
     public function store(ProductRequest $request)
     {
         $request->merge([
             'price' => preg_replace('/[^0-9]+/', '', $request->price),
             'sale_price' => preg_replace('/[^0-9]+/', '', $request->sale_price),
         ]);
+
+        if( !$request->has('draft_id') ){
+            return abort(403, 'دسترسی به این صفحه ممکن نیست ؛ لطفا مجددا تلاش کنید.');
+        }
+        $draft = Draft::findOrFail( $request->draft_id );
         $data = [
             'title' => $request->title,
              'slug' => $request->slug,
@@ -105,33 +98,38 @@ class ProductController extends Controller
             'discription' => $request->discription,
             'creator_id' => auth()->user()->id
         ];
-//        \DB::enableQueryLog();
+//        if( $draft->attachments->count() == 0 ){
+//            return redirect()->back()
+//                ->withErrors( ['error_message' => 'نامه باید حداقل یک ضمیمه داشته باشد.'] )
+//                ->withInput( $request->all() );
+//        }
+//        dd($draft->attachments);
         $product = Product::create($data);
         $product->categories()->sync($request->categories);
-//        dd(\DB::getQueryLog());
+
+        Attachment::where('parent_type', 'Packages\Product')->where('parent_id', $draft->id)
+            ->update(['parent_type' => 'Packages\Product', 'parent_id' => $product->id]);
+        $draft->delete();
 
         if ($request->has('gallery_image')) {
             $file = $request->only('gallery_image', 'captionGallery', 'descriptionGallery');
             $type = 'gallery_image';
             $diskName = 'product';
             $this->uploadGallery($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->uploadFeature($file, $diskName, $product, $type, $info);
-
         }
 
         $msg = 'ذخیره محصول با موفقیت انجام شد ';
         return redirect(route('products.edit', $product->id))->with('success', $msg);
     }
-
     // ******************************************************edit page product*********************************************
-
     public function edit(Product $product)
     {
         $categories = Category::where('parent_id', 0)->orderBy('title')->get();
@@ -237,7 +235,6 @@ class ProductController extends Controller
 
     public function updateFeature($file, $diskName, $product, $type,$info)
     {
-
         $fileExtension = $file->getClientOriginalExtension();
 
         $fileMimeType = $file->getMimeType();
@@ -253,8 +250,6 @@ class ProductController extends Controller
         }
 
         $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
-
-
         $uploadData = [
             'name' => $fileName,
             'path' => $upload,
@@ -263,14 +258,8 @@ class ProductController extends Controller
             'type' => $type,
             'descriptionImg' => $info['featureDescription'],
             'caption' => $info['featureCaption'],
-
-
         ];
-
-
         $uploaded = $product->uploads()->update($uploadData);
-
-
     }
 
 //************************delete image in th product**********************
@@ -283,7 +272,6 @@ class ProductController extends Controller
     }
     public function uploadGallery($files, $diskName, $product, $type)
     {
-//        dd($files);
         foreach ($files['gallery_image'] as $key => $file) {
             $fileExtension = $file->getClientOriginalExtension();
 
@@ -298,10 +286,7 @@ class ProductController extends Controller
             if (File::exists($filePath)) {
                 $fileName = time() . '_' . $fileName;
             }
-
             $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
-
-
             $uploadData = [
                 'name' => $fileName,
                 'path' => $upload,
@@ -313,34 +298,23 @@ class ProductController extends Controller
             ];
 
             $uploaded = $product->uploads()->create($uploadData);
-
         }
-
     }
 
 
 
     public function updateGallery($files, $diskName, $product, $type)
     {
-//        dd($files);
         foreach ($files['gallery_image'] as $key => $file) {
             $fileExtension = $file->getClientOriginalExtension();
-
             $fileMimeType = $file->getMimeType();
-
             $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate()->format('m');
-
             $fileName = jdate(time())->format('Ymd') . '_' . $file->getClientOriginalName();
-
             $filePath = storage_path('app/public/' . $diskName . $afterDiskRoot . '/' . $fileName);
-
             if (File::exists($filePath)) {
                 $fileName = time() . '_' . $fileName;
             }
-
             $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
-
-
             $uploadData = [
                 'name' => $fileName,
                 'path' => $upload,
@@ -350,30 +324,25 @@ class ProductController extends Controller
                 'descriptionImg' => $files['descriptionGallery'][$key],
                 'caption' => $files['captionGallery'][$key],
             ];
-
             $uploaded = $product->uploads()->update($uploadData);
-
         }
-
     }
 
-    public function SaveFormProductByAjax(Request $request)
-    {
-        $title = $this->input->post('title');
-        $price = $this->input->post('price');
-        $slug = $this->input->post('slug');
-        $type= $this->input->post('type');
-        $ReleaseStatus = $this->input->post('ReleaseStatus');
-        $status = $this->input->post('status');
-        $discription = $this->input->post('discription');
-        $categories = $this->input->post('categories');
-        $featured_image = $this->input->post('featured_image');
-
-        $data=array('title' => $title,"price" =>  $price,"slug" => $slug,"type" => $type,"status" => $status,"discription" => $discription,"categories" => $categories,"featured_image" => $featured_image,"ReleaseStatus" => $ReleaseStatus);
-       $prouduct = Product::update($data);
-        return $prouduct->with('success', 'اطلاعات با موفقیت ثبت شد .') ;
-
-
-
-    }
+//    public function SaveFormProductByAjax(Request $request)
+//    {
+//        $title = $this->input->post('title');
+//        $price = $this->input->post('price');
+//        $slug = $this->input->post('slug');
+//        $type= $this->input->post('type');
+//        $ReleaseStatus = $this->input->post('ReleaseStatus');
+//        $status = $this->input->post('status');
+//        $discription = $this->input->post('discription');
+//        $categories = $this->input->post('categories');
+//        $featured_image = $this->input->post('featured_image');
+//
+//        $data=array('title' => $title,"price" =>  $price,"slug" => $slug,"type" => $type,"status" => $status,"discription" => $discription,"categories" => $categories,"featured_image" => $featured_image,"ReleaseStatus" => $ReleaseStatus);
+//       $prouduct = Product::update($data);
+//        return $prouduct->with('success', 'اطلاعات با موفقیت ثبت شد .') ;
+//
+//    }
 }

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

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

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

@@ -3,6 +3,7 @@
 namespace Packages\Product\Models;
 use App\Models\Category;
 use App\Models\Upload;
+use Packages\Product\Traits\Attachments;
 use Illuminate\Database\Eloquent\Model;
 use App\User;
 use Illuminate\Database\Eloquent\SoftDeletes;
@@ -11,17 +12,15 @@ use Cviebrock\EloquentSluggable\Sluggable;
 class Product extends Model
 {
     use Sluggable;
+    use Attachments;
     use SoftDeletes;
 
    protected $fillable = ['title', 'discription', 'price', 'creator_id', 'type', 'status', 'sale_price', 'sku', 'ReleaseStatus'];
 //    protected $guarded = [];
-
     public function user()
     {
         return $this->belongsTo(User::class, 'creator_id', 'id');
     }
-
-
     public function categories()
     {
         return $this->morphToMany(Category::class, 'categorizable');
@@ -57,10 +56,8 @@ class Product extends Model
         }
         return $type;
     }
-
     public function getStatusAttribute($value)
     {
-
         switch ($value) {
             case 0:
                 $type = 'ناموجود';
@@ -68,16 +65,13 @@ class Product extends Model
             case 1:
                 $type = 'موجود';
                 break;
-
             default:
                 $type = '';
-
         }
         return $type;
     }
     public function getReleaseStatusAttribute($value)
     {
-
         switch ($value) {
             case 'draft':
                 $ReleaseStatus = 'پیشنویس';
@@ -88,10 +82,8 @@ class Product extends Model
                 case 'published':
                 $ReleaseStatus = 'منتشر شده';
                 break;
-
             default:
                 $ReleaseStatus = '';
-
         }
         return $ReleaseStatus;
     }

+ 3 - 1
packages/product/src/Traits/Attachments.php

@@ -2,8 +2,10 @@
 
 namespace Packages\Product\Traits;
 
+use Packages\Product\Models\Attachment;
+
 trait Attachments{
     public function attachments(){
-        return $this->morphMany('App\Attachment', 'parent');
+        return $this->morphMany(Attachment::class, 'parent');
     }
 }

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

@@ -16,7 +16,7 @@ Route::group([
 
 
 
-Route::post('/product/SaveFormProductByAjax','ProductControllerController@SaveFormProductByAjax')->name('SaveFormProductByAjax');
+//Route::post('/product/SaveFormProductByAjax','ProductController@SaveFormProductByAjax')->name('SaveFormProductByAjax');
 
 
 

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

@@ -25,7 +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 }}">--}}
+                                    <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>
@@ -35,6 +35,7 @@
                                                 </span>
                                         @enderror
                                     </div>
+
                                     <div class="form-row">
                                         <div class="form-group col-lg-4">
                                             <label for="slug">{{ __('product.slug') }}</label>

+ 0 - 38
resources/views/panel/categories/create.blade.php

@@ -122,44 +122,6 @@
     @endslot
 
     @slot('script')
-        <script>
-            {{--$(document).ready(function() {--}}
-            {{--    $(".sendForme").click(function(e){--}}
-            {{--        e.preventDefault();--}}
-
-            {{--        var title = $("input[name='title']").val();--}}
-            {{--        var price = $("input[name='price']").val();--}}
-            {{--        var slug = $("input[name='slug']").val();--}}
-            {{--        var type = $("input[name='type']").val();--}}
-            {{--        var status = $("input[name='status']").val();--}}
-            {{--        var discription = $("textarea[name='discription']").val();--}}
-
-            {{--        $.ajax({--}}
-            {{--            url: {{route('product.myformProduct')}},--}}
-            {{--            method:'POST',--}}
-            {{--            data: {title:title, price:price, slug:slug, type:type, status:status, discription:discription},--}}
-            {{--            dataType:'json',--}}
-            {{--            success: function(data) {--}}
-            {{--                if($.isEmptyObject(data.error)){--}}
-            {{--                    alert(data.success);--}}
-            {{--                }else{--}}
-            {{--                    printErrorMsg(data.error);--}}
-            {{--                }--}}
-            {{--            }--}}
-            {{--        });--}}
-            {{--    });--}}
-
-            {{--    function printErrorMsg (msg) {--}}
-            {{--        $(".print-error-msg").find("ul").html('');--}}
-            {{--        $(".print-error-msg").css('display','block');--}}
-            {{--        $.each( msg, function( key, value ) {--}}
-            {{--            $(".print-error-msg").find("ul").append('<li>'+value+'</li>');--}}
-            {{--        });--}}
-            {{--    }--}}
-            {{--});--}}
-
-
-        </script>
     @endslot
 
 @endcomponent