Bitrix-D7  20.5.0
Класс CurrencyManager

Открытые статические члены

static checkCurrencyID ($currency)
 Check currency id. Подробнее...
 
static checkLanguage ($language)
 Check language id. Подробнее...
 
static getBaseCurrency ()
 Return base currency. Подробнее...
 
static getCurrencyList ()
 Return currency short list. Подробнее...
 
static getSymbolList ()
 Returns currency symbol list. Подробнее...
 
static getNameList ()
 Returns currency name list. Подробнее...
 
static isCurrencyExist ($currency)
 Verifying the existence of the currency by its code. Подробнее...
 
static getInstalledCurrencies ()
 Return currency list, create to install module. Подробнее...
 
static clearCurrencyCache ($language='')
 Clear currency cache. Подробнее...
 
static clearTagCache ($currency)
 Clear tag currency cache. Подробнее...
 
static currencyBaseRateAgent ()
 Agent for update current currencies rates to base currency. Подробнее...
 
static updateBaseRates ($updateCurrency='')
 Update current currencies rates to base currency. Подробнее...
 
static updateBaseCurrency ($currency)
 Update base currency. Подробнее...
 

Поля данных

const CACHE_BASE_CURRENCY_ID = 'currency_base_currency'
 
const CACHE_CURRENCY_LIST_ID = 'currency_currency_list'
 
const CACHE_CURRENCY_SHORT_LIST_ID = 'currency_short_list_'
 
const CACHE_CURRENCY_SYMBOL_LIST_ID = 'currency_symbol_list_'
 
const CACHE_CURRENCY_NAME_LIST_ID = 'currency_name_list_'
 
const EVENT_ON_AFTER_UPDATE_BASE_RATE = 'onAfterUpdateCurrencyBaseRate'
 
const EVENT_ON_UPDATE_BASE_CURRENCY = 'onUpdateBaseCurrency'
 
const EVENT_ON_AFTER_UPDATE_BASE_CURRENCY = 'onAfterUpdateBaseCurrency'
 

Статические защищенные данные

static $baseCurrency = ''
 

Подробное описание

См. определение в файле currencymanager.php строка 18

Методы

◆ checkCurrencyID()

static checkCurrencyID (   $currency)
static

Check currency id.

Аргументы
string$currencyCurrency id.
Возвращает
bool|string

См. определение в файле currencymanager.php строка 38

39  {
40  $currency = (string)$currency;
41  return ($currency === '' || mb_strlen($currency) > 3 ? false : $currency);
42  }

◆ checkLanguage()

static checkLanguage (   $language)
static

Check language id.

Аргументы
string$languageLanguage.
Возвращает
bool|string

См. определение в файле currencymanager.php строка 50

51  {
52  $language = (string)$language;
53  return ($language === '' || mb_strlen($language) > 2 ? false : $language);
54  }

◆ clearCurrencyCache()

static clearCurrencyCache (   $language = '')
static

Clear currency cache.

Аргументы
string$languageLanguage id.
Возвращает
void

См. определение в файле currencymanager.php строка 340

341  {
342  $language = static::checkLanguage($language);
343  $currencyTableName = CurrencyTable::getTableName();
344 
345  $managedCache = Application::getInstance()->getManagedCache();
346  $managedCache->clean(self::CACHE_CURRENCY_LIST_ID, $currencyTableName);
347  if (empty($language))
348  {
349  $languageIterator = LanguageTable::getList(array(
350  'select' => array('ID')
351  ));
352  while ($oneLanguage = $languageIterator->fetch())
353  {
354  $managedCache->clean(self::CACHE_CURRENCY_LIST_ID.'_'.$oneLanguage['ID'], $currencyTableName);
355  $managedCache->clean(self::CACHE_CURRENCY_SHORT_LIST_ID.$oneLanguage['ID'], $currencyTableName);
356  $managedCache->clean(self::CACHE_CURRENCY_SYMBOL_LIST_ID.$oneLanguage['ID'], $currencyTableName);
357  $managedCache->clean(self::CACHE_CURRENCY_NAME_LIST_ID.$oneLanguage['ID'], $currencyTableName);
358  }
359  unset($oneLanguage, $languageIterator);
360  }
361  else
362  {
363  $managedCache->clean(self::CACHE_CURRENCY_LIST_ID.'_'.$language, $currencyTableName);
364  $managedCache->clean(self::CACHE_CURRENCY_SHORT_LIST_ID.$language, $currencyTableName);
365  $managedCache->clean(self::CACHE_CURRENCY_SYMBOL_LIST_ID.$language, $currencyTableName);
366  $managedCache->clean(self::CACHE_CURRENCY_NAME_LIST_ID.$language, $currencyTableName);
367  }
368  $managedCache->clean(self::CACHE_BASE_CURRENCY_ID, $currencyTableName);
369 
370  /** @global \CStackCacheManager $stackCacheManager */
371  global $stackCacheManager;
372  $stackCacheManager->clear('currency_rate');
373  $stackCacheManager->clear('currency_currency_lang');
374  }
static getTableName()
Returns DB table name for entity.
static getInstance()
Returns current instance of the Application.
static getList(array $parameters=array())
Executes the query and returns selection by parameters of the query.

