Skip to content
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

[Improvement] Implement customer group form on ui component #13887

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Group\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class BackButton
*/
class BackButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Back'),
'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
'class' => 'back',
'sort_order' => 10
];
}

/**
* Get URL for back (reset) button
*
* @return string
*/
public function getBackUrl()
{
return $this->getUrl('*/*/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Group\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class DeleteButton
*/
class DeleteButton extends GenericButton implements ButtonProviderInterface
{
/**
* @var \Magento\Customer\Api\GroupManagementInterface
*/
protected $groupManagement;

/**
* Constructor
*
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
*/
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Customer\Api\GroupManagementInterface $groupManagement
) {
parent::__construct($context, $registry);
$this->groupManagement = $groupManagement;
}

/**
* @return array
*/
public function getButtonData()
{
$groupId = $this->getGroupId();
$canModify = $groupId && !$this->groupManagement->isReadonly($this->getGroupId());
$data = [];
if ($canModify) {
$data = [
'label' => __('Delete Customer Group'),
'class' => 'delete',
'on_click' => 'deleteConfirm(\'' . __(
'Are you sure you want to do this?'
) . '\', \'' . $this->getDeleteUrl() . '\')',
'sort_order' => 20,
];
}

return $data;
}

/**
* @return string
*/
public function getDeleteUrl()
{
return $this->getUrl('*/*/delete', ['id' => $this->getGroupId()]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Group\Edit;

use Magento\Customer\Controller\RegistryConstants;

/**
* Class GenericButton
*/
class GenericButton
{
/**
* Url Builder
*
* @var \Magento\Framework\UrlInterface
*/
protected $urlBuilder;

/**
* Registry
*
* @var \Magento\Framework\Registry
*/
protected $registry;

/**
* Constructor
*
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Framework\Registry $registry
*/
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry
) {
$this->urlBuilder = $context->getUrlBuilder();
$this->registry = $registry;
}

/**
* Return the group Id.
*
* @return int|null
*/
public function getGroupId()
{
return $this->registry->registry(RegistryConstants::CURRENT_GROUP_ID);
}

/**
* Generate url by route and parameters
*
* @param string $route
* @param array $params
* @return string
*/
public function getUrl($route = '', $params = [])
{
return $this->urlBuilder->getUrl($route, $params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Group\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class ResetButton
*/
class ResetButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Reset'),
'class' => 'reset',
'on_click' => 'location.reload();',
'sort_order' => 30
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Group\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class SaveAndContinueButton
*/
class SaveAndContinueButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Save and Continue Edit'),
'class' => 'save',
'data_attribute' => [
'mage-init' => [
'button' => ['event' => 'saveAndContinueEdit'],
],
],
'sort_order' => 80,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Group\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Class SaveButton
*/
class SaveButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Save Customer Group'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save']],
'form-role' => 'save',
],
'sort_order' => 90,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public function execute()
);
}

$resultPage->getLayout()->addBlock(\Magento\Customer\Block\Adminhtml\Group\Edit::class, 'group', 'content')
->setEditMode((bool)$groupId);

return $resultPage;
}
}
17 changes: 11 additions & 6 deletions app/code/Magento/Customer/Controller/Adminhtml/Group/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ protected function storeCustomerGroupDataToSession($customerGroupData)
*/
public function execute()
{
$taxClass = (int)$this->getRequest()->getParam('tax_class');
$taxClass = (int)$this->getRequest()->getParam('tax_class_id');

/** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
$customerGroup = null;
if ($taxClass) {
$id = $this->getRequest()->getParam('id');
$id = $this->getRequest()->getParam('customer_group_id');
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
try {
$customerGroupCode = (string)$this->getRequest()->getParam('code');
if ($id !== null) {
$customerGroupCode = (string)$this->getRequest()->getParam('customer_group_code');
if ($id !== null && $id !== '') {
$customerGroup = $this->groupRepository->getById((int)$id);
$customerGroupCode = $customerGroupCode ?: $customerGroup->getCode();
} else {
Expand All @@ -87,10 +88,14 @@ public function execute()
$customerGroup->setCode(!empty($customerGroupCode) ? $customerGroupCode : null);
$customerGroup->setTaxClassId($taxClass);

$this->groupRepository->save($customerGroup);
$customerGroup = $this->groupRepository->save($customerGroup);

$this->messageManager->addSuccess(__('You saved the customer group.'));
$resultRedirect->setPath('customer/group');
if ($this->getRequest()->getParam('back')) {
$resultRedirect->setPath('customer/group/edit', ['id' => $customerGroup->getId()]);
} else {
$resultRedirect->setPath('customer/group');
}
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
if ($customerGroup != null) {
Expand Down
Loading