Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Newsletter.php
Go to the documentation of this file.
1 <?php
7 
11 
15 class Newsletter extends \Magento\Backend\Block\Widget\Form\Generic implements TabInterface
16 {
20  protected $_template = 'Magento_Customer::tab/newsletter.phtml';
21 
26 
31 
37  protected $_coreRegistry = null;
38 
49  public function __construct(
50  \Magento\Backend\Block\Template\Context $context,
51  \Magento\Framework\Registry $registry,
52  \Magento\Framework\Data\FormFactory $formFactory,
53  \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
55  array $data = []
56  ) {
57  $this->_subscriberFactory = $subscriberFactory;
58  $this->customerAccountManagement = $customerAccountManagement;
59  parent::__construct($context, $registry, $formFactory, $data);
60  }
61 
67  public function getTabLabel()
68  {
69  return __('Newsletter');
70  }
71 
77  public function getTabTitle()
78  {
79  return __('Newsletter');
80  }
81 
87  public function getTabClass()
88  {
89  return '';
90  }
91 
97  public function getTabUrl()
98  {
99  return '';
100  }
101 
107  public function isAjaxLoaded()
108  {
109  return false;
110  }
111 
117  public function canShowTab()
118  {
119  return $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
120  }
121 
127  public function isHidden()
128  {
129  return false;
130  }
131 
138  public function initForm()
139  {
140  if (!$this->canShowTab()) {
141  return $this;
142  }
144  $form = $this->_formFactory->create();
145  $form->setHtmlIdPrefix('_newsletter');
146  $customerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
147  $subscriber = $this->_subscriberFactory->create()->loadByCustomerId($customerId);
148  $this->_coreRegistry->register('subscriber', $subscriber, true);
149 
150  $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Newsletter Information')]);
151 
152  $fieldset->addField(
153  'subscription',
154  'checkbox',
155  [
156  'label' => __('Subscribed to Newsletter'),
157  'name' => 'subscription',
158  'data-form-part' => $this->getData('target_form'),
159  'onchange' => 'this.value = this.checked;'
160  ]
161  );
162 
163  if ($this->customerAccountManagement->isReadonly($customerId)) {
164  $form->getElement('subscription')->setReadonly(true, true);
165  }
166  $isSubscribed = $subscriber->isSubscribed();
167  $form->setValues(['subscription' => $isSubscribed ? 'true' : 'false']);
168  $form->getElement('subscription')->setIsChecked($isSubscribed);
169 
170  $this->updateFromSession($form, $customerId);
171 
172  $changedDate = $this->getStatusChangedDate();
173  if ($changedDate) {
174  $fieldset->addField(
175  'change_status_date',
176  'label',
177  [
178  'label' => $isSubscribed ? __('Last Date Subscribed') : __('Last Date Unsubscribed'),
179  'value' => $changedDate,
180  'bold' => true
181  ]
182  );
183  }
184 
185  $this->setForm($form);
186  return $this;
187  }
188 
196  protected function updateFromSession(\Magento\Framework\Data\Form $form, $customerId)
197  {
198  $data = $this->_backendSession->getCustomerFormData();
199  if (!empty($data)) {
200  $dataCustomerId = isset($data['customer']['entity_id']) ? $data['customer']['entity_id'] : null;
201  if (isset($data['subscription']) && $dataCustomerId == $customerId) {
202  $form->getElement('subscription')->setIsChecked($data['subscription']);
203  }
204  }
205  }
206 
212  public function getStatusChangedDate()
213  {
214  $subscriber = $this->_coreRegistry->registry('subscriber');
215  if ($subscriber->getChangeStatusAt()) {
216  return $this->formatDate(
217  $subscriber->getChangeStatusAt(),
218  \IntlDateFormatter::MEDIUM,
219  true
220  );
221  }
222 
223  return null;
224  }
225 
229  protected function _toHtml()
230  {
231  if ($this->canShowTab()) {
232  $this->initForm();
233  return parent::_toHtml();
234  } else {
235  return '';
236  }
237  }
238 }
getData($key='', $index=null)
Definition: DataObject.php:119
updateFromSession(\Magento\Framework\Data\Form $form, $customerId)
Definition: Newsletter.php:196
setForm(\Magento\Framework\Data\Form $form)
Definition: Form.php:112
formatDate( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
__()
Definition: __.php:13
$subscriber
Definition: subscribers.php:20
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory, AccountManagementInterface $customerAccountManagement, array $data=[])
Definition: Newsletter.php:49