2018-07-27 06:22:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Product\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
2018-12-21 12:48:34 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2018-08-09 04:35:13 +00:00
|
|
|
use Webkul\Product\Http\Requests\ProductForm;
|
2018-07-27 06:22:12 +00:00
|
|
|
use Webkul\Product\Repositories\ProductRepository as Product;
|
2018-10-22 13:18:46 +00:00
|
|
|
use Webkul\Product\Repositories\ProductGridRepository as ProductGrid;
|
2019-01-19 10:05:12 +00:00
|
|
|
use Webkul\Product\Repositories\ProductFlatRepository as ProductFlat;
|
2019-01-21 05:26:50 +00:00
|
|
|
use Webkul\Product\Repositories\ProductAttributeValueRepository as ProductAttributeValue;
|
2018-07-31 07:50:54 +00:00
|
|
|
use Webkul\Attribute\Repositories\AttributeFamilyRepository as AttributeFamily;
|
2018-08-09 04:35:13 +00:00
|
|
|
use Webkul\Category\Repositories\CategoryRepository as Category;
|
|
|
|
|
use Webkul\Inventory\Repositories\InventorySourceRepository as InventorySource;
|
2019-01-19 10:05:12 +00:00
|
|
|
|
2018-07-27 06:22:12 +00:00
|
|
|
/**
|
|
|
|
|
* Product controller
|
|
|
|
|
*
|
|
|
|
|
* @author Jitendra Singh <jitendra@webkul.com>
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class ProductController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Contains route related configuration
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $_config;
|
2018-08-16 13:39:51 +00:00
|
|
|
|
2018-07-31 07:50:54 +00:00
|
|
|
/**
|
|
|
|
|
* AttributeFamilyRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $attributeFamily;
|
2018-08-16 13:39:51 +00:00
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
/**
|
|
|
|
|
* CategoryRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $category;
|
2018-08-16 13:39:51 +00:00
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
/**
|
|
|
|
|
* InventorySourceRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $inventorySource;
|
2018-08-16 13:39:51 +00:00
|
|
|
|
2018-07-27 06:22:12 +00:00
|
|
|
/**
|
|
|
|
|
* ProductRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $product;
|
|
|
|
|
|
2018-10-22 13:18:46 +00:00
|
|
|
/**
|
|
|
|
|
* ProductGrid Repository object
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $productGrid;
|
2019-01-19 10:05:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ProductFlat Repository Object
|
|
|
|
|
*
|
|
|
|
|
* @vatr array
|
|
|
|
|
*/
|
|
|
|
|
protected $productFlat;
|
2019-01-21 05:26:50 +00:00
|
|
|
protected $productAttributeValue;
|
2019-01-21 09:50:01 +00:00
|
|
|
protected $attribute;
|
2018-10-22 13:18:46 +00:00
|
|
|
|
2018-07-27 06:22:12 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
2018-07-31 07:50:54 +00:00
|
|
|
* @param Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamily
|
2018-08-09 04:35:13 +00:00
|
|
|
* @param Webkul\Category\Repositories\CategoryRepository $category
|
|
|
|
|
* @param Webkul\Inventory\Repositories\InventorySourceRepository $inventorySource
|
2018-07-31 07:50:54 +00:00
|
|
|
* @param Webkul\Product\Repositories\ProductRepository $product
|
2018-07-27 06:22:12 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-08-09 04:35:13 +00:00
|
|
|
public function __construct(
|
|
|
|
|
AttributeFamily $attributeFamily,
|
|
|
|
|
Category $category,
|
|
|
|
|
InventorySource $inventorySource,
|
2018-10-22 13:18:46 +00:00
|
|
|
Product $product,
|
2019-01-18 12:06:58 +00:00
|
|
|
ProductGrid $productGrid,
|
2019-01-21 05:26:50 +00:00
|
|
|
ProductFlat $productFlat,
|
2019-01-21 10:59:20 +00:00
|
|
|
ProductAttributeValue $productAttributeValue)
|
2018-07-27 06:22:12 +00:00
|
|
|
{
|
2018-07-31 07:50:54 +00:00
|
|
|
$this->attributeFamily = $attributeFamily;
|
|
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
$this->category = $category;
|
|
|
|
|
|
|
|
|
|
$this->inventorySource = $inventorySource;
|
|
|
|
|
|
2018-07-27 06:22:12 +00:00
|
|
|
$this->product = $product;
|
|
|
|
|
|
2018-10-22 13:18:46 +00:00
|
|
|
$this->productGrid = $productGrid;
|
|
|
|
|
|
2019-01-19 10:05:12 +00:00
|
|
|
$this->productFlat = $productFlat;
|
2019-01-18 12:06:58 +00:00
|
|
|
|
2019-01-21 10:59:20 +00:00
|
|
|
$this->productAttributeValue = $productAttributeValue;
|
2019-01-21 05:26:50 +00:00
|
|
|
|
2018-07-27 06:22:12 +00:00
|
|
|
$this->_config = request('_config');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
return view($this->_config['view']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
2018-07-31 07:50:54 +00:00
|
|
|
$families = $this->attributeFamily->all();
|
|
|
|
|
|
2019-01-18 05:30:55 +00:00
|
|
|
$configurableFamily = null;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($familyId = request()->get('family')) {
|
2018-08-22 09:44:35 +00:00
|
|
|
$configurableFamily = $this->attributeFamily->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-01-18 12:06:58 +00:00
|
|
|
if (!request()->get('family') && request()->input('type') == 'configurable' && request()->input('sku') != '') {
|
2018-08-01 06:11:33 +00:00
|
|
|
return redirect(url()->current() . '?family=' . request()->input('attribute_family_id') . '&sku=' . request()->input('sku'));
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (request()->input('type') == 'configurable' && (! request()->has('super_attributes') || ! count(request()->get('super_attributes')))) {
|
2018-08-01 06:11:33 +00:00
|
|
|
session()->flash('error', 'Please select atleast one configurable attribute.');
|
|
|
|
|
|
|
|
|
|
return back();
|
2018-07-31 07:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2018-07-27 06:22:12 +00:00
|
|
|
$this->validate(request(), [
|
2018-07-31 07:50:54 +00:00
|
|
|
'type' => 'required',
|
|
|
|
|
'attribute_family_id' => 'required',
|
2018-08-16 13:39:51 +00:00
|
|
|
'sku' => ['required', 'unique:products,sku', new \Webkul\Core\Contracts\Validations\Slug]
|
2018-07-27 06:22:12 +00:00
|
|
|
]);
|
|
|
|
|
|
2018-07-31 07:50:54 +00:00
|
|
|
$product = $this->product->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.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function edit($id)
|
|
|
|
|
{
|
2018-08-22 09:44:35 +00:00
|
|
|
$product = $this->product->with(['variants'])->find($id);
|
2018-07-27 06:22:12 +00:00
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
$categories = $this->category->getCategoryTree();
|
|
|
|
|
|
|
|
|
|
$inventorySources = $this->inventorySource->all();
|
|
|
|
|
|
|
|
|
|
return view($this->_config['view'], compact('product', 'categories', 'inventorySources'));
|
2018-07-27 06:22:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*
|
2018-08-09 04:35:13 +00:00
|
|
|
* @param \Webkul\Product\Http\Requests\ProductForm $request
|
2018-07-27 06:22:12 +00:00
|
|
|
* @param int $id
|
|
|
|
|
* @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
|
|
|
{
|
2018-10-25 13:59:34 +00:00
|
|
|
$product = $this->product->update(request()->all(), $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']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
2018-10-17 07:21:47 +00:00
|
|
|
$this->product->delete($id);
|
2018-10-17 10:30:31 +00:00
|
|
|
|
2019-01-16 08:38:39 +00:00
|
|
|
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Product']));
|
2018-10-17 10:30:31 +00:00
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-07-27 06:22:12 +00:00
|
|
|
}
|
2018-10-22 13:18:46 +00:00
|
|
|
|
2018-12-07 09:15:31 +00:00
|
|
|
/**
|
|
|
|
|
* Mass Delete the products
|
|
|
|
|
*
|
|
|
|
|
* @return response
|
|
|
|
|
*/
|
2018-12-27 13:27:39 +00:00
|
|
|
public function massDestroy()
|
|
|
|
|
{
|
|
|
|
|
$productIds = explode(',', request()->input('indexes'));
|
2018-12-21 12:48:34 +00:00
|
|
|
|
2018-12-27 13:27:39 +00:00
|
|
|
foreach ($productIds as $productId) {
|
|
|
|
|
$this->product->delete($productId);
|
2018-12-07 09:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session()->flash('success', trans('admin::app.catalog.products.mass-delete-success'));
|
|
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mass updates the products
|
|
|
|
|
*
|
|
|
|
|
* @return response
|
|
|
|
|
*/
|
2018-12-27 13:27:39 +00:00
|
|
|
public function massUpdate()
|
|
|
|
|
{
|
2018-12-07 09:15:31 +00:00
|
|
|
$data = request()->all();
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! isset($data['massaction-type'])) {
|
2018-12-07 09:15:31 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $data['massaction-type'] == 'update') {
|
2019-01-09 11:41:00 +00:00
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-27 13:27:39 +00:00
|
|
|
$productIds = explode(',', $data['indexes']);
|
2018-12-07 09:15:31 +00:00
|
|
|
|
2018-12-27 13:27:39 +00:00
|
|
|
foreach ($productIds as $productId) {
|
|
|
|
|
$this->product->update([
|
|
|
|
|
'channel' => null,
|
|
|
|
|
'locale' => null,
|
|
|
|
|
'status' => $data['update-options']
|
|
|
|
|
], $productId);
|
2018-12-07 09:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-22 06:26:26 +00:00
|
|
|
session()->flash('success', trans('admin::app.catalog.products.mass-update-success'));
|
2018-12-07 09:15:31 +00:00
|
|
|
|
|
|
|
|
return redirect()->route($this->_config['redirect']);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 10:55:23 +00:00
|
|
|
/*
|
|
|
|
|
* To be manually invoked when data is seeded into products
|
|
|
|
|
*/
|
2018-12-27 13:27:39 +00:00
|
|
|
public function sync()
|
|
|
|
|
{
|
2018-11-28 10:40:48 +00:00
|
|
|
Event::fire('products.datagrid.sync', true);
|
|
|
|
|
|
|
|
|
|
return redirect()->route('admin.catalog.products.index');
|
2018-10-22 13:18:46 +00:00
|
|
|
}
|
2019-01-18 12:06:58 +00:00
|
|
|
|
2019-01-21 05:26:50 +00:00
|
|
|
/**
|
|
|
|
|
* Testing for the product flat sync method on product creation and updation
|
|
|
|
|
*/
|
2019-01-19 10:05:12 +00:00
|
|
|
public function testProductFlat() {
|
2019-01-21 05:26:50 +00:00
|
|
|
$product = $this->product->find(4);
|
2019-01-21 09:50:01 +00:00
|
|
|
$allChannelAndLocales = [];
|
|
|
|
|
$productMapped = [];
|
|
|
|
|
$channelLocaleMap = [];
|
2019-01-19 10:05:12 +00:00
|
|
|
|
|
|
|
|
foreach(core()->getAllChannels() as $channel) {
|
2019-01-21 09:50:01 +00:00
|
|
|
array_push($productMapped, [
|
|
|
|
|
'product_id' => $product->id,
|
|
|
|
|
'type' => $product->type,
|
|
|
|
|
'channel_code' => $channel->code,
|
|
|
|
|
'locale_code' => 'null',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
array_push($channelLocaleMap, [
|
|
|
|
|
'product_id' => $product->id,
|
|
|
|
|
'type' => $product->type,
|
|
|
|
|
'channel_code' => $channel->code,
|
|
|
|
|
'locale_code' => 'null',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
foreach($channel->locales as $locale) {
|
|
|
|
|
array_push($productMapped, [
|
|
|
|
|
'product_id' => $product->id,
|
|
|
|
|
'type' => $product->type,
|
|
|
|
|
'channel_code' => $channel->code,
|
|
|
|
|
'locale_code' => $locale->code
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
array_push($channelLocaleMap, [
|
|
|
|
|
'product_id' => $product->id,
|
|
|
|
|
'type' => $product->type,
|
|
|
|
|
'channel_code' => $channel->code,
|
|
|
|
|
'locale_code' => $locale->code,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
array_push($productMapped, [
|
|
|
|
|
'product_id' => $product->id,
|
|
|
|
|
'type' => $product->type,
|
|
|
|
|
'channel_code' => 'null',
|
|
|
|
|
'locale_code' => $locale->code,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
array_push($channelLocaleMap, [
|
|
|
|
|
'product_id' => $product->id,
|
|
|
|
|
'type' => $product->type,
|
|
|
|
|
'channel_code' => 'null',
|
|
|
|
|
'locale_code' => $locale->code,
|
|
|
|
|
]);
|
2019-01-21 05:26:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-19 10:05:12 +00:00
|
|
|
|
2019-01-21 09:50:01 +00:00
|
|
|
$attributes = $product->attribute_family->custom_attributes;
|
2019-01-21 05:26:50 +00:00
|
|
|
|
2019-01-21 09:50:01 +00:00
|
|
|
foreach($attributes as $key => $attribute) {
|
2019-01-21 10:59:20 +00:00
|
|
|
if($attribute->value_per_channel && $attribute->value_per_locale) {
|
|
|
|
|
$values = $this->productAttributeValue->findWhere(['attribute_id' => $attribute->id, 'product_id' => $product->id]);
|
|
|
|
|
dd($values);
|
|
|
|
|
foreach($values as $key => $value) {
|
|
|
|
|
dd($value->channel, $value->locale);
|
|
|
|
|
// $this->pushCorrect($attribute->channel, );
|
2019-01-21 05:26:50 +00:00
|
|
|
}
|
2019-01-21 10:59:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if($attribute->value_per_channel && !$attribute->value_per_locale) {
|
|
|
|
|
// $this->pushCorrect();
|
|
|
|
|
} else if($attribute->value_per_locale) {
|
2019-01-21 09:50:01 +00:00
|
|
|
// $this->pushCorrect();
|
2019-01-21 05:26:50 +00:00
|
|
|
} else {
|
2019-01-21 09:50:01 +00:00
|
|
|
// $this->pushCorrect();
|
2019-01-19 10:05:12 +00:00
|
|
|
}
|
2019-01-21 10:59:20 +00:00
|
|
|
|
|
|
|
|
// if($attribute->type == 'select') {
|
|
|
|
|
// if($attribute->value_per_channel && $attribute->value_per_locale) {
|
|
|
|
|
// dd($this->productAttributeValue->findWhere(['attribute_id' => $attribute->id]));
|
|
|
|
|
|
|
|
|
|
// // $this->pushCorrect($attribute->channel);
|
|
|
|
|
// } else if($attribute->value_per_channel && !$attribute->value_per_locale) {
|
|
|
|
|
// // $this->pushCorrect();
|
|
|
|
|
// } else if($attribute->value_per_locale) {
|
|
|
|
|
// // $this->pushCorrect();
|
|
|
|
|
// } else {
|
|
|
|
|
// // $this->pushCorrect();
|
|
|
|
|
// }
|
|
|
|
|
// } else if($attribute->type == 'multiselect') {
|
|
|
|
|
// // $this->pushCorrect();
|
|
|
|
|
// } else {
|
|
|
|
|
// if($attribute->value_per_channel && $attribute->value_per_locale) {
|
|
|
|
|
// dd($this->productAttributeValue->findWhere(['attribute_id' => $attribute->id]));
|
|
|
|
|
|
|
|
|
|
// // $this->pushCorrect($attribute->channel);
|
|
|
|
|
// } else if($attribute->value_per_channel && !$attribute->value_per_locale) {
|
|
|
|
|
// // $this->pushCorrect();
|
|
|
|
|
// } else if($attribute->value_per_locale) {
|
|
|
|
|
// // $this->pushCorrect();
|
|
|
|
|
// } else {
|
|
|
|
|
// // $this->pushCorrect();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2019-01-19 10:05:12 +00:00
|
|
|
}
|
2019-01-18 12:06:58 +00:00
|
|
|
|
2019-01-21 10:59:20 +00:00
|
|
|
dd($productMapped);
|
2019-01-21 09:50:01 +00:00
|
|
|
}
|
2019-01-21 05:26:50 +00:00
|
|
|
|
2019-01-21 09:50:01 +00:00
|
|
|
public function pushCorrect($channelCode = null, $localeCode = null, $productMapped) {
|
|
|
|
|
dd($channelCode, $localeCode, $productMapped);
|
2019-01-18 12:06:58 +00:00
|
|
|
}
|
2018-07-27 06:22:12 +00:00
|
|
|
}
|