Fieldset.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Form;
  7. /**
  8. * Eav Form Fieldset Model
  9. *
  10. * @api
  11. * @method int getTypeId()
  12. * @method \Magento\Eav\Model\Form\Fieldset setTypeId(int $value)
  13. * @method string getCode()
  14. * @method \Magento\Eav\Model\Form\Fieldset setCode(string $value)
  15. * @method int getSortOrder()
  16. * @method \Magento\Eav\Model\Form\Fieldset setSortOrder(int $value)
  17. * @since 100.0.2
  18. */
  19. class Fieldset extends \Magento\Framework\Model\AbstractModel
  20. {
  21. /**
  22. * Prefix of model events names
  23. *
  24. * @var string
  25. */
  26. protected $_eventPrefix = 'eav_form_fieldset';
  27. /**
  28. * @var \Magento\Store\Model\StoreManagerInterface
  29. */
  30. protected $_storeManager;
  31. /**
  32. * @param \Magento\Framework\Model\Context $context
  33. * @param \Magento\Framework\Registry $registry
  34. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  35. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  36. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  37. * @param array $data
  38. * @codeCoverageIgnore
  39. */
  40. public function __construct(
  41. \Magento\Framework\Model\Context $context,
  42. \Magento\Framework\Registry $registry,
  43. \Magento\Store\Model\StoreManagerInterface $storeManager,
  44. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  45. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  46. array $data = []
  47. ) {
  48. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  49. $this->_storeManager = $storeManager;
  50. }
  51. /**
  52. * Initialize resource model
  53. *
  54. * @return void
  55. * @codeCoverageIgnore
  56. */
  57. protected function _construct()
  58. {
  59. $this->_init(\Magento\Eav\Model\ResourceModel\Form\Fieldset::class);
  60. }
  61. /**
  62. * Validate data before save data
  63. *
  64. * @throws \Magento\Framework\Exception\LocalizedException
  65. * @return $this
  66. */
  67. public function beforeSave()
  68. {
  69. if (!$this->getTypeId()) {
  70. throw new \Magento\Framework\Exception\LocalizedException(
  71. __('The form type is invalid. Reset the type and try again.')
  72. );
  73. }
  74. if (!$this->getStoreId() && $this->getLabel()) {
  75. $this->setStoreLabel($this->getStoreId(), $this->getLabel());
  76. }
  77. return parent::beforeSave();
  78. }
  79. /**
  80. * Retrieve fieldset labels for stores
  81. *
  82. * @return array
  83. */
  84. public function getLabels()
  85. {
  86. if (!$this->hasData('labels')) {
  87. $this->setData('labels', $this->_getResource()->getLabels($this));
  88. }
  89. return $this->_getData('labels');
  90. }
  91. /**
  92. * Set fieldset store labels
  93. * Input array where key - store_id and value = label
  94. *
  95. * @param array $labels
  96. * @return $this
  97. * @codeCoverageIgnore
  98. */
  99. public function setLabels(array $labels)
  100. {
  101. return $this->setData('labels', $labels);
  102. }
  103. /**
  104. * Set fieldset store label
  105. *
  106. * @param int $storeId
  107. * @param string $label
  108. * @return $this
  109. */
  110. public function setStoreLabel($storeId, $label)
  111. {
  112. $labels = $this->getLabels();
  113. $labels[$storeId] = $label;
  114. return $this->setLabels($labels);
  115. }
  116. /**
  117. * Retrieve label store scope
  118. *
  119. * @return int
  120. */
  121. public function getStoreId()
  122. {
  123. if (!$this->hasStoreId()) {
  124. $this->setData('store_id', $this->_storeManager->getStore()->getId());
  125. }
  126. return $this->_getData('store_id');
  127. }
  128. }