◆ clearTagCache()

static clearTagCache (   $currency)
static

Clear tag currency cache.

Аргументы
string$currencyCurrency id.
Возвращает
void

См. определение в файле currencymanager.php строка 382

383  {
384  if (!defined('BX_COMP_MANAGED_CACHE'))
385  return;
386  $currency = static::checkCurrencyID($currency);
387  if ($currency === false)
388  return;
389  Application::getInstance()->getTaggedCache()->clearByTag('currency_id_'.$currency);
390  }

◆ currencyBaseRateAgent()

static currencyBaseRateAgent ( )
static

Agent for update current currencies rates to base currency.

Возвращает
string

См. определение в файле currencymanager.php строка 397

398  {
399  static::updateBaseRates();
400  return '\Bitrix\Currency\CurrencyManager::currencyBaseRateAgent();';
401  }

◆ getBaseCurrency()

static getBaseCurrency ( )
static

Return base currency.

Возвращает
string

См. определение в файле currencymanager.php строка 61

62  {
63  if (self::$baseCurrency === '')
64  {
65  /** @var \Bitrix\Main\Data\ManagedCache $managedCache */
66  $skipCache = (defined('CURRENCY_SKIP_CACHE') && CURRENCY_SKIP_CACHE);
67  $currencyFound = false;
68  $currencyFromCache = false;
69  if (!$skipCache)
70  {
71  $cacheTime = (int)(defined('CURRENCY_CACHE_TIME') ? CURRENCY_CACHE_TIME : CURRENCY_CACHE_DEFAULT_TIME);
72  $managedCache = Application::getInstance()->getManagedCache();
73  $currencyFromCache = $managedCache->read($cacheTime, self::CACHE_BASE_CURRENCY_ID, CurrencyTable::getTableName());
74  if ($currencyFromCache)
75  {
76  $currencyFound = true;
77  self::$baseCurrency = (string)$managedCache->get(self::CACHE_BASE_CURRENCY_ID);
78  }
79  }
80  if ($skipCache || !$currencyFound)
81  {
82  $currencyIterator = CurrencyTable::getList(array(
83  'select' => array('CURRENCY'),
84  'filter' => array('=BASE' => 'Y', '=AMOUNT' => 1)
85  ));
86  if ($currency = $currencyIterator->fetch())
87  {
88  $currencyFound = true;
89  self::$baseCurrency = $currency['CURRENCY'];
90  }
91  unset($currency, $currencyIterator);
92  }
93  if (!$skipCache && $currencyFound && !$currencyFromCache)
94  {
95  $managedCache->set(self::CACHE_BASE_CURRENCY_ID, self::$baseCurrency);
96  }
97  }
98  return self::$baseCurrency;
99  }

◆ getCurrencyList()

static getCurrencyList ( )
static

Return currency short list.

Возвращает
array
Исключения

См. определение в файле currencymanager.php строка 107

108  {
109  $currencyTableName = CurrencyTable::getTableName();
110  $managedCache = Application::getInstance()->getManagedCache();
111 
112  $cacheTime = (int)(defined('CURRENCY_CACHE_TIME') ? CURRENCY_CACHE_TIME : CURRENCY_CACHE_DEFAULT_TIME);
113  $cacheId = self::CACHE_CURRENCY_SHORT_LIST_ID.LANGUAGE_ID;
114 
115  if ($managedCache->read($cacheTime, $cacheId, $currencyTableName))
116  {
117  $currencyList = $managedCache->get($cacheId);
118  }
119  else
120  {
121  $currencyList = array();
122  $currencyIterator = CurrencyTable::getList(array(
123  'select' => array('CURRENCY', 'FULL_NAME' => 'CURRENT_LANG_FORMAT.FULL_NAME', 'SORT'),
124  'order' => array('SORT' => 'ASC', 'CURRENCY' => 'ASC')
125  ));
126  while ($currency = $currencyIterator->fetch())
127  {
128  $currency['FULL_NAME'] = (string)$currency['FULL_NAME'];
129  $currencyList[$currency['CURRENCY']] = $currency['CURRENCY'].($currency['FULL_NAME'] != '' ? ' ('.$currency['FULL_NAME'].')' : '');
130  }
131  unset($currency, $currencyIterator);
132  $managedCache->set($cacheId, $currencyList);
133  }
134  return $currencyList;
135  }

