|
@@ -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', 'اطلاعات با موفقیت ثبت شد .') ;
|
|
|
+//
|
|
|
+// }
|
|
|
}
|