Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PersonalInfo.php
Go to the documentation of this file.
1 <?php
7 
13 
20 {
27 
33  protected $customer;
34 
40  protected $customerLog;
41 
47  protected $customerLogger;
48 
54  protected $customerRegistry;
55 
61  protected $accountManagement;
62 
68  protected $groupRepository;
69 
76 
82  protected $addressHelper;
83 
89  protected $dateTime;
90 
96  protected $coreRegistry;
97 
103  protected $addressMapper;
104 
110  protected $dataObjectHelper;
111 
126  public function __construct(
127  \Magento\Backend\Block\Template\Context $context,
130  \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
132  \Magento\Framework\Stdlib\DateTime $dateTime,
133  \Magento\Framework\Registry $registry,
135  \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
136  \Magento\Customer\Model\Logger $customerLogger,
137  array $data = []
138  ) {
139  $this->coreRegistry = $registry;
140  $this->accountManagement = $accountManagement;
141  $this->groupRepository = $groupRepository;
142  $this->customerDataFactory = $customerDataFactory;
143  $this->addressHelper = $addressHelper;
144  $this->dateTime = $dateTime;
145  $this->addressMapper = $addressMapper;
146  $this->dataObjectHelper = $dataObjectHelper;
147  $this->customerLogger = $customerLogger;
148 
149  parent::__construct($context, $data);
150  }
151 
160  {
161 
162  $this->customerRegistry = $customerRegistry;
163  }
164 
171  public function getCustomerRegistry()
172  {
173 
174  if (!($this->customerRegistry instanceof \Magento\Customer\Model\CustomerRegistry)) {
175  return \Magento\Framework\App\ObjectManager::getInstance()->get(
176  \Magento\Customer\Model\CustomerRegistry::class
177  );
178  } else {
180  }
181  }
182 
188  public function getCustomer()
189  {
190  if (!$this->customer) {
191  $this->customer = $this->customerDataFactory->create();
192  $data = $this->_backendSession->getCustomerData();
193  $this->dataObjectHelper->populateWithArray(
194  $this->customer,
195  $data['account'],
196  \Magento\Customer\Api\Data\CustomerInterface::class
197  );
198  }
199  return $this->customer;
200  }
201 
207  public function getCustomerId()
208  {
209  return $this->coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
210  }
211 
217  protected function getCustomerLog()
218  {
219  if (!$this->customerLog) {
220  $this->customerLog = $this->customerLogger->get(
221  $this->getCustomer()->getId()
222  );
223  }
224 
225  return $this->customerLog;
226  }
227 
233  public function getStoreCreateDate()
234  {
235  $createdAt = $this->getCustomer()->getCreatedAt();
236  try {
237  return $this->formatDate(
238  $createdAt,
239  \IntlDateFormatter::MEDIUM,
240  true,
242  );
243  } catch (\Exception $e) {
244  $this->_logger->critical($e);
245  return '';
246  }
247  }
248 
254  public function getStoreCreateDateTimezone()
255  {
256  return $this->_scopeConfig->getValue(
257  $this->_localeDate->getDefaultTimezonePath(),
259  $this->getCustomer()->getStoreId()
260  );
261  }
262 
268  public function getCreateDate()
269  {
270  return $this->formatDate(
271  $this->getCustomer()->getCreatedAt(),
272  \IntlDateFormatter::MEDIUM,
273  true
274  );
275  }
276 
282  public function getIsConfirmedStatus()
283  {
284  $id = $this->getCustomerId();
285  switch ($this->accountManagement->getConfirmationStatus($id)) {
287  return __('Confirmed');
289  return __('Confirmation Required');
291  return __('Confirmation Not Required');
292  }
293  return __('Indeterminate');
294  }
295 
301  public function getCreatedInStore()
302  {
303  return $this->_storeManager->getStore(
304  $this->getCustomer()->getStoreId()
305  )->getName();
306  }
307 
313  public function getBillingAddressHtml()
314  {
315  try {
316  $address = $this->accountManagement->getDefaultBillingAddress($this->getCustomer()->getId());
317  } catch (NoSuchEntityException $e) {
318  return __('The customer does not have default billing address.');
319  }
320 
321  if ($address === null) {
322  return __('The customer does not have default billing address.');
323  }
324 
325  return $this->addressHelper->getFormatTypeRenderer(
326  'html'
327  )->renderArray(
328  $this->addressMapper->toFlatArray($address)
329  );
330  }
331 
337  public function getGroupName()
338  {
339  $customer = $this->getCustomer();
340  if ($groupId = $customer->getId() ? $customer->getGroupId() : null) {
341  if ($group = $this->getGroup($groupId)) {
342  return $group->getCode();
343  }
344  }
345 
346  return null;
347  }
348 
355  private function getGroup($groupId)
356  {
357  try {
358  $group = $this->groupRepository->getById($groupId);
359  } catch (NoSuchEntityException $e) {
360  $group = null;
361  }
362  return $group;
363  }
364 
371  {
372  return $this->_scopeConfig->getValue(
373  $this->_localeDate->getDefaultTimezonePath(),
375  $this->getCustomer()->getStoreId()
376  );
377  }
378 
392  public function getCurrentStatus()
393  {
394  $lastLoginTime = $this->getCustomerLog()->getLastLoginAt();
395 
396  // Customer has never been logged in.
397  if (!$lastLoginTime) {
398  return __('Offline');
399  }
400 
401  $lastLogoutTime = $this->getCustomerLog()->getLastLogoutAt();
402 
403  // Customer clicked 'Log Out' link\button.
404  if ($lastLogoutTime && strtotime($lastLogoutTime) > strtotime($lastLoginTime)) {
405  return __('Offline');
406  }
407 
408  // Predefined interval has passed since customer's last activity.
409  $interval = $this->getOnlineMinutesInterval();
410  $currentTimestamp = (new \DateTime())->getTimestamp();
411  $lastVisitTime = $this->getCustomerLog()->getLastVisitAt();
412 
413  if ($lastVisitTime && $currentTimestamp - strtotime($lastVisitTime) > $interval * 60) {
414  return __('Offline');
415  }
416 
417  return __('Online');
418  }
419 
425  public function getLastLoginDate()
426  {
427  $date = $this->getCustomerLog()->getLastLoginAt();
428 
429  if ($date) {
430  return $this->formatDate($date, \IntlDateFormatter::MEDIUM, true);
431  }
432 
433  return __('Never');
434  }
435 
441  public function getStoreLastLoginDate()
442  {
443  $date = strtotime($this->getCustomerLog()->getLastLoginAt());
444 
445  if ($date) {
446  $date = $this->_localeDate->scopeDate($this->getCustomer()->getStoreId(), $date, true);
447  return $this->formatDate($date, \IntlDateFormatter::MEDIUM, true);
448  }
449 
450  return __('Never');
451  }
452 
458  protected function getOnlineMinutesInterval()
459  {
460  $configValue = $this->_scopeConfig->getValue(
461  'customer/online_customers/online_minutes_interval',
462  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
463  );
464  return intval($configValue) > 0 ? intval($configValue) : self::DEFAULT_ONLINE_MINUTES_INTERVAL;
465  }
466 
472  public function getAccountLock()
473  {
474  $customerModel = $this->getCustomerRegistry()->retrieve($this->getCustomerId());
475  $customerStatus = __('Unlocked');
476  if ($customerModel->isCustomerLocked()) {
477  $customerStatus = __('Locked');
478  }
479  return $customerStatus;
480  }
481 }
$id
Definition: fieldset.phtml:14
formatDate( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
setCustomerRegistry(\Magento\Customer\Model\CustomerRegistry $customerRegistry)
$address
Definition: customer.php:38
__construct(\Magento\Backend\Block\Template\Context $context, AccountManagementInterface $accountManagement, \Magento\Customer\Api\GroupRepositoryInterface $groupRepository, \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory, \Magento\Customer\Helper\Address $addressHelper, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Registry $registry, Mapper $addressMapper, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, \Magento\Customer\Model\Logger $customerLogger, array $data=[])