From 42d067c44a9de1d00cac2095e86e04e1b24ca1b2 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Sun, 14 Jul 2019 07:30:16 +0530 Subject: [PATCH] Optimization in exception handling for separate admin and shop providers, whilst convertxtoproductid is in UI development --- packages/Webkul/Admin/src/Config/system.php | 4 +- .../Webkul/Admin/src/Exceptions/Handler.php | 2 +- .../Admin/src/Resources/lang/en/app.php | 9 +- .../promotions/cart-rule/create.blade.php | 96 +++++++++++++++---- .../src/Repositories/AttributeRepository.php | 2 +- .../src/Repositories/CategoryRepository.php | 5 +- .../Webkul/Discount/src/Config/system.php | 16 ++-- ...s_selection_column_in_cart_rules_table.php | 32 +++++++ .../Http/Controllers/CartRuleController.php | 33 +++++-- .../Webkul/Shop/src/Exceptions/Handler.php | 79 +++++++++++++++ 10 files changed, 232 insertions(+), 46 deletions(-) create mode 100644 packages/Webkul/Discount/src/Database/Migrations/2019_07_14_070809_add_products_selection_column_in_cart_rules_table.php create mode 100644 packages/Webkul/Shop/src/Exceptions/Handler.php diff --git a/packages/Webkul/Admin/src/Config/system.php b/packages/Webkul/Admin/src/Config/system.php index adad091bd..5ab93dee3 100644 --- a/packages/Webkul/Admin/src/Config/system.php +++ b/packages/Webkul/Admin/src/Config/system.php @@ -8,7 +8,7 @@ return [ ], [ 'key' => 'catalog.products', 'name' => 'admin::app.admin.system.products', - 'sort' => 1, + 'sort' => 2 ], [ 'key' => 'catalog.products.review', 'name' => 'admin::app.admin.system.review', @@ -39,7 +39,7 @@ return [ ], [ 'key' => 'customer', 'name' => 'admin::app.admin.system.customer', - 'sort' => 2, + 'sort' => 3, ], [ 'key' => 'customer.settings', 'name' => 'admin::app.admin.system.settings', diff --git a/packages/Webkul/Admin/src/Exceptions/Handler.php b/packages/Webkul/Admin/src/Exceptions/Handler.php index bb75160c1..273d8329f 100755 --- a/packages/Webkul/Admin/src/Exceptions/Handler.php +++ b/packages/Webkul/Admin/src/Exceptions/Handler.php @@ -28,7 +28,7 @@ class Handler extends ExceptionHandler */ public function render($request, Exception $exception) { - $path = $this->isAdminUri() ? 'admin' : 'shop'; + $path = 'admin'; if ($exception instanceof HttpException) { $statusCode = in_array($exception->getStatusCode(), [401, 403, 404, 503]) ? $exception->getStatusCode() : 500; diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index 9eaf1850d..2526ddb7d 100755 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -815,20 +815,17 @@ return [ 'save-btn-title' => 'Create', 'edit-btn-title' => 'Edit', 'save' => 'Save', - 'select-attr' => 'Select Attribute', - 'select-attr-fam' => 'Select Attribute Family', 'select-cart-attr' => 'Select Cart Attribute', - 'select-products' => 'Select product attributes', + 'select-products' => 'How to choose products ?', 'select-category' => 'Select Categories', + 'select-attribute' => 'Select Attributes', 'general-info' => [ 'sku-like' => 'SKU Like', 'on-shipping' => 'If applied on shipping', 'discount' => 'Discount', - 'to-be-applied' => 'Applied On', - 'applied-on-info' => 'It signifies how cart rule should be applied on items in cart', 'shipping-apply-info' => 'It signifies how rule behaves when it is used for shipping', 'general-info' => 'General Info', - 'config' => 'Configure', + 'options' => 'Options', 'name' => 'Rule Name', 'description' => 'Description', 'starts-from' => 'Start', diff --git a/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php b/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php index 5150a89bd..14513a3a0 100644 --- a/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/promotions/cart-rule/create.blade.php @@ -343,24 +343,61 @@
-
+
- + -
-                                            @{{ value }}
-                                        
+ @{{ category_values }}
-
- + {{--
+ - + -
-                                            @{{ value }}
-                                        
+ @{{ attribute_values }} +
--}} + +
+ + +
+ + +
+ +
+ +
+ +
+ + +
+ +
+ + + +
+ +
@@ -404,10 +441,6 @@ data () { return { - options: [ - 1, 2, 3, 4, 5, 6 - ], - categories: null, name: null, description: null, conditions_list: [], @@ -463,13 +496,20 @@ condition: null, value: [] }, - country_and_states: @json($cart_rule[2]) + country_and_states: @json($cart_rule[2]), + + category_options: @json($cart_rule[1]), + category_values: null, + + attribute_list: [], + attribute_input: @json($cart_rule[3]), + attribute_options: [], + attribute_values: [] } }, - methods: { - addTag (newTag) { + categoryTag (newTag) { const tag = { name: newTag, code: newTag.substring(0, 2) + Math.floor((Math.random() * 10000000)) @@ -479,6 +519,14 @@ this.value.push(tag) }, + categoryLabel (category_options) { + return category_options.name + ' [ ' + category_options.slug + ' ]'; + }, + + attributeLabel (attribute_options) { + return attribute_options.name + ' [ ' + attribute_options.type + ' ]'; + }, + addCondition () { if (this.criteria == 'product_subselection' || this.criteria == 'cart') { this.condition_on = this.criteria; @@ -567,6 +615,18 @@ pre { position: absolute; } + + .multiselect__tag { + background: #4CAF50; + } + + .multiselect__option--selected.multiselect__option--highlight { + background: rgba(0, 64, 255, 0.6); + } + + .multiselect__option--highlight { + background: #4CAF50; + } @endpush @stop \ No newline at end of file diff --git a/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php b/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php index 0c80612dc..fa4fc8ee3 100755 --- a/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php +++ b/packages/Webkul/Attribute/src/Repositories/AttributeRepository.php @@ -225,7 +225,7 @@ class AttributeRepository extends Repository 'id' => $attribute->id, 'name' => $attribute->name, 'type' => $attribute->type, - 'code' => $attribute->code + 'code' => $attribute->code, ]); } } diff --git a/packages/Webkul/Category/src/Repositories/CategoryRepository.php b/packages/Webkul/Category/src/Repositories/CategoryRepository.php index 3d5f44304..260b721d8 100755 --- a/packages/Webkul/Category/src/Repositories/CategoryRepository.php +++ b/packages/Webkul/Category/src/Repositories/CategoryRepository.php @@ -213,7 +213,7 @@ class CategoryRepository extends Repository } } - public function getPartial() + public function getPartial($columns = null) { $categories = $this->model->all(); $trimmed = array(); @@ -222,7 +222,8 @@ class CategoryRepository extends Repository if ($category->name != null || $category->name != "") { $trimmed[$key] = [ 'id' => $category->id, - 'name' => $category->name + 'name' => $category->name, + 'slug' => $category->slug ]; } } diff --git a/packages/Webkul/Discount/src/Config/system.php b/packages/Webkul/Discount/src/Config/system.php index e518bd249..0447f793b 100644 --- a/packages/Webkul/Discount/src/Config/system.php +++ b/packages/Webkul/Discount/src/Config/system.php @@ -11,25 +11,25 @@ return [ 'sort' => 1 ], [ 'key' => 'discount.cart-rules.settings', - 'name' => 'admin::app.promotion.general-info.config', + 'name' => 'admin::app.promotion.general-info.options', 'sort' => 1, 'fields' => [ [ - 'name' => 'applied_on', - 'title' => 'admin::app.promotion.general-info.to-be-applied', + 'name' => 'if_applied_on_shipping', + 'title' => 'admin::app.promotion.general-info.on-shipping', 'type' => 'select', 'validation' => 'required', 'channel_based' => true, 'locale_based' => false, 'options' => [ [ - 'title' => 'Least Worth Item', - 'value' => 0 + 'title' => 'W.R.T grand total', + 'value' => 'grand_total' ], [ - 'title' => 'Max Worth Item', + 'title' => 'W.R.T sub total', 'value' => 1 ], [ - 'title' => 'All Items', + 'title' => 'W.R.T shipping total', 'value' => 2 ], [ 'title' => 'Please select an option', @@ -37,7 +37,7 @@ return [ ] ], - 'info' => 'admin::app.promotion.general-info.applied-on-info' + 'info' => 'admin::app.promotion.general-info.shipping-apply-info' ] ] ], diff --git a/packages/Webkul/Discount/src/Database/Migrations/2019_07_14_070809_add_products_selection_column_in_cart_rules_table.php b/packages/Webkul/Discount/src/Database/Migrations/2019_07_14_070809_add_products_selection_column_in_cart_rules_table.php new file mode 100644 index 000000000..c8c86276b --- /dev/null +++ b/packages/Webkul/Discount/src/Database/Migrations/2019_07_14_070809_add_products_selection_column_in_cart_rules_table.php @@ -0,0 +1,32 @@ +json('products_selection')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('cart_rules', function (Blueprint $table) { + $table->dropColumn('products_selection'); + }); + } +} diff --git a/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php b/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php index ad6c78a53..6c8c09c3d 100644 --- a/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php +++ b/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php @@ -3,7 +3,8 @@ namespace Webkul\Discount\Http\Controllers; use Webkul\Discount\Repositories\CartRuleRepository as CartRule; -use Webkul\Checkout\Repositories\CartRepository as Cart; +use Webkul\Category\Repositories\CategoryRepository as Category; +use Webkul\Attribute\Repositories\AttributeRepository as Attribute; use Webkul\Discount\Repositories\CartRuleLabelsRepository as CartRuleLabels; use Webkul\Discount\Repositories\CartRuleCouponsRepository as CartRuleCoupons; @@ -40,6 +41,16 @@ class CartRuleController extends Controller */ protected $cartRuleCoupon; + /** + * To hold category repository instance + */ + protected $category; + + /** + * To hold attribute repository instance + */ + protected $attribute; + /** * To hold the cart repository instance */ @@ -48,7 +59,9 @@ class CartRuleController extends Controller public function __construct( CartRule $cartRule, CartRuleCoupons $cartRuleCoupon, - CartRuleLabels $cartRuleLabel + CartRuleLabels $cartRuleLabel, + Attribute $attribute, + Category $category ) { $this->_config = request('_config'); @@ -59,6 +72,10 @@ class CartRuleController extends Controller $this->cartRuleLabel = $cartRuleLabel; + $this->attribute = $attribute; + + $this->category = $category; + $this->appliedConfig = config('pricerules.cart'); } @@ -75,7 +92,7 @@ class CartRuleController extends Controller */ public function create() { - return view($this->_config['view'])->with('cart_rule', [$this->appliedConfig, [], $this->getStatesAndCountries()]); + return view($this->_config['view'])->with('cart_rule', [$this->appliedConfig, $this->category->getPartial(), $this->getStatesAndCountries(), $this->attribute->getPartial()]); } /** @@ -141,11 +158,11 @@ class CartRuleController extends Controller // prepare json object from actions if (isset($data['disc_amount']) && $data['action_type'] == config('pricerules.cart.validations.2')) { - $data['actions'] = [ - 'action_type' => $data['action_type'], - 'disc_amount' => $data['disc_amount'], - 'disc_threshold' => $data['disc_threshold'] - ]; + $data['actions'] = [ + 'action_type' => $data['action_type'], + 'disc_amount' => $data['disc_amount'], + 'disc_threshold' => $data['disc_threshold'] + ]; $data['disc_quantity'] = $data['disc_amount']; } else { diff --git a/packages/Webkul/Shop/src/Exceptions/Handler.php b/packages/Webkul/Shop/src/Exceptions/Handler.php new file mode 100644 index 000000000..a4d8ab3a9 --- /dev/null +++ b/packages/Webkul/Shop/src/Exceptions/Handler.php @@ -0,0 +1,79 @@ + 'Resource not found', + 403 => '403 forbidden Error', + 401 => 'Unauthenticated', + 500 => '500 Internal Server Error', + ]; + + /** + * Render an exception into an HTTP response. + * + * @param \Illuminate\Http\Request $request + * @param \Exception $exception + * @return \Illuminate\Http\Response + */ + public function render($request, Exception $exception) + { + $path = 'shop'; + + if ($exception instanceof HttpException) { + $statusCode = in_array($exception->getStatusCode(), [401, 403, 404, 503]) ? $exception->getStatusCode() : 500; + + return $this->response($path, $statusCode); + } else if ($exception instanceof ModelNotFoundException) { + return $this->response($path, 404); + } else if ($exception instanceof PDOException) { + return $this->response($path, 500); + } + + return parent::render($request, $exception); + } + + /** + * Convert an authentication exception into a response. + * + * @param \Illuminate\Http\Request $request + * @param \Illuminate\Auth\AuthenticationException $exception + * @return \Illuminate\Http\Response + */ + protected function unauthenticated($request, AuthenticationException $exception) + { + if ($request->expectsJson()) { + return response()->json(['error' => $this->jsonErrorMessages[401]], 401); + } + + return redirect()->guest(route('auth.login')); + } + + private function isSopUri() + { + return strpos($_SERVER['REQUEST_URI'], 'shop') !== false ? true : false; + } + + private function response($path, $statusCode) + { + if (request()->expectsJson()) { + return response()->json([ + 'error' => isset($this->jsonErrorMessages[$statusCode]) + ? $this->jsonErrorMessages[$statusCode] + : 'Something went wrong, please try again later.' + ], $statusCode); + } + + return response()->view("{$path}::errors.{$statusCode}", [], $statusCode); + } +} \ No newline at end of file