◆ getInstalledCurrencies()

static getInstalledCurrencies ( )
static

Return currency list, create to install module.

Возвращает
array

См. определение в файле currencymanager.php строка 262

263  {
264  $installedCurrencies = (string)Option::get('currency', 'installed_currencies');
265  if ($installedCurrencies === '')
266  {
267  $bitrix24 = Main\ModuleManager::isModuleInstalled('bitrix24');
268 
269  $languageID = '';
270  $siteIterator = Main\SiteTable::getList(array(
271  'select' => array('LID', 'LANGUAGE_ID'),
272  'filter' => array('=DEF' => 'Y', '=ACTIVE' => 'Y')
273  ));
274  if ($site = $siteIterator->fetch())
275  $languageID = (string)$site['LANGUAGE_ID'];
276  unset($site, $siteIterator);
277 
278  if ($languageID == '')
279  $languageID = 'en';
280 
281  if (!$bitrix24 && $languageID == 'ru')
282  {
283  $languageList = array();
284  $languageIterator = LanguageTable::getList(array(
285  'select' => array('ID'),
286  'filter' => array('@ID' => array('kz', 'by', 'ua'), '=ACTIVE' => 'Y')
287  ));
288  while ($language = $languageIterator->fetch())
289  $languageList[$language['ID']] = $language['ID'];
290  unset($language, $languageIterator);
291  if (isset($languageList['kz']))
292  $languageID = 'kz';
293  elseif (isset($languageList['by']))
294  $languageID = 'by';
295  elseif (isset($languageList['ua']))
296  $languageID = 'ua';
297  unset($languageList);
298  }
299  unset($bitrix24);
300 
301  switch ($languageID)
302  {
303  case 'br':
304  $currencyList = array('BYN', 'RUB', 'USD', 'EUR');
305  break;
306  case 'ua':
307  $currencyList = array('UAH', 'RUB', 'USD', 'EUR');
308  break;
309  case 'kz':
310  $currencyList = array('KZT', 'RUB', 'USD', 'EUR');
311  break;
312  case 'ru':
313  $currencyList = array('RUB', 'USD', 'EUR', 'UAH', 'BYN');
314  break;
315  case 'de':
316  case 'en':
317  case 'tc':
318  case 'sc':
319  case 'la':
320  default:
321  $currencyList = array('USD', 'EUR', 'CNY', 'BRL', 'INR');
322  break;
323  }
324 
325  Option::set('currency', 'installed_currencies', implode(',', $currencyList), '');
326  return $currencyList;
327  }
328  else
329  {
330  return explode(',', $installedCurrencies);
331  }
332  }
static get($moduleId, $name, $default="", $siteId=false)
Returns a value of an option.
static set($moduleId, $name, $value="", $siteId="")
Sets an option value and saves it into a DB.
static isModuleInstalled($moduleName)

◆ getNameList()

static getNameList ( )
static

Returns currency name list.

Возвращает
array
Исключения

См. определение в файле currencymanager.php строка 199

199  : array
200  {
201  $currencyTableName = CurrencyTable::getTableName();
202  $managedCache = Application::getInstance()->getManagedCache();
203 
204  $cacheTime = defined('CURRENCY_CACHE_TIME') ? (int)CURRENCY_CACHE_TIME : CURRENCY_CACHE_DEFAULT_TIME;
205  $cacheId = self::CACHE_CURRENCY_NAME_LIST_ID.LANGUAGE_ID;
206 
207  if ($managedCache->read($cacheTime, $cacheId, $currencyTableName))
208  {
209  $currencyList = $managedCache->get($cacheId);
210  }
211  else
212  {
213  $currencyList = [];
214  $currencyIterator = CurrencyTable::getList([
215  'select' => [
216  'CURRENCY',
217  'FULL_NAME' => 'CURRENT_LANG_FORMAT.FULL_NAME',
218  'SORT'
219  ],
220  'order' => [
221  'SORT' => 'ASC',
222  'CURRENCY' => 'ASC'
223  ]
224  ]);
225  while ($currency = $currencyIterator->fetch())
226  {
227  $fullName = (string)$currency['FULL_NAME'];
228  if ($fullName === '')
229  {
230  $fullName = $currency['CURRENCY'];
231  }
232 
233  $currencyList[$currency['CURRENCY']] = $fullName;
234  }
235 
236  $managedCache->set($cacheId, $currencyList);
237  }
238 
239  return $currencyList;
240  }

