PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Resource/Translate/String.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 256 lines | 147 code | 23 blank | 86 comment | 18 complexity | 14389d4db4ba5795ccfbe923b876fd7d MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * String translate resource model
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_Resource_Translate_String extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Define main table
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('core/translate', 'key_id');
  42. }
  43. /**
  44. * Load
  45. *
  46. * @param Mage_Core_Model_Abstract $object
  47. * @param String $value
  48. * @param String $field
  49. * @return array
  50. */
  51. public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
  52. {
  53. if (is_string($value)) {
  54. $select = $this->_getReadAdapter()->select()
  55. ->from($this->getMainTable())
  56. ->where($this->getMainTable().'.string=:tr_string');
  57. $result = $this->_getReadAdapter()->fetchRow($select, array('tr_string'=>$value));
  58. $object->setData($result);
  59. $this->_afterLoad($object);
  60. return $result;
  61. } else {
  62. return parent::load($object, $value, $field);
  63. }
  64. }
  65. /**
  66. * Retrieve select for load
  67. *
  68. * @param String $field
  69. * @param String $value
  70. * @param Mage_Core_Model_Abstract $object
  71. * @return Varien_Db_Select
  72. */
  73. protected function _getLoadSelect($field, $value, $object)
  74. {
  75. $select = parent::_getLoadSelect($field, $value, $object);
  76. $select->where('store_id = ?', Mage_Core_Model_App::ADMIN_STORE_ID);
  77. return $select;
  78. }
  79. /**
  80. * After translation loading
  81. *
  82. * @param Mage_Core_Model_Abstract $object
  83. * @return Mage_Core_Model_Resource_Db_Abstract
  84. */
  85. public function _afterLoad(Mage_Core_Model_Abstract $object)
  86. {
  87. $adapter = $this->_getReadAdapter();
  88. $select = $adapter->select()
  89. ->from($this->getMainTable(), array('store_id', 'translate'))
  90. ->where('string = :translate_string');
  91. $translations = $adapter->fetchPairs($select, array('translate_string' => $object->getString()));
  92. $object->setStoreTranslations($translations);
  93. return parent::_afterLoad($object);
  94. }
  95. /**
  96. * Before save
  97. *
  98. * @param Mage_Core_Model_Abstract $object
  99. * @return Mage_Core_Model_Resource_Translate_String
  100. */
  101. protected function _beforeSave(Mage_Core_Model_Abstract $object)
  102. {
  103. $adapter = $this->_getWriteAdapter();
  104. $select = $adapter->select()
  105. ->from($this->getMainTable(), 'key_id')
  106. ->where('string = :string')
  107. ->where('store_id = :store_id');
  108. $bind = array(
  109. 'string' => $object->getString(),
  110. 'store_id' => Mage_Core_Model_App::ADMIN_STORE_ID
  111. );
  112. $object->setId($adapter->fetchOne($select, $bind));
  113. return parent::_beforeSave($object);
  114. }
  115. /**
  116. * After save
  117. *
  118. * @param Mage_Core_Model_Abstract $object
  119. * @return Mage_Core_Model_Resource_Translate_String
  120. */
  121. protected function _afterSave(Mage_Core_Model_Abstract $object)
  122. {
  123. $adapter = $this->_getWriteAdapter();
  124. $select = $adapter->select()
  125. ->from($this->getMainTable(), array('store_id', 'key_id'))
  126. ->where('string = :string');
  127. $stores = $adapter->fetchPairs($select, array('string' => $object->getString()));
  128. $translations = $object->getStoreTranslations();
  129. if (is_array($translations)) {
  130. foreach ($translations as $storeId => $translate) {
  131. if (is_null($translate) || $translate=='') {
  132. $where = array(
  133. 'store_id = ?' => $storeId,
  134. 'string = ?' => $object->getString()
  135. );
  136. $adapter->delete($this->getMainTable(), $where);
  137. } else {
  138. $data = array(
  139. 'store_id' => $storeId,
  140. 'string' => $object->getString(),
  141. 'translate' => $translate,
  142. );
  143. if (isset($stores[$storeId])) {
  144. $adapter->update(
  145. $this->getMainTable(),
  146. $data,
  147. array('key_id = ?' => $stores[$storeId]));
  148. } else {
  149. $adapter->insert($this->getMainTable(), $data);
  150. }
  151. }
  152. }
  153. }
  154. return parent::_afterSave($object);
  155. }
  156. /**
  157. * Delete translates
  158. *
  159. * @param string $string
  160. * @param string $locale
  161. * @param int|null $storeId
  162. * @return Mage_Core_Model_Resource_Translate_String
  163. */
  164. public function deleteTranslate($string, $locale = null, $storeId = null)
  165. {
  166. if (is_null($locale)) {
  167. $locale = Mage::app()->getLocale()->getLocaleCode();
  168. }
  169. $where = array(
  170. 'locale = ?' => $locale,
  171. 'string = ?' => $string
  172. );
  173. if ($storeId === false) {
  174. $where['store_id > ?'] = Mage_Core_Model_App::ADMIN_STORE_ID;
  175. } elseif ($storeId !== null) {
  176. $where['store_id = ?'] = $storeId;
  177. }
  178. $this->_getWriteAdapter()->delete($this->getMainTable(), $where);
  179. return $this;
  180. }
  181. /**
  182. * Save translation
  183. *
  184. * @param String $string
  185. * @param String $translate
  186. * @param String $locale
  187. * @param int|null $storeId
  188. * @return Mage_Core_Model_Resource_Translate_String
  189. */
  190. public function saveTranslate($string, $translate, $locale = null, $storeId = null)
  191. {
  192. $write = $this->_getWriteAdapter();
  193. $table = $this->getMainTable();
  194. if (is_null($locale)) {
  195. $locale = Mage::app()->getLocale()->getLocaleCode();
  196. }
  197. if (is_null($storeId)) {
  198. $storeId = Mage::app()->getStore()->getId();
  199. }
  200. $select = $write->select()
  201. ->from($table, array('key_id', 'translate'))
  202. ->where('store_id = :store_id')
  203. ->where('locale = :locale')
  204. ->where('string = :string')
  205. ->where('crc_string = :crc_string');
  206. $bind = array(
  207. 'store_id' => $storeId,
  208. 'locale' => $locale,
  209. 'string' => $string,
  210. 'crc_string' => crc32($string),
  211. );
  212. if ($row = $write->fetchRow($select, $bind)) {
  213. $original = $string;
  214. if (strpos($original, '::') !== false) {
  215. list($scope, $original) = explode('::', $original);
  216. }
  217. if ($original == $translate) {
  218. $write->delete($table, array('key_id=?' => $row['key_id']));
  219. } elseif ($row['translate'] != $translate) {
  220. $write->update($table, array('translate' => $translate), array('key_id=?' => $row['key_id']));
  221. }
  222. } else {
  223. $write->insert($table, array(
  224. 'store_id' => $storeId,
  225. 'locale' => $locale,
  226. 'string' => $string,
  227. 'translate' => $translate,
  228. 'crc_string' => crc32($string),
  229. ));
  230. }
  231. return $this;
  232. }
  233. }