Moved catalog event from controllers to repositories

This commit is contained in:
jitendra 2018-12-27 18:57:39 +05:30
parent 9581082f56
commit abd3bba617
8 changed files with 123 additions and 134 deletions

View File

@ -4,7 +4,6 @@ namespace Webkul\Attribute\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Webkul\Attribute\Repositories\AttributeRepository as Attribute;
@ -76,12 +75,8 @@ class AttributeController extends Controller
'type' => 'required'
]);
Event::fire('catalog.attribute.create.before');
$attribute = $this->attribute->create(request()->all());
Event::fire('catalog.attribute.create.after', $attribute);
session()->flash('success', 'Attribute created successfully.');
return redirect()->route($this->_config['redirect']);
@ -115,12 +110,8 @@ class AttributeController extends Controller
'type' => 'required'
]);
Event::fire('catalog.attribute.update.before', $id);
$attribute = $this->attribute->update(request()->all(), $id);
Event::fire('catalog.attribute.update.after', $attribute);
session()->flash('success', 'Attribute updated successfully.');
return redirect()->route($this->_config['redirect']);
@ -140,12 +131,8 @@ class AttributeController extends Controller
session()->flash('error', 'Can not delete system attribute.');
} else {
try {
Event::fire('catalog.attribute.delete.before', $id);
$this->attribute->delete($id);
Event::fire('catalog.attribute.delete.after', $id);
session()->flash('success', 'Attribute deleted successfully.');
} catch(\Exception $e) {
session()->flash('error', 'Attribute is used in configurable products.');
@ -160,33 +147,30 @@ class AttributeController extends Controller
*
* @return response \Illuminate\Http\Response
*/
public function massDestroy() {
public function massDestroy()
{
$suppressFlash = false;
if(request()->isMethod('post')) {
if (request()->isMethod('post')) {
$indexes = explode(',', request()->input('indexes'));
foreach($indexes as $key => $value) {
foreach ($indexes as $key => $value) {
$attribute = $this->attribute->findOrFail($value);
try {
if(!$attribute->is_user_defined) {
if (!$attribute->is_user_defined) {
continue;
} else {
Event::fire('catalog.attribute.delete.before', $value);
$this->attribute->delete($value);
Event::fire('catalog.attribute.delete.after', $value);
}
} catch(\Exception $e) {
} catch (\Exception $e) {
$suppressFlash = true;
continue;
}
}
if(!$suppressFlash)
if (!$suppressFlash)
session()->flash('success', trans('admin::app.datagrid.mass-ops.delete-success', ['resource' => 'attributes']));
else
session()->flash('info', trans('admin::app.datagrid.mass-ops.partial-action', ['resource' => 'attributes']));

View File

@ -4,7 +4,6 @@ namespace Webkul\Attribute\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Webkul\Attribute\Repositories\AttributeFamilyRepository as AttributeFamily;
use Webkul\Attribute\Repositories\AttributeRepository as Attribute;
@ -81,12 +80,8 @@ class AttributeFamilyController extends Controller
'name' => 'required'
]);
Event::fire('catalog.attribute_family.create.before');
$attributeFamily = $this->attributeFamily->create(request()->all());
Event::fire('catalog.attribute_family.create.after', $attributeFamily);
session()->flash('success', 'Family created successfully.');
return redirect()->route($this->_config['redirect']);
@ -122,12 +117,8 @@ class AttributeFamilyController extends Controller
'name' => 'required'
]);
Event::fire('catalog.attribute_family.update.before', $id);
$attributeFamily = $this->attributeFamily->update(request()->all(), $id);
Event::fire('catalog.attribute_family.update.after', $attributeFamily);
session()->flash('success', 'Family updated successfully.');
return redirect()->route($this->_config['redirect']);
@ -143,17 +134,13 @@ class AttributeFamilyController extends Controller
{
$attributeFamily = $this->attributeFamily->find($id);
if($this->attributeFamily->count() == 1) {
if ($this->attributeFamily->count() == 1) {
session()->flash('error', 'At least one family is required.');
} else if ($attributeFamily->products()->count()) {
session()->flash('error', 'Attribute family is used in products.');
} else {
Event::fire('catalog.attribute_family.delete.before', $id);
$this->attributeFamily->delete($id);
Event::fire('catalog.attribute_family.delete.after', $id);
session()->flash('success', 'Family deleted successfully.');
}
@ -168,24 +155,20 @@ class AttributeFamilyController extends Controller
public function massDestroy() {
$suppressFlash = false;
if(request()->isMethod('delete')) {
if (request()->isMethod('delete')) {
$indexes = explode(',', request()->input('indexes'));
foreach($indexes as $key => $value) {
foreach ($indexes as $key => $value) {
try {
Event::fire('catalog.attribute_family.delete.before', $value);
$this->attributeFamily->delete($value);
Event::fire('catalog.attribute_family.delete.after', $value);
} catch(\Exception $e) {
} catch (\Exception $e) {
$suppressFlash = true;
continue;
}
}
if(!$suppressFlash)
if (!$suppressFlash)
session()->flash('success', trans('admin::app.datagrid.mass-ops.delete-success'));
else
session()->flash('info', trans('admin::app.datagrid.mass-ops.partial-action', ['resource' => 'Attribute Family']));

View File

@ -3,6 +3,7 @@
namespace Webkul\Attribute\Repositories;
use Webkul\Core\Eloquent\Repository;
use Illuminate\Support\Facades\Event;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Attribute\Repositories\AttributeGroupRepository;
use Illuminate\Container\Container as App;
@ -61,6 +62,8 @@ class AttributeFamilyRepository extends Repository
*/
public function create(array $data)
{
Event::fire('catalog.attribute_family.create.before');
$attributeGroups = isset($data['attribute_groups']) ? $data['attribute_groups'] : [];
unset($data['attribute_groups']);
$family = $this->model->create($data);
@ -81,6 +84,8 @@ class AttributeFamilyRepository extends Repository
}
}
Event::fire('catalog.attribute_family.create.after', $attributeFamily);
return $family;
}
@ -94,6 +99,8 @@ class AttributeFamilyRepository extends Repository
{
$family = $this->find($id);
Event::fire('catalog.attribute_family.update.before', $id);
$family->update($data);
$previousAttributeGroupIds = $family->attribute_groups()->pluck('id');
@ -141,6 +148,21 @@ class AttributeFamilyRepository extends Repository
$this->attributeGroup->delete($attributeGroupId);
}
Event::fire('catalog.attribute_family.update.after', $family);
return $family;
}
/**
* @param $id
* @return void
*/
public function delete($id)
{
Event::fire('catalog.attribute_family.delete.before', $id);
parent::delete($id);
Event::fire('catalog.attribute_family.delete.after', $id);
}
}

View File

@ -3,6 +3,7 @@
namespace Webkul\Attribute\Repositories;
use Webkul\Core\Eloquent\Repository;
use Illuminate\Support\Facades\Event;
use Webkul\Attribute\Repositories\AttributeOptionRepository;
use Illuminate\Container\Container as App;
@ -50,6 +51,8 @@ class AttributeRepository extends Repository
*/
public function create(array $data)
{
Event::fire('catalog.attribute.create.before');
$data = $this->validateUserInput($data);
$options = isset($data['options']) ? $data['options'] : [];
@ -62,6 +65,8 @@ class AttributeRepository extends Repository
}
}
Event::fire('catalog.attribute.create.after', $attribute);
return $attribute;
}
@ -77,6 +82,8 @@ class AttributeRepository extends Repository
$attribute = $this->find($id);
Event::fire('catalog.attribute.update.before', $id);
$attribute->update($data);
$previousOptionIds = $attribute->options()->pluck('id');
@ -101,9 +108,24 @@ class AttributeRepository extends Repository
$this->attributeOption->delete($optionId);
}
Event::fire('catalog.attribute.update.after', $attribute);
return $attribute;
}
/**
* @param $id
* @return void
*/
public function delete($id)
{
Event::fire('catalog.attribute.delete.before', $id);
parent::delete($id);
Event::fire('catalog.attribute.delete.after', $id);
}
/**
* @param array $data
* @return array

View File

@ -4,7 +4,6 @@ namespace Webkul\Category\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Webkul\Category\Repositories\CategoryRepository as Category;
/**
@ -77,12 +76,8 @@ class CategoryController extends Controller
'image.*' => 'mimes:jpeg,jpg,bmp,png'
]);
Event::fire('catalog.category.create.before');
$category = $this->category->create(request()->all());
Event::fire('catalog.category.create.after', $category);
session()->flash('success', 'Category created successfully.');
return redirect()->route($this->_config['redirect']);
@ -124,12 +119,8 @@ class CategoryController extends Controller
'image.*' => 'mimes:jpeg,jpg,bmp,png'
]);
Event::fire('catalog.category.update.before', $id);
$this->category->update(request()->all(), $id);
Event::fire('catalog.category.update.after', $id);
session()->flash('success', 'Category updated successfully.');
return redirect()->route($this->_config['redirect']);

View File

@ -2,10 +2,11 @@
namespace Webkul\Category\Repositories;
use Webkul\Category\Models\Category;
use Webkul\Core\Eloquent\Repository;
use Illuminate\Support\Facades\Storage;
use Illuminate\Container\Container as App;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Event;
use Webkul\Core\Eloquent\Repository;
use Webkul\Category\Models\Category;
use Webkul\Category\Models\CategoryTranslation;
use Illuminate\Database\Eloquent\ModelNotFoundException;
@ -43,6 +44,8 @@ class CategoryRepository extends Repository
*/
public function create(array $data)
{
Event::fire('catalog.category.create.before');
if (isset($data['locale']) && $data['locale'] == 'all') {
$model = app()->make($this->model());
@ -59,6 +62,8 @@ class CategoryRepository extends Repository
$this->uploadImages($data, $category);
Event::fire('catalog.category.create.after', $category);
return $category;
}
@ -135,13 +140,30 @@ class CategoryRepository extends Repository
{
$category = $this->find($id);
Event::fire('catalog.category.update.before', $id);
$category->update($data);
$this->uploadImages($data, $category);
Event::fire('catalog.category.update.after', $id);
return $category;
}
/**
* @param $id
* @return void
*/
public function delete($id)
{
Event::fire('catalog.category.delete.before', $id);
parent::delete($id);
Event::fire('catalog.category.delete.after', $id);
}
/**
* @param array $data
* @param mixed $category

View File

@ -140,14 +140,8 @@ class ProductController extends Controller
'sku' => ['required', 'unique:products,sku', new \Webkul\Core\Contracts\Validations\Slug]
]);
//before store of the product
Event::fire('catalog.product.create.before');
$product = $this->product->create(request()->all());
//after store of the product
Event::fire('catalog.product.create.after', $product);
session()->flash('success', 'Product created successfully.');
return redirect()->route($this->_config['redirect'], ['id' => $product->id]);
@ -179,12 +173,8 @@ class ProductController extends Controller
*/
public function update(ProductForm $request, $id)
{
Event::fire('catalog.product.update.before', $id);
$product = $this->product->update(request()->all(), $id);
Event::fire('catalog.product.update.after', $product);
session()->flash('success', 'Product updated successfully.');
return redirect()->route($this->_config['redirect']);
@ -198,12 +188,8 @@ class ProductController extends Controller
*/
public function destroy($id)
{
Event::fire('catalog.product.delete.before', $id);
$this->product->delete($id);
Event::fire('catalog.product.delete.after', $id);
session()->flash('success', 'Product deleted successfully.');
return redirect()->back();
@ -214,22 +200,12 @@ class ProductController extends Controller
*
* @return response
*/
public function massDestroy() {
$data = request()->all();
$productIds = explode(',', $data['indexes']);
public function massDestroy()
{
$productIds = explode(',', request()->input('indexes'));
if(count($productIds)) {
foreach($productIds as $productId) {
$product = $this->product->find($productId);
if(!is_null($product)) {
Event::fire('catalog.product.delete.before', $productId);
$product->delete();
Event::fire('catalog.product.delete.after', $productId);
}
}
foreach ($productIds as $productId) {
$this->product->delete($productId);
}
session()->flash('success', trans('admin::app.catalog.products.mass-delete-success'));
@ -242,36 +218,22 @@ class ProductController extends Controller
*
* @return response
*/
public function massUpdate() {
public function massUpdate()
{
$data = request()->all();
$attribute = 'status';
$productIds = explode(',', $data['indexes']);
if(!isset($data['massaction-type'])) {
if (!isset($data['massaction-type'])) {
return redirect()->back();
}
if(count($productIds)) {
foreach($productIds as $productId) {
$product = $this->product->find($productId);
$productIds = explode(',', $data['indexes']);
if($data['update-options'] == 0 && $data['selected-option-text'] == 'In Active') {
Event::fire('catelog.product.update.before', $productId);
$result = $this->product->updateAttribute($product, $attribute, $data['update-options']);
if($result)
Event::fire('catelog.product.update.after', $product);
} else if($data['update-options'] == 1 && $data['selected-option-text'] == 'Active') {
Event::fire('catelog.product.update.before', $productId);
$result = $this->product->updateAttribute($product, $attribute, $data['update-options']);
if($result)
Event::fire('product.update.after', $product);
}
}
foreach ($productIds as $productId) {
$this->product->update([
'channel' => null,
'locale' => null,
'status' => $data['update-options']
], $productId);
}
session()->flash('success', trans('admin::app.catalog.products.mass-update-success'));
@ -282,7 +244,8 @@ class ProductController extends Controller
/*
* To be manually invoked when data is seeded into products
*/
public function sync() {
public function sync()
{
Event::fire('products.datagrid.sync', true);
return redirect()->route('admin.catalog.products.index');

View File

@ -3,6 +3,7 @@
namespace Webkul\Product\Repositories;
use Illuminate\Container\Container as App;
use Illuminate\Support\Facades\Event;
use Webkul\Core\Eloquent\Repository;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Attribute\Repositories\AttributeOptionRepository;
@ -111,6 +112,9 @@ class ProductRepository extends Repository
*/
public function create(array $data)
{
//before store of the product
Event::fire('catalog.product.create.before');
$product = $this->model->create($data);
$nameAttribute = $this->attribute->findOneByField('code', 'status');
@ -137,6 +141,10 @@ class ProductRepository extends Repository
}
}
//after store of the product
Event::fire('catalog.product.create.after', $product);
return $product;
}
@ -148,6 +156,8 @@ class ProductRepository extends Repository
*/
public function update(array $data, $id, $attribute = "id")
{
Event::fire('catalog.product.update.before', $id);
$product = $this->find($id);
if($product->parent_id && $this->checkVariantOptionAvailabiliy($data, $product)) {
@ -221,9 +231,24 @@ class ProductRepository extends Repository
$this->productImage->uploadImages($data, $product);
Event::fire('catalog.product.update.after', $product);
return $product;
}
/**
* @param $id
* @return mixed
*/
public function delete($id)
{
Event::fire('catalog.product.delete.before', $id);
parent::delete($id);
Event::fire('catalog.product.delete.after', $id);
}
/**
* @param mixed $product
* @param array $permutation
@ -350,29 +375,6 @@ class ProductRepository extends Repository
return $variant;
}
/**
* Change an attribute's value of the product
*
* @return boolean
*/
public function updateAttribute($product, $attribute, $value) {
$attribute = $this->attribute->findOneByField('code', 'status');
$attributeValue = $this->attributeValue->findOneWhere([
'product_id' => $product->id,
'attribute_id' => $attribute->id,
]);
$result = $this->attributeValue->update([
ProductAttributeValue::$attributeTypeFields[$attribute->type] => $value
], $attributeValue->id);
if($result)
return true;
else
return false;
}
/**
* @param array $data
* @param mixed $product