_config = request('_config'); $this->categoryRepository = $categoryRepository; $this->productRepository = $productRepository; $this->productDownloadableLinkRepository = $productDownloadableLinkRepository; $this->productDownloadableSampleRepository = $productDownloadableSampleRepository; $this->attributeFamilyRepository = $attributeFamilyRepository; $this->inventorySourceRepository = $inventorySourceRepository; } /** * Display a listing of the resource. * * @return \Illuminate\View\View */ public function index() { 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], ]); $product = $this->productRepository->create(request()->all()); 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) { $data = request()->all(); $multiselectAttributeCodes = array(); $productAttributes = $this->productRepository->findOrFail($id); foreach ($productAttributes->attribute_family->attribute_groups as $attributeGroup) { $customAttributes = $productAttributes->getEditableAttributes($attributeGroup); if (count($customAttributes)) { foreach ($customAttributes as $attribute) { if ($attribute->type == 'multiselect') { array_push($multiselectAttributeCodes, $attribute->code); } } } } if (count($multiselectAttributeCodes)) { foreach ($multiselectAttributeCodes as $multiselectAttributeCode) { if (! isset($data[$multiselectAttributeCode])) { $data[$multiselectAttributeCode] = array(); } } } $product = $this->productRepository->update($data, $id); session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Product'])); return redirect()->route($this->_config['redirect']); } /** * 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. Do not copy the images. * Always make the copy is inactive so the admin is able to configure it before setting it live. */ public function copy(int $productId) { $originalProduct = $this->productRepository ->findOrFail($productId) ->load('attribute_family') ->load('categories') ->load('customer_group_prices') ->load('inventories') ->load('inventory_sources'); $copiedProduct = $originalProduct ->replicate() ->fill([ // the sku and url_key needs to be unique and should be entered again newly by the admin: 'sku' => 'temporary-sku-' . substr(md5(microtime()), 0, 6), ]); $copiedProduct->save(); $attributeName = Attribute::query()->where(['code' => 'name'])->firstOrFail(); $attributeSku = Attribute::query()->where(['code' => 'sku'])->firstOrFail(); $attributeStatus = Attribute::query()->where(['code' => 'status'])->firstOrFail(); $attributeUrlKey = Attribute::query()->where(['code' => 'url_key'])->firstOrFail(); $newProductFlat = new ProductFlat(); // only obey copied locale and channel: if (isset($originalProduct->product_flats[0])) { $newProductFlat = $originalProduct->product_flats[0]->replicate(); } $newProductFlat->product_id = $copiedProduct->id; foreach ($originalProduct->attribute_values as $oldValue) { $newValue = $oldValue->replicate(); if ($oldValue->attribute_id === $attributeName->id) { $newValue->text_value = __('admin::app.copy-of') . ' ' . $originalProduct->name; $newProductFlat->name = __('admin::app.copy-of') . ' ' . $originalProduct->name; } if ($oldValue->attribute_id === $attributeUrlKey->id) { $newValue->text_value = __('admin::app.copy-of-slug') . '-' . $originalProduct->url_key; $newProductFlat->url_key = __('admin::app.copy-of-slug') . '-' . $originalProduct->url_key; } if ($oldValue->attribute_id === $attributeSku->id) { $newValue->text_value = $copiedProduct->sku; $newProductFlat->sku = $copiedProduct->sku; } if ($oldValue->attribute_id === $attributeStatus->id) { $newValue->boolean_value = 0; $newProductFlat->status = 0; } $copiedProduct->attribute_values()->save($newValue); } $newProductFlat->save(); foreach ($originalProduct->categories as $category) { $copiedProduct->categories()->save($category); } foreach ($originalProduct->inventories as $inventory) { $copiedProduct->inventories()->save($inventory); } foreach ($originalProduct->customer_group_prices as $customer_group_price) { $copiedProduct->customer_group_prices()->save($customer_group_price); } 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 { $this->productRepository->delete($id); session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Product'])); return response()->json(['message' => true], 200); } catch (Exception $e) { report($e); session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Product'])); } return response()->json(['message' => false], 400); } /** * 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)) { $this->productRepository->delete($productId); } } session()->flash('success', trans('admin::app.catalog.products.mass-delete-success')); return redirect()->route($this->_config['redirect']); } /** * Mass updates 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) { $this->productRepository->update([ 'channel' => null, 'locale' => null, 'status' => $data['update-options'], ], $productId); } 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->productAttributeValue->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')) ); } }