From 930018320d024f00506b3e8b4a96ccc1cc98f37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szyma=C5=84ski?= Date: Sun, 17 Sep 2017 15:29:18 +0200 Subject: [PATCH 1/5] Add possibility To Delete Stocks in Admin Panel --- .../Controller/Adminhtml/Stock/Delete.php | 72 ++++++++++++++++++ .../Controller/Adminhtml/Stock/Edit.php | 1 - .../Controller/Adminhtml/Stock/MassDelete.php | 74 +++++++++++++++++++ .../Component/Control/Stock/DeleteButton.php | 61 +++++++++++++++ .../Component/Control/Stock/GenericButton.php | 60 +++++++++++++++ .../Ui/Component/MassAction/Filter.php | 46 ++++++++++++ .../ui_component/inventory_stock_form.xml | 1 + .../ui_component/inventory_stock_listing.xml | 21 +++++- 8 files changed, 334 insertions(+), 2 deletions(-) create mode 100644 app/code/Magento/Inventory/Controller/Adminhtml/Stock/Delete.php create mode 100644 app/code/Magento/Inventory/Controller/Adminhtml/Stock/MassDelete.php create mode 100644 app/code/Magento/Inventory/Ui/Component/Control/Stock/DeleteButton.php create mode 100644 app/code/Magento/Inventory/Ui/Component/Control/Stock/GenericButton.php create mode 100644 app/code/Magento/Inventory/Ui/Component/MassAction/Filter.php diff --git a/app/code/Magento/Inventory/Controller/Adminhtml/Stock/Delete.php b/app/code/Magento/Inventory/Controller/Adminhtml/Stock/Delete.php new file mode 100644 index 000000000000..a2849a2ff3f6 --- /dev/null +++ b/app/code/Magento/Inventory/Controller/Adminhtml/Stock/Delete.php @@ -0,0 +1,72 @@ +stockRepository = $stockRepository; + } + + /** + * @inheritdoc + */ + public function execute() + { + $resultRedirect = $this->resultRedirectFactory->create(); + $stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID); + if ($stockId === null) { + $this->messageManager->addErrorMessage(__('Wrong request.')); + + return $resultRedirect->setPath('*/*'); + } + try { + $this->stockRepository->deleteById($stockId); + $this->messageManager->addSuccessMessage(__('The Stock has been deleted.')); + $resultRedirect->setPath('*/*'); + } catch (NoSuchEntityException $e) { + $this->messageManager->addErrorMessage(__('Stock with id "%1" does not exist.', $stockId)); + $resultRedirect->setPath('*/*'); + } catch (CouldNotDeleteException $e) { + $this->messageManager->addErrorMessage($e->getMessage()); + $resultRedirect->setPath('*/*/edit', [ + StockInterface::STOCK_ID => $stockId, + '_current' => true, + ]); + } + + return $resultRedirect; + } +} \ No newline at end of file diff --git a/app/code/Magento/Inventory/Controller/Adminhtml/Stock/Edit.php b/app/code/Magento/Inventory/Controller/Adminhtml/Stock/Edit.php index 84a77734a9e6..87d50b440e53 100644 --- a/app/code/Magento/Inventory/Controller/Adminhtml/Stock/Edit.php +++ b/app/code/Magento/Inventory/Controller/Adminhtml/Stock/Edit.php @@ -49,7 +49,6 @@ public function execute() $stockId = $this->getRequest()->getParam(StockInterface::STOCK_ID); try { $stock = $this->stockRepository->get($stockId); - /** @var Page $result */ $result = $this->resultFactory->create(ResultFactory::TYPE_PAGE); $result->setActiveMenu('Magento_Inventory::stock') diff --git a/app/code/Magento/Inventory/Controller/Adminhtml/Stock/MassDelete.php b/app/code/Magento/Inventory/Controller/Adminhtml/Stock/MassDelete.php new file mode 100644 index 000000000000..ebff47d752e7 --- /dev/null +++ b/app/code/Magento/Inventory/Controller/Adminhtml/Stock/MassDelete.php @@ -0,0 +1,74 @@ +stockRepository = $stockRepository; + $this->massActionFilter = $massActionFilter; + } + + /** + * @inheritdoc + */ + public function execute() + { + if ($this->getRequest()->isPost() !== true) { + $this->messageManager->addErrorMessage(__('Wrong request.')); + + return $this->resultRedirectFactory->create()->setPath('*/*'); + } + $deletedItemsCount = 0; + foreach ($this->massActionFilter->getIds() as $id) { + try { + $this->stockRepository->deleteById($id); + $deletedItemsCount++; + } catch (CouldNotDeleteException $e) { + $errorMessage = __('[ID: %1] ', $id) . $e->getMessage(); + $this->messageManager->addErrorMessage($errorMessage); + } + } + $this->messageManager->addSuccessMessage(__('You deleted %1 Stock(s).', $deletedItemsCount)); + + return $this->resultRedirectFactory->create()->setPath('*/*'); + } +} \ No newline at end of file diff --git a/app/code/Magento/Inventory/Ui/Component/Control/Stock/DeleteButton.php b/app/code/Magento/Inventory/Ui/Component/Control/Stock/DeleteButton.php new file mode 100644 index 000000000000..4756d39e6e22 --- /dev/null +++ b/app/code/Magento/Inventory/Ui/Component/Control/Stock/DeleteButton.php @@ -0,0 +1,61 @@ +button = $button; + } + + /** + * Get stock delete button data such as label, css class, on click action and sort order + * + * @return array + */ + public function getButtonData() + { + $data = []; + if ($this->button->getId()) { + $data = [ + 'label' => __('Delete Stock'), + 'class' => 'delete', + 'on_click' => 'deleteConfirm(\'' . __('Are you sure you want to delete this stock ?') . '\', \'' . $this->getDeleteUrl() . '\')', + 'sort_order' => 20, + ]; + } + + return $data; + } + + /** + * Get stock delete url + * + * @return string + */ + public function getDeleteUrl() + { + return $this->button->getUrl('*/*/delete', [StockInterface::STOCK_ID => $this->button->getId()]); + } +} \ No newline at end of file diff --git a/app/code/Magento/Inventory/Ui/Component/Control/Stock/GenericButton.php b/app/code/Magento/Inventory/Ui/Component/Control/Stock/GenericButton.php new file mode 100644 index 000000000000..7daab6d61069 --- /dev/null +++ b/app/code/Magento/Inventory/Ui/Component/Control/Stock/GenericButton.php @@ -0,0 +1,60 @@ +urlBuilder = $urlBuilder; + $this->request = $request; + } + + /** + * Get stock id + * + * @return int|null + */ + public function getId() + { + return $this->request->getParam(StockInterface::STOCK_ID) ?? null; + } + + /** + * Get button url base on route and parameters + * + * @param string $route + * @param array $params + * + * @return string + */ + public function getUrl($route = '', $params = []) + { + return $this->urlBuilder->getUrl($route, $params); + } +} \ No newline at end of file diff --git a/app/code/Magento/Inventory/Ui/Component/MassAction/Filter.php b/app/code/Magento/Inventory/Ui/Component/MassAction/Filter.php new file mode 100644 index 000000000000..08e451b66b04 --- /dev/null +++ b/app/code/Magento/Inventory/Ui/Component/MassAction/Filter.php @@ -0,0 +1,46 @@ +filter = $filter; + } + + /** + * Get ids from search filter + * + * @return array + */ + public function getIds() + { + $this->filter->applySelectionOnTargetProvider(); + + return array_map(function(DocumentInterface $item) { + return $item->getId(); + }, $this->filter->getComponent()->getContext()->getDataProvider()->getSearchResult()->getItems()); + } +} \ No newline at end of file diff --git a/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_form.xml b/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_form.xml index 8fd88706bcdd..50e3b47b127a 100644 --- a/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_form.xml +++ b/app/code/Magento/Inventory/view/adminhtml/ui_component/inventory_stock_form.xml @@ -29,6 +29,7 @@ reset +