StringUtils.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Model\ResourceModel;
  7. class StringUtils extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  8. {
  9. /**
  10. * @var \Magento\Framework\Locale\ResolverInterface
  11. */
  12. protected $_localeResolver;
  13. /**
  14. * @var \Magento\Framework\App\ScopeResolverInterface
  15. */
  16. protected $scopeResolver;
  17. /**
  18. * @var null|string
  19. */
  20. protected $scope;
  21. /**
  22. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  23. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  24. * @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
  25. * @param string $connectionName
  26. * @param string|null $scope
  27. */
  28. public function __construct(
  29. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  30. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  31. \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
  32. $connectionName = null,
  33. $scope = null
  34. ) {
  35. $this->_localeResolver = $localeResolver;
  36. $this->scopeResolver = $scopeResolver;
  37. $this->scope = $scope;
  38. parent::__construct($context, $connectionName);
  39. }
  40. /**
  41. * Define main table
  42. *
  43. * @return void
  44. */
  45. protected function _construct()
  46. {
  47. $this->_init('translation', 'key_id');
  48. }
  49. /**
  50. * Load
  51. *
  52. * @param \Magento\Framework\Model\AbstractModel $object
  53. * @param String $value
  54. * @param String $field
  55. * @return array|$this
  56. */
  57. public function load(\Magento\Framework\Model\AbstractModel $object, $value, $field = null)
  58. {
  59. if (is_string($value)) {
  60. $select = $this->getConnection()->select()->from(
  61. $this->getMainTable()
  62. )->where(
  63. $this->getMainTable() . '.string=:tr_string'
  64. );
  65. $result = $this->getConnection()->fetchRow($select, ['tr_string' => $value]);
  66. $object->setData($result);
  67. $this->_afterLoad($object);
  68. return $result;
  69. } else {
  70. return parent::load($object, $value, $field);
  71. }
  72. }
  73. /**
  74. * Retrieve select for load
  75. *
  76. * @param String $field
  77. * @param String $value
  78. * @param \Magento\Framework\Model\AbstractModel $object
  79. * @return \Magento\Framework\DB\Select
  80. */
  81. protected function _getLoadSelect($field, $value, $object)
  82. {
  83. $select = parent::_getLoadSelect($field, $value, $object);
  84. $select->where('store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
  85. return $select;
  86. }
  87. /**
  88. * After translation loading
  89. *
  90. * @param \Magento\Framework\Model\AbstractModel $object
  91. * @return $this
  92. */
  93. public function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
  94. {
  95. $connection = $this->getConnection();
  96. $select = $connection->select()->from(
  97. $this->getMainTable(),
  98. ['store_id', 'translate']
  99. )->where(
  100. 'string = :translate_string'
  101. );
  102. $translations = $connection->fetchPairs($select, ['translate_string' => $object->getString()]);
  103. $object->setStoreTranslations($translations);
  104. return parent::_afterLoad($object);
  105. }
  106. /**
  107. * Before save
  108. *
  109. * @param \Magento\Framework\Model\AbstractModel $object
  110. * @return $this
  111. */
  112. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  113. {
  114. $connection = $this->getConnection();
  115. $select = $connection->select()
  116. ->from($this->getMainTable(), 'key_id')
  117. ->where('string = :string')
  118. ->where('store_id = :store_id');
  119. $bind = ['string' => $object->getString(), 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID];
  120. $object->setId($connection->fetchOne($select, $bind));
  121. return parent::_beforeSave($object);
  122. }
  123. /**
  124. * After save
  125. *
  126. * @param \Magento\Framework\Model\AbstractModel $object
  127. * @return $this
  128. */
  129. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  130. {
  131. $connection = $this->getConnection();
  132. $select = $connection->select()->from(
  133. $this->getMainTable(),
  134. ['store_id', 'key_id']
  135. )->where(
  136. 'string = :string'
  137. );
  138. $stores = $connection->fetchPairs($select, ['string' => $object->getString()]);
  139. $translations = $object->getStoreTranslations();
  140. if (is_array($translations)) {
  141. foreach ($translations as $storeId => $translate) {
  142. if ($translate === null || $translate == '') {
  143. $where = ['store_id = ?' => $storeId, 'string = ?' => $object->getString()];
  144. $connection->delete($this->getMainTable(), $where);
  145. } else {
  146. $data = ['store_id' => $storeId, 'string' => $object->getString(), 'translate' => $translate];
  147. if (isset($stores[$storeId])) {
  148. $connection->update($this->getMainTable(), $data, ['key_id = ?' => $stores[$storeId]]);
  149. } else {
  150. $connection->insert($this->getMainTable(), $data);
  151. }
  152. }
  153. }
  154. }
  155. return parent::_afterSave($object);
  156. }
  157. /**
  158. * Delete translates
  159. *
  160. * @param string $string
  161. * @param string $locale
  162. * @param int|null $storeId
  163. * @return $this
  164. */
  165. public function deleteTranslate($string, $locale = null, $storeId = null)
  166. {
  167. if ($locale === null) {
  168. $locale = $this->_localeResolver->getLocale();
  169. }
  170. $where = ['locale = ?' => $locale, 'string = ?' => $string];
  171. if ($storeId === false) {
  172. $where['store_id > ?'] = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
  173. } elseif ($storeId !== null) {
  174. $where['store_id = ?'] = $storeId;
  175. }
  176. $this->getConnection()->delete($this->getMainTable(), $where);
  177. return $this;
  178. }
  179. /**
  180. * Save translation
  181. *
  182. * @param String $string
  183. * @param String $translate
  184. * @param String $locale
  185. * @param int|null $storeId
  186. * @return $this
  187. */
  188. public function saveTranslate($string, $translate, $locale = null, $storeId = null)
  189. {
  190. $string = htmlspecialchars_decode($string);
  191. $connection = $this->getConnection();
  192. $table = $this->getMainTable();
  193. $translate = htmlspecialchars($translate, ENT_QUOTES);
  194. if ($locale === null) {
  195. $locale = $this->_localeResolver->getLocale();
  196. }
  197. if ($storeId === null) {
  198. $storeId = $this->getStoreId();
  199. }
  200. $select = $connection->select()->from(
  201. $table,
  202. ['key_id', 'translate']
  203. )->where(
  204. 'store_id = :store_id'
  205. )->where(
  206. 'locale = :locale'
  207. )->where(
  208. 'string = :string'
  209. )->where(
  210. 'crc_string = :crc_string'
  211. );
  212. $bind = [
  213. 'store_id' => $storeId,
  214. 'locale' => $locale,
  215. 'string' => $string,
  216. 'crc_string' => crc32($string),
  217. ];
  218. if ($row = $connection->fetchRow($select, $bind)) {
  219. $original = $string;
  220. if (strpos($original, '::') !== false) {
  221. list(, $original) = explode('::', $original);
  222. }
  223. if ($original == $translate) {
  224. $connection->delete($table, ['key_id=?' => $row['key_id']]);
  225. } elseif ($row['translate'] != $translate) {
  226. $connection->update($table, ['translate' => $translate], ['key_id=?' => $row['key_id']]);
  227. }
  228. } else {
  229. $connection->insert(
  230. $table,
  231. [
  232. 'store_id' => $storeId,
  233. 'locale' => $locale,
  234. 'string' => $string,
  235. 'translate' => $translate,
  236. 'crc_string' => crc32($string)
  237. ]
  238. );
  239. }
  240. return $this;
  241. }
  242. /**
  243. * Retrieve current store identifier
  244. *
  245. * @return int
  246. */
  247. protected function getStoreId()
  248. {
  249. return $this->scopeResolver->getScope($this->scope)->getId();
  250. }
  251. }