Value.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Adminhtml\Source\Rules;
  3. class Value
  4. {
  5. /**
  6. * @var \Magento\Eav\Model\ConfigFactory
  7. */
  8. private $configFactory;
  9. /**
  10. * @var \Magento\Config\Model\Config\Source\Yesno
  11. */
  12. private $yesno;
  13. /**
  14. * @var \Magento\Directory\Model\Config\Source\Country
  15. */
  16. private $country;
  17. /**
  18. * @var \Magento\Directory\Model\Config\Source\Allregion
  19. */
  20. private $allregion;
  21. /**
  22. * @var \Magento\Shipping\Model\Config\Source\Allmethods
  23. */
  24. private $allShippingMethods;
  25. /**
  26. * @var \Magento\Payment\Model\Config\Source\Allmethods
  27. */
  28. private $allPaymentMethods;
  29. /**
  30. * @var \Magento\Customer\Model\Config\Source\Group
  31. */
  32. private $sourceGroup;
  33. /**
  34. * Value constructor.
  35. *
  36. * @param \Magento\Eav\Model\ConfigFactory $configFactory
  37. * @param \Magento\Config\Model\Config\Source\Yesno $yesno
  38. * @param \Magento\Directory\Model\Config\Source\Country $country
  39. * @param \Magento\Directory\Model\Config\Source\Allregion $allregion
  40. * @param \Magento\Shipping\Model\Config\Source\Allmethods $allShippingMethods
  41. * @param \Magento\Payment\Model\Config\Source\Allmethods $allPaymentMethods
  42. * @param \Magento\Customer\Model\Config\Source\Group $group
  43. */
  44. public function __construct(
  45. \Magento\Eav\Model\ConfigFactory $configFactory,
  46. \Magento\Config\Model\Config\Source\Yesno $yesno,
  47. \Magento\Directory\Model\Config\Source\Country $country,
  48. \Magento\Directory\Model\Config\Source\Allregion $allregion,
  49. \Magento\Shipping\Model\Config\Source\Allmethods $allShippingMethods,
  50. \Magento\Payment\Model\Config\Source\Allmethods $allPaymentMethods,
  51. \Magento\Customer\Model\Config\Source\Group $group
  52. ) {
  53. $this->configFactory = $configFactory->create();
  54. $this->yesno = $yesno;
  55. $this->country = $country;
  56. $this->allregion = $allregion;
  57. $this->allShippingMethods = $allShippingMethods;
  58. $this->allPaymentMethods = $allPaymentMethods;
  59. $this->sourceGroup = $group;
  60. }
  61. /**
  62. * Get element type.
  63. *
  64. * @param string $attribute
  65. *
  66. * @return string
  67. */
  68. public function getValueElementType($attribute)
  69. {
  70. switch ($attribute) {
  71. case 'method':
  72. case 'shipping_method':
  73. case 'country_id':
  74. case 'region_id':
  75. case 'customer_group_id':
  76. return 'select';
  77. default:
  78. $attribute
  79. = $this->configFactory->getAttribute(
  80. 'catalog_product',
  81. $attribute
  82. );
  83. if ($attribute->usesSource()) {
  84. return 'select';
  85. }
  86. }
  87. return 'text';
  88. }
  89. /**
  90. * Get options array.
  91. *
  92. * @param string $attribute
  93. * @param bool $isEmpty
  94. *
  95. * @return array
  96. */
  97. public function getValueSelectOptions($attribute, $isEmpty = false)
  98. {
  99. $options = [];
  100. if ($isEmpty) {
  101. $options
  102. = $this->yesno->toOptionArray();
  103. return $options;
  104. }
  105. switch ($attribute) {
  106. case 'country_id':
  107. $options = $this->country->toOptionArray();
  108. break;
  109. case 'region_id':
  110. $options = $this->allregion->toOptionArray();
  111. break;
  112. case 'shipping_method':
  113. $options = $this->allShippingMethods->toOptionArray();
  114. break;
  115. case 'method':
  116. $options = $this->allPaymentMethods->toOptionArray();
  117. break;
  118. case 'customer_group_id':
  119. $options = $this->sourceGroup->toOptionArray();
  120. break;
  121. default:
  122. $attribute
  123. = $this->configFactory->getAttribute(
  124. 'catalog_product',
  125. $attribute
  126. );
  127. if ($attribute->usesSource()) {
  128. $options = $attribute->getSource()->getAllOptions();
  129. }
  130. }
  131. return $options;
  132. }
  133. /**
  134. * Options array.
  135. *
  136. * @return array
  137. */
  138. public function toOptionArray()
  139. {
  140. return $this->allPaymentMethods->toOptionArray();
  141. }
  142. }