Newsletter.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Adminhtml\Edit\Tab;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Controller\RegistryConstants;
  9. use Magento\Ui\Component\Layout\Tabs\TabInterface;
  10. /**
  11. * Customer account form block
  12. */
  13. class Newsletter extends \Magento\Backend\Block\Widget\Form\Generic implements TabInterface
  14. {
  15. /**
  16. * @var string
  17. */
  18. protected $_template = 'Magento_Customer::tab/newsletter.phtml';
  19. /**
  20. * @var \Magento\Newsletter\Model\SubscriberFactory
  21. */
  22. protected $_subscriberFactory;
  23. /**
  24. * @var AccountManagementInterface
  25. */
  26. protected $customerAccountManagement;
  27. /**
  28. * Core registry
  29. *
  30. * @var \Magento\Framework\Registry
  31. */
  32. protected $_coreRegistry = null;
  33. /**
  34. * Constructor
  35. *
  36. * @param \Magento\Backend\Block\Template\Context $context
  37. * @param \Magento\Framework\Registry $registry
  38. * @param \Magento\Framework\Data\FormFactory $formFactory
  39. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  40. * @param AccountManagementInterface $customerAccountManagement
  41. * @param array $data
  42. */
  43. public function __construct(
  44. \Magento\Backend\Block\Template\Context $context,
  45. \Magento\Framework\Registry $registry,
  46. \Magento\Framework\Data\FormFactory $formFactory,
  47. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  48. AccountManagementInterface $customerAccountManagement,
  49. array $data = []
  50. ) {
  51. $this->_subscriberFactory = $subscriberFactory;
  52. $this->customerAccountManagement = $customerAccountManagement;
  53. parent::__construct($context, $registry, $formFactory, $data);
  54. }
  55. /**
  56. * Return Tab label
  57. *
  58. * @return \Magento\Framework\Phrase
  59. */
  60. public function getTabLabel()
  61. {
  62. return __('Newsletter');
  63. }
  64. /**
  65. * Return Tab title
  66. *
  67. * @return \Magento\Framework\Phrase
  68. */
  69. public function getTabTitle()
  70. {
  71. return __('Newsletter');
  72. }
  73. /**
  74. * Tab class getter
  75. *
  76. * @return string
  77. */
  78. public function getTabClass()
  79. {
  80. return '';
  81. }
  82. /**
  83. * Return URL link to Tab content
  84. *
  85. * @return string
  86. */
  87. public function getTabUrl()
  88. {
  89. return '';
  90. }
  91. /**
  92. * Tab should be loaded trough Ajax call
  93. *
  94. * @return bool
  95. */
  96. public function isAjaxLoaded()
  97. {
  98. return false;
  99. }
  100. /**
  101. * Can show tab in tabs
  102. *
  103. * @return boolean
  104. */
  105. public function canShowTab()
  106. {
  107. return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
  108. }
  109. /**
  110. * Tab is hidden
  111. *
  112. * @return boolean
  113. */
  114. public function isHidden()
  115. {
  116. return false;
  117. }
  118. /**
  119. * Initialize the form.
  120. *
  121. * @return $this
  122. * @SuppressWarnings(PHPMD.NPathComplexity)
  123. */
  124. public function initForm()
  125. {
  126. if (!$this->canShowTab()) {
  127. return $this;
  128. }
  129. /**@var \Magento\Framework\Data\Form $form */
  130. $form = $this->_formFactory->create();
  131. $form->setHtmlIdPrefix('_newsletter');
  132. $customerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
  133. $subscriber = $this->_subscriberFactory->create()->loadByCustomerId($customerId);
  134. $this->_coreRegistry->register('subscriber', $subscriber, true);
  135. $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Newsletter Information')]);
  136. $fieldset->addField(
  137. 'subscription',
  138. 'checkbox',
  139. [
  140. 'label' => __('Subscribed to Newsletter'),
  141. 'name' => 'subscription',
  142. 'data-form-part' => $this->getData('target_form'),
  143. 'onchange' => 'this.value = this.checked;'
  144. ]
  145. );
  146. if ($this->customerAccountManagement->isReadonly($customerId)) {
  147. $form->getElement('subscription')->setReadonly(true, true);
  148. }
  149. $isSubscribed = $subscriber->isSubscribed();
  150. $form->setValues(['subscription' => $isSubscribed ? 'true' : 'false']);
  151. $form->getElement('subscription')->setIsChecked($isSubscribed);
  152. $this->updateFromSession($form, $customerId);
  153. $changedDate = $this->getStatusChangedDate();
  154. if ($changedDate) {
  155. $fieldset->addField(
  156. 'change_status_date',
  157. 'label',
  158. [
  159. 'label' => $isSubscribed ? __('Last Date Subscribed') : __('Last Date Unsubscribed'),
  160. 'value' => $changedDate,
  161. 'bold' => true
  162. ]
  163. );
  164. }
  165. $this->setForm($form);
  166. return $this;
  167. }
  168. /**
  169. * Update form elements from session data
  170. *
  171. * @param \Magento\Framework\Data\Form $form
  172. * @param int $customerId
  173. * @return void
  174. */
  175. protected function updateFromSession(\Magento\Framework\Data\Form $form, $customerId)
  176. {
  177. $data = $this->_backendSession->getCustomerFormData();
  178. if (!empty($data)) {
  179. $dataCustomerId = isset($data['customer']['entity_id']) ? $data['customer']['entity_id'] : null;
  180. if (isset($data['subscription']) && $dataCustomerId == $customerId) {
  181. $form->getElement('subscription')->setIsChecked($data['subscription']);
  182. }
  183. }
  184. }
  185. /**
  186. * Retrieve the date when the subscriber status changed.
  187. *
  188. * @return null|string
  189. */
  190. public function getStatusChangedDate()
  191. {
  192. $subscriber = $this->_coreRegistry->registry('subscriber');
  193. if ($subscriber->getChangeStatusAt()) {
  194. return $this->formatDate(
  195. $subscriber->getChangeStatusAt(),
  196. \IntlDateFormatter::MEDIUM,
  197. true
  198. );
  199. }
  200. return null;
  201. }
  202. /**
  203. * @return string
  204. */
  205. protected function _toHtml()
  206. {
  207. if ($this->canShowTab()) {
  208. $this->initForm();
  209. return parent::_toHtml();
  210. } else {
  211. return '';
  212. }
  213. }
  214. }