sarga/packages/Webkul/Product/src/Http/Controllers/ProductController.php

414 lines
12 KiB
PHP
Raw Normal View History

2018-07-27 06:22:12 +00:00
<?php
namespace Webkul\Product\Http\Controllers;
2020-07-30 12:15:54 +00:00
use Exception;
2018-12-21 12:48:34 +00:00
use Illuminate\Support\Facades\Event;
2019-11-21 15:56:21 +00:00
use Webkul\Product\Helpers\ProductType;
2020-07-30 12:15:54 +00:00
use Illuminate\Support\Facades\Storage;
use Webkul\Core\Contracts\Validations\Slug;
use Webkul\Product\Http\Requests\ProductForm;
2019-06-28 14:18:52 +00:00
use Webkul\Product\Repositories\ProductRepository;
2020-07-30 12:15:54 +00:00
use Webkul\Category\Repositories\CategoryRepository;
2019-06-28 14:18:52 +00:00
use Webkul\Attribute\Repositories\AttributeFamilyRepository;
use Webkul\Inventory\Repositories\InventorySourceRepository;
2020-07-30 12:15:54 +00:00
use Webkul\Product\Repositories\ProductDownloadableLinkRepository;
use Webkul\Product\Repositories\ProductDownloadableSampleRepository;
2018-07-27 06:22:12 +00:00
class ProductController extends Controller
{
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
2018-07-31 07:50:54 +00:00
/**
2019-06-28 14:18:52 +00:00
* CategoryRepository object
2018-07-31 07:50:54 +00:00
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Category\Repositories\CategoryRepository
2018-07-31 07:50:54 +00:00
*/
2019-06-28 14:18:52 +00:00
protected $categoryRepository;
2018-08-09 04:35:13 +00:00
/**
2019-06-28 14:18:52 +00:00
* ProductRepository object
2018-08-09 04:35:13 +00:00
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Product\Repositories\ProductRepository
2018-08-09 04:35:13 +00:00
*/
2019-06-28 14:18:52 +00:00
protected $productRepository;
2018-08-09 04:35:13 +00:00
/**
2019-06-28 14:18:52 +00:00
* ProductDownloadableLinkRepository object
2018-08-09 04:35:13 +00:00
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Product\Repositories\ProductDownloadableLinkRepository
2018-08-09 04:35:13 +00:00
*/
2019-06-28 14:18:52 +00:00
protected $productDownloadableLinkRepository;
2018-07-27 06:22:12 +00:00
/**
2019-06-28 14:18:52 +00:00
* ProductDownloadableSampleRepository object
2018-07-27 06:22:12 +00:00
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Product\Repositories\ProductDownloadableSampleRepository
2018-07-27 06:22:12 +00:00
*/
2019-06-28 14:18:52 +00:00
protected $productDownloadableSampleRepository;
2018-07-27 06:22:12 +00:00
/**
2019-06-28 14:18:52 +00:00
* AttributeFamilyRepository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Attribute\Repositories\AttributeFamilyRepository
*/
2019-06-28 14:18:52 +00:00
protected $attributeFamilyRepository;
/**
* InventorySourceRepository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Inventory\Repositories\InventorySourceRepository
2019-06-28 14:18:52 +00:00
*/
protected $inventorySourceRepository;
2018-07-27 06:22:12 +00:00
/**
* Create a new controller instance.
*
2020-07-30 12:15:54 +00:00
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\Product\Repositories\ProductRepository $productRepository
* @param \Webkul\Product\Repositories\ProductDownloadableLinkRepository $productDownloadableLinkRepository
* @param \Webkul\Product\Repositories\ProductDownloadableSampleRepository $productDownloadableSampleRepository
* @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository
* @param \Webkul\Inventory\Repositories\InventorySourceRepository $inventorySourceRepository
*
2018-07-27 06:22:12 +00:00
* @return void
*/
2018-08-09 04:35:13 +00:00
public function __construct(
2019-06-28 14:18:52 +00:00
CategoryRepository $categoryRepository,
ProductRepository $productRepository,
ProductDownloadableLinkRepository $productDownloadableLinkRepository,
ProductDownloadableSampleRepository $productDownloadableSampleRepository,
AttributeFamilyRepository $attributeFamilyRepository,
2019-08-21 13:54:26 +00:00
InventorySourceRepository $inventorySourceRepository
2019-05-09 11:19:52 +00:00
)
2018-07-27 06:22:12 +00:00
{
2019-06-28 14:18:52 +00:00
$this->_config = request('_config');
2018-07-31 07:50:54 +00:00
2019-06-28 14:18:52 +00:00
$this->categoryRepository = $categoryRepository;
2018-08-09 04:35:13 +00:00
2019-06-28 14:18:52 +00:00
$this->productRepository = $productRepository;
2018-08-09 04:35:13 +00:00
2019-06-28 14:18:52 +00:00
$this->productDownloadableLinkRepository = $productDownloadableLinkRepository;
2018-07-27 06:22:12 +00:00
2019-06-28 14:18:52 +00:00
$this->productDownloadableSampleRepository = $productDownloadableSampleRepository;
2019-06-28 14:18:52 +00:00
$this->attributeFamilyRepository = $attributeFamilyRepository;
$this->inventorySourceRepository = $inventorySourceRepository;
2018-07-27 06:22:12 +00:00
}
/**
* Display a listing of the resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-07-27 06:22:12 +00:00
*/
public function index()
{
return view($this->_config['view']);
}
/**
* Show the form for creating a new resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-07-27 06:22:12 +00:00
*/
public function create()
{
2019-06-28 14:18:52 +00:00
$families = $this->attributeFamilyRepository->all();
2018-07-31 07:50:54 +00:00
2019-01-18 05:30:55 +00:00
$configurableFamily = null;
2019-01-15 11:54:41 +00:00
if ($familyId = request()->get('family')) {
2019-06-28 14:18:52 +00:00
$configurableFamily = $this->attributeFamilyRepository->find($familyId);
2018-07-31 07:50:54 +00:00
}
return view($this->_config['view'], compact('families', 'configurableFamily'));
2018-07-27 06:22:12 +00:00
}
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store()
{
2019-06-28 14:18:52 +00:00
if (! request()->get('family')
2019-11-21 15:56:21 +00:00
&& ProductType::hasVariants(request()->input('type'))
2020-02-28 10:25:53 +00:00
&& request()->input('sku') != ''
) {
2019-06-28 14:18:52 +00:00
return redirect(url()->current() . '?type=' . request()->input('type') . '&family=' . request()->input('attribute_family_id') . '&sku=' . request()->input('sku'));
2018-08-01 06:11:33 +00:00
}
2019-11-21 15:56:21 +00:00
if (ProductType::hasVariants(request()->input('type'))
2019-06-28 14:18:52 +00:00
&& (! request()->has('super_attributes')
2020-07-30 12:15:54 +00:00
|| ! count(request()->get('super_attributes')))
2020-02-28 10:25:53 +00:00
) {
2019-02-13 12:12:07 +00:00
session()->flash('error', trans('admin::app.catalog.products.configurable-error'));
2018-08-01 06:11:33 +00:00
return back();
2018-07-31 07:50:54 +00:00
}
2018-07-27 06:22:12 +00:00
$this->validate(request(), [
2020-02-20 06:12:17 +00:00
'type' => 'required',
2018-07-31 07:50:54 +00:00
'attribute_family_id' => 'required',
2020-07-30 12:15:54 +00:00
'sku' => ['required', 'unique:products,sku', new Slug],
2018-07-27 06:22:12 +00:00
]);
2019-06-28 14:18:52 +00:00
$product = $this->productRepository->create(request()->all());
2018-07-27 06:22:12 +00:00
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Product']));
2018-07-27 06:22:12 +00:00
2018-07-31 07:50:54 +00:00
return redirect()->route($this->_config['redirect'], ['id' => $product->id]);
2018-07-27 06:22:12 +00:00
}
/**
* Show the form for editing the specified resource.
*
2020-07-30 12:15:54 +00:00
* @param int $id
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-07-27 06:22:12 +00:00
*/
public function edit($id)
{
2019-06-28 14:18:52 +00:00
$product = $this->productRepository->with(['variants', 'variants.inventories'])->findOrFail($id);
2018-07-27 06:22:12 +00:00
2019-06-28 14:18:52 +00:00
$categories = $this->categoryRepository->getCategoryTree();
2018-08-09 04:35:13 +00:00
2020-06-11 06:06:32 +00:00
$inventorySources = $this->inventorySourceRepository->findWhere(['status' => 1]);
2018-08-09 04:35:13 +00:00
2019-05-21 14:41:36 +00:00
return view($this->_config['view'], compact('product', 'categories', 'inventorySources'));
2018-07-27 06:22:12 +00:00
}
/**
* Update the specified resource in storage.
*
2020-07-30 12:15:54 +00:00
* @param \Webkul\Product\Http\Requests\ProductForm $request
* @param int $id
*
2018-07-27 06:22:12 +00:00
* @return \Illuminate\Http\Response
*/
2018-08-09 04:35:13 +00:00
public function update(ProductForm $request, $id)
2018-07-27 06:22:12 +00:00
{
2020-07-27 13:40:00 +00:00
$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') {
2020-07-30 12:15:54 +00:00
array_push($multiselectAttributeCodes, $attribute->code);
}
2020-07-27 13:40:00 +00:00
}
}
}
if (count($multiselectAttributeCodes)) {
foreach ($multiselectAttributeCodes as $multiselectAttributeCode) {
2020-07-30 12:15:54 +00:00
if (! isset($data[$multiselectAttributeCode])) {
2020-07-27 13:40:00 +00:00
$data[$multiselectAttributeCode] = array();
}
2020-07-30 12:15:54 +00:00
}
2020-07-27 13:40:00 +00:00
}
2020-07-30 12:15:54 +00:00
2020-07-27 13:40:00 +00:00
$product = $this->productRepository->update($data, $id);
2018-07-27 06:22:12 +00:00
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Product']));
2018-07-27 06:22:12 +00:00
return redirect()->route($this->_config['redirect']);
}
2019-06-28 14:18:52 +00:00
/**
* Uploads downloadable file
*
2020-07-30 12:15:54 +00:00
* @param int $id
*
2019-06-28 14:18:52 +00:00
* @return \Illuminate\Http\Response
*/
public function uploadLink($id)
{
return response()->json(
$this->productDownloadableLinkRepository->upload(request()->all(), $id)
);
}
2020-07-30 12:15:54 +00:00
/**
* 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)
{
$copiedProduct = $this->productRepository->copy($productId);
2020-07-30 12:15:54 +00:00
return redirect()->to(route('admin.catalog.products.edit', ['id' => $copiedProduct->id]));
}
2019-06-28 14:18:52 +00:00
/**
* Uploads downloadable sample file
*
2020-07-30 12:15:54 +00:00
* @param int $id
*
2019-06-28 14:18:52 +00:00
* @return \Illuminate\Http\Response
*/
public function uploadSample($id)
{
return response()->json(
$this->productDownloadableSampleRepository->upload(request()->all(), $id)
);
}
2018-07-27 06:22:12 +00:00
/**
* Remove the specified resource from storage.
*
2020-07-30 12:15:54 +00:00
* @param int $id
*
2018-07-27 06:22:12 +00:00
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
2019-06-28 14:18:52 +00:00
$product = $this->productRepository->findOrFail($id);
2018-10-17 10:30:31 +00:00
2019-04-09 01:01:52 +00:00
try {
2019-06-28 14:18:52 +00:00
$this->productRepository->delete($id);
2018-10-17 10:30:31 +00:00
2019-04-09 01:01:52 +00:00
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Product']));
return response()->json(['message' => true], 200);
2020-07-30 12:15:54 +00:00
} catch (Exception $e) {
2020-01-27 16:06:03 +00:00
report($e);
2020-02-27 08:03:03 +00:00
2019-04-09 01:01:52 +00:00
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Product']));
}
return response()->json(['message' => false], 400);
2018-07-27 06:22:12 +00:00
}
/**
* Mass Delete the products
*
2020-03-05 13:37:08 +00:00
* @return \Illuminate\Http\Response
*/
public function massDestroy()
{
$productIds = explode(',', request()->input('indexes'));
2018-12-21 12:48:34 +00:00
foreach ($productIds as $productId) {
2019-06-28 14:18:52 +00:00
$product = $this->productRepository->find($productId);
2019-04-02 07:36:13 +00:00
if (isset($product)) {
2019-06-28 14:18:52 +00:00
$this->productRepository->delete($productId);
2019-04-02 07:36:13 +00:00
}
}
session()->flash('success', trans('admin::app.catalog.products.mass-delete-success'));
return redirect()->route($this->_config['redirect']);
}
/**
* Mass updates the products
*
2020-03-05 13:37:08 +00:00
* @return \Illuminate\Http\Response
*/
public function massUpdate()
{
$data = request()->all();
2019-11-07 09:58:10 +00:00
if (! isset($data['massaction-type'])) {
return redirect()->back();
}
2020-02-20 06:12:17 +00:00
if (! $data['massaction-type'] == 'update') {
return redirect()->back();
}
$productIds = explode(',', $data['indexes']);
foreach ($productIds as $productId) {
2019-06-28 14:18:52 +00:00
$this->productRepository->update([
2019-01-22 05:05:05 +00:00
'channel' => null,
2020-02-20 06:12:17 +00:00
'locale' => null,
2020-03-04 06:32:53 +00:00
'status' => $data['update-options'],
2019-01-22 05:05:05 +00:00
], $productId);
}
session()->flash('success', trans('admin::app.catalog.products.mass-update-success'));
return redirect()->route($this->_config['redirect']);
}
2020-03-05 13:37:08 +00:00
/**
* To be manually invoked when data is seeded into products
2020-06-10 14:37:50 +00:00
*
2020-03-05 13:37:08 +00:00
* @return \Illuminate\Http\Response
*/
public function sync()
{
2019-12-24 14:01:13 +00:00
Event::dispatch('products.datagrid.sync', true);
return redirect()->route('admin.catalog.products.index');
}
2019-03-12 15:35:10 +00:00
/**
* Result of search product.
*
2020-03-05 13:37:08 +00:00
* @return \Illuminate\View\View|\Illuminate\Http\Response
2019-03-12 15:35:10 +00:00
*/
public function productLinkSearch()
{
if (request()->ajax()) {
$results = [];
2019-06-28 14:18:52 +00:00
foreach ($this->productRepository->searchProductByAttribute(request()->input('query')) as $row) {
2019-03-12 15:35:10 +00:00
$results[] = [
2020-02-27 08:03:03 +00:00
'id' => $row->product_id,
'sku' => $row->sku,
'name' => $row->name,
];
2019-03-12 15:35:10 +00:00
}
return response()->json($results);
} else {
return view($this->_config['view']);
}
}
2020-07-30 12:15:54 +00:00
/**
* Download image or file
*
2020-07-30 12:15:54 +00:00
* @param int $productId
* @param int $attributeId
*
* @return \Illuminate\Http\Response
*/
public function download($productId, $attributeId)
{
$productAttribute = $this->productAttributeValue->findOneWhere([
'product_id' => $productId,
2020-03-04 06:32:53 +00:00
'attribute_id' => $attributeId,
]);
return Storage::download($productAttribute['text_value']);
}
2019-08-19 09:30:24 +00:00
/**
2019-08-21 13:54:26 +00:00
* Search simple products
2019-08-19 09:30:24 +00:00
*
2020-03-05 13:37:08 +00:00
* @return \Illuminate\Http\Response
2019-08-19 09:30:24 +00:00
*/
2019-08-21 13:54:26 +00:00
public function searchSimpleProducts()
2019-08-19 09:30:24 +00:00
{
return response()->json(
2019-08-21 13:54:26 +00:00
$this->productRepository->searchSimpleProducts(request()->input('query'))
2019-08-19 09:30:24 +00:00
);
}
2018-07-27 06:22:12 +00:00
}