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

425 lines
13 KiB
PHP
Raw Normal View History

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;
use Webkul\Product\Repositories\ProductGridRepository as ProductGrid;
use Webkul\Product\Repositories\ProductFlatRepository as ProductFlat;
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;
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-07-31 07:50:54 +00:00
/**
* AttributeFamilyRepository object
*
* @var array
*/
protected $attributeFamily;
2018-08-09 04:35:13 +00:00
/**
* CategoryRepository object
*
* @var array
*/
protected $category;
2018-08-09 04:35:13 +00:00
/**
* InventorySourceRepository object
*
* @var array
*/
protected $inventorySource;
2018-07-27 06:22:12 +00:00
/**
* ProductRepository object
*
* @var array
*/
protected $product;
/**
* ProductGrid Repository object
*
* @var array
*/
protected $productGrid;
protected $productFlat;
protected $productAttributeValue;
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,
Product $product,
ProductGrid $productGrid,
ProductFlat $productFlat,
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;
$this->productGrid = $productGrid;
$this->productFlat = $productFlat;
$this->productAttributeValue = $productAttributeValue;
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()
{
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',
'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
{
$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
}
/**
* Mass Delete the products
*
* @return response
*/
public function massDestroy()
{
$productIds = explode(',', request()->input('indexes'));
2018-12-21 12:48:34 +00:00
foreach ($productIds as $productId) {
$this->product->delete($productId);
}
session()->flash('success', trans('admin::app.catalog.products.mass-delete-success'));
return redirect()->route($this->_config['redirect']);
}
/**
* Mass updates the products
*
* @return response
*/
public function massUpdate()
{
$data = request()->all();
2019-01-22 05:05:05 +00:00
if (!isset($data['massaction-type'])) {
return redirect()->back();
}
2019-01-22 05:05:05 +00:00
if (!$data['massaction-type'] == 'update') {
return redirect()->back();
}
$productIds = explode(',', $data['indexes']);
foreach ($productIds as $productId) {
$this->product->update([
2019-01-22 05:05:05 +00:00
'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
*/
public function sync()
{
Event::fire('products.datagrid.sync', true);
return redirect()->route('admin.catalog.products.index');
}
// public function testProductFlat() {
// $product = $this->product->find(2);
// $productAttributes = $product->attribute_family->custom_attributes;
// $allLocales = core()->getAllLocales();
// $productsFlat = array();
// $channelLocaleMap = array();
// $nonDependentAttributes = array();
// $localeDependentAttributes = array();
// $channelDependentAttributes = array();
// $channelLocaleDependentAttributes = array();
// foreach($productAttributes as $key => $productAttribute) {
// if($productAttribute->value_per_channel) {
// if($productAttribute->value_per_locale) {
// array_push($channelLocaleDependentAttributes, ['id' => $productAttribute->id, 'code' => $productAttribute->code]);
// } else {
// array_push($channelDependentAttributes, ['id' => $productAttribute->id, 'code' => $productAttribute->code]);
// }
// } else if($productAttribute->value_per_locale && !$productAttribute->value_per_channel) {
// array_push($localeDependentAttributes, ['id' => $productAttribute->id, 'code' => $productAttribute->code]);
// } else {
// array_push($nonDependentAttributes, ['id' => $productAttribute->id, 'code' => $productAttribute->code]);
// }
// }
// foreach(core()->getAllChannels() as $channel) {
// $dummy = [
// 'product_id' => $product->id,
// 'channel' => $channel->code,
// 'locale' => null,
// 'data' => $channelDependentAttributes
// ];
// array_push($channelLocaleMap, $dummy);
// $dummy = [];
// foreach($channel->locales as $locale) {
// $dummy = [
// 'product_id' => $product->id,
// 'channel' => $channel->code,
// 'locale' => $locale->code,
// 'data' => $channelLocaleDependentAttributes
// ];
// array_push($channelLocaleMap, $dummy);
// $dummy = [];
// }
// }
// $dummy = [
// 'product_id' => $product->id,
// 'channel' => null,
// 'locale' => null,
// 'data' => $nonDependentAttributes
// ];
// array_push($channelLocaleMap, $dummy);
// $dummy = [];
// foreach($allLocales as $key => $allLocale) {
// $dummy = [
// 'product_id' => $product->id,
// 'channel' => null,
// 'locale' => $allLocale->code,
// 'data' => $localeDependentAttributes
// ];
// array_push($channelLocaleMap, $dummy);
// $dummy = [];
// }
// $productFlatObjects = $channelLocaleMap;
// $keyOfNonDependentAttributes = null;
// foreach($productAttributes as $productAttribute) {
// foreach($productFlatObjects as $flatKey => $productFlatObject) {
// if($productFlatObject['channel'] == null && $productFlatObject['locale'] == null) {
// $keyOfNonDependentAttributes = $flatKey;
// }
// foreach($productFlatObject['data'] as $key => $value) {
// if($productAttribute->code == $value['code']) {
// $valueOf = $this->productAttributeValue->findOneWhere([
// 'product_id' => $product->id,
// 'channel' => $productFlatObject['channel'],
// 'locale' => $productFlatObject['locale'],
// 'attribute_id' => $productAttribute->id
// ]);
// if($valueOf != null) {
// $productAttributeColumn = $this->productAttributeValue->model()::$attributeTypeFields[$productAttribute->type];
// $valueOf = $valueOf->{$productAttributeColumn};
// $productFlatObjects[$flatKey][$productAttribute->code] = $valueOf;
// } else {
// $productFlatObjects[$flatKey][$productAttribute->code] = 'null';
// }
// }
// }
// }
// }
// $nonDependentAttributes = $productFlatObjects[$keyOfNonDependentAttributes];
// array_forget($nonDependentAttributes, ['product_id', 'channel', 'locale', 'data', 'visible_individually', 'width', 'height', 'depth']);
// unset($productFlatObjects[$keyOfNonDependentAttributes]);
// $productFlatEntryObject = array();
// $tempFlatObject = array();
// foreach($productFlatObjects as $flatKey => $productFlatObject) {
// unset($productFlatObject['data']);
// if(isset($productFlatObject['short_description'])) {
// $productFlatObject['description'] = $productFlatObject['short_description'];
// unset($productFlatObject['short_description']);
// }
// if(isset($productFlatObject['meta_title'])) {
// unset($productFlatObject['meta_title']);
// unset($productFlatObject['meta_description']);
// unset($productFlatObject['meta_keywords']);
// }
// $tempFlatObject = array_merge($productFlatObject, $nonDependentAttributes);
// $tempFlatObject = core()->convertEmptyStringsToNull($tempFlatObject);
// $exists = $this->productFlat->findWhere([
// 'product_id' => $product->id,
// 'channel' => $tempFlatObject['channel'],
// 'locale' => $tempFlatObject['locale']
// ]);
// if($exists->count() == 0) {
// $result = $this->productFlat->create($tempFlatObject);
// } else {
// $result = $exists->first();
// $result->update($tempFlatObject);
// }
// unset($tempFlatObject);
// }
// return 'true';
// }
2018-07-27 06:22:12 +00:00
}