-
Notifications
You must be signed in to change notification settings - Fork 254
Add possibility To Delete Stocks in Admin Panel #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9300183
44db36f
16bfd46
a2697d7
67c0692
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Backend\Ui\Component\Control; | ||
|
||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\UrlInterface; | ||
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface; | ||
|
||
/** | ||
* Represents delete button with pre-configured options | ||
* Provide an ability to show confirmation message on click on the "Delete" button | ||
* | ||
* @api | ||
*/ | ||
class DeleteButton implements ButtonProviderInterface | ||
{ | ||
/** | ||
* @var RequestInterface | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var UrlInterface | ||
*/ | ||
private $urlBuilder; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $confirmationMessage; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $idFieldName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $deleteRoutePath; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $sortOrder; | ||
|
||
/** | ||
* @param RequestInterface $request | ||
* @param UrlInterface $urlBuilder | ||
* @param string $confirmationMessage | ||
* @param string $idFieldName | ||
* @param string $deleteRoutePath | ||
* @param int $sortOrder | ||
*/ | ||
public function __construct( | ||
RequestInterface $request, | ||
UrlInterface $urlBuilder, | ||
string $confirmationMessage, | ||
string $idFieldName, | ||
string $deleteRoutePath, | ||
int $sortOrder | ||
) { | ||
$this->request = $request; | ||
$this->urlBuilder = $urlBuilder; | ||
$this->confirmationMessage = $confirmationMessage; | ||
$this->idFieldName = $idFieldName; | ||
$this->deleteRoutePath = $deleteRoutePath; | ||
$this->sortOrder = $sortOrder; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getButtonData() | ||
{ | ||
$data = []; | ||
$id = $this->request->getParam($this->idFieldName); | ||
if (null !== $id) { | ||
$url = $this->urlBuilder->getUrl($this->deleteRoutePath); | ||
$data = [ | ||
'label' => __('Delete'), | ||
'class' => 'delete', | ||
'on_click' => | ||
"deleteConfirm('{$this->confirmationMessage}', '{$url}', {data:{{$this->idFieldName}:{$id}}})", | ||
'sort_order' => $this->sortOrder, | ||
]; | ||
} | ||
return $data; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Inventory\Controller\Adminhtml\Stock; | ||
|
||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Framework\Exception\CouldNotDeleteException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\InventoryApi\Api\StockRepositoryInterface; | ||
use Magento\InventoryApi\Api\Data\StockInterface; | ||
|
||
/** | ||
* Delete Controller | ||
*/ | ||
class Delete extends Action | ||
{ | ||
/** | ||
* @see _isAllowed() | ||
*/ | ||
const ADMIN_RESOURCE = 'Magento_Inventory::stock'; | ||
|
||
/** | ||
* @var StockRepositoryInterface | ||
*/ | ||
private $stockRepository; | ||
|
||
/** | ||
* @param Context $context | ||
* @param StockRepositoryInterface $stockRepository | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
StockRepositoryInterface $stockRepository | ||
) { | ||
parent::__construct($context); | ||
$this->stockRepository = $stockRepository; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute() | ||
{ | ||
$resultRedirect = $this->resultRedirectFactory->create(); | ||
|
||
$stockId = $this->getRequest()->getPost(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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Inventory\Controller\Adminhtml\Stock; | ||
|
||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Inventory\Ui\Component\MassAction\Filter; | ||
use Magento\Framework\Exception\CouldNotDeleteException; | ||
use Magento\InventoryApi\Api\StockRepositoryInterface; | ||
|
||
/** | ||
* MassDelete Controller | ||
*/ | ||
class MassDelete extends Action | ||
{ | ||
/** | ||
* @see _isAllowed() | ||
*/ | ||
const ADMIN_RESOURCE = 'Magento_Inventory::stock'; | ||
|
||
/** | ||
* @var StockRepositoryInterface | ||
*/ | ||
private $stockRepository; | ||
|
||
/** | ||
* @var Filter | ||
*/ | ||
private $massActionFilter; | ||
|
||
/** | ||
* @param Context $context | ||
* @param StockRepositoryInterface $stockRepository | ||
* @param Filter $massActionFilter | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
StockRepositoryInterface $stockRepository, | ||
Filter $massActionFilter | ||
) { | ||
parent::__construct($context); | ||
$this->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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about NoSuchEntityException? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that it is not necessary because deleteById method throw CouldNotDeleteException and this scenario is served. See There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bartekszymanski correct |
||
} | ||
$this->messageManager->addSuccessMessage(__('You deleted %1 Stock(s).', $deletedItemsCount)); | ||
|
||
return $this->resultRedirectFactory->create()->setPath('*/*'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Inventory\Ui\Component\MassAction; | ||
|
||
use Magento\Ui\Component\MassAction\Filter as BaseFilter; | ||
use Magento\Framework\Api\Search\DocumentInterface; | ||
|
||
/** | ||
* Temporary solution | ||
* @todo Need to remove after fixing the issue | ||
* @see https://github.com/magento/magento2/issues/10988 | ||
*/ | ||
class Filter | ||
{ | ||
/** | ||
* @var BaseFilter | ||
*/ | ||
private $filter; | ||
|
||
/** | ||
* @param BaseFilter $filter | ||
*/ | ||
public function __construct( | ||
BaseFilter $filter | ||
) { | ||
$this->filter = $filter; | ||
} | ||
|
||
/** | ||
* Get ids from search filter | ||
* | ||
* @return array | ||
*/ | ||
public function getIds() | ||
{ | ||
$this->filter->applySelectionOnTargetProvider(); | ||
$component = $this->filter->getComponent(); | ||
$dataProvider = $component->getContext()->getDataProvider(); | ||
$searchResult = $dataProvider->getSearchResult(); | ||
|
||
|
||
return array_map(function (DocumentInterface $item) { | ||
return $item->getId(); | ||
}, $searchResult->getItems()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in both Delete and MassDelete we have a duplication of this constant, that means that it should be declared in another place.
Because currently, such code violates DRY