Fax.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Widget;
  7. use Magento\Customer\Api\AddressMetadataInterface;
  8. use Magento\Customer\Api\CustomerMetadataInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Customer\Helper\Address as AddressHelper;
  11. use Magento\Customer\Model\Options;
  12. use Magento\Framework\View\Element\Template\Context;
  13. /**
  14. * Widget for showing customer company.
  15. *
  16. * @method CustomerInterface getObject()
  17. * @method Name setObject(CustomerInterface $customer)
  18. *
  19. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  20. */
  21. class Fax extends AbstractWidget
  22. {
  23. /**
  24. * the attribute code
  25. */
  26. const ATTRIBUTE_CODE = 'fax';
  27. /**
  28. * @var AddressMetadataInterface
  29. */
  30. protected $addressMetadata;
  31. /**
  32. * @var Options
  33. */
  34. protected $options;
  35. /**
  36. * @param Context $context
  37. * @param AddressHelper $addressHelper
  38. * @param CustomerMetadataInterface $customerMetadata
  39. * @param Options $options
  40. * @param AddressMetadataInterface $addressMetadata
  41. * @param array $data
  42. */
  43. public function __construct(
  44. Context $context,
  45. AddressHelper $addressHelper,
  46. CustomerMetadataInterface $customerMetadata,
  47. Options $options,
  48. AddressMetadataInterface $addressMetadata,
  49. array $data = []
  50. ) {
  51. $this->options = $options;
  52. parent::__construct($context, $addressHelper, $customerMetadata, $data);
  53. $this->addressMetadata = $addressMetadata;
  54. $this->_isScopePrivate = true;
  55. }
  56. /**
  57. * @return void
  58. */
  59. public function _construct()
  60. {
  61. parent::_construct();
  62. // default template location
  63. $this->setTemplate('Magento_Customer::widget/fax.phtml');
  64. }
  65. /**
  66. * Can show config value
  67. *
  68. * @param string $key
  69. *
  70. * @return bool
  71. */
  72. protected function _showConfig($key)
  73. {
  74. return (bool)$this->getConfig($key);
  75. }
  76. /**
  77. * Can show prefix
  78. *
  79. * @return bool
  80. */
  81. public function showFax()
  82. {
  83. return $this->_isAttributeVisible(self::ATTRIBUTE_CODE);
  84. }
  85. /**
  86. * @inheritdoc
  87. */
  88. protected function _getAttribute($attributeCode)
  89. {
  90. if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof CustomerInterface) {
  91. return parent::_getAttribute($attributeCode);
  92. }
  93. try {
  94. $attribute = $this->addressMetadata->getAttributeMetadata($attributeCode);
  95. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  96. return null;
  97. }
  98. if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) {
  99. $customerAttribute = parent::_getAttribute($attributeCode);
  100. if ($customerAttribute && $customerAttribute->isRequired()) {
  101. $attribute = $customerAttribute;
  102. }
  103. }
  104. return $attribute;
  105. }
  106. /**
  107. * Retrieve store attribute label
  108. *
  109. * @param string $attributeCode
  110. *
  111. * @return string
  112. */
  113. public function getStoreLabel($attributeCode)
  114. {
  115. $attribute = $this->_getAttribute($attributeCode);
  116. return $attribute ? __($attribute->getStoreLabel()) : '';
  117. }
  118. /**
  119. * Get string with frontend validation classes for attribute
  120. *
  121. * @param string $attributeCode
  122. *
  123. * @return string
  124. */
  125. public function getAttributeValidationClass($attributeCode)
  126. {
  127. return $this->_addressHelper->getAttributeValidationClass($attributeCode);
  128. }
  129. /**
  130. * @param string $attributeCode
  131. *
  132. * @return bool
  133. */
  134. private function _isAttributeVisible($attributeCode)
  135. {
  136. $attributeMetadata = $this->_getAttribute($attributeCode);
  137. return $attributeMetadata ? (bool)$attributeMetadata->isVisible() : false;
  138. }
  139. /**
  140. * Check if company attribute enabled in system
  141. *
  142. * @return bool
  143. */
  144. public function isEnabled()
  145. {
  146. return $this->_getAttribute(self::ATTRIBUTE_CODE) ? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)->isVisible(
  147. ) : false;
  148. }
  149. /**
  150. * Check if company attribute marked as required
  151. *
  152. * @return bool
  153. */
  154. public function isRequired()
  155. {
  156. return $this->_getAttribute(self::ATTRIBUTE_CODE) ? (bool)$this->_getAttribute(self::ATTRIBUTE_CODE)
  157. ->isRequired() : false;
  158. }
  159. }