1: <?php
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: class Mage_Core_Model_Resource_Variable extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('core/variable', 'variable_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: public function loadByCode(Mage_Core_Model_Variable $object, $code)
54: {
55: if ($result = $this->getVariableByCode($code, true, $object->getStoreId())) {
56: $object->setData($result);
57: }
58: return $this;
59: }
60:
61: 62: 63: 64: 65: 66: 67: 68:
69: public function getVariableByCode($code, $withValue = false, $storeId = 0)
70: {
71: $select = $this->_getReadAdapter()->select()
72: ->from($this->getMainTable())
73: ->where($this->getMainTable() . '.code = ?', $code);
74: if ($withValue) {
75: $this->_addValueToSelect($select, $storeId);
76: }
77: return $this->_getReadAdapter()->fetchRow($select);
78: }
79:
80: 81: 82: 83: 84: 85:
86: protected function _afterSave(Mage_Core_Model_Abstract $object)
87: {
88: parent::_afterSave($object);
89: if ($object->getUseDefaultValue()) {
90: 91: 92:
93: $this->_getWriteAdapter()->delete(
94: $this->getTable('core/variable_value'), array(
95: 'variable_id = ?' => $object->getId(),
96: 'store_id = ?' => $object->getStoreId()
97: ));
98: } else {
99: $data = array(
100: 'variable_id' => $object->getId(),
101: 'store_id' => $object->getStoreId(),
102: 'plain_value' => $object->getPlainValue(),
103: 'html_value' => $object->getHtmlValue()
104: );
105: $data = $this->_prepareDataForTable(new Varien_Object($data), $this->getTable('core/variable_value'));
106: $this->_getWriteAdapter()->insertOnDuplicate(
107: $this->getTable('core/variable_value'),
108: $data,
109: array('plain_value', 'html_value')
110: );
111: }
112: return $this;
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122:
123: protected function _getLoadSelect($field, $value, $object)
124: {
125: $select = parent::_getLoadSelect($field, $value, $object);
126: $this->_addValueToSelect($select, $object->getStoreId());
127: return $select;
128: }
129:
130: 131: 132: 133: 134: 135: 136:
137: protected function _addValueToSelect(Zend_Db_Select $select, $storeId = Mage_Core_Model_App::ADMIN_STORE_ID)
138: {
139: $adapter = $this->_getReadAdapter();
140: $ifNullPlainValue = $adapter->getCheckSql('store.plain_value IS NULL', 'def.plain_value', 'store.plain_value');
141: $ifNullHtmlValue = $adapter->getCheckSql('store.html_value IS NULL', 'def.html_value', 'store.html_value');
142:
143: $select->joinLeft(
144: array('def' => $this->getTable('core/variable_value')),
145: 'def.variable_id = '.$this->getMainTable().'.variable_id AND def.store_id = 0',
146: array())
147: ->joinLeft(
148: array('store' => $this->getTable('core/variable_value')),
149: 'store.variable_id = def.variable_id AND store.store_id = ' . $adapter->quote($storeId),
150: array())
151: ->columns(array(
152: 'plain_value' => $ifNullPlainValue,
153: 'html_value' => $ifNullHtmlValue,
154: 'store_plain_value' => 'store.plain_value',
155: 'store_html_value' => 'store.html_value'
156: ));
157:
158: return $this;
159: }
160: }
161: