forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariable.php
151 lines (142 loc) · 4.46 KB
/
Variable.php
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
148
149
150
151
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Variable\Model\ResourceModel;
/**
* Custom variable resource model
*/
class Variable extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Constructor
*
* @return void
*/
protected function _construct()
{
$this->_init('variable', 'variable_id');
}
/**
* Load variable by code
*
* @param \Magento\Variable\Model\Variable $object
* @param string $code
* @return $this
*/
public function loadByCode(\Magento\Variable\Model\Variable $object, $code)
{
if ($result = $this->getVariableByCode($code, true, $object->getStoreId())) {
$object->setData($result);
}
return $this;
}
/**
* Retrieve variable data by code
*
* @param string $code
* @param bool $withValue
* @param integer $storeId
* @return array
*/
public function getVariableByCode($code, $withValue = false, $storeId = 0)
{
$select = $this->getConnection()->select()->from(
$this->getMainTable()
)->where(
$this->getMainTable() . '.code = ?',
$code
);
if ($withValue) {
$this->_addValueToSelect($select, $storeId);
}
return $this->getConnection()->fetchRow($select);
}
/**
* Perform actions after object save
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
{
parent::_afterSave($object);
if ($object->getUseDefaultValue()) {
/*
* remove store value
*/
$this->getConnection()->delete(
$this->getTable('variable_value'),
['variable_id = ?' => $object->getId(), 'store_id = ?' => $object->getStoreId()]
);
} else {
$data = [
'variable_id' => $object->getId(),
'store_id' => $object->getStoreId(),
'plain_value' => $object->getPlainValue(),
'html_value' => $object->getHtmlValue(),
];
$data = $this->_prepareDataForTable(
new \Magento\Framework\DataObject($data),
$this->getTable('variable_value')
);
$this->getConnection()->insertOnDuplicate(
$this->getTable('variable_value'),
$data,
['plain_value', 'html_value']
);
}
return $this;
}
/**
* Retrieve select object for load object data
*
* @param string $field
* @param mixed $value
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _getLoadSelect($field, $value, $object)
{
$select = parent::_getLoadSelect($field, $value, $object);
$this->_addValueToSelect($select, $object->getStoreId());
return $select;
}
/**
* Add variable store and default value to select
*
* @param \Magento\Framework\DB\Select $select
* @param integer $storeId
* @return \Magento\Variable\Model\ResourceModel\Variable
*/
protected function _addValueToSelect(
\Magento\Framework\DB\Select $select,
$storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID
) {
$connection = $this->getConnection();
$ifNullPlainValue = $connection->getCheckSql(
'store.plain_value IS NULL',
'def.plain_value',
'store.plain_value'
);
$ifNullHtmlValue = $connection->getCheckSql('store.html_value IS NULL', 'def.html_value', 'store.html_value');
$select->joinLeft(
['def' => $this->getTable('variable_value')],
'def.variable_id = ' . $this->getMainTable() . '.variable_id AND def.store_id = 0',
[]
)->joinLeft(
['store' => $this->getTable('variable_value')],
'store.variable_id = def.variable_id AND store.store_id = ' . $connection->quote($storeId),
[]
)->columns(
[
'plain_value' => $ifNullPlainValue,
'html_value' => $ifNullHtmlValue,
'store_plain_value' => 'store.plain_value',
'store_html_value' => 'store.html_value',
]
);
return $this;
}
}