Edit.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Adminhtml;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Controller\RegistryConstants;
  10. /**
  11. * @deprecated 100.2.0 for UiComponent replacement
  12. * @see app/code/Magento/Customer/view/base/ui_component/customer_form.xml
  13. */
  14. class Edit extends \Magento\Backend\Block\Widget\Form\Container
  15. {
  16. /**
  17. * Core registry
  18. *
  19. * @var \Magento\Framework\Registry
  20. */
  21. protected $_coreRegistry = null;
  22. /**
  23. * @var AccountManagementInterface
  24. */
  25. protected $customerAccountManagement;
  26. /**
  27. * @var CustomerRepositoryInterface
  28. */
  29. protected $customerRepository;
  30. /**
  31. * Customer view helper
  32. *
  33. * @var \Magento\Customer\Helper\View
  34. */
  35. protected $_viewHelper;
  36. /**
  37. * Constructor
  38. *
  39. * @param \Magento\Backend\Block\Widget\Context $context
  40. * @param \Magento\Framework\Registry $registry
  41. * @param AccountManagementInterface $customerAccountManagement
  42. * @param CustomerRepositoryInterface $customerRepository
  43. * @param \Magento\Customer\Helper\View $viewHelper
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Backend\Block\Widget\Context $context,
  48. \Magento\Framework\Registry $registry,
  49. AccountManagementInterface $customerAccountManagement,
  50. CustomerRepositoryInterface $customerRepository,
  51. \Magento\Customer\Helper\View $viewHelper,
  52. array $data = []
  53. ) {
  54. $this->_coreRegistry = $registry;
  55. $this->customerAccountManagement = $customerAccountManagement;
  56. $this->customerRepository = $customerRepository;
  57. $this->_viewHelper = $viewHelper;
  58. parent::__construct($context, $data);
  59. }
  60. /**
  61. * @return void
  62. */
  63. protected function _construct()
  64. {
  65. $this->_objectId = 'id';
  66. $this->_controller = 'adminhtml';
  67. $this->_blockGroup = 'Magento_Customer';
  68. $customerId = $this->getCustomerId();
  69. if ($customerId && $this->_authorization->isAllowed('Magento_Sales::create')) {
  70. $this->buttonList->add(
  71. 'order',
  72. [
  73. 'label' => __('Create Order'),
  74. 'onclick' => 'setLocation(\'' . $this->getCreateOrderUrl() . '\')',
  75. 'class' => 'add'
  76. ],
  77. 0
  78. );
  79. }
  80. parent::_construct();
  81. $this->buttonList->update('save', 'label', __('Save Customer'));
  82. $this->buttonList->update('delete', 'label', __('Delete Customer'));
  83. if ($customerId && $this->customerAccountManagement->isReadonly($customerId)) {
  84. $this->buttonList->remove('save');
  85. $this->buttonList->remove('reset');
  86. }
  87. if (!$customerId || $this->customerAccountManagement->isReadonly($customerId)) {
  88. $this->buttonList->remove('delete');
  89. }
  90. if ($customerId) {
  91. $url = $this->getUrl('customer/index/resetPassword', ['customer_id' => $customerId]);
  92. $this->buttonList->add(
  93. 'reset_password',
  94. [
  95. 'label' => __('Reset Password'),
  96. 'onclick' => 'setLocation(\'' . $url . '\')',
  97. 'class' => 'reset reset-password'
  98. ],
  99. 0
  100. );
  101. }
  102. if ($customerId) {
  103. $url = $this->getUrl('customer/customer/invalidateToken', ['customer_id' => $customerId]);
  104. $deleteConfirmMsg = __("Are you sure you want to revoke the customer's tokens?");
  105. $this->buttonList->add(
  106. 'invalidate_token',
  107. [
  108. 'label' => __('Force Sign-In'),
  109. 'onclick' => 'deleteConfirm(\'' . $this->escapeJs($this->escapeHtml($deleteConfirmMsg)) .
  110. '\', \'' . $url . '\')',
  111. 'class' => 'invalidate-token'
  112. ],
  113. 10
  114. );
  115. }
  116. }
  117. /**
  118. * Retrieve the Url for creating an order.
  119. *
  120. * @return string
  121. */
  122. public function getCreateOrderUrl()
  123. {
  124. return $this->getUrl('sales/order_create/start', ['customer_id' => $this->getCustomerId()]);
  125. }
  126. /**
  127. * Return the customer Id.
  128. *
  129. * @return int|null
  130. */
  131. public function getCustomerId()
  132. {
  133. $customerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
  134. return $customerId;
  135. }
  136. /**
  137. * Retrieve the header text, either the name of an existing customer or 'New Customer'.
  138. *
  139. * @return \Magento\Framework\Phrase|string
  140. */
  141. public function getHeaderText()
  142. {
  143. $customerId = $this->getCustomerId();
  144. if ($customerId) {
  145. $customerData = $this->customerRepository->getById($customerId);
  146. return $this->escapeHtml($this->_viewHelper->getCustomerName($customerData));
  147. } else {
  148. return __('New Customer');
  149. }
  150. }
  151. /**
  152. * Prepare form Html. Add block for configurable product modification interface.
  153. *
  154. * @return string
  155. */
  156. public function getFormHtml()
  157. {
  158. $html = parent::getFormHtml();
  159. $html .= $this->getLayout()->createBlock(
  160. \Magento\Catalog\Block\Adminhtml\Product\Composite\Configure::class
  161. )->toHtml();
  162. return $html;
  163. }
  164. /**
  165. * Retrieve customer validation Url.
  166. *
  167. * @return string
  168. */
  169. public function getValidationUrl()
  170. {
  171. return $this->getUrl('customer/*/validate', ['_current' => true]);
  172. }
  173. /**
  174. * Prepare the layout.
  175. *
  176. * @return $this
  177. */
  178. protected function _prepareLayout()
  179. {
  180. $customerId = $this->getCustomerId();
  181. if (!$customerId || !$this->customerAccountManagement->isReadonly($customerId)) {
  182. $this->buttonList->add(
  183. 'save_and_continue',
  184. [
  185. 'label' => __('Save and Continue Edit'),
  186. 'class' => 'save',
  187. 'data_attribute' => [
  188. 'mage-init' => [
  189. 'button' => ['event' => 'saveAndContinueEdit', 'target' => '#edit_form'],
  190. ],
  191. ]
  192. ],
  193. 10
  194. );
  195. }
  196. return parent::_prepareLayout();
  197. }
  198. /**
  199. * Retrieve the save and continue edit Url.
  200. *
  201. * @return string
  202. */
  203. protected function _getSaveAndContinueUrl()
  204. {
  205. return $this->getUrl(
  206. 'customer/index/save',
  207. ['_current' => true, 'back' => 'edit', 'tab' => '{{tab_id}}']
  208. );
  209. }
  210. }