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

154 lines
4.0 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;
use Webkul\Product\Repositories\ProductRepository as Product;
2018-07-31 07:50:54 +00:00
use Webkul\Attribute\Repositories\AttributeFamilyRepository as AttributeFamily;
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-07-27 06:22:12 +00:00
/**
* ProductRepository object
*
* @var array
*/
protected $product;
/**
* Create a new controller instance.
*
2018-07-31 07:50:54 +00:00
* @param Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamily
* @param Webkul\Product\Repositories\ProductRepository $product
2018-07-27 06:22:12 +00:00
* @return void
*/
2018-07-31 07:50:54 +00:00
public function __construct(AttributeFamily $attributeFamily, Product $product)
2018-07-27 06:22:12 +00:00
{
2018-07-31 07:50:54 +00:00
$this->attributeFamily = $attributeFamily;
2018-07-27 06:22:12 +00:00
$this->product = $product;
$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();
if($familyId = request()->get('family')) {
$configurableFamily = $this->attributeFamily->findOrFail($familyId);
}
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()
{
2018-08-01 06:11:33 +00:00
if(!request()->get('family') && request()->input('type') == 'configurable' && request()->input('sku') != '') {
return redirect(url()->current() . '?family=' . request()->input('attribute_family_id') . '&sku=' . request()->input('sku'));
}
if(request()->input('type') == 'configurable' && (!request()->has('super_attributes') || !count(request()->get('super_attributes')))) {
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-01 06:11:33 +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
session()->flash('success', 'Product created successfully.');
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)
{
$product = $this->product->findOrFail($id);
return view($this->_config['view'], compact('product'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate(request(), [
'name' => 'required',
]);
$this->product->update(request()->all(), $id);
session()->flash('success', 'Product updated successfully.');
return redirect()->route($this->_config['redirect']);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}