_config = request('_config'); } /** * Display a listing of the resource. * * @return \Illuminate\View\View */ public function index() { if (request()->ajax()) { return app(ProductDataGrid::class)->toJson(); } return view($this->_config['view']); } /** * Show the form for creating a new resource. * * @return \Illuminate\View\View */ public function create() { $families = $this->attributeFamilyRepository->all(); $configurableFamily = null; if ($familyId = request()->get('family')) { $configurableFamily = $this->attributeFamilyRepository->find($familyId); } return view($this->_config['view'], compact('families', 'configurableFamily')); } /** * Store a newly created resource in storage. * * @return \Illuminate\Http\Response */ public function store() { if ( ! request()->get('family') && ProductType::hasVariants(request()->input('type')) && request()->input('sku') != '' ) { return redirect(url()->current() . '?type=' . request()->input('type') . '&family=' . request()->input('attribute_family_id') . '&sku=' . request()->input('sku')); } if ( ProductType::hasVariants(request()->input('type')) && (! request()->has('super_attributes') || ! count(request()->get('super_attributes'))) ) { session()->flash('error', trans('admin::app.catalog.products.configurable-error')); return back(); } $this->validate(request(), [ 'type' => 'required', 'attribute_family_id' => 'required', 'sku' => ['required', 'unique:products,sku', new Slug], ]); Event::dispatch('catalog.product.create.before'); $product = $this->productRepository->create(request()->all()); Event::dispatch('catalog.product.create.after', $product); session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Product'])); return redirect()->route($this->_config['redirect'], ['id' => $product->id]); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\View\View */ public function edit($id) { $product = $this->productRepository->with(['variants', 'variants.inventories'])->findOrFail($id); $categories = $this->categoryRepository->getCategoryTree(); $inventorySources = $this->inventorySourceRepository->findWhere(['status' => 1]); return view($this->_config['view'], compact('product', 'categories', 'inventorySources')); } /** * Update the specified resource in storage. * * @param \Webkul\Product\Http\Requests\ProductForm $request * @param int $id * @return \Illuminate\Http\Response */ public function update(ProductForm $request, $id) { foreach (request()->images['files'] as $fileExists) { if (! $fileExists) { session()->flash('error', trans('admin::app.admin.system.image-required')); return redirect()->back(); } } $this->productRepository->update(request()->all(), $id); Event::dispatch('catalog.product.update.before', $id); $product = $this->productRepository->update(request()->all(), $id); Event::dispatch('catalog.product.update.after', $product); session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Product'])); return redirect()->route($this->_config['redirect']); } /** * Update inventories. * * @param int $id * @return \Illuminate\Http\Response */ public function updateInventories(InventoryRequest $inventoryRequest, $id) { $product = $this->productRepository->findOrFail($id); $this->productInventoryRepository->saveInventories(request()->all(), $product); return response()->json([ 'message' => __('admin::app.catalog.products.saved-inventory-message'), 'updatedTotal' => $this->productInventoryRepository->where('product_id', $product->id)->sum('qty'), ]); } /** * Uploads downloadable file. * * @param int $id * @return \Illuminate\Http\Response */ public function uploadLink($id) { return response()->json( $this->productDownloadableLinkRepository->upload(request()->all(), $id) ); } /** * Copy a given Product. * * @param int $productId * @return \Illuminate\Http\Response */ public function copy(int $productId) { $originalProduct = $this->productRepository->findOrFail($productId); if (! $originalProduct->getTypeInstance()->canBeCopied()) { session()->flash( 'error', trans('admin::app.response.product-can-not-be-copied', [ 'type' => $originalProduct->type, ]) ); return redirect()->to(route('admin.catalog.products.index')); } if ($originalProduct->parent_id) { session()->flash( 'error', trans('admin::app.catalog.products.variant-already-exist-message') ); return redirect()->to(route('admin.catalog.products.index')); } $copiedProduct = $this->productRepository->copy($originalProduct); if ( $copiedProduct instanceof Product && $copiedProduct->id ) { session()->flash('success', trans('admin::app.response.product-copied')); } else { session()->flash('error', trans('admin::app.response.error-while-copying')); } return redirect()->to(route('admin.catalog.products.edit', ['id' => $copiedProduct->id])); } /** * Uploads downloadable sample file. * * @param int $id * @return \Illuminate\Http\Response */ public function uploadSample($id) { return response()->json( $this->productDownloadableSampleRepository->upload(request()->all(), $id) ); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $product = $this->productRepository->findOrFail($id); try { Event::dispatch('catalog.product.delete.before', $id); $this->productRepository->delete($id); Event::dispatch('catalog.product.delete.after', $id); return response()->json([ 'message' => trans('admin::app.response.delete-success', ['name' => 'Product']), ]); } catch (Exception $e) { report($e); } return response()->json([ 'message' => trans('admin::app.response.delete-failed', ['name' => 'Product']), ], 500); } /** * Mass delete the products. * * @return \Illuminate\Http\Response */ public function massDestroy() { $productIds = explode(',', request()->input('indexes')); foreach ($productIds as $productId) { $product = $this->productRepository->find($productId); if (isset($product)) { Event::dispatch('catalog.product.delete.before', $productId); $this->productRepository->delete($productId); Event::dispatch('catalog.product.delete.after', $productId); } } session()->flash('success', trans('admin::app.catalog.products.mass-delete-success')); return redirect()->route($this->_config['redirect']); } /** * Mass update the products. * * @return \Illuminate\Http\Response */ public function massUpdate() { $data = request()->all(); if (! isset($data['massaction-type'])) { return redirect()->back(); } if (! $data['massaction-type'] == 'update') { return redirect()->back(); } $productIds = explode(',', $data['indexes']); foreach ($productIds as $productId) { Event::dispatch('catalog.product.update.before', $productId); $product = $this->productRepository->update([ 'channel' => null, 'locale' => null, 'status' => $data['update-options'], ], $productId); Event::dispatch('catalog.product.update.after', $product); } session()->flash('success', trans('admin::app.catalog.products.mass-update-success')); return redirect()->route($this->_config['redirect']); } /** * To be manually invoked when data is seeded into products. * * @return \Illuminate\Http\Response */ public function sync() { Event::dispatch('products.datagrid.sync', true); return redirect()->route('admin.catalog.products.index'); } /** * Result of search product. * * @return \Illuminate\View\View|\Illuminate\Http\Response */ public function productLinkSearch() { if (request()->ajax()) { $results = []; foreach ($this->productRepository->searchProductByAttribute(request()->input('query')) as $row) { $results[] = [ 'id' => $row->product_id, 'sku' => $row->sku, 'name' => $row->name, ]; } return response()->json($results); } else { return view($this->_config['view']); } } /** * Download image or file. * * @param int $productId * @param int $attributeId * @return \Illuminate\Http\Response */ public function download($productId, $attributeId) { $productAttribute = $this->productAttributeValueRepository->findOneWhere([ 'product_id' => $productId, 'attribute_id' => $attributeId, ]); return Storage::download($productAttribute['text_value']); } /** * Search simple products. * * @return \Illuminate\Http\Response */ public function searchSimpleProducts() { return response()->json( $this->productRepository->searchSimpleProducts(request()->input('query')) ); } }