PersonalInfo.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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\View;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Controller\RegistryConstants;
  9. use Magento\Customer\Model\Address\Mapper;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Customer\Model\Customer;
  12. /**
  13. * Adminhtml customer view personal information sales block.
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class PersonalInfo extends \Magento\Backend\Block\Template
  18. {
  19. /**
  20. * Interval in minutes that shows how long customer will be marked 'Online'
  21. * since his last activity. Used only if it's impossible to get such setting
  22. * from configuration.
  23. */
  24. const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;
  25. /**
  26. * Customer
  27. *
  28. * @var \Magento\Customer\Api\Data\CustomerInterface
  29. */
  30. protected $customer;
  31. /**
  32. * Customer log
  33. *
  34. * @var \Magento\Customer\Model\Log
  35. */
  36. protected $customerLog;
  37. /**
  38. * Customer logger
  39. *
  40. * @var \Magento\Customer\Model\Logger
  41. */
  42. protected $customerLogger;
  43. /**
  44. * Customer registry
  45. *
  46. * @var \Magento\Customer\Model\CustomerRegistry
  47. */
  48. protected $customerRegistry;
  49. /**
  50. * Account management
  51. *
  52. * @var AccountManagementInterface
  53. */
  54. protected $accountManagement;
  55. /**
  56. * Customer group repository
  57. *
  58. * @var \Magento\Customer\Api\GroupRepositoryInterface
  59. */
  60. protected $groupRepository;
  61. /**
  62. * Customer data factory
  63. *
  64. * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
  65. */
  66. protected $customerDataFactory;
  67. /**
  68. * Address helper
  69. *
  70. * @var \Magento\Customer\Helper\Address
  71. */
  72. protected $addressHelper;
  73. /**
  74. * Date time
  75. *
  76. * @var \Magento\Framework\Stdlib\DateTime
  77. */
  78. protected $dateTime;
  79. /**
  80. * Core registry
  81. *
  82. * @var \Magento\Framework\Registry
  83. */
  84. protected $coreRegistry;
  85. /**
  86. * Address mapper
  87. *
  88. * @var Mapper
  89. */
  90. protected $addressMapper;
  91. /**
  92. * Data object helper
  93. *
  94. * @var \Magento\Framework\Api\DataObjectHelper
  95. */
  96. protected $dataObjectHelper;
  97. /**
  98. * @param \Magento\Backend\Block\Template\Context $context
  99. * @param AccountManagementInterface $accountManagement
  100. * @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
  101. * @param \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory
  102. * @param \Magento\Customer\Helper\Address $addressHelper
  103. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  104. * @param \Magento\Framework\Registry $registry
  105. * @param Mapper $addressMapper
  106. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  107. * @param \Magento\Customer\Model\Logger $customerLogger
  108. * @param array $data
  109. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  110. */
  111. public function __construct(
  112. \Magento\Backend\Block\Template\Context $context,
  113. AccountManagementInterface $accountManagement,
  114. \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
  115. \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerDataFactory,
  116. \Magento\Customer\Helper\Address $addressHelper,
  117. \Magento\Framework\Stdlib\DateTime $dateTime,
  118. \Magento\Framework\Registry $registry,
  119. Mapper $addressMapper,
  120. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  121. \Magento\Customer\Model\Logger $customerLogger,
  122. array $data = []
  123. ) {
  124. $this->coreRegistry = $registry;
  125. $this->accountManagement = $accountManagement;
  126. $this->groupRepository = $groupRepository;
  127. $this->customerDataFactory = $customerDataFactory;
  128. $this->addressHelper = $addressHelper;
  129. $this->dateTime = $dateTime;
  130. $this->addressMapper = $addressMapper;
  131. $this->dataObjectHelper = $dataObjectHelper;
  132. $this->customerLogger = $customerLogger;
  133. parent::__construct($context, $data);
  134. }
  135. /**
  136. * Set customer registry
  137. *
  138. * @param \Magento\Framework\Registry $customerRegistry
  139. * @return void
  140. * @deprecated 100.1.0
  141. */
  142. public function setCustomerRegistry(\Magento\Customer\Model\CustomerRegistry $customerRegistry)
  143. {
  144. $this->customerRegistry = $customerRegistry;
  145. }
  146. /**
  147. * Get customer registry
  148. *
  149. * @return \Magento\Customer\Model\CustomerRegistry
  150. * @deprecated 100.1.0
  151. */
  152. public function getCustomerRegistry()
  153. {
  154. if (!($this->customerRegistry instanceof \Magento\Customer\Model\CustomerRegistry)) {
  155. return \Magento\Framework\App\ObjectManager::getInstance()->get(
  156. \Magento\Customer\Model\CustomerRegistry::class
  157. );
  158. } else {
  159. return $this->customerRegistry;
  160. }
  161. }
  162. /**
  163. * Retrieve customer object
  164. *
  165. * @return \Magento\Customer\Api\Data\CustomerInterface
  166. */
  167. public function getCustomer()
  168. {
  169. if (!$this->customer) {
  170. $this->customer = $this->customerDataFactory->create();
  171. $data = $this->_backendSession->getCustomerData();
  172. $this->dataObjectHelper->populateWithArray(
  173. $this->customer,
  174. $data['account'],
  175. \Magento\Customer\Api\Data\CustomerInterface::class
  176. );
  177. }
  178. return $this->customer;
  179. }
  180. /**
  181. * Retrieve customer id
  182. *
  183. * @return string|null
  184. */
  185. public function getCustomerId()
  186. {
  187. return $this->coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
  188. }
  189. /**
  190. * Retrieves customer log model
  191. *
  192. * @return \Magento\Customer\Model\Log
  193. */
  194. protected function getCustomerLog()
  195. {
  196. if (!$this->customerLog) {
  197. $this->customerLog = $this->customerLogger->get(
  198. $this->getCustomer()->getId()
  199. );
  200. }
  201. return $this->customerLog;
  202. }
  203. /**
  204. * Returns customer's created date in the assigned store
  205. *
  206. * @return string
  207. */
  208. public function getStoreCreateDate()
  209. {
  210. $createdAt = $this->getCustomer()->getCreatedAt();
  211. try {
  212. return $this->formatDate(
  213. $createdAt,
  214. \IntlDateFormatter::MEDIUM,
  215. true,
  216. $this->getStoreCreateDateTimezone()
  217. );
  218. } catch (\Exception $e) {
  219. $this->_logger->critical($e);
  220. return '';
  221. }
  222. }
  223. /**
  224. * Retrieve store default timezone from configuration
  225. *
  226. * @return string
  227. */
  228. public function getStoreCreateDateTimezone()
  229. {
  230. return $this->_scopeConfig->getValue(
  231. $this->_localeDate->getDefaultTimezonePath(),
  232. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  233. $this->getCustomer()->getStoreId()
  234. );
  235. }
  236. /**
  237. * Get customer creation date
  238. *
  239. * @return string
  240. */
  241. public function getCreateDate()
  242. {
  243. return $this->formatDate(
  244. $this->getCustomer()->getCreatedAt(),
  245. \IntlDateFormatter::MEDIUM,
  246. true
  247. );
  248. }
  249. /**
  250. * Check if account is confirmed
  251. *
  252. * @return \Magento\Framework\Phrase
  253. */
  254. public function getIsConfirmedStatus()
  255. {
  256. $id = $this->getCustomerId();
  257. switch ($this->accountManagement->getConfirmationStatus($id)) {
  258. case AccountManagementInterface::ACCOUNT_CONFIRMED:
  259. return __('Confirmed');
  260. case AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED:
  261. return __('Confirmation Required');
  262. case AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED:
  263. return __('Confirmation Not Required');
  264. }
  265. return __('Indeterminate');
  266. }
  267. /**
  268. * Retrieve store
  269. *
  270. * @return null|string
  271. */
  272. public function getCreatedInStore()
  273. {
  274. return $this->_storeManager->getStore(
  275. $this->getCustomer()->getStoreId()
  276. )->getName();
  277. }
  278. /**
  279. * Retrieve billing address html
  280. *
  281. * @return \Magento\Framework\Phrase|string
  282. */
  283. public function getBillingAddressHtml()
  284. {
  285. try {
  286. $address = $this->accountManagement->getDefaultBillingAddress($this->getCustomer()->getId());
  287. } catch (NoSuchEntityException $e) {
  288. return __('The customer does not have default billing address.');
  289. }
  290. if ($address === null) {
  291. return __('The customer does not have default billing address.');
  292. }
  293. return $this->addressHelper->getFormatTypeRenderer(
  294. 'html'
  295. )->renderArray(
  296. $this->addressMapper->toFlatArray($address)
  297. );
  298. }
  299. /**
  300. * Retrieve group name
  301. *
  302. * @return string|null
  303. */
  304. public function getGroupName()
  305. {
  306. $customer = $this->getCustomer();
  307. if ($groupId = $customer->getId() ? $customer->getGroupId() : null) {
  308. if ($group = $this->getGroup($groupId)) {
  309. return $group->getCode();
  310. }
  311. }
  312. return null;
  313. }
  314. /**
  315. * Retrieve customer group by id
  316. *
  317. * @param int $groupId
  318. * @return \Magento\Customer\Api\Data\GroupInterface|null
  319. */
  320. private function getGroup($groupId)
  321. {
  322. try {
  323. $group = $this->groupRepository->getById($groupId);
  324. } catch (NoSuchEntityException $e) {
  325. $group = null;
  326. }
  327. return $group;
  328. }
  329. /**
  330. * Returns timezone of the store to which customer assigned.
  331. *
  332. * @return string
  333. */
  334. public function getStoreLastLoginDateTimezone()
  335. {
  336. return $this->_scopeConfig->getValue(
  337. $this->_localeDate->getDefaultTimezonePath(),
  338. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  339. $this->getCustomer()->getStoreId()
  340. );
  341. }
  342. /**
  343. * Get customer's current status.
  344. *
  345. * Customer considered 'Offline' in the next cases:
  346. *
  347. * - customer has never been logged in;
  348. * - customer clicked 'Log Out' link\button;
  349. * - predefined interval has passed since customer's last activity.
  350. *
  351. * In all other cases customer considered 'Online'.
  352. *
  353. * @return \Magento\Framework\Phrase
  354. */
  355. public function getCurrentStatus()
  356. {
  357. $lastLoginTime = $this->getCustomerLog()->getLastLoginAt();
  358. // Customer has never been logged in.
  359. if (!$lastLoginTime) {
  360. return __('Offline');
  361. }
  362. $lastLogoutTime = $this->getCustomerLog()->getLastLogoutAt();
  363. // Customer clicked 'Log Out' link\button.
  364. if ($lastLogoutTime && strtotime($lastLogoutTime) > strtotime($lastLoginTime)) {
  365. return __('Offline');
  366. }
  367. // Predefined interval has passed since customer's last activity.
  368. $interval = $this->getOnlineMinutesInterval();
  369. $currentTimestamp = (new \DateTime())->getTimestamp();
  370. $lastVisitTime = $this->getCustomerLog()->getLastVisitAt();
  371. if ($lastVisitTime && $currentTimestamp - strtotime($lastVisitTime) > $interval * 60) {
  372. return __('Offline');
  373. }
  374. return __('Online');
  375. }
  376. /**
  377. * Get customer last login date.
  378. *
  379. * @return \Magento\Framework\Phrase|string
  380. */
  381. public function getLastLoginDate()
  382. {
  383. $date = $this->getCustomerLog()->getLastLoginAt();
  384. if ($date) {
  385. return $this->formatDate($date, \IntlDateFormatter::MEDIUM, true);
  386. }
  387. return __('Never');
  388. }
  389. /**
  390. * Returns customer last login date in store's timezone.
  391. *
  392. * @return \Magento\Framework\Phrase|string
  393. */
  394. public function getStoreLastLoginDate()
  395. {
  396. $date = strtotime($this->getCustomerLog()->getLastLoginAt());
  397. if ($date) {
  398. $date = $this->_localeDate->scopeDate($this->getCustomer()->getStoreId(), $date, true);
  399. return $this->formatDate($date, \IntlDateFormatter::MEDIUM, true);
  400. }
  401. return __('Never');
  402. }
  403. /**
  404. * Returns interval that shows how long customer will be considered 'Online'.
  405. *
  406. * @return int Interval in minutes
  407. */
  408. protected function getOnlineMinutesInterval()
  409. {
  410. $configValue = $this->_scopeConfig->getValue(
  411. 'customer/online_customers/online_minutes_interval',
  412. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  413. );
  414. return (int)$configValue > 0 ? (int)$configValue : self::DEFAULT_ONLINE_MINUTES_INTERVAL;
  415. }
  416. /**
  417. * Get customer account lock status
  418. *
  419. * @return \Magento\Framework\Phrase
  420. */
  421. public function getAccountLock()
  422. {
  423. $customerModel = $this->getCustomerRegistry()->retrieve($this->getCustomerId());
  424. $customerStatus = __('Unlocked');
  425. if ($customerModel->isCustomerLocked()) {
  426. $customerStatus = __('Locked');
  427. }
  428. return $customerStatus;
  429. }
  430. }