Skip to content

Commit

Permalink
Merge pull request magento#3524 from magento-panda/MAGETWO-94346
Browse files Browse the repository at this point in the history
Fixed issues:
  - MAGETWO-95249: [Part 1] Implement handling of large number of addresses on admin edit customer page
  - MAGETWO-96212: [Part 2] Implement handling of large number of addresses on admin edit customer page
  - MAGETWO-96906: [MAGETWO-94346] PR stabilization
  • Loading branch information
igrybkov authored Dec 13, 2018
2 parents 5f89b03 + 020dd1c commit b8892f0
Show file tree
Hide file tree
Showing 87 changed files with 6,266 additions and 1,236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<data key="City">Yerevan</data>
<data key="Zip">9999</data>
<data key="PhoneNumber">9999</data>
<data key="Country">Armenia</data>
</entity>

</entities>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Edit\Address;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Customer\Block\Adminhtml\Edit\GenericButton;

/**
* Class CancelButton
*/
class CancelButton extends GenericButton implements ButtonProviderInterface
{
/**
* @inheritdoc
*
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Cancel'),
'on_click' => '',
'data_attribute' => [
'mage-init' => [
'Magento_Ui/js/form/button-adapter' => [
'actions' => [
[
'targetName' => 'customer_form.areas.address.address.customer_address_update_modal',
'actionName' => 'closeModal'
],
],
],
],
],
'sort_order' => 20
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Edit\Address;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Customer\Ui\Component\Listing\Address\Column\Actions;

/**
* Delete button on edit customer address form
*/
class DeleteButton extends GenericButton implements ButtonProviderInterface
{
/**
* Get delete button data.
*
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getButtonData()
{
$data = [];
if ($this->getAddressId()) {
$data = [
'label' => __('Delete'),
'on_click' => '',
'data_attribute' => [
'mage-init' => [
'Magento_Ui/js/form/button-adapter' => [
'actions' => [
[
'targetName' => 'customer_address_form.customer_address_form',
'actionName' => 'deleteAddress',
'params' => [
$this->getDeleteUrl(),
],

]
],
],
],
],
'sort_order' => 20
];
}
return $data;
}

/**
* Get delete button url.
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function getDeleteUrl(): string
{
return $this->getUrl(
Actions::CUSTOMER_ADDRESS_PATH_DELETE,
['parent_id' => $this->getCustomerId(), 'id' => $this->getAddressId()]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Block\Adminhtml\Edit\Address;

use Magento\Customer\Model\AddressFactory;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\UrlInterface;
use Magento\Customer\Model\ResourceModel\Address;
use Magento\Customer\Model\ResourceModel\AddressRepository;

/**
* Class for common code for buttons on the create/edit address form
*/
class GenericButton
{
/**
* @var AddressFactory
*/
private $addressFactory;

/**
* @var UrlInterface
*/
private $urlBuilder;

/**
* @var Address
*/
private $addressResourceModel;

/**
* @var RequestInterface
*/
private $request;

/**
* @var AddressRepository
*/
private $addressRepository;

/**
* @param AddressFactory $addressFactory
* @param UrlInterface $urlBuilder
* @param Address $addressResourceModel
* @param RequestInterface $request
* @param AddressRepository $addressRepository
*/
public function __construct(
AddressFactory $addressFactory,
UrlInterface $urlBuilder,
Address $addressResourceModel,
RequestInterface $request,
AddressRepository $addressRepository
) {
$this->addressFactory = $addressFactory;
$this->urlBuilder = $urlBuilder;
$this->addressResourceModel = $addressResourceModel;
$this->request = $request;
$this->addressRepository = $addressRepository;
}

/**
* Return address Id.
*
* @return int|null
*/
public function getAddressId()
{
$address = $this->addressFactory->create();

$entityId = $this->request->getParam('entity_id');
$this->addressResourceModel->load(
$address,
$entityId
);

return $address->getEntityId() ?: null;
}

/**
* Get customer id.
*
* @return int|null
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getCustomerId()
{
$addressId = $this->request->getParam('entity_id');

$address = $this->addressRepository->getById($addressId);

return $address->getCustomerId() ?: null;
}

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

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Customer\Block\Adminhtml\Edit\GenericButton;

/**
* Class SaveButton
*/
class SaveButton extends GenericButton implements ButtonProviderInterface
{
/**
* @inheritdoc
*
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Save'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save']],
'form-role' => 'save',
],
'sort_order' => 10
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Controller\Adminhtml\Address;

use Magento\Backend\App\Action;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;
use Psr\Log\LoggerInterface;

/**
* Class to process set default billing address action
*/
class DefaultBillingAddress extends Action implements HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
public const ADMIN_RESOURCE = 'Magento_Customer::manage';

/**
* @var AddressRepositoryInterface
*/
private $addressRepository;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var JsonFactory
*/
private $resultJsonFactory;

/**
* @param Action\Context $context
* @param AddressRepositoryInterface $addressRepository
* @param LoggerInterface $logger
* @param JsonFactory $resultJsonFactory
*/
public function __construct(
Action\Context $context,
AddressRepositoryInterface $addressRepository,
LoggerInterface $logger,
JsonFactory $resultJsonFactory
) {
parent::__construct($context);
$this->addressRepository = $addressRepository;
$this->logger = $logger;
$this->resultJsonFactory = $resultJsonFactory;
}

/**
* Execute action to set customer default billing address
*
* @return Json
*/
public function execute(): Json
{
$customerId = $this->getRequest()->getParam('parent_id', false);
$addressId = $this->getRequest()->getParam('id', false);
$error = true;
$message = __('There is no address id in setting default billing address.');

if ($addressId) {
try {
$address = $this->addressRepository->getById($addressId)->setCustomerId($customerId);
$this->setAddressAsDefault($address);
$this->addressRepository->save($address);
$message = __('Default billing address has been changed.');
$error = false;
} catch (\Exception $e) {
$message = __('We can\'t change default billing address right now.');
$this->logger->critical($e);
}
}

$resultJson = $this->resultJsonFactory->create();
$resultJson->setData(
[
'message' => $message,
'error' => $error,
]
);

return $resultJson;
}

/**
* Set address as default billing address
*
* @param AddressInterface $address
* @return void
*/
private function setAddressAsDefault(AddressInterface $address): void
{
$address->setIsDefaultBilling(true);
}
}
Loading

0 comments on commit b8892f0

Please sign in to comment.