Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
themefonts.php
1<?php
2
4
13use Bitrix\Main\Entity\Query;
14use Bitrix\Main\Page\Asset;
18
22class ThemeFonts extends Hook\Page
23{
24 protected const BASE_HTML_SIZE = '14px';
25
30 protected function getMap(): array
31 {
32 return [
33 'USE' => new Field\Checkbox('USE', [
34 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_USE_2'),
35 ]),
36 'CODE_H' => new Field\Text('CODE_H', [
37 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_BASE_2'),
38 'default' => 'Open Sans',
39 ]),
40 'CODE' => new Field\Text('CODE', [
41 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_BASE_2'),
42 'default' => 'Open Sans',
43 ]),
44 'SIZE' => new Field\Select('SIZE', [
45 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_SIZE'),
46 'default' => '1',
47 'options' => [
48 '0.92857' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_12'),
49 '1' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_14'),
50 '1.14286' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_16'),
51 ],
52 ]),
53 'COLOR' => new Field\Text('COLOR', [
54 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_COLOR'),
55 'help' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_COLOR_HELP'),
56 ]),
57 'COLOR_H' => new Field\Text('COLOR_H', [
58 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_COLOR_H'),
59 'help' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_COLOR_HELP_H'),
60 ]),
61 'LINE_HEIGHT' => new Field\Select('LINE_HEIGHT', [
62 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_LINE_HEIGHT'),
63 'default' => '1.6',
64 'options' => self::getLineHeightOptions(),
65 ]),
66 'FONT_WEIGHT' => new Field\Select('FONT_WEIGHT', [
67 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_WEIGHT_2'),
68 'default' => '400',
69 'options' => self::getFontWeightOptions(),
70 'help' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_WEIGHT_HELP_2'),
71 ]),
72 'FONT_WEIGHT_H' => new Field\Select('FONT_WEIGHT_H', [
73 'title' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_WEIGHT_H_2'),
74 'default' => '400',
75 'options' => self::getFontWeightOptions(),
76 'help' => Loc::getMessage('LNDNGHOOK_THEMEFONTS_FONT_WEIGHT_HELP_H'),
77 ]),
78 ];
79 }
80
81 protected static function getLineHeightOptions(): array
82 {
83 return [
84 '0.7' => '0.7',
85 '0.8' => '0.8',
86 '0.9' => '0.9',
87 '1' => '1',
88 '1.1' => '1.1',
89 '1.2' => '1.2',
90 '1.3' => '1.3',
91 '1.4' => '1.4',
92 '1.5' => '1.5',
93 '1.6' => '1.6',
94 '1.7' => '1.7',
95 '1.8' => '1.8',
96 '2' => '2',
97 ];
98 }
99
100 protected static function getFontWeightOptions(): array
101 {
102 return [
103 '100' => '100',
104 '200' => '200',
105 '300' => '300',
106 '400' => '400',
107 '500' => '500',
108 '600' => '600',
109 '700' => '700',
110 '800' => '800',
111 '900' => '900',
112 ];
113 }
114
119 public function enabled(): bool
120 {
121 if ($this->issetCustomExec())
122 {
123 return true;
124 }
125
126 if ($this->isPage())
127 {
128 return $this->fields['USE']->getValue() === 'Y';
129 }
130
131 return true; //always enable on site to default value
132 }
133
138 public function exec(): void
139 {
140 if ($this->execCustom())
141 {
142 return;
143 }
144
145 $this->setThemeFont();
146 $this->setHFontTheme();
147 $this->setSize();
148 $this->setColors();
149 $this->setTypo();
150 }
151
152 protected function getField(string $name): ?string
153 {
154 if ($field = $this->fields[$name]->getValue())
155 {
156 return HtmlFilter::encode(trim($field));
157 }
158
159 return self::getDefaultValues()[$name] ?? null;
160 }
161
162 protected static function getDefaultValues(): array
163 {
164 return [
165 'CODE' => 'Open Sans',
166 'CODE_H' => 'Open Sans',
167 'SIZE' => '1',
168 'LINE_HEIGHT' => '1.6',
169 'FONT_WEIGHT' => '400',
170 'FONT_WEIGHT_H' => '400',
171 ];
172 }
173
177 protected function setThemeFont(): void
178 {
179 $font = $this->getField('CODE');
180 $font = self::convertFontName($font);
181
182 $assets = Assets\Manager::getInstance();
183 if ($this->fields['CODE']->getValue() !== null)
184 {
185 $assets->addString(
186 "<style>
187 body {
188 --landing-font-family: {$font}
189 }
190 </style>"
191 );
192 }
193
194 $assets->addString(self::getFontLink($font));
195 $assets->addString(
196 '<style>
197 body {
198 font-weight: 400;
199 font-family: ' . $font . ';
200 -webkit-font-smoothing: antialiased;
201 -moz-osx-font-smoothing: grayscale;
202 -moz-font-feature-settings: "liga", "kern";
203 text-rendering: optimizelegibility;
204 }
205 </style>'
206 );
207 }
208
212 protected function setHFontTheme(): void
213 {
214 $font = $this->getField('CODE_H');
215 $font = self::convertFontName($font);
216
217 $assets = Assets\Manager::getInstance();
218 $assets->addString(self::getFontLink($font));
219 $assets->addString(
220 '<style>
221 h1, h2, h3, h4, h5, h6 {
222 font-family: ' . $font . ';
223 }
224 </style>'
225 );
226 }
227
233 protected static function convertFontName(string $fontName): string
234 {
235 $fontName = str_replace(['g-font-', '-', 'ibm ', 'pt '], ['', ' ', 'IBM ', 'PT '], $fontName);
236
237 $pattern = [
238 '/sc(?:(?![a-z]))/i',
239 '/jp(?:(?![a-z]))/i',
240 '/kr(?:(?![a-z]))/i',
241 '/tc(?:(?![a-z]))/i',
242 ];
243 $replace = ['SC', 'JP', 'KR', 'TC'];
244 $fontNameNew = preg_filter($pattern, $replace, $fontName);
245 if ($fontNameNew)
246 {
247 $fontName = $fontNameNew;
248 }
249
250 $fontName = ucwords($fontName);
251
252 return $fontName;
253 }
254
255 protected static function getFontLink(string $font): string
256 {
257 $domain = 'fonts.googleapis.com';
258 if (Loader::includeModule('ui'))
259 {
260 $domain = Fonts\Proxy::resolveDomain(Manager::getZone());
261 }
262
263 $fontLink = "<link rel=\"stylesheet\" href=\"https://{$domain}/css2?family=";
264 $fontLink .= str_replace(" ", "+", $font);
265 $fontLink .= ':wght@100;200;300;400;500;600;700;800;900">';
266
267 return $fontLink;
268 }
269
273 protected function setSize(): void
274 {
275 $size = $this->getField('SIZE');
276
277 $assets = Assets\Manager::getInstance();
278 $assets->addString(
279 '<style>
280 html {font-size: ' . self::BASE_HTML_SIZE . ';}
281 body {font-size: ' . $size . 'rem;}
282 .g-font-size-default {font-size: ' . $size . 'rem;}
283 </style>'
284 );
285 }
286
290 protected function setColors(): void
291 {
292 $color = $this->getField('COLOR');
293 $hColor = $this->getField('COLOR_H');
294 $css = '';
295
296 if ($color && Theme::isHex($color))
297 {
298 $css .= "--theme-color-main: {$color} !important;";
299 }
300 if ($hColor && Theme::isHex($hColor))
301 {
302 $css .= "--theme-color-title: {$hColor} !important;";
303 }
304
305 if (!empty($css))
306 {
307 Asset::getInstance()->addString(
308 "<style>:root {{$css}}</style>",
309 false,
310 AssetLocation::BEFORE_CSS
311 )
312 ;
313 }
314 }
315
319 protected function setTypo(): void
320 {
321 $lineHeight = $this->getField('LINE_HEIGHT');
322 $fontWeight = $this->getField('FONT_WEIGHT');
323 $hFontWeight = $this->getField('FONT_WEIGHT_H');
324
325 $mainClass = Landing::getEditMode() ? '.landing-edit-mode' : '.landing-public-mode';
326 $assets = Assets\Manager::getInstance();
327 $assets->addString(
328 "<style>
329 main{$mainClass} {
330 line-height: {$lineHeight};
331 font-weight: {$fontWeight};
332 }
333
334 {$mainClass} .h1, {$mainClass} .h2, {$mainClass} .h3, {$mainClass} .h4,
335 {$mainClass} .h5, {$mainClass} .h6, {$mainClass} .h7,
336 {$mainClass} h1, {$mainClass} h2, {$mainClass} h3, {$mainClass} h4,
337 {$mainClass} h5, {$mainClass} h6 {
338 font-weight: {$hFontWeight};
339 }
340 </style>"
341 );
342 }
343
353 public static function migrateFromTypoThemes(int $lid, int $siteId): void
354 {
355 $migrations = [
356 '1construction' => [
357 'CODE' => 'Alegreya Sans',
358 'CODE_H' => 'Alegreya Sans',
359 'SIZE' => '1.14286',
360 ],
361 '2business' => [
362 'CODE' => 'Roboto',
363 'CODE_H' => 'Roboto',
364 'SIZE' => '1',
365 ],
366 '3corporate' => [
367 'CODE' => 'Roboto',
368 'CODE_H' => 'Roboto',
369 'SIZE' => '1',
370 ],
371 'accounting' => [
372 'CODE' => 'Open Sans',
373 'CODE_H' => 'Open Sans',
374 'SIZE' => '1',
375 ],
376 'agency' => [
377 'CODE' => 'Roboto',
378 'CODE_H' => 'Roboto',
379 'SIZE' => '1',
380 ],
381 'app' => [
382 'CODE' => 'Open Sans',
383 'CODE_H' => 'Open Sans',
384 'SIZE' => '1.14286',
385 ],
386 'architecture' => [
387 'CODE' => 'Open Sans',
388 'CODE_H' => 'Open Sans',
389 'SIZE' => '1',
390 ],
391 'charity' => [
392 'CODE' => 'Open Sans',
393 'CODE_H' => 'Open Sans',
394 'SIZE' => '0.92857',
395 ],
396 'consulting' => [
397 'CODE' => 'Open Sans',
398 'CODE_H' => 'Open Sans',
399 'SIZE' => '1',
400 ],
401 'courses' => [
402 'CODE' => 'Alegreya Sans',
403 'CODE_H' => 'Alegreya Sans',
404 'SIZE' => '1',
405 ],
406 'event' => [
407 'CODE' => 'Open Sans',
408 'CODE_H' => 'Open Sans',
409 'SIZE' => '1.14286',
410 ],
411 'gym' => [
412 'CODE' => 'Roboto',
413 'CODE_H' => 'Roboto',
414 'SIZE' => '1',
415 ],
416 'lawyer' => [
417 'CODE' => 'Roboto',
418 'CODE_H' => 'Open Sans',
419 'SIZE' => '1',
420 ],
421 'music' => [
422 'CODE' => 'Open Sans',
423 'CODE_H' => 'Open Sans',
424 'SIZE' => '0.92857',
425 ],
426 'photography' => [
427 'CODE' => 'Roboto',
428 'CODE_H' => 'Roboto',
429 'SIZE' => '0.92857',
430 ],
431 'real-estate' => [
432 'CODE' => 'Open Sans',
433 'CODE_H' => 'Open Sans',
434 'SIZE' => '1',
435 ],
436 'restaurant' => [
437 'CODE' => 'Montserrat',
438 'CODE_H' => 'Montserrat',
439 'SIZE' => '0.92857',
440 ],
441 'shipping' => [
442 'CODE' => 'Open Sans',
443 'CODE_H' => 'Open Sans',
444 'SIZE' => '1',
445 ],
446 'spa' => [
447 'CODE' => 'Open Sans',
448 'CODE_H' => 'Open Sans',
449 'SIZE' => '1',
450 ],
451 'travel' => [
452 'CODE' => 'Roboto',
453 'CODE_H' => 'Roboto',
454 'SIZE' => '1',
455 ],
456 'wedding' => [
457 'CODE' => 'Montserrat',
458 'CODE_H' => 'Montserrat',
459 'SIZE' => '1',
460 ],
461 ];
462
463 $queryOld = HookDataTable::query()
464 ->addSelect('ID')
465 ->addSelect('VALUE')
466 ->addSelect('PUBLIC')
467 ->addSelect('ENTITY_ID')
468 ->addSelect('ENTITY_TYPE')
469 ->where('HOOK', 'THEME')
470 ->where('CODE', 'CODE_TYPO')
471 ->where(Query::filter()
472 ->logic('or')
473 ->where(Query::filter()
474 ->where('ENTITY_ID', $lid)
475 ->where('ENTITY_TYPE', Hook::ENTITY_TYPE_LANDING)
476 )
477 ->where(Query::filter()
478 ->where('ENTITY_ID', $siteId)
479 ->where('ENTITY_TYPE', Hook::ENTITY_TYPE_SITE)
480 )
481 );
482
483 while ($old = $queryOld->fetch())
484 {
485 // check exist
486 $existing = HookDataTable::query()
487 ->addSelect('ID')
488 ->where('HOOK', 'THEMEFONTS')
489 ->where('ENTITY_ID', $old['ENTITY_ID'])
490 ->where('ENTITY_TYPE', $old['ENTITY_TYPE'])
491 ->where('PUBLIC', $old['PUBLIC'])
492 ->fetch();
493 if (!$existing)
494 {
495 //process
496 $migrations[$old['VALUE']]['USE'] = 'Y';
497 foreach ($migrations[$old['VALUE']] as $code => $value)
498 {
499 HookDataTable::add(
500 [
501 'ENTITY_ID' => $old['ENTITY_ID'],
502 'ENTITY_TYPE' => $old['ENTITY_TYPE'],
503 'PUBLIC' => $old['PUBLIC'],
504 'HOOK' => 'THEMEFONTS',
505 'CODE' => $code,
506 'VALUE' => $value,
507 ]
508 );
509 }
510 }
511
512 HookDataTable::delete($old['ID']);
513 }
514 }
515}
static migrateFromTypoThemes(int $lid, int $siteId)
static convertFontName(string $fontName)
static isHex(string $color)
Definition theme.php:509
const ENTITY_TYPE_SITE
Definition hook.php:20
const ENTITY_TYPE_LANDING
Definition hook.php:25
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29