diff --git a/packages/Webkul/Admin/src/DataGrids/CategoryDataGrid.php b/packages/Webkul/Admin/src/DataGrids/CategoryDataGrid.php index 092814009..b6e7237ab 100755 --- a/packages/Webkul/Admin/src/DataGrids/CategoryDataGrid.php +++ b/packages/Webkul/Admin/src/DataGrids/CategoryDataGrid.php @@ -99,6 +99,14 @@ class CategoryDataGrid extends DataGrid 'route' => 'admin.catalog.categories.delete', 'confirm_text' => trans('ui::app.datagrid.massaction.delete', ['resource' => 'product']), 'icon' => 'icon trash-icon', + 'function' => 'deleteFunction($event, "delete")' + ]); + + $this->addMassAction([ + 'type' => 'delete', + 'label' => trans('admin::app.datagrid.delete'), + 'action' => route('admin.catalog.categories.massdelete'), + 'method' => 'DELETE', ]); } } \ No newline at end of file diff --git a/packages/Webkul/Admin/src/Http/routes.php b/packages/Webkul/Admin/src/Http/routes.php index b0db82981..1094e5bd7 100755 --- a/packages/Webkul/Admin/src/Http/routes.php +++ b/packages/Webkul/Admin/src/Http/routes.php @@ -335,6 +335,13 @@ Route::group(['middleware' => ['web']], function () { Route::post('/categories/delete/{id}', 'Webkul\Category\Http\Controllers\CategoryController@destroy')->name('admin.catalog.categories.delete'); + //category massdelete + Route::post('categories/massdelete', 'Webkul\Category\Http\Controllers\CategoryController@massDestroy')->defaults('_config', [ + 'redirect' => 'admin.catalog.categories.index', + ])->name('admin.catalog.categories.massdelete'); + + Route::post('/categories/product/count', 'Webkul\Category\Http\Controllers\CategoryController@categoryProductCount')->name('admin.catalog.categories.product.count'); + // Catalog Attribute Routes Route::get('/attributes', 'Webkul\Attribute\Http\Controllers\AttributeController@index')->defaults('_config', [ diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/categories/index.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/categories/index.blade.php index 00e445e5f..7b532ffb2 100755 --- a/packages/Webkul/Admin/src/Resources/views/catalog/categories/index.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/categories/index.blade.php @@ -26,4 +26,58 @@ {!! view_render_event('bagisto.admin.catalog.categories.list.after') !!} -@stop \ No newline at end of file +@stop + +@push('scripts') + +@endpush \ No newline at end of file diff --git a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php index 6270e70f0..7c10d524a 100755 --- a/packages/Webkul/Category/src/Http/Controllers/CategoryController.php +++ b/packages/Webkul/Category/src/Http/Controllers/CategoryController.php @@ -153,7 +153,10 @@ class CategoryController extends Controller try { Event::dispatch('catalog.category.delete.before', $id); - $this->categoryRepository->delete($id); + if($category->products->count() > 0) { + $category->products()->delete(); + } + $category->delete(); Event::dispatch('catalog.category.delete.after', $id); @@ -175,36 +178,52 @@ class CategoryController extends Controller */ public function massDestroy() { - $suppressFlash = false; + $suppressFlash = true; + $categoryIds = explode(',', request()->input('indexes')); - if (request()->isMethod('delete') || request()->isMethod('post')) { - $indexes = explode(',', request()->input('indexes')); + foreach ($categoryIds as $categoryId) { + $category = $this->categoryRepository->find($categoryId); - foreach ($indexes as $key => $value) { - try { - Event::dispatch('catalog.category.delete.before', $value); + if (isset($category)) { + if(strtolower($category->name) == "root") { + $suppressFlash = false; + session()->flash('warning', trans('admin::app.response.delete-category-root', ['name' => 'Category'])); + } else { + try { + $suppressFlash = true; + Event::dispatch('catalog.category.delete.before', $categoryId); - $this->categoryRepository->delete($value); + if($category->products->count() > 0) { + $category->products()->delete(); + } + $category->delete(); - Event::dispatch('catalog.category.delete.after', $value); - } catch(\Exception $e) { - $suppressFlash = true; - - continue; + Event::dispatch('catalog.category.delete.after', $categoryId); + + } catch(\Exception $e) { + session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Category'])); + } } } - - 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'])); - } - - return redirect()->back(); - } else { - session()->flash('error', trans('admin::app.datagrid.mass-ops.method-error')); - - return redirect()->back(); } + + if (count($categoryIds) != 1 || $suppressFlash == true) { + session()->flash('success', trans('admin::app.datagrid.mass-ops.delete-success', ['resource' => 'Category'])); + } + + return redirect()->route($this->_config['redirect']); + } + + + public function categoryProductCount() { + $indexes = explode(",", request()->input('indexes')); + $product_count = 0; + + foreach($indexes as $index) { + $category = $this->categoryRepository->find($index); + $product_count += $category->products->count(); + } + + return response()->json(['product_count' => $product_count], 200); } } \ No newline at end of file diff --git a/packages/Webkul/Category/src/Models/Category.php b/packages/Webkul/Category/src/Models/Category.php index 70e95f12a..7091e0eac 100755 --- a/packages/Webkul/Category/src/Models/Category.php +++ b/packages/Webkul/Category/src/Models/Category.php @@ -9,6 +9,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Webkul\Category\Contracts\Category as CategoryContract; use Webkul\Attribute\Models\AttributeProxy; use Webkul\Category\Repositories\CategoryRepository; +use Webkul\Product\Models\ProductProxy; /** * Class Category @@ -129,4 +130,12 @@ class Category extends TranslatableModel implements CategoryContract return $this->findInTree($category->children); } + + /** + * The products that belong to the category. + */ + public function products() + { + return $this->belongsToMany(ProductProxy::modelClass(), 'product_categories'); + } } \ No newline at end of file diff --git a/packages/Webkul/Ui/src/Resources/lang/ar/app.php b/packages/Webkul/Ui/src/Resources/lang/ar/app.php index 5ca0d7df6..411fddbb5 100644 --- a/packages/Webkul/Ui/src/Resources/lang/ar/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/ar/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'هل تريد حقا تحديث الحالة من منتقى :resource?', 'delete' => 'هل تريد حقا حذف هذا :resource?', 'edit' => 'هل تريد حقا تحرير هذا :resource?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'يمكن أن تحتوي أعمدة الفهرس على قيم أكبر من الصفر فقط', diff --git a/packages/Webkul/Ui/src/Resources/lang/de/app.php b/packages/Webkul/Ui/src/Resources/lang/de/app.php index b42420d92..2c2d0248c 100755 --- a/packages/Webkul/Ui/src/Resources/lang/de/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/de/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'Möchten Sie den Status der ausgewählten :resource wirklich aktualisieren?', 'delete' => 'Möchten Sie diese Aktion wirklich ausführen?', 'edit' => 'Möchten Sie :resource wirklich bearbeiten?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'Indexspalten können nur Werte größer als Null haben', diff --git a/packages/Webkul/Ui/src/Resources/lang/en/app.php b/packages/Webkul/Ui/src/Resources/lang/en/app.php index b87b501c1..6be9038ce 100755 --- a/packages/Webkul/Ui/src/Resources/lang/en/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/en/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'Do you really want to update status of these selected :resource?', 'delete' => 'Do you really want to perform this action?', 'edit' => 'Do you really want to edit this :resource?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'Index columns can have values greater than zero only', diff --git a/packages/Webkul/Ui/src/Resources/lang/fa/app.php b/packages/Webkul/Ui/src/Resources/lang/fa/app.php index 68c19f8a1..bd7301bd1 100644 --- a/packages/Webkul/Ui/src/Resources/lang/fa/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/fa/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'آیا واقعاً می خواهید وضعیت انتخاب شده را به روز کنید :resource?', 'delete' => 'آیا واقعاً می خواهید این عمل را انجام دهید؟', 'edit' => 'آیا واقعاً می خواهید این را ویرایش کنید :resource?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'ستون های فهرست می توانند مقادیری بیشتر از صفر داشته باشند', diff --git a/packages/Webkul/Ui/src/Resources/lang/it/app.php b/packages/Webkul/Ui/src/Resources/lang/it/app.php index 93125f800..fa704ad06 100644 --- a/packages/Webkul/Ui/src/Resources/lang/it/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/it/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'Vuoi aggiornare davvero lo stato dei :resource selezionati?', 'delete' => 'Vuoi davvero effettuare questa azione?', 'edit' => 'Vuoi davvero modificare questo :resource?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'Le colonnne indice possono avere solo valori maggiori di zero', diff --git a/packages/Webkul/Ui/src/Resources/lang/nl/app.php b/packages/Webkul/Ui/src/Resources/lang/nl/app.php index 929dab2a2..94d3cc1bf 100644 --- a/packages/Webkul/Ui/src/Resources/lang/nl/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/nl/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'Do you really want to update status of these selected :resource?', 'delete' => 'Wilt u deze actie echt uitvoeren?', 'edit' => 'Wil je dit echt bewerken :resource?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'Index columns can have values greater than zero only', diff --git a/packages/Webkul/Ui/src/Resources/lang/pl/app.php b/packages/Webkul/Ui/src/Resources/lang/pl/app.php index 6949c5121..ab922faa8 100644 --- a/packages/Webkul/Ui/src/Resources/lang/pl/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/pl/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'Czy naprawdę chcesz zaktualizować status tych wybranych :resource?', 'delete' => 'Czy naprawdę chcesz wykonać tę akcję?', 'edit' => 'Czy naprawdę chcesz edytować :resource?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'Kolumny indeksu mogą mieć wartości większe niż tylko zero', diff --git a/packages/Webkul/Ui/src/Resources/lang/pt_BR/app.php b/packages/Webkul/Ui/src/Resources/lang/pt_BR/app.php index 997bcc419..93d9f51ef 100755 --- a/packages/Webkul/Ui/src/Resources/lang/pt_BR/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/pt_BR/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'Você realmente deseja atualizar o status desses itens selecionados :resource?', 'delete' => 'Você realmente deseja excluir este :resource?', 'edit' => 'Você realmente quer editar este :resource?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'Colunas do índice podem ter valores maiores que zero apenas', diff --git a/packages/Webkul/Ui/src/Resources/lang/tr/app.php b/packages/Webkul/Ui/src/Resources/lang/tr/app.php index 55930995f..4d7abc7a3 100644 --- a/packages/Webkul/Ui/src/Resources/lang/tr/app.php +++ b/packages/Webkul/Ui/src/Resources/lang/tr/app.php @@ -9,6 +9,7 @@ return [ 'mass-update-status' => 'Seçili :resource kayıtlarının durumunu güncellemek istediğinizden emin misiniz?', 'delete' => 'Bu işlemi gerçekleştirmek istediğinizden emin misiniz?', 'edit' => ':resource kaydını düzenlemek istediğinizden emin misiniz?', + 'delete-category-product' => 'The selected categories contains products. Performing this action will remove the related products. Do you really want to perform this action?' ], 'zero-index' => 'Index sütunları sadece sıfırdan büyük değere sahip olmalı', diff --git a/packages/Webkul/Ui/src/Resources/views/datagrid/body.blade.php b/packages/Webkul/Ui/src/Resources/views/datagrid/body.blade.php index 15a0ab40a..6e48f0526 100644 --- a/packages/Webkul/Ui/src/Resources/views/datagrid/body.blade.php +++ b/packages/Webkul/Ui/src/Resources/views/datagrid/body.blade.php @@ -48,25 +48,32 @@ @if ($toDisplay) + @if (isset($action['target'])) + target="{{ $action['target'] }}" + @endif + + @if (isset($action['title'])) + title="{{ $action['title'] }}" + @endif + > @endif diff --git a/packages/Webkul/Ui/src/Resources/views/datagrid/partials/mass-action-header.blade.php b/packages/Webkul/Ui/src/Resources/views/datagrid/partials/mass-action-header.blade.php index f6dcd09fd..e36830f81 100644 --- a/packages/Webkul/Ui/src/Resources/views/datagrid/partials/mass-action-header.blade.php +++ b/packages/Webkul/Ui/src/Resources/views/datagrid/partials/mass-action-header.blade.php @@ -8,7 +8,7 @@ -
+ @csrf() diff --git a/packages/Webkul/Ui/src/Resources/views/datagrid/table.blade.php b/packages/Webkul/Ui/src/Resources/views/datagrid/table.blade.php index 1b20365c6..c8f0fc365 100644 --- a/packages/Webkul/Ui/src/Resources/views/datagrid/table.blade.php +++ b/packages/Webkul/Ui/src/Resources/views/datagrid/table.blade.php @@ -272,6 +272,7 @@ massActions: @json($results['massactions']), massActionsToggle: false, massActionTarget: null, + massActionConfirmText: '{{ __('ui::app.datagrid.click_on_action') }}', massActionType: null, massActionValues: [], massActionTargets: [], @@ -457,7 +458,8 @@ for (let id in this.massActions) { targetObj = { 'type': this.massActions[id].type, - 'action': this.massActions[id].action + 'action': this.massActions[id].action, + 'confirm_text': this.massActions[id].confirm_text }; this.massActionTargets.push(targetObj); @@ -483,6 +485,7 @@ for (let i in this.massActionTargets) { if (this.massActionTargets[i].type === 'delete') { this.massActionTarget = this.massActionTargets[i].action; + this.massActionConfirmText = this.massActionTargets[i].confirm_text ? this.massActionTargets[i].confirm_text : this.massActionConfirmText; break; } @@ -493,6 +496,7 @@ for (let i in this.massActionTargets) { if (this.massActionTargets[i].type === 'update') { this.massActionTarget = this.massActionTargets[i].action; + this.massActionConfirmText = this.massActionTargets[i].confirm_text ? this.massActionTargets[i].confirm_text : this.massActionConfirmText; break; } @@ -816,31 +820,6 @@ } }, - doAction: function (e) { - var element = e.currentTarget; - - if (confirm('{{__('ui::app.datagrid.massaction.delete') }}')) { - axios.post(element.getAttribute('data-action'), { - _token: element.getAttribute('data-token'), - _method: element.getAttribute('data-method') - }).then(function (response) { - this.result = response; - - if (response.data.redirect) { - window.location.href = response.data.redirect; - } else { - location.reload(); - } - }).catch(function (error) { - location.reload(); - }); - - e.preventDefault(); - } else { - e.preventDefault(); - } - }, - captureColumn: function (id) { element = document.getElementById(id); @@ -869,6 +848,37 @@ } } }); + + + function doAction(e, message, type) { + var element = e.currentTarget; + if (message) { + element = e.target.parentElement; + } + + message = message || '{{__('ui::app.datagrid.massaction.delete') }}'; + + if (confirm(message)) { + axios.post(element.getAttribute('data-action'), { + _token: element.getAttribute('data-token'), + _method: element.getAttribute('data-method') + }).then(function (response) { + this.result = response; + + if (response.data.redirect) { + window.location.href = response.data.redirect; + } else { + location.reload(); + } + }).catch(function (error) { + location.reload(); + }); + + e.preventDefault(); + } else { + e.preventDefault(); + } + } @endpush