00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Core_Model_Mysql4_Translate_String extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036 protected function _construct()
00037 {
00038 $this->_init('core/translate', 'key_id');
00039 }
00040
00041 public function load(Mage_Core_Model_Abstract $object, $value, $field=null)
00042 {
00043 if (is_string($value)) {
00044 $select = $this->_getReadAdapter()->select()
00045 ->from($this->getMainTable())
00046 ->where($this->getMainTable().'.string=:tr_string');
00047 $result = $this->_getReadAdapter()->fetchRow($select, array('tr_string'=>$value));
00048 $object->setData($result);
00049 $this->_afterLoad($object);
00050 return $result;
00051 }
00052 else {
00053 return parent::load($object, $value, $field);
00054 }
00055 }
00056
00057 protected function _getLoadSelect($field, $value, $object)
00058 {
00059 $select = parent::_getLoadSelect($field, $value, $object);
00060 $select->where('store_id', 0);
00061 return $select;
00062 }
00063
00064
00065 public function _afterLoad(Mage_Core_Model_Abstract $object)
00066 {
00067 $connection = $this->_getReadAdapter();
00068 $select = $connection->select()
00069 ->from($this->getMainTable(), array('store_id', 'translate'))
00070 ->where('string=:translate_string');
00071 $translations = $connection->fetchPairs($select, array('translate_string' => $object->getString()));
00072 $object->setStoreTranslations($translations);
00073 return parent::_afterLoad($object);
00074 }
00075
00076 protected function _beforeSave(Mage_Core_Model_Abstract $object)
00077 {
00078 $connection = $this->_getWriteAdapter();
00079 $select = $connection->select()
00080 ->from($this->getMainTable(), 'key_id')
00081 ->where('string=?', $object->getString())
00082 ->where('store_id=?', 0);
00083
00084 $object->setId($connection->fetchOne($select));
00085 return parent::_beforeSave($object);
00086 }
00087
00088 protected function _afterSave(Mage_Core_Model_Abstract $object)
00089 {
00090 $connection = $this->_getWriteAdapter();
00091 $select = $connection->select()
00092 ->from($this->getMainTable(), array('store_id', 'key_id'))
00093 ->where('string=?', $object->getString());
00094 $stors = $connection->fetchPairs($select);
00095
00096 $translations = $object->getStoreTranslations();
00097
00098 if (is_array($translations)) {
00099 foreach ($translations as $storeId => $translate) {
00100 $condition = $connection->quoteInto('store_id=? AND ', $storeId) .
00101 $connection->quoteInto('string=?', $object->getString());
00102
00103 if (is_null($translate) || $translate=='') {
00104 $connection->delete($this->getMainTable(), $condition);
00105 }
00106 else {
00107 $data = array(
00108 'store_id' => $storeId,
00109 'string' => $object->getString(),
00110 'translate' =>$translate,
00111 );
00112
00113 if (isset($stors[$storeId])) {
00114 $connection->update(
00115 $this->getMainTable(),
00116 $data,
00117 $connection->quoteInto('key_id=?', $stors[$storeId]));
00118 }
00119 else {
00120 $connection->insert($this->getMainTable(), $data);
00121 }
00122 }
00123 }
00124 }
00125 return parent::_afterSave($object);
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 public function deleteTranslate($string, $locale = null, $storeId = null)
00138 {
00139 if (is_null($locale)) {
00140 $locale = Mage::app()->getLocale()->getLocaleCode();
00141 }
00142
00143 $where = array(
00144 $this->_getWriteAdapter()->quoteInto('locale=?', $locale),
00145 $this->_getWriteAdapter()->quoteInto('string=?', $string),
00146 );
00147
00148 if ($storeId === false) {
00149 $where[] = $this->_getWriteAdapter()->quoteInto('store_id>?', 0);
00150 }
00151 elseif (!is_null($storeId)) {
00152 $where[] = $this->_getWriteAdapter()->quoteInto('store_id=?', $storeId);
00153 }
00154
00155 $this->_getWriteAdapter()->delete($this->getMainTable(), $where);
00156
00157 return $this;
00158 }
00159
00160 public function saveTranslate($string, $translate, $locale=null, $storeId=null)
00161 {
00162 $write = $this->_getWriteAdapter();
00163 $table = $this->getMainTable();
00164
00165 if (is_null($locale)) {
00166 $locale = Mage::app()->getLocale()->getLocaleCode();
00167 }
00168
00169 if (is_null($storeId)) {
00170 $storeId = Mage::app()->getStore()->getId();
00171 }
00172
00173 $select = $write->select()
00174 ->from($table, array('key_id', 'translate'))
00175 ->where('store_id=?', $storeId)
00176 ->where('locale=?', $locale)
00177 ->where('string=?', $string)
00178 ;
00179 if ($row = $write->fetchRow($select)) {
00180 $original = $string;
00181 if (strpos($original, '::')!==false) {
00182 list($scope, $original) = explode('::', $original);
00183 }
00184 if ($original == $translate) {
00185 $write->delete($table, 'key_id='.(int)$row['key_id']);
00186 } elseif ($row['translate']!=$translate) {
00187 $write->update($table, array('translate'=>$translate), 'key_id='.(int)$row['key_id']);
00188 }
00189 } else {
00190 $write->insert($table, array(
00191 'store_id'=>$storeId,
00192 'locale'=>$locale,
00193 'string'=>$string,
00194 'translate'=>$translate,
00195 ));
00196 }
00197
00198 return $this;
00199 }
00200 }