◆ getSymbolList()

static getSymbolList ( )
static

Returns currency symbol list.

Возвращает
array
Исключения

См. определение в файле currencymanager.php строка 143

143  : array
144  {
145  $currencyTableName = CurrencyTable::getTableName();
146  $managedCache = Application::getInstance()->getManagedCache();
147 
148  $cacheTime = defined('CURRENCY_CACHE_TIME') ? (int)CURRENCY_CACHE_TIME : CURRENCY_CACHE_DEFAULT_TIME;
149  $cacheId = self::CACHE_CURRENCY_SYMBOL_LIST_ID.LANGUAGE_ID;
150 
151  if ($managedCache->read($cacheTime, $cacheId, $currencyTableName))
152  {
153  $currencyList = $managedCache->get($cacheId);
154  }
155  else
156  {
157  $currencyList = [];
158  $currencyIterator = CurrencyTable::getList([
159  'select' => [
160  'CURRENCY',
161  'FORMAT_STRING' => 'CURRENT_LANG_FORMAT.FORMAT_STRING',
162  'SORT'
163  ],
164  'order' => [
165  'SORT' => 'ASC',
166  'CURRENCY' => 'ASC'
167  ]
168  ]);
169  while ($currency = $currencyIterator->fetch())
170  {
171  $currencyFormat = (string)$currency['FORMAT_STRING'];
172 
173  if ($currencyFormat !== '')
174  {
175  $symbol = \CCurrencyLang::applyTemplate('', $currencyFormat);
176  if (is_string($symbol))
177  {
178  $symbol = trim($symbol);
179  if ($symbol !== '')
180  {
181  $currencyList[$currency['CURRENCY']] = $symbol;
182  }
183  }
184  }
185  }
186 
187  $managedCache->set($cacheId, $currencyList);
188  }
189 
190  return $currencyList;
191  }

◆ isCurrencyExist()

static isCurrencyExist (   $currency)
static

Verifying the existence of the currency by its code.

Аргументы
string$currencyCurrency code.
Возвращает
bool

См. определение в файле currencymanager.php строка 248

249  {
250  $currency = static::checkCurrencyID($currency);
251  if ($currency === false)
252  return false;
253  $currencyList = static::getCurrencyList();
254  return isset($currencyList[$currency]);
255  }

◆ updateBaseCurrency()

static updateBaseCurrency (   $currency)
static

Update base currency.

Аргументы
string$currencyCurrency id.
Возвращает
bool

См. определение в файле currencymanager.php строка 454

455  {
456  /** @global \CUser $USER */
457  global $USER;
458  $currency = CurrencyManager::checkCurrencyID($currency);
459  if ($currency === false)
460  return false;
461 
462  $event = new Main\Event(
463  'currency',
464  self::EVENT_ON_UPDATE_BASE_CURRENCY,
465  array(
466  'NEW_BASE_CURRENCY' => $currency
467  )
468  );
469  $event->send();
470  unset($event);
471 
473  $helper = $conn->getSqlHelper();
474 
475  $userID = (isset($USER) && $USER instanceof \CUser ? (int)$USER->getID() : 0);
476 
477  $tableName = $helper->quote(CurrencyTable::getTableName());
478  $baseField = $helper->quote('BASE');
479  $dateUpdateField = $helper->quote('DATE_UPDATE');
480  $modifiedByField = $helper->quote('MODIFIED_BY');
481  $amountField = $helper->quote('AMOUNT');
482  $amountCntField = $helper->quote('AMOUNT_CNT');
483  $currencyField = $helper->quote('CURRENCY');
484  $query = 'update '.$tableName.' set '.$baseField.' = \'N\', '.
485  $dateUpdateField.' = '.$helper->getCurrentDateTimeFunction().', '.
486  $modifiedByField.' = '.($userID == 0 ? 'NULL' : $userID).
487  ' where '.$currencyField.' <> \''.$helper->forSql($currency).'\' and '.$baseField.' = \'Y\'';
488  $conn->queryExecute($query);
489  $query = 'update '.$tableName.' set '.$baseField.' = \'Y\', '.
490  $dateUpdateField.' = '.$helper->getCurrentDateTimeFunction().', '.
491  $modifiedByField.' = '.($userID == 0 ? 'NULL' : $userID).', '.
492  $amountField.' = 1, '.$amountCntField.' = 1 where '.$currencyField.' = \''.$helper->forSql($currency).'\'';
493  $conn->queryExecute($query);
494 
495  static::updateBaseRates();
496 
497  $event = new Main\Event(
498  'currency',
499  self::EVENT_ON_AFTER_UPDATE_BASE_CURRENCY,
500  array(
501  'NEW_BASE_CURRENCY' => $currency
502  )
503  );
504  $event->send();
505  unset($event);
506  self::$baseCurrency = '';
507 
508  return true;
509  }
static checkCurrencyID($currency)
Check currency id.
static getConnection($name="")
Static method returns database connection for the specified name.

