Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StringUtils.php
Go to the documentation of this file.
1 <?php
7 
9 {
13  protected $_localeResolver;
14 
18  protected $scopeResolver;
19 
23  protected $scope;
24 
32  public function __construct(
33  \Magento\Framework\Model\ResourceModel\Db\Context $context,
34  \Magento\Framework\Locale\ResolverInterface $localeResolver,
35  \Magento\Framework\App\ScopeResolverInterface $scopeResolver,
36  $connectionName = null,
37  $scope = null
38  ) {
39  $this->_localeResolver = $localeResolver;
40  $this->scopeResolver = $scopeResolver;
41  $this->scope = $scope;
42  parent::__construct($context, $connectionName);
43  }
44 
50  protected function _construct()
51  {
52  $this->_init('translation', 'key_id');
53  }
54 
63  public function load(\Magento\Framework\Model\AbstractModel $object, $value, $field = null)
64  {
65  if (is_string($value)) {
66  $select = $this->getConnection()->select()->from(
67  $this->getMainTable()
68  )->where(
69  $this->getMainTable() . '.string=:tr_string'
70  );
71  $result = $this->getConnection()->fetchRow($select, ['tr_string' => $value]);
72  $object->setData($result);
73  $this->_afterLoad($object);
74  return $result;
75  } else {
76  return parent::load($object, $value, $field);
77  }
78  }
79 
88  protected function _getLoadSelect($field, $value, $object)
89  {
90  $select = parent::_getLoadSelect($field, $value, $object);
91  $select->where('store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
92  return $select;
93  }
94 
101  public function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
102  {
103  $connection = $this->getConnection();
104  $select = $connection->select()->from(
105  $this->getMainTable(),
106  ['store_id', 'translate']
107  )->where(
108  'string = :translate_string'
109  );
110  $translations = $connection->fetchPairs($select, ['translate_string' => $object->getString()]);
111  $object->setStoreTranslations($translations);
112  return parent::_afterLoad($object);
113  }
114 
121  protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
122  {
123  $connection = $this->getConnection();
124  $select = $connection->select()
125  ->from($this->getMainTable(), 'key_id')
126  ->where('string = :string')
127  ->where('store_id = :store_id');
128 
129  $bind = ['string' => $object->getString(), 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID];
130 
131  $object->setId($connection->fetchOne($select, $bind));
132  return parent::_beforeSave($object);
133  }
134 
141  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
142  {
143  $connection = $this->getConnection();
144  $select = $connection->select()->from(
145  $this->getMainTable(),
146  ['store_id', 'key_id']
147  )->where(
148  'string = :string'
149  );
150  $stores = $connection->fetchPairs($select, ['string' => $object->getString()]);
151 
152  $translations = $object->getStoreTranslations();
153 
154  if (is_array($translations)) {
155  foreach ($translations as $storeId => $translate) {
156  if ($translate === null || $translate == '') {
157  $where = ['store_id = ?' => $storeId, 'string = ?' => $object->getString()];
158  $connection->delete($this->getMainTable(), $where);
159  } else {
160  $data = ['store_id' => $storeId, 'string' => $object->getString(), 'translate' => $translate];
161 
162  if (isset($stores[$storeId])) {
163  $connection->update($this->getMainTable(), $data, ['key_id = ?' => $stores[$storeId]]);
164  } else {
165  $connection->insert($this->getMainTable(), $data);
166  }
167  }
168  }
169  }
170  return parent::_afterSave($object);
171  }
172 
181  public function deleteTranslate($string, $locale = null, $storeId = null)
182  {
183  if ($locale === null) {
184  $locale = $this->_localeResolver->getLocale();
185  }
186 
187  $where = ['locale = ?' => $locale, 'string = ?' => $string];
188 
189  if ($storeId === false) {
190  $where['store_id > ?'] = \Magento\Store\Model\Store::DEFAULT_STORE_ID;
191  } elseif ($storeId !== null) {
192  $where['store_id = ?'] = $storeId;
193  }
194 
195  $this->getConnection()->delete($this->getMainTable(), $where);
196 
197  return $this;
198  }
199 
209  public function saveTranslate($string, $translate, $locale = null, $storeId = null)
210  {
211  $string = htmlspecialchars_decode($string);
212  $connection = $this->getConnection();
213  $table = $this->getMainTable();
214  $translate = htmlspecialchars($translate, ENT_QUOTES);
215 
216  if ($locale === null) {
217  $locale = $this->_localeResolver->getLocale();
218  }
219 
220  if ($storeId === null) {
221  $storeId = $this->getStoreId();
222  }
223 
224  $select = $connection->select()->from(
225  $table,
226  ['key_id', 'translate']
227  )->where(
228  'store_id = :store_id'
229  )->where(
230  'locale = :locale'
231  )->where(
232  'string = :string'
233  )->where(
234  'crc_string = :crc_string'
235  );
236  $bind = [
237  'store_id' => $storeId,
238  'locale' => $locale,
239  'string' => $string,
240  'crc_string' => crc32($string),
241  ];
242 
243  if ($row = $connection->fetchRow($select, $bind)) {
244  $original = $string;
245  if (strpos($original, '::') !== false) {
246  list(, $original) = explode('::', $original);
247  }
248  if ($original == $translate) {
249  $connection->delete($table, ['key_id=?' => $row['key_id']]);
250  } elseif ($row['translate'] != $translate) {
251  $connection->update($table, ['translate' => $translate], ['key_id=?' => $row['key_id']]);
252  }
253  } else {
254  $connection->insert(
255  $table,
256  [
257  'store_id' => $storeId,
258  'locale' => $locale,
259  'string' => $string,
260  'translate' => $translate,
261  'crc_string' => crc32($string)
262  ]
263  );
264  }
265 
266  return $this;
267  }
268 
274  protected function getStoreId()
275  {
276  return $this->scopeResolver->getScope($this->scope)->getId();
277  }
278 }
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_afterSave(\Magento\Framework\Model\AbstractModel $object)
load(\Magento\Framework\Model\AbstractModel $object, $value, $field=null)
Definition: StringUtils.php:63
$value
Definition: gender.phtml:16
saveTranslate($string, $translate, $locale=null, $storeId=null)
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Framework\App\ScopeResolverInterface $scopeResolver, $connectionName=null, $scope=null)
Definition: StringUtils.php:32
$connection
Definition: bulk.php:13
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
$table
Definition: trigger.php:14
deleteTranslate($string, $locale=null, $storeId=null)