Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
intl.php
1<?php
9
16
17Loc::loadLanguageFile(__FILE__);
18
23class Intl
24{
26 protected $languageId = null;
27
29 protected $data = [];
30
32 protected static $virtualLanguageMap = ['kz' => 'ru', 'by' => 'ru'];
33
34
40 public function __construct($languageId = null)
41 {
42 if ($languageId)
43 {
44 $this->load($languageId);
45 }
46 }
47
54 public function load($languageId)
55 {
56 $this->data = array();
57 $this->languageId = null;
58
59 $list = self::getList();
60 foreach ($list as $item)
61 {
62 if ($item['LANGUAGE_ID'] == $languageId)
63 {
64 $this->data = $item;
65 $this->languageId = $languageId;
66 break;
67 }
68 }
69 }
70
76 public function getFields()
77 {
78 return $this->data['FIELDS'] ?? array();
79 }
80
86 public function getText()
87 {
88 return $this->data['AGREEMENT_TEXT'] ?? '';
89 }
90
96 public function getLabelText()
97 {
98 return $this->data['LABEL_TEXT'] ?? '';
99 }
100
106 public function getNotifyText()
107 {
108 return $this->data['NOTIFY_TEXT'] ?? '';
109 }
110
117 public function getPhrase($code)
118 {
119 return $this->data['PHRASES'][$code] ?? '';
120 }
121
128 public function getDataValue($code)
129 {
130 return $this->data[$code] ?? null;
131 }
132
133
139 public static function getList()
140 {
141 static $list = null;
142 if (is_array($list))
143 {
144 return $list;
145 }
146
147 $user = UserTable::getList(array(
148 'select' => array('EMAIL'),
149 'filter' => array(
150 '=ID' => array_slice(\CGroup::getGroupUser(1), 0, 200),
151 '=ACTIVE' => 'Y'
152 ),
153 'limit' => 1
154 ))->fetch();
155
156 if ($user && $user['EMAIL'])
157 {
158 $email = $user['EMAIL'];
159 }
160 else
161 {
162 $email = Option::get('main', 'email_from', '');
163 }
164
165 $list = array();
166 $intl = array(
167 'ru' => array(
168 'PHRASES' => array('COMPANY_NAME', 'IP_NAME'),
169 'FIELDS' => array(
170 array(
171 'CODE' => 'COMPANY_NAME',
172 'TYPE' => 'text',
173 'TAB' => 'text',
174 ),
175 array(
176 'CODE' => 'COMPANY_ADDRESS',
177 'TYPE' => 'text',
178 'TAB' => 'text',
179 ),
180 array(
181 'CODE' => 'PURPOSES',
182 'TYPE' => 'text',
183 'SHOW_BY_CHECKBOX' => true,
184 'TAB' => 'settings',
185 ),
186 array(
187 'CODE' => 'THIRD_PARTIES',
188 'TYPE' => 'text',
189 'SHOW_BY_CHECKBOX' => true,
190 'TAB' => 'settings',
191 ),
192 array(
193 'CODE' => 'EMAIL',
194 'TYPE' => 'string',
195 'PLACEHOLDER' => $email,
196 'DEFAULT_VALUE' => $email,
197 'TAB' => 'settings',
198 ),
199 ),
200 ),
201 'ua' => array(
202 'PHRASES' => array(),
203 'FIELDS' => array(
204 array(
205 'CODE' => 'COMPANY_NAME',
206 'TYPE' => 'text',
207 'TAB' => 'text',
208 ),
209 ),
210 ),
211 'kz' => array(
212 'PHRASES' => array('COMPANY_NAME', 'IP_NAME'),
213 'FIELDS' => array(
214 array(
215 'CODE' => 'COMPANY_NAME',
216 'TYPE' => 'text',
217 'TAB' => 'text',
218 ),
219 array(
220 'CODE' => 'COMPANY_ADDRESS',
221 'TYPE' => 'text',
222 'TAB' => 'text',
223 ),
224 array(
225 'CODE' => 'PURPOSES',
226 'TYPE' => 'text',
227 'SHOW_BY_CHECKBOX' => true,
228 'TAB' => 'settings',
229 ),
230 array(
231 'CODE' => 'THIRD_PARTIES',
232 'TYPE' => 'text',
233 'SHOW_BY_CHECKBOX' => true,
234 'TAB' => 'settings',
235 ),
236 array(
237 'CODE' => 'EMAIL',
238 'TYPE' => 'string',
239 'PLACEHOLDER' => $email,
240 'DEFAULT_VALUE' => $email,
241 'TAB' => 'settings',
242 ),
243 ),
244 ),
245 'by' => array(
246 'PHRASES' => array('COMPANY_NAME', 'IP_NAME'),
247 'FIELDS' => array(
248 array(
249 'CODE' => 'COMPANY_NAME',
250 'TYPE' => 'text',
251 'TAB' => 'text',
252 ),
253 array(
254 'CODE' => 'COMPANY_ADDRESS',
255 'TYPE' => 'text',
256 'TAB' => 'text',
257 ),
258 array(
259 'CODE' => 'PURPOSES',
260 'TYPE' => 'text',
261 'SHOW_BY_CHECKBOX' => true,
262 'TAB' => 'settings',
263 ),
264 array(
265 'CODE' => 'THIRD_PARTIES',
266 'TYPE' => 'text',
267 'SHOW_BY_CHECKBOX' => true,
268 'TAB' => 'settings',
269 ),
270 array(
271 'CODE' => 'EMAIL',
272 'TYPE' => 'string',
273 'PLACEHOLDER' => $email,
274 'DEFAULT_VALUE' => $email,
275 'TAB' => 'settings',
276 ),
277 ),
278 )
279 );
280
281 $messageMap = array(
282 'AGREEMENT_TEXT' => 'MAIN_USER_CONSENT_INTL_TEXT',
283 'LABEL_TEXT' => 'MAIN_USER_CONSENT_INTL_LABEL',
284 'FIELDS_HINT' => 'MAIN_USER_CONSENT_INTL_FIELDS_HINT',
285 'DESCRIPTION' => 'MAIN_USER_CONSENT_INTL_DESCRIPTION',
286 'NOTIFY_TEXT' => 'MAIN_USER_CONSENT_INTL_NOTIFY_TEXT',
287 );
288 $languages = self::getLanguages();
289 foreach ($languages as $languageId => $languageName)
290 {
291 $item = self::getLanguageMessages($languageId, $messageMap);
292 if (!$item['AGREEMENT_TEXT'])
293 {
294 continue;
295 }
296
297 $item['NAME'] = Loc::getMessage('MAIN_USER_CONSENT_INTL_NAME', array('%language_name%' => $languageName));
298 $item['BASE_LANGUAGE_ID'] = self::$virtualLanguageMap[$languageId] ?? $languageId;
299 $item['LANGUAGE_ID'] = $languageId;
300 $item['LANGUAGE_NAME'] = $languageName;
301
302 $item['PHRASES'] = array();
303 if (isset($intl[$languageId]['PHRASES']))
304 {
305 $phraseMap = array();
306 foreach ($intl[$languageId]['PHRASES'] as $phraseCode)
307 {
308 $phraseMap[$phraseCode] = "MAIN_USER_CONSENT_INTL_PHRASE_{$phraseCode}";
309 }
310 $item['PHRASES'] = self::getLanguageMessages($languageId, $phraseMap);
311 }
312
313 $item['FIELDS'] = array();
314 if (isset($intl[$languageId]['FIELDS']))
315 {
316 foreach ($intl[$languageId]['FIELDS'] as $field)
317 {
318 $messageFieldsMap = array(
319 'CAPTION' => "MAIN_USER_CONSENT_INTL_FIELD_{$field['CODE']}",
320 'PLACEHOLDER' => "MAIN_USER_CONSENT_INTL_FIELD_{$field['CODE']}_HINT",
321 'DEFAULT_VALUE' => "MAIN_USER_CONSENT_INTL_FIELD_{$field['CODE']}_DEFAULT",
322 );
323
324 $field = $field + self::getLanguageMessages($languageId, $messageFieldsMap);
325 if ($field['TYPE'] == 'text' && $field['DEFAULT_VALUE'])
326 {
327 $field['PLACEHOLDER'] .= "\n" . Loc::getMessage('MAIN_USER_CONSENT_INTL_HINT_FIELD_DEFAULT');
328 $field['PLACEHOLDER'] .= "\n" . $field['DEFAULT_VALUE'];
329 }
330
331 $item['FIELDS'][] = $field;
332 }
333 }
334
335 $list[] = $item;
336 }
337
338 return $list;
339 }
340
346 public static function getLanguages()
347 {
348 static $list = null;
349 if (is_array($list))
350 {
351 return $list;
352 }
353
354 $list = array();
355
356 // set virtual languages
357 foreach (self::$virtualLanguageMap as $virtualLanguageId => $languageId)
358 {
359 $languageName = Loc::getMessage('MAIN_USER_CONSENT_INTL_LANG_NAME_'.mb_strtoupper($virtualLanguageId));
360 if (!$languageName)
361 {
362 $languageName = $virtualLanguageId;
363 }
364 $list[$virtualLanguageId] = $languageName;
365 }
366
367 // read lang dirs
368 $dirLanguages = array();
369 $langDir = Application::getDocumentRoot() . '/bitrix/modules/main/lang/';
370 $dir = new IO\Directory($langDir);
371 if (!$dir->isExists())
372 {
373 return $list;
374 }
375
376 foreach($dir->getChildren() as $childDir)
377 {
378 if (!$childDir->isDirectory())
379 {
380 continue;
381 }
382
383 $dirLanguages[] = $childDir->getName();
384 }
385
386 if (empty($dirLanguages))
387 {
388 return $list;
389 }
390
391 // set languages from DB by dir languages
392 $listDb = LanguageTable::getList(array(
393 'select' => array('LID', 'NAME'),
394 'filter' => array(
395 '=LID' => $dirLanguages,
396 '=ACTIVE' => 'Y'
397 ),
398 'order' => array('SORT' => 'ASC')
399 ));
400 while ($item = $listDb->fetch())
401 {
402 $list[$item['LID']] = $item['NAME'];
403 }
404
405 return $list;
406 }
407
415 public static function getLanguageMessages($languageId, array $map = array())
416 {
417 // rewrite $languageId by real value from virtual language
418 $virtualLanguageId = null;
419 if (isset(self::$virtualLanguageMap[$languageId]))
420 {
421 $virtualLanguageId = $languageId;
422 $languageId = self::$virtualLanguageMap[$virtualLanguageId];
423 }
424
425 // load messages
426 $messages = Loc::loadLanguageFile(__FILE__, $languageId, true);
427 // set map by all message codes
428 if (empty($map))
429 {
430 $keys = array_keys($messages);
431 foreach ($keys as $key)
432 {
433 $map[$key] = $key;
434 }
435 }
436
437 // append postfix to message codes from virtual language
438 if ($virtualLanguageId)
439 {
440 $postfix = '_'.mb_strtoupper($virtualLanguageId);
441 foreach ($map as $itemKey => $messageKey)
442 {
443 if (mb_substr($itemKey, -mb_strlen($postfix)) == $postfix)
444 {
445 $oldItemKey = $itemKey;
446 $itemKey = mb_substr($itemKey, 0, -mb_strlen($postfix));
447 unset($map[$oldItemKey]);
448 }
449
450 if (mb_substr($messageKey, -mb_strlen($postfix)) != $postfix)
451 {
452 if (isset($messages[$messageKey . $postfix]))
453 {
454 $messageKey .= $postfix;
455 }
456 }
457
458 $map[$itemKey] = $messageKey;
459 }
460 }
461
462 // set messages by map
463 $item = array();
464 foreach ($map as $itemKey => $messageKey)
465 {
466 $message = $messages[$messageKey] ?? '';
467 $message = trim($message);
468 $message = $message == '-' ? '' : $message;
469
470 $item[$itemKey] = $message;
471 }
472
473 return $item;
474 }
475}
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())