Fieldset.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel\Form;
  7. use Magento\Eav\Model\Form\Fieldset as FormFieldset;
  8. use Magento\Framework\DB\Select;
  9. use Magento\Framework\Model\AbstractModel;
  10. /**
  11. * Eav Form Fieldset Resource Model
  12. *
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. */
  15. class Fieldset extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  16. {
  17. /**
  18. * Initialize connection and define main table
  19. *
  20. * @return void
  21. */
  22. protected function _construct()
  23. {
  24. $this->_init('eav_form_fieldset', 'fieldset_id');
  25. $this->addUniqueField(
  26. ['field' => ['type_id', 'code'], 'title' => __('Form Fieldset with the same code')]
  27. );
  28. }
  29. /**
  30. * After save (save labels)
  31. *
  32. * @param FormFieldset|AbstractModel $object
  33. * @return $this
  34. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  35. * @SuppressWarnings(PHPMD.NPathComplexity)
  36. */
  37. protected function _afterSave(AbstractModel $object)
  38. {
  39. if ($object->hasLabels()) {
  40. $new = $object->getLabels();
  41. $old = $this->getLabels($object);
  42. $connection = $this->getConnection();
  43. $insert = array_diff(array_keys($new), array_keys($old));
  44. $delete = array_diff(array_keys($old), array_keys($new));
  45. $update = [];
  46. foreach ($new as $storeId => $label) {
  47. if (isset($old[$storeId]) && $old[$storeId] != $label) {
  48. $update[$storeId] = $label;
  49. } elseif (isset($old[$storeId]) && empty($label)) {
  50. $delete[] = $storeId;
  51. }
  52. }
  53. if (!empty($insert)) {
  54. $data = [];
  55. foreach ($insert as $storeId) {
  56. $label = $new[$storeId];
  57. if (empty($label)) {
  58. continue;
  59. }
  60. $data[] = [
  61. 'fieldset_id' => (int)$object->getId(),
  62. 'store_id' => (int)$storeId,
  63. 'label' => $label,
  64. ];
  65. }
  66. if ($data) {
  67. $connection->insertMultiple($this->getTable('eav_form_fieldset_label'), $data);
  68. }
  69. }
  70. if (!empty($delete)) {
  71. $where = ['fieldset_id = ?' => $object->getId(), 'store_id IN(?)' => $delete];
  72. $connection->delete($this->getTable('eav_form_fieldset_label'), $where);
  73. }
  74. if (!empty($update)) {
  75. foreach ($update as $storeId => $label) {
  76. $bind = ['label' => $label];
  77. $where = ['fieldset_id =?' => $object->getId(), 'store_id =?' => $storeId];
  78. $connection->update($this->getTable('eav_form_fieldset_label'), $bind, $where);
  79. }
  80. }
  81. }
  82. return parent::_afterSave($object);
  83. }
  84. /**
  85. * Retrieve fieldset labels for stores
  86. *
  87. * @param FormFieldset $object
  88. * @return array
  89. */
  90. public function getLabels($object)
  91. {
  92. $objectId = $object->getId();
  93. if (!$objectId) {
  94. return [];
  95. }
  96. $connection = $this->getConnection();
  97. $bind = [':fieldset_id' => $objectId];
  98. $select = $connection->select()->from(
  99. $this->getTable('eav_form_fieldset_label'),
  100. ['store_id', 'label']
  101. )->where(
  102. 'fieldset_id = :fieldset_id'
  103. );
  104. return $connection->fetchPairs($select, $bind);
  105. }
  106. /**
  107. * Retrieve select object for load object data
  108. *
  109. * @param string $field
  110. * @param mixed $value
  111. * @param FormFieldset $object
  112. * @return Select
  113. */
  114. protected function _getLoadSelect($field, $value, $object)
  115. {
  116. $select = parent::_getLoadSelect($field, $value, $object);
  117. $labelExpr = $select->getConnection()->getIfNullSql('store_label.label', 'default_label.label');
  118. $select->joinLeft(
  119. ['default_label' => $this->getTable('eav_form_fieldset_label')],
  120. $this->getMainTable() . '.fieldset_id = default_label.fieldset_id AND default_label.store_id=0',
  121. []
  122. )->joinLeft(
  123. ['store_label' => $this->getTable('eav_form_fieldset_label')],
  124. $this->getMainTable() .
  125. '.fieldset_id = store_label.fieldset_id AND default_label.store_id=' .
  126. (int)$object->getStoreId(),
  127. ['label' => $labelExpr]
  128. );
  129. return $select;
  130. }
  131. }