123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- namespace Packages\Product\Http\Controllers;
- use Illuminate\Support\Facades\Auth;
- use App\Http\Controllers\Controller;
- use Facade\Ignition\Support\Packagist\Package;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\Facade;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- use Packages\Product\Models\Draft;
- use Packages\Product\Http\Requests\ProductRequest;
- use Packages\Product\Http\Requests\ProductRequestEdit;
- use Packages\Product\Models\Product;
- use Illuminate\Http\Request;
- use App\Models\Category;
- use App\Models\Upload;
- use phpDocumentor\Reflection\Types\Compound;
- class ProductController extends Controller
- {
- public function __construct()
- {
- }
- // *******************************************show page product ************
- 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') != '')
- $products = $products->whereHas('user', function (Builder $query) {
- $query->whereRaw('name= ?', [request('creatorId')]);
- });
- $products = $products->paginate();
- 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();
- $uploads = Upload::orderBy('id')->get()->first();
- if (!$request->has('draft_id')) {
- $draft = new Draft();
- $draft->object_type = 'Packages\Product\Draft';
- $draft->delete();
- $draft->save();
- return redirect()->route('products.create', ['draft_id' => $draft->id]);
- } else {
- $draft = Draft::find($request->draft_id);
- if (is_null($draft)) {
- return redirect()->route('products.create');
- }
- }
- $args = [
- 'draft' => $draft
- ];
- $product= Product:: orderBy('id', 'DESC');
- return view('product::product.create', $args, compact('categories', 'product', 'uploads'));
- }
- // *********************************************************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),
- ]);
- 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,
- 'sku' => $request->sku,
- 'ReleaseStatus' => $request->ReleaseStatus,
- 'price' => $request->price,
- 'sale_price' => $request->sale_price,
- 'type' => $request->type,
- 'status' => $request->status,
- 'discription' => $request->discription,
- 'creator_id' => auth()->user()->id,
- // 'descriptionImg' => $request->featureDescription,
- // 'caption' => $request->featureCaption,
- ];
- // if( $draft->attachments->count() == 0 ){
- // return redirect()->back()
- // ->withErrors( ['error_message' => 'حتما باید یک فایل آپلود شود.'] )
- // ->withInput( $request->all() );
- // }
- // dd($draft->attachments->count() == 0);
- $product = Product::create($data);
- $product->categories()->sync($request->categories);
- 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();
- $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();
- return view('product::product.edit', compact('product', 'categories'));
- }
- // *******************************************update page product*****************************************
- public function update(ProductRequestEdit $request, Product $product)
- {
- $request->merge([
- 'price' => preg_replace('/[^0-9]+/', '', $request->price),
- 'sale_price' => preg_replace('/[^0-9]+/', '', $request->sale_price),
- ]);
- $data = [
- 'title' => $request->title,
- 'slug' => $request->slug,
- 'sku' => $request->sku,
- 'price' => $request->price,
- 'sale_price' => $request->sale_price,
- 'type' => $request->type,
- 'ReleaseStatus' => $request->ReleaseStatus,
- 'status' => $request->status,
- 'discription' => $request->discription,
- 'creator_id' => auth()->user()->id,
- ];
- if ($request->has('gallery_image')) {
- $file = $request->only('gallery_image', 'captionGallery', 'descriptionGallery');
- $type = 'gallery_image';
- $diskName = 'product';
- $this->updateGallery($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->updateFeature($file, $diskName, $product, $type, $info);
- }
- $product->update($data);
- $product->categories()->sync($request->categories);
- $msg = 'ویرایش محصول با موفقیت انجام شد ';
- return redirect(route('products.index'))->with('success', $msg);
- }
- //***************************** delete products*****************************************
- public function destroy(Product $product)
- {
- $product->delete();
- {
- return back();
- }
- }
- //****************upload images in the product************************
- public function uploadFeature(Request $request)
- {
- if ($request->has('featured_image')) {
- $info = $request->only(['featureDescription', 'featureCaption']);
- $file = $request->file('featured_image');
- $type = 'featured_image';
- $diskName = 'product';
- $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;
- }
- $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' => $info['featureDescription'],
- 'caption' => $info['featureCaption'],
- ];
- $uploads = Upload::where('type', $type)->where('uploadable_id', $draft->id)->first();
- if (empty($uploads)) {
- $uploaded = Upload::create($uploadData);
- } else {
- $uploaded = $uploads->update($uploadData);
- }
- }
- }
- public function updateFeature($file, $diskName, $product, $type, $info)
- {
- $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,
- 'mime_type' => $fileMimeType,
- 'extension' => $fileExtension,
- 'type' => $type,
- 'descriptionImg' => $info['featureDescription'],
- 'caption' => $info['featureCaption'],
- ];
- $uploaded = $product->uploads()->update($uploadData);
- }
- //************************delete image in the product**********************
- public function destroyFile(Upload $upload)
- {
- $upload->delete();
- $msg = 'حذف عکس محصول با موفقیت انجام شد ';
- session()->flash('image_deleted', $msg);
- return redirect()->back();
- }
- // ****************************uploadGallery********************
- public function uploadGallery(Request $request)
- {
- 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');
- $fileName = jdate(time())->format('Ymd') . '_' . $file->getClientOriginalName();
- $filePath = storage_path('app/public/' . $diskName . $afterDiskRoot . '/' . $fileName);
- 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],
- ];
- // dd($uploadData);
- // $uploaded = Upload::where('uploadable_id', $draft->id)->update($uploadData);
- $uploaded = Upload::create($uploadData);
- dd($uploaded);
- }
- }
- }
- public function updateGallery($files, $diskName, $product, $type)
- {
- 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,
- 'mime_type' => $fileMimeType,
- 'extension' => $fileExtension,
- 'type' => $type,
- 'descriptionImg' => $files['descriptionGallery'][$key],
- 'caption' => $files['captionGallery'][$key],
- ];
- $uploaded = $product->uploads()->update($uploadData);
- }
- }
- }
|