Data.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Contact\Helper;
  7. use Magento\Contact\Model\ConfigInterface;
  8. use Magento\Customer\Api\Data\CustomerInterface;
  9. use Magento\Customer\Helper\View as CustomerViewHelper;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Framework\App\Request\DataPersistorInterface;
  12. /**
  13. * Contact base helper
  14. *
  15. * @deprecated 100.2.0
  16. * @see \Magento\Contact\Model\ConfigInterface
  17. */
  18. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  19. {
  20. const XML_PATH_ENABLED = ConfigInterface::XML_PATH_ENABLED;
  21. /**
  22. * Customer session
  23. *
  24. * @var \Magento\Customer\Model\Session
  25. */
  26. protected $_customerSession;
  27. /**
  28. * @var \Magento\Customer\Helper\View
  29. */
  30. protected $_customerViewHelper;
  31. /**
  32. * @var DataPersistorInterface
  33. */
  34. private $dataPersistor;
  35. /**
  36. * @var array
  37. */
  38. private $postData = null;
  39. /**
  40. * @param \Magento\Framework\App\Helper\Context $context
  41. * @param \Magento\Customer\Model\Session $customerSession
  42. * @param CustomerViewHelper $customerViewHelper
  43. */
  44. public function __construct(
  45. \Magento\Framework\App\Helper\Context $context,
  46. \Magento\Customer\Model\Session $customerSession,
  47. CustomerViewHelper $customerViewHelper
  48. ) {
  49. $this->_customerSession = $customerSession;
  50. $this->_customerViewHelper = $customerViewHelper;
  51. parent::__construct($context);
  52. }
  53. /**
  54. * Check if enabled
  55. *
  56. * @return string|null
  57. * @deprecated 100.2.0 use \Magento\Contact\Api\ConfigInterface::isEnabled() instead
  58. */
  59. public function isEnabled()
  60. {
  61. return $this->scopeConfig->getValue(
  62. self::XML_PATH_ENABLED,
  63. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  64. );
  65. }
  66. /**
  67. * Get user name
  68. *
  69. * @return string
  70. */
  71. public function getUserName()
  72. {
  73. if (!$this->_customerSession->isLoggedIn()) {
  74. return '';
  75. }
  76. /**
  77. * @var \Magento\Customer\Api\Data\CustomerInterface $customer
  78. */
  79. $customer = $this->_customerSession->getCustomerDataObject();
  80. return trim($this->_customerViewHelper->getCustomerName($customer));
  81. }
  82. /**
  83. * Get user email
  84. *
  85. * @return string
  86. */
  87. public function getUserEmail()
  88. {
  89. if (!$this->_customerSession->isLoggedIn()) {
  90. return '';
  91. }
  92. /**
  93. * @var CustomerInterface $customer
  94. */
  95. $customer = $this->_customerSession->getCustomerDataObject();
  96. return $customer->getEmail();
  97. }
  98. /**
  99. * Get value from POST by key
  100. *
  101. * @param string $key
  102. * @return string
  103. */
  104. public function getPostValue($key)
  105. {
  106. if (null === $this->postData) {
  107. $this->postData = (array) $this->getDataPersistor()->get('contact_us');
  108. $this->getDataPersistor()->clear('contact_us');
  109. }
  110. if (isset($this->postData[$key])) {
  111. return (string) $this->postData[$key];
  112. }
  113. return '';
  114. }
  115. /**
  116. * Get Data Persistor
  117. *
  118. * @return DataPersistorInterface
  119. */
  120. private function getDataPersistor()
  121. {
  122. if ($this->dataPersistor === null) {
  123. $this->dataPersistor = ObjectManager::getInstance()
  124. ->get(DataPersistorInterface::class);
  125. }
  126. return $this->dataPersistor;
  127. }
  128. }