◆ updateBaseRates()

static updateBaseRates (   $updateCurrency = '')
static

Update current currencies rates to base currency.

Аргументы
string$updateCurrencyUpdate currency id.
Возвращает
void
Исключения
Main

См. определение в файле currencymanager.php строка 411

412  {
413  $currency = (string)static::getBaseCurrency();
414  if ($currency === '')
415  return;
416 
417  $currencyIterator = CurrencyTable::getList(array(
418  'select' => array('CURRENCY', 'CURRENT_BASE_RATE'),
419  'filter' => ($updateCurrency == '' ? array() : array('=CURRENCY' => $updateCurrency))
420  ));
421  while ($existCurrency = $currencyIterator->fetch())
422  {
423  $baseRate = ($existCurrency['CURRENCY'] != $currency
424  ? \CCurrencyRates::getConvertFactorEx($existCurrency['CURRENCY'], $currency)
425  : 1
426  );
427  $updateResult = CurrencyTable::update($existCurrency['CURRENCY'], array('CURRENT_BASE_RATE' => $baseRate));
428  if ($updateResult->isSuccess())
429  {
430  $event = new Main\Event(
431  'currency',
432  self::EVENT_ON_AFTER_UPDATE_BASE_RATE,
433  array(
434  'OLD_BASE_RATE' => (float)$existCurrency['CURRENT_BASE_RATE'],
435  'CURRENT_BASE_RATE' => $baseRate,
436  'BASE_CURRENCY' => $currency,
437  'CURRENCY' => $existCurrency['CURRENCY']
438  )
439  );
440  $event->send();
441  }
442  unset($updateResult);
443  unset($baseRate);
444  }
445  unset($existCurrency, $currencyIterator);
446  }

Поля

◆ $baseCurrency

$baseCurrency = ''
staticprotected

См. определение в файле currencymanager.php строка 30

◆ CACHE_BASE_CURRENCY_ID

const CACHE_BASE_CURRENCY_ID = 'currency_base_currency'

См. определение в файле currencymanager.php строка 20

◆ CACHE_CURRENCY_LIST_ID

const CACHE_CURRENCY_LIST_ID = 'currency_currency_list'

См. определение в файле currencymanager.php строка 21

◆ CACHE_CURRENCY_NAME_LIST_ID

const CACHE_CURRENCY_NAME_LIST_ID = 'currency_name_list_'

См. определение в файле currencymanager.php строка 24

◆ CACHE_CURRENCY_SHORT_LIST_ID

const CACHE_CURRENCY_SHORT_LIST_ID = 'currency_short_list_'

См. определение в файле currencymanager.php строка 22

◆ CACHE_CURRENCY_SYMBOL_LIST_ID

const CACHE_CURRENCY_SYMBOL_LIST_ID = 'currency_symbol_list_'

См. определение в файле currencymanager.php строка 23

◆ EVENT_ON_AFTER_UPDATE_BASE_CURRENCY

const EVENT_ON_AFTER_UPDATE_BASE_CURRENCY = 'onAfterUpdateBaseCurrency'

См. определение в файле currencymanager.php строка 28

◆ EVENT_ON_AFTER_UPDATE_BASE_RATE

const EVENT_ON_AFTER_UPDATE_BASE_RATE = 'onAfterUpdateCurrencyBaseRate'

См. определение в файле currencymanager.php строка 26

◆ EVENT_ON_UPDATE_BASE_CURRENCY

const EVENT_ON_UPDATE_BASE_CURRENCY = 'onUpdateBaseCurrency'

См. определение в файле currencymanager.php строка 27


Объявления и описания членов класса находятся в файле: