Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InlineEdit.php
Go to the documentation of this file.
1 <?php
7 
15 
20 {
26  const ADMIN_RESOURCE = 'Magento_Customer::manage';
27 
31  private $customer;
32 
37 
41  protected $resultJsonFactory;
42 
46  protected $customerMapper;
47 
51  protected $dataObjectHelper;
52 
56  protected $logger;
57 
61  private $emailNotification;
62 
71  public function __construct(
72  Action\Context $context,
74  \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
75  \Magento\Customer\Model\Customer\Mapper $customerMapper,
77  \Psr\Log\LoggerInterface $logger
78  ) {
79  $this->customerRepository = $customerRepository;
80  $this->resultJsonFactory = $resultJsonFactory;
81  $this->customerMapper = $customerMapper;
82  $this->dataObjectHelper = $dataObjectHelper;
83  $this->logger = $logger;
84  parent::__construct($context);
85  }
86 
93  private function getEmailNotification()
94  {
95  if (!($this->emailNotification instanceof EmailNotificationInterface)) {
96  return \Magento\Framework\App\ObjectManager::getInstance()->get(
97  EmailNotificationInterface::class
98  );
99  } else {
100  return $this->emailNotification;
101  }
102  }
103 
107  public function execute()
108  {
110  $resultJson = $this->resultJsonFactory->create();
111 
112  $postItems = $this->getRequest()->getParam('items', []);
113  if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
114  return $resultJson->setData([
115  'messages' => [__('Please correct the data sent.')],
116  'error' => true,
117  ]);
118  }
119 
120  foreach (array_keys($postItems) as $customerId) {
121  $this->setCustomer($this->customerRepository->getById($customerId));
122  $currentCustomer = clone $this->getCustomer();
123 
124  if ($this->getCustomer()->getDefaultBilling()) {
125  $this->updateDefaultBilling($this->getData($postItems[$customerId]));
126  }
127  $this->updateCustomer($this->getData($postItems[$customerId], true));
128  $this->saveCustomer($this->getCustomer());
129 
130  $this->getEmailNotification()->credentialsChanged($this->getCustomer(), $currentCustomer->getEmail());
131  }
132 
133  return $resultJson->setData([
134  'messages' => $this->getErrorMessages(),
135  'error' => $this->isErrorExists()
136  ]);
137  }
138 
146  protected function getData(array $data, $isCustomerData = null)
147  {
148  $addressKeys = preg_grep(
149  '/^(' . AttributeRepository::BILLING_ADDRESS_PREFIX . '\w+)/',
150  array_keys($data),
151  $isCustomerData
152  );
153  $result = array_intersect_key($data, array_flip($addressKeys));
154  if ($isCustomerData === null) {
155  foreach ($result as $key => $value) {
156  if (strpos($key, AttributeRepository::BILLING_ADDRESS_PREFIX) !== false) {
157  unset($result[$key]);
158  $result[str_replace(AttributeRepository::BILLING_ADDRESS_PREFIX, '', $key)] = $value;
159  }
160  }
161  }
162  return $result;
163  }
164 
171  protected function updateCustomer(array $data)
172  {
173  $customer = $this->getCustomer();
174  $customerData = array_merge(
175  $this->customerMapper->toFlatArray($customer),
176  $data
177  );
178  $this->dataObjectHelper->populateWithArray(
179  $customer,
181  \Magento\Customer\Api\Data\CustomerInterface::class
182  );
183  }
184 
191  protected function updateDefaultBilling(array $data)
192  {
193  $addresses = $this->getCustomer()->getAddresses();
195  foreach ($addresses as $address) {
196  if ($address->isDefaultBilling()) {
197  $this->dataObjectHelper->populateWithArray(
198  $address,
199  $this->processAddressData($data),
200  \Magento\Customer\Api\Data\AddressInterface::class
201  );
202  break;
203  }
204  }
205  }
206 
213  protected function saveCustomer(CustomerInterface $customer)
214  {
215  try {
216  $this->customerRepository->save($customer);
217  } catch (\Magento\Framework\Exception\InputException $e) {
218  $this->getMessageManager()->addError($this->getErrorWithCustomerId($e->getMessage()));
219  $this->logger->critical($e);
220  } catch (\Magento\Framework\Exception\LocalizedException $e) {
221  $this->getMessageManager()->addError($this->getErrorWithCustomerId($e->getMessage()));
222  $this->logger->critical($e);
223  } catch (\Exception $e) {
224  $this->getMessageManager()->addError($this->getErrorWithCustomerId('We can\'t save the customer.'));
225  $this->logger->critical($e);
226  }
227  }
228 
235  protected function processAddressData(array $data)
236  {
237  foreach (['firstname', 'lastname'] as $requiredField) {
238  if (empty($data[$requiredField])) {
239  $data[$requiredField] = $this->getCustomer()->{'get' . ucfirst($requiredField)}();
240  }
241  }
242  return $data;
243  }
244 
250  protected function getErrorMessages()
251  {
252  $messages = [];
253  foreach ($this->getMessageManager()->getMessages()->getItems() as $error) {
254  $messages[] = $error->getText();
255  }
256  return $messages;
257  }
258 
264  protected function isErrorExists()
265  {
266  return (bool)$this->getMessageManager()->getMessages(true)->getCount();
267  }
268 
276  {
277  $this->customer = $customer;
278  return $this;
279  }
280 
286  protected function getCustomer()
287  {
288  return $this->customer;
289  }
290 
297  protected function getErrorWithCustomerId($errorText)
298  {
299  return '[Customer ID: ' . $this->getCustomer()->getId() . '] ' . __($errorText);
300  }
301 }
$customerData
$customer
Definition: customers.php:11
$addresses
Definition: address_list.php:7
__()
Definition: __.php:13
__construct(Action\Context $context, CustomerRepositoryInterface $customerRepository, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, \Magento\Customer\Model\Customer\Mapper $customerMapper, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, \Psr\Log\LoggerInterface $logger)
Definition: InlineEdit.php:71
$address
Definition: customer.php:38
$value
Definition: gender.phtml:16
getData(array $data, $isCustomerData=null)
Definition: InlineEdit.php:146