Store.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Customer\Attribute\Source;
  7. /**
  8. * Customer store attribute source
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Store extends \Magento\Eav\Model\Entity\Attribute\Source\Table
  13. {
  14. /**
  15. * @var \Magento\Store\Model\System\Store
  16. */
  17. protected $_store;
  18. /**
  19. * @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
  20. */
  21. protected $_storesFactory;
  22. /**
  23. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
  24. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
  25. * @param \Magento\Store\Model\System\Store $store
  26. * @param \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storesFactory
  27. */
  28. public function __construct(
  29. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
  30. \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
  31. \Magento\Store\Model\System\Store $store,
  32. \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storesFactory
  33. ) {
  34. parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
  35. $this->_store = $store;
  36. $this->_storesFactory = $storesFactory;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function getAllOptions($withEmpty = true, $defaultValues = false)
  42. {
  43. if (!$this->_options) {
  44. $collection = $this->_createStoresCollection();
  45. if ('store_id' == $this->getAttribute()->getAttributeCode()) {
  46. $collection->setWithoutDefaultFilter();
  47. }
  48. $this->_options = $this->_store->getStoreValuesForForm();
  49. if ('created_in' == $this->getAttribute()->getAttributeCode()) {
  50. array_unshift($this->_options, ['value' => '0', 'label' => __('Admin')]);
  51. }
  52. }
  53. return $this->_options;
  54. }
  55. /**
  56. * @param string $value
  57. * @return array|string
  58. */
  59. public function getOptionText($value)
  60. {
  61. if (!$value) {
  62. $value = '0';
  63. }
  64. $isMultiple = false;
  65. if (strpos($value, ',') !== false) {
  66. $isMultiple = true;
  67. $value = explode(',', $value);
  68. }
  69. if (!$this->_options) {
  70. $collection = $this->_createStoresCollection();
  71. if ('store_id' == $this->getAttribute()->getAttributeCode()) {
  72. $collection->setWithoutDefaultFilter();
  73. }
  74. $this->_options = $collection->load()->toOptionArray();
  75. if ('created_in' == $this->getAttribute()->getAttributeCode()) {
  76. array_unshift($this->_options, ['value' => '0', 'label' => __('Admin')]);
  77. }
  78. }
  79. if ($isMultiple) {
  80. $values = [];
  81. foreach ($value as $val) {
  82. $values[] = $this->_options[$val];
  83. }
  84. return $values;
  85. } else {
  86. return $this->_options[$value];
  87. }
  88. }
  89. /**
  90. * @return \Magento\Store\Model\ResourceModel\Store\Collection
  91. */
  92. protected function _createStoresCollection()
  93. {
  94. return $this->_storesFactory->create();
  95. }
  96. }