Register.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Form;
  7. use Magento\Customer\Model\AccountManagement;
  8. use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
  9. /**
  10. * Customer register form block
  11. *
  12. * @api
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. * @since 100.0.2
  15. */
  16. class Register extends \Magento\Directory\Block\Data
  17. {
  18. /**
  19. * @var \Magento\Customer\Model\Session
  20. */
  21. protected $_customerSession;
  22. /**
  23. * @var \Magento\Framework\Module\Manager
  24. */
  25. protected $_moduleManager;
  26. /**
  27. * @var \Magento\Customer\Model\Url
  28. */
  29. protected $_customerUrl;
  30. /**
  31. * Constructor
  32. *
  33. * @param \Magento\Framework\View\Element\Template\Context $context
  34. * @param \Magento\Directory\Helper\Data $directoryHelper
  35. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  36. * @param \Magento\Framework\App\Cache\Type\Config $configCacheType
  37. * @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory
  38. * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
  39. * @param \Magento\Framework\Module\Manager $moduleManager
  40. * @param \Magento\Customer\Model\Session $customerSession
  41. * @param \Magento\Customer\Model\Url $customerUrl
  42. * @param array $data
  43. *
  44. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  45. */
  46. public function __construct(
  47. \Magento\Framework\View\Element\Template\Context $context,
  48. \Magento\Directory\Helper\Data $directoryHelper,
  49. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  50. \Magento\Framework\App\Cache\Type\Config $configCacheType,
  51. \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
  52. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
  53. \Magento\Framework\Module\Manager $moduleManager,
  54. \Magento\Customer\Model\Session $customerSession,
  55. \Magento\Customer\Model\Url $customerUrl,
  56. array $data = []
  57. ) {
  58. $this->_customerUrl = $customerUrl;
  59. $this->_moduleManager = $moduleManager;
  60. $this->_customerSession = $customerSession;
  61. parent::__construct(
  62. $context,
  63. $directoryHelper,
  64. $jsonEncoder,
  65. $configCacheType,
  66. $regionCollectionFactory,
  67. $countryCollectionFactory,
  68. $data
  69. );
  70. $this->_isScopePrivate = false;
  71. }
  72. /**
  73. * Get config
  74. *
  75. * @param string $path
  76. * @return string|null
  77. */
  78. public function getConfig($path)
  79. {
  80. return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
  81. }
  82. /**
  83. * Prepare layout
  84. *
  85. * @return $this
  86. */
  87. protected function _prepareLayout()
  88. {
  89. $this->pageConfig->getTitle()->set(__('Create New Customer Account'));
  90. return parent::_prepareLayout();
  91. }
  92. /**
  93. * Retrieve form posting url
  94. *
  95. * @return string
  96. */
  97. public function getPostActionUrl()
  98. {
  99. return $this->_customerUrl->getRegisterPostUrl();
  100. }
  101. /**
  102. * Retrieve back url
  103. *
  104. * @return string
  105. */
  106. public function getBackUrl()
  107. {
  108. $url = $this->getData('back_url');
  109. if ($url === null) {
  110. $url = $this->_customerUrl->getLoginUrl();
  111. }
  112. return $url;
  113. }
  114. /**
  115. * Retrieve form data
  116. *
  117. * @return mixed
  118. */
  119. public function getFormData()
  120. {
  121. $data = $this->getData('form_data');
  122. if ($data === null) {
  123. $formData = $this->_customerSession->getCustomerFormData(true);
  124. $data = new \Magento\Framework\DataObject();
  125. if ($formData) {
  126. $data->addData($formData);
  127. $data->setCustomerData(1);
  128. }
  129. if (isset($data['region_id'])) {
  130. $data['region_id'] = (int)$data['region_id'];
  131. }
  132. $this->setData('form_data', $data);
  133. }
  134. return $data;
  135. }
  136. /**
  137. * Retrieve customer country identifier
  138. *
  139. * @return int
  140. */
  141. public function getCountryId()
  142. {
  143. $countryId = $this->getFormData()->getCountryId();
  144. if ($countryId) {
  145. return $countryId;
  146. }
  147. return parent::getCountryId();
  148. }
  149. /**
  150. * Retrieve customer region identifier
  151. *
  152. * @return mixed
  153. */
  154. public function getRegion()
  155. {
  156. if (null !== ($region = $this->getFormData()->getRegion())) {
  157. return $region;
  158. } elseif (null !== ($region = $this->getFormData()->getRegionId())) {
  159. return $region;
  160. }
  161. return null;
  162. }
  163. /**
  164. * Newsletter module availability
  165. *
  166. * @return bool
  167. */
  168. public function isNewsletterEnabled()
  169. {
  170. return $this->_moduleManager->isOutputEnabled('Magento_Newsletter')
  171. && $this->getConfig(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE);
  172. }
  173. /**
  174. * Restore entity data from session
  175. *
  176. * Entity and form code must be defined for the form
  177. *
  178. * @param \Magento\Customer\Model\Metadata\Form $form
  179. * @param string|null $scope
  180. * @return $this
  181. */
  182. public function restoreSessionData(\Magento\Customer\Model\Metadata\Form $form, $scope = null)
  183. {
  184. if ($this->getFormData()->getCustomerData()) {
  185. $request = $form->prepareRequest($this->getFormData()->getData());
  186. $data = $form->extractData($request, $scope, false);
  187. $form->restoreData($data);
  188. }
  189. return $this;
  190. }
  191. /**
  192. * Get minimum password length
  193. *
  194. * @return string
  195. * @since 100.1.0
  196. */
  197. public function getMinimumPasswordLength()
  198. {
  199. return $this->_scopeConfig->getValue(AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH);
  200. }
  201. /**
  202. * Get number of password required character classes
  203. *
  204. * @return string
  205. * @since 100.1.0
  206. */
  207. public function getRequiredCharacterClassesNumber()
  208. {
  209. return $this->_scopeConfig->getValue(AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER);
  210. }
  211. }