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_Translate_String extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('core/translate', 'key_id');
44: }
45:
46: 47: 48: 49: 50: 51: 52: 53:
54: public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
55: {
56: if (is_string($value)) {
57: $select = $this->_getReadAdapter()->select()
58: ->from($this->getMainTable())
59: ->where($this->getMainTable().'.string=:tr_string');
60: $result = $this->_getReadAdapter()->fetchRow($select, array('tr_string'=>$value));
61: $object->setData($result);
62: $this->_afterLoad($object);
63: return $result;
64: } else {
65: return parent::load($object, $value, $field);
66: }
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76:
77: protected function _getLoadSelect($field, $value, $object)
78: {
79: $select = parent::_getLoadSelect($field, $value, $object);
80: $select->where('store_id = ?', Mage_Core_Model_App::ADMIN_STORE_ID);
81: return $select;
82: }
83:
84: 85: 86: 87: 88: 89:
90: public function _afterLoad(Mage_Core_Model_Abstract $object)
91: {
92: $adapter = $this->_getReadAdapter();
93: $select = $adapter->select()
94: ->from($this->getMainTable(), array('store_id', 'translate'))
95: ->where('string = :translate_string');
96: $translations = $adapter->fetchPairs($select, array('translate_string' => $object->getString()));
97: $object->setStoreTranslations($translations);
98: return parent::_afterLoad($object);
99: }
100:
101: 102: 103: 104: 105: 106:
107: protected function _beforeSave(Mage_Core_Model_Abstract $object)
108: {
109: $adapter = $this->_getWriteAdapter();
110: $select = $adapter->select()
111: ->from($this->getMainTable(), 'key_id')
112: ->where('string = :string')
113: ->where('store_id = :store_id');
114:
115: $bind = array(
116: 'string' => $object->getString(),
117: 'store_id' => Mage_Core_Model_App::ADMIN_STORE_ID
118: );
119:
120: $object->setId($adapter->fetchOne($select, $bind));
121: return parent::_beforeSave($object);
122: }
123:
124: 125: 126: 127: 128: 129:
130: protected function _afterSave(Mage_Core_Model_Abstract $object)
131: {
132: $adapter = $this->_getWriteAdapter();
133: $select = $adapter->select()
134: ->from($this->getMainTable(), array('store_id', 'key_id'))
135: ->where('string = :string');
136: $stores = $adapter->fetchPairs($select, array('string' => $object->getString()));
137:
138: $translations = $object->getStoreTranslations();
139:
140: if (is_array($translations)) {
141: foreach ($translations as $storeId => $translate) {
142: if (is_null($translate) || $translate=='') {
143: $where = array(
144: 'store_id = ?' => $storeId,
145: 'string = ?' => $object->getString()
146: );
147: $adapter->delete($this->getMainTable(), $where);
148: } else {
149: $data = array(
150: 'store_id' => $storeId,
151: 'string' => $object->getString(),
152: 'translate' => $translate,
153: );
154:
155: if (isset($stores[$storeId])) {
156: $adapter->update(
157: $this->getMainTable(),
158: $data,
159: array('key_id = ?' => $stores[$storeId]));
160: } else {
161: $adapter->insert($this->getMainTable(), $data);
162: }
163: }
164: }
165: }
166: return parent::_afterSave($object);
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176:
177: public function deleteTranslate($string, $locale = null, $storeId = null)
178: {
179: if (is_null($locale)) {
180: $locale = Mage::app()->getLocale()->getLocaleCode();
181: }
182:
183: $where = array(
184: 'locale = ?' => $locale,
185: 'string = ?' => $string
186: );
187:
188: if ($storeId === false) {
189: $where['store_id > ?'] = Mage_Core_Model_App::ADMIN_STORE_ID;
190: } elseif ($storeId !== null) {
191: $where['store_id > ?'] = $storeId;
192: }
193:
194: $this->_getWriteAdapter()->delete($this->getMainTable(), $where);
195:
196: return $this;
197: }
198:
199: 200: 201: 202: 203: 204: 205: 206: 207:
208: public function saveTranslate($string, $translate, $locale = null, $storeId = null)
209: {
210: $write = $this->_getWriteAdapter();
211: $table = $this->getMainTable();
212:
213: if (is_null($locale)) {
214: $locale = Mage::app()->getLocale()->getLocaleCode();
215: }
216:
217: if (is_null($storeId)) {
218: $storeId = Mage::app()->getStore()->getId();
219: }
220:
221: $select = $write->select()
222: ->from($table, array('key_id', 'translate'))
223: ->where('store_id = :store_id')
224: ->where('locale = :locale')
225: ->where('string = :string')
226: ;
227: $bind = array(
228: 'store_id' => $storeId,
229: 'locale' => $locale,
230: 'string' => $string
231: );
232:
233: if ($row = $write->fetchRow($select, $bind)) {
234: $original = $string;
235: if (strpos($original, '::') !== false) {
236: list($scope, $original) = explode('::', $original);
237: }
238: if ($original == $translate) {
239: $write->delete($table, array('key_id=?' => $row['key_id']));
240: } elseif ($row['translate'] != $translate) {
241: $write->update($table, array('translate' => $translate), array('key_id=?' => $row['key_id']));
242: }
243: } else {
244: $write->insert($table, array(
245: 'store_id' => $storeId,
246: 'locale' => $locale,
247: 'string' => $string,
248: 'translate' => $translate,
249: ));
250: }
251:
252: return $this;
253: }
254: }
255: