1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Abstract resource model
29: *
30: * @category Mage
31: * @package Mage_Core
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_Core_Model_Resource_Abstract
35: {
36: /**
37: * Main constructor
38: */
39: public function __construct()
40: {
41: /**
42: * Please override this one instead of overriding real __construct constructor
43: */
44: $this->_construct();
45: }
46:
47: /**
48: * Array of callbacks subscribed to commit transaction commit
49: *
50: * @var array
51: */
52: static protected $_commitCallbacks = array();
53:
54: /**
55: * Resource initialization
56: */
57: abstract protected function _construct();
58:
59: /**
60: * Retrieve connection for read data
61: */
62: abstract protected function _getReadAdapter();
63:
64: /**
65: * Retrieve connection for write data
66: */
67: abstract protected function _getWriteAdapter();
68:
69: /**
70: * Start resource transaction
71: *
72: * @return Mage_Core_Model_Resource_Abstract
73: */
74: public function beginTransaction()
75: {
76: $this->_getWriteAdapter()->beginTransaction();
77: return $this;
78: }
79:
80: /**
81: * Subscribe some callback to transaction commit
82: *
83: * @param callback $callback
84: * @return Mage_Core_Model_Resource_Abstract
85: */
86: public function addCommitCallback($callback)
87: {
88: $adapterKey = spl_object_hash($this->_getWriteAdapter());
89: self::$_commitCallbacks[$adapterKey][] = $callback;
90: return $this;
91: }
92:
93: /**
94: * Commit resource transaction
95: *
96: * @return Mage_Core_Model_Resource_Abstract
97: */
98: public function commit()
99: {
100: $this->_getWriteAdapter()->commit();
101: /**
102: * Process after commit callbacks
103: */
104: if ($this->_getWriteAdapter()->getTransactionLevel() === 0) {
105: $adapterKey = spl_object_hash($this->_getWriteAdapter());
106: if (isset(self::$_commitCallbacks[$adapterKey])) {
107: $callbacks = self::$_commitCallbacks[$adapterKey];
108: self::$_commitCallbacks[$adapterKey] = array();
109: foreach ($callbacks as $index => $callback) {
110: call_user_func($callback);
111: }
112: }
113: }
114: return $this;
115: }
116:
117: /**
118: * Roll back resource transaction
119: *
120: * @return Mage_Core_Model_Resource_Abstract
121: */
122: public function rollBack()
123: {
124: $this->_getWriteAdapter()->rollBack();
125: return $this;
126: }
127:
128: /**
129: * Format date to internal format
130: *
131: * @param string|Zend_Date $date
132: * @param bool $includeTime
133: * @return string
134: */
135: public function formatDate($date, $includeTime=true)
136: {
137: return Varien_Date::formatDate($date, $includeTime);
138: }
139:
140: /**
141: * Convert internal date to UNIX timestamp
142: *
143: * @param string $str
144: * @return int
145: */
146: public function mktime($str)
147: {
148: return Varien_Date::toTimestamp($str);
149: }
150:
151: /**
152: * Serialize specified field in an object
153: *
154: * @param Varien_Object $object
155: * @param string $field
156: * @param mixed $defaultValue
157: * @param bool $unsetEmpty
158: * @return Mage_Core_Model_Resource_Abstract
159: */
160: protected function _serializeField(Varien_Object $object, $field, $defaultValue = null, $unsetEmpty = false)
161: {
162: $value = $object->getData($field);
163: if (empty($value)) {
164: if ($unsetEmpty) {
165: $object->unsetData($field);
166: } else {
167: if (is_object($defaultValue) || is_array($defaultValue)) {
168: $defaultValue = serialize($defaultValue);
169: }
170: $object->setData($field, $defaultValue);
171: }
172: } elseif (is_array($value) || is_object($value)) {
173: $object->setData($field, serialize($value));
174: }
175:
176: return $this;
177: }
178:
179: /**
180: * Unserialize Varien_Object field in an object
181: *
182: * @param Mage_Core_Model_Abstract $object
183: * @param string $field
184: * @param mixed $defaultValue
185: */
186: protected function _unserializeField(Varien_Object $object, $field, $defaultValue = null)
187: {
188: $value = $object->getData($field);
189: if (empty($value)) {
190: $object->setData($field, $defaultValue);
191: } elseif (!is_array($value) && !is_object($value)) {
192: $object->setData($field, unserialize($value));
193: }
194: }
195:
196: /**
197: * Prepare data for passed table
198: *
199: * @param Varien_Object $object
200: * @param string $table
201: * @return array
202: */
203: protected function _prepareDataForTable(Varien_Object $object, $table)
204: {
205: $data = array();
206: $fields = $this->_getWriteAdapter()->describeTable($table);
207: foreach (array_keys($fields) as $field) {
208: if ($object->hasData($field)) {
209: $fieldValue = $object->getData($field);
210: if ($fieldValue instanceof Zend_Db_Expr) {
211: $data[$field] = $fieldValue;
212: } else {
213: if (null !== $fieldValue) {
214: $fieldValue = $this->_prepareTableValueForSave($fieldValue, $fields[$field]['DATA_TYPE']);
215: $data[$field] = $this->_getWriteAdapter()->prepareColumnValue($fields[$field], $fieldValue);
216: } else if (!empty($fields[$field]['NULLABLE'])) {
217: $data[$field] = null;
218: }
219: }
220: }
221: }
222: return $data;
223: }
224:
225: /**
226: * Prepare value for save
227: *
228: * @param mixed $value
229: * @param string $type
230: * @return mixed
231: */
232: protected function _prepareTableValueForSave($value, $type)
233: {
234: $type = strtolower($type);
235: if ($type == 'decimal' || $type == 'numeric' || $type == 'float') {
236: $value = Mage::app()->getLocale()->getNumber($value);
237: }
238: return $value;
239: }
240: }
241: