Your IP : 13.58.104.73
<?php
/**
* Bitrix Framework
* @package bitrix
* @subpackage main
* @copyright 2001-2017 Bitrix
*/
namespace Bitrix\Main\UserConsent;
use Bitrix\Main\Config\Option;
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\IO;
use Bitrix\Main\Localization\LanguageTable;
use Bitrix\Main\Application;
use Bitrix\Main\UserTable;
Loc::loadLanguageFile(__FILE__);
/**
* Class Intl
* @package Bitrix\Main\UserConsent
*/
class Intl
{
/** @var string|null */
protected $languageId = null;
/** @var array */
protected $data = array();
/** @var array */
protected static $virtualLanguageMap = array('kz' => 'ru');
/**
* Constructor.
*
* @param string $languageId Language ID.
*/
public function __construct($languageId = null)
{
if ($languageId)
{
$this->load($languageId);
}
}
/**
* Load.
*
* @param string $languageId Language ID.
* @return void
*/
public function load($languageId)
{
$this->data = array();
$this->languageId = null;
$list = self::getList();
foreach ($list as $item)
{
if ($item['LANGUAGE_ID'] == $languageId)
{
$this->data = $item;
$this->languageId = $languageId;
break;
}
}
}
/**
* Return standard fields for current language.
*
* @return array
*/
public function getFields()
{
return isset($this->data['FIELDS']) ? $this->data['FIELDS'] : array();
}
/**
* Get text.
*
* @return string
*/
public function getText()
{
return isset($this->data['AGREEMENT_TEXT']) ? $this->data['AGREEMENT_TEXT'] : '';
}
/**
* Get label text.
*
* @return string
*/
public function getLabelText()
{
return isset($this->data['LABEL_TEXT']) ? $this->data['LABEL_TEXT'] : '';
}
/**
* Get label text.
*
* @return string
*/
public function getNotifyText()
{
return isset($this->data['NOTIFY_TEXT']) ? $this->data['NOTIFY_TEXT'] : '';
}
/**
* Get phrase.
*
* @param string $code Phrase code.
* @return string
*/
public function getPhrase($code)
{
return isset($this->data['PHRASES'][$code]) ? $this->data['PHRASES'][$code] : '';
}
/**
* Get data value by code.
*
* @param string $code Data code.
* @return string
*/
public function getDataValue($code)
{
return isset($this->data[$code]) ? $this->data[$code] : null;
}
/**
* Get list of language settings.
*
* @return array
*/
public static function getList()
{
static $list = null;
if (is_array($list))
{
return $list;
}
$user = UserTable::getList(array(
'select' => array('EMAIL'),
'filter' => array(
'=ID' => array_slice(\CGroup::getGroupUser(1), 0, 200),
'=ACTIVE' => 'Y'
),
'limit' => 1
))->fetch();
if ($user && $user['EMAIL'])
{
$email = $user['EMAIL'];
}
else
{
$email = Option::get('main', 'email_from', '');
}
$list = array();
$intl = array(
'ru' => array(
'PHRASES' => array('COMPANY_NAME', 'IP_NAME'),
'FIELDS' => array(
array(
'CODE' => 'COMPANY_NAME',
'TYPE' => 'text',
),
array(
'CODE' => 'COMPANY_ADDRESS',
'TYPE' => 'text',
),
array(
'CODE' => 'PURPOSES',
'TYPE' => 'text',
'SHOW_BY_CHECKBOX' => true,
),
array(
'CODE' => 'THIRD_PARTIES',
'TYPE' => 'text',
'SHOW_BY_CHECKBOX' => true,
),
array(
'CODE' => 'EMAIL',
'TYPE' => 'string',
'PLACEHOLDER' => $email,
'DEFAULT_VALUE' => $email,
),
),
),
'ua' => array(
'PHRASES' => array(),
'FIELDS' => array(
array(
'CODE' => 'COMPANY_NAME',
'TYPE' => 'text',
),
),
),
'kz' => array(
'PHRASES' => array('COMPANY_NAME', 'IP_NAME'),
'FIELDS' => array(
array(
'CODE' => 'COMPANY_NAME',
'TYPE' => 'text',
),
array(
'CODE' => 'COMPANY_ADDRESS',
'TYPE' => 'text',
),
array(
'CODE' => 'PURPOSES',
'TYPE' => 'text',
'SHOW_BY_CHECKBOX' => true,
),
array(
'CODE' => 'THIRD_PARTIES',
'TYPE' => 'text',
'SHOW_BY_CHECKBOX' => true,
),
array(
'CODE' => 'EMAIL',
'TYPE' => 'string',
'PLACEHOLDER' => $email,
'DEFAULT_VALUE' => $email,
),
),
)
);
$messageMap = array(
'AGREEMENT_TEXT' => 'MAIN_USER_CONSENT_INTL_TEXT',
'LABEL_TEXT' => 'MAIN_USER_CONSENT_INTL_LABEL',
'FIELDS_HINT' => 'MAIN_USER_CONSENT_INTL_FIELDS_HINT',
'DESCRIPTION' => 'MAIN_USER_CONSENT_INTL_DESCRIPTION',
'NOTIFY_TEXT' => 'MAIN_USER_CONSENT_INTL_NOTIFY_TEXT',
);
$languages = self::getLanguages();
foreach ($languages as $languageId => $languageName)
{
$item = self::getLanguageMessages($languageId, $messageMap);
if (!$item['AGREEMENT_TEXT'])
{
continue;
}
$item['NAME'] = Loc::getMessage('MAIN_USER_CONSENT_INTL_NAME', array('%language_name%' => $languageName));
$item['BASE_LANGUAGE_ID'] = isset(self::$virtualLanguageMap[$languageId]) ? self::$virtualLanguageMap[$languageId] : $languageId;
$item['LANGUAGE_ID'] = $languageId;
$item['LANGUAGE_NAME'] = $languageName;
$item['PHRASES'] = array();
if (isset($intl[$languageId]['PHRASES']))
{
$phraseMap = array();
foreach ($intl[$languageId]['PHRASES'] as $phraseCode)
{
$phraseMap[$phraseCode] = "MAIN_USER_CONSENT_INTL_PHRASE_{$phraseCode}";
}
$item['PHRASES'] = self::getLanguageMessages($languageId, $phraseMap);
}
$item['FIELDS'] = array();
if (isset($intl[$languageId]['FIELDS']))
{
foreach ($intl[$languageId]['FIELDS'] as $field)
{
$messageFieldsMap = array(
'CAPTION' => "MAIN_USER_CONSENT_INTL_FIELD_{$field['CODE']}",
'PLACEHOLDER' => "MAIN_USER_CONSENT_INTL_FIELD_{$field['CODE']}_HINT",
'DEFAULT_VALUE' => "MAIN_USER_CONSENT_INTL_FIELD_{$field['CODE']}_DEFAULT",
);
$field = $field + self::getLanguageMessages($languageId, $messageFieldsMap);
if ($field['TYPE'] == 'text' && $field['DEFAULT_VALUE'])
{
$field['PLACEHOLDER'] .= "\n" . Loc::getMessage('MAIN_USER_CONSENT_INTL_HINT_FIELD_DEFAULT');
$field['PLACEHOLDER'] .= "\n" . $field['DEFAULT_VALUE'];
}
$item['FIELDS'][] = $field;
}
}
$list[] = $item;
}
return $list;
}
/**
* Get languages.
*
* @return array
*/
public static function getLanguages()
{
static $list = null;
if (is_array($list))
{
return $list;
}
$list = array();
// set virtual languages
foreach (self::$virtualLanguageMap as $virtualLanguageId => $languageId)
{
$languageName = Loc::getMessage('MAIN_USER_CONSENT_INTL_LANG_NAME_' . strtoupper($virtualLanguageId));
if (!$languageName)
{
$languageName = $virtualLanguageId;
}
$list[$virtualLanguageId] = $languageName;
}
// read lang dirs
$dirLanguages = array();
$langDir = Application::getDocumentRoot() . '/bitrix/modules/main/lang/';
$dir = new IO\Directory($langDir);
if (!$dir->isExists())
{
return $list;
}
foreach($dir->getChildren() as $childDir)
{
if (!$childDir->isDirectory())
{
continue;
}
$dirLanguages[] = $childDir->getName();
}
if (count($dirLanguages) == 0)
{
return $list;
}
// set languages from DB by dir languages
$listDb = LanguageTable::getList(array(
'select' => array('LID', 'NAME'),
'filter' => array(
'=LID' => $dirLanguages,
'=ACTIVE' => 'Y'
),
'order' => array('SORT' => 'ASC')
));
while ($item = $listDb->fetch())
{
$list[$item['LID']] = $item['NAME'];
}
return $list;
}
/**
* Get provider fields.
*
* @param string $languageId Language ID.
* @param array $map Message key map.
* @return array
*/
public static function getLanguageMessages($languageId, array $map = array())
{
// rewrite $languageId by real value from virtual language
$virtualLanguageId = null;
if (isset(self::$virtualLanguageMap[$languageId]))
{
$virtualLanguageId = $languageId;
$languageId = self::$virtualLanguageMap[$virtualLanguageId];
}
// load messages
$messages = Loc::loadLanguageFile(__FILE__, $languageId, true);
// set map by all message codes
if (count($map) === 0)
{
$keys = array_keys($messages);
foreach ($keys as $key)
{
$map[$key] = $key;
}
}
// append postfix to message codes from virtual language
if ($virtualLanguageId)
{
$postfix = '_' . strtoupper($virtualLanguageId);
foreach ($map as $itemKey => $messageKey)
{
if (substr($itemKey, -strlen($postfix)) == $postfix)
{
$oldItemKey = $itemKey;
$itemKey = substr($itemKey, 0, -strlen($postfix));
unset($map[$oldItemKey]);
}
if (substr($messageKey, -strlen($postfix)) != $postfix)
{
if (isset($messages[$messageKey . $postfix]))
{
$messageKey .= $postfix;
}
}
$map[$itemKey] = $messageKey;
}
}
// set messages by map
$item = array();
foreach ($map as $itemKey => $messageKey)
{
$message = isset($messages[$messageKey]) ? $messages[$messageKey] : '';
$message = trim($message);
$message = $message == '-' ? '' : $message;
$item[$itemKey] = $message;
}
return $item;
}
}