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