Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Fieldset.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Eav\Model\Form\Fieldset as FormFieldset;
11 
18 {
24  protected function _construct()
25  {
26  $this->_init('eav_form_fieldset', 'fieldset_id');
27  $this->addUniqueField(
28  ['field' => ['type_id', 'code'], 'title' => __('Form Fieldset with the same code')]
29  );
30  }
31 
40  protected function _afterSave(AbstractModel $object)
41  {
42  if ($object->hasLabels()) {
43  $new = $object->getLabels();
44  $old = $this->getLabels($object);
45 
46  $connection = $this->getConnection();
47 
48  $insert = array_diff(array_keys($new), array_keys($old));
49  $delete = array_diff(array_keys($old), array_keys($new));
50  $update = [];
51 
52  foreach ($new as $storeId => $label) {
53  if (isset($old[$storeId]) && $old[$storeId] != $label) {
54  $update[$storeId] = $label;
55  } elseif (isset($old[$storeId]) && empty($label)) {
56  $delete[] = $storeId;
57  }
58  }
59 
60  if (!empty($insert)) {
61  $data = [];
62  foreach ($insert as $storeId) {
63  $label = $new[$storeId];
64  if (empty($label)) {
65  continue;
66  }
67  $data[] = [
68  'fieldset_id' => (int)$object->getId(),
69  'store_id' => (int)$storeId,
70  'label' => $label,
71  ];
72  }
73  if ($data) {
74  $connection->insertMultiple($this->getTable('eav_form_fieldset_label'), $data);
75  }
76  }
77 
78  if (!empty($delete)) {
79  $where = ['fieldset_id = ?' => $object->getId(), 'store_id IN(?)' => $delete];
80  $connection->delete($this->getTable('eav_form_fieldset_label'), $where);
81  }
82 
83  if (!empty($update)) {
84  foreach ($update as $storeId => $label) {
85  $bind = ['label' => $label];
86  $where = ['fieldset_id =?' => $object->getId(), 'store_id =?' => $storeId];
87  $connection->update($this->getTable('eav_form_fieldset_label'), $bind, $where);
88  }
89  }
90  }
91 
92  return parent::_afterSave($object);
93  }
94 
101  public function getLabels($object)
102  {
103  $objectId = $object->getId();
104  if (!$objectId) {
105  return [];
106  }
107  $connection = $this->getConnection();
108  $bind = [':fieldset_id' => $objectId];
109  $select = $connection->select()->from(
110  $this->getTable('eav_form_fieldset_label'),
111  ['store_id', 'label']
112  )->where(
113  'fieldset_id = :fieldset_id'
114  );
115 
116  return $connection->fetchPairs($select, $bind);
117  }
118 
127  protected function _getLoadSelect($field, $value, $object)
128  {
129  $select = parent::_getLoadSelect($field, $value, $object);
130 
131  $labelExpr = $select->getConnection()->getIfNullSql('store_label.label', 'default_label.label');
132 
133  $select->joinLeft(
134  ['default_label' => $this->getTable('eav_form_fieldset_label')],
135  $this->getMainTable() . '.fieldset_id = default_label.fieldset_id AND default_label.store_id=0',
136  []
137  )->joinLeft(
138  ['store_label' => $this->getTable('eav_form_fieldset_label')],
139  $this->getMainTable() .
140  '.fieldset_id = store_label.fieldset_id AND default_label.store_id=' .
141  (int)$object->getStoreId(),
142  ['label' => $labelExpr]
143  );
144 
145  return $select;
146  }
147 }
$old
Definition: website.php:27
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$label
Definition: details.phtml:21
$value
Definition: gender.phtml:16
_getLoadSelect($field, $value, $object)
Definition: Fieldset.php:127
$connection
Definition: bulk.php:13