Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
theme.php
1<?php
2
4
10use Bitrix\Main\Page\Asset;
14
15Loc::loadMessages(__FILE__);
16
17class Theme extends Page
18{
19 protected const DEFAULT_COLOR = '#34bcf2';
20
25 protected function getMap()
26 {
27 return [
28 'CODE' => new Field\Select('CODE', [
29 'title' => Loc::getMessage('LANDING_HOOK_THEMECODE_NEW'),
30 'options' => array_merge(
31 [
32 '' => [
33 'color' => '#f0f0f0',
34 ]
35 ],
36 self::getColorCodes()
37 ),
38 ]),
39 'USE' => new Field\Checkbox('USE', [
40 'title' => Loc::getMessage('LANDING_HOOK_THEME_CUSTOM_USE')
41 ]),
42 'COLOR' => new Field\Text('COLOR', [
43 'title' => Loc::getMessage('LANDING_HOOK_THEME_CUSTOM_COLOR')
44 ]),
45 ];
46 }
47
52 public static function getColorCodes(): array
53 {
54 static $colors = [
55 '2business' => [
56 'color' => '#3949a0',
57 'main' => '#333333',
58 'base' => true,
59 ],
60 'gym' => [
61 'color' => '#6b7de0',
62 'main' => '#444444',
63 ],
64 '3corporate' => [
65 'color' => '#6ab8ee',
66 'main' => '#12222d',
67 'secondary' => '#fafbfc',
68 'base' => true,
69 ],
70 'wiki-dark' => [
71 'color' => '#60e7f5',
72 ],
73 'app' => [
74 'color' => '#4fd2c2',
75 'main' => '#999999',
76 'colorTitle' => '#111111',
77 'base' => true,
78 ],
79 'consulting' => [
80 'color' => '#21a79b',
81 'main' => '#464c5e',
82 'secondary' => '#f5fafa',
83 ],
84 'courses' => [
85 'color' => '#6bda95',
86 'main' => '#999999',
87 'colorTitle' => '#000000',
88 ],
89 'accounting' => [
90 'color' => '#a5c33c',
91 'main' => '#999999',
92 'base' => true,
93 ],
94 'spa' => [
95 'color' => '#9dba04',
96 'main' => '#999999',
97 'colorTitle' => '#000000',
98 ],
99 'charity' => [
100 'color' => '#f5f219',
101 'main' => '#999999',
102 'colorTitle' => '#111111',
103 ],
104 '1construction' => [
105 'color' => '#f7b70b',
106 'main' => '#a7a7a7',
107 'base' => true,
108 ],
109 'twentyFourth' => [
110 'color' => '#AD8F47',
111 ],
112 'travel' => [
113 'color' => '#ee4136',
114 'main' => '#333333',
115 ],
116 'architecture' => [
117 'color' => '#c94645',
118 'main' => '#7d7d8f',
119 'colorTitle' => '#383339',
120 ],
121 'event' => [
122 'color' => '#f73859',
123 'main' => '#979aa7',
124 'secondary' => '#1a2e39',
125 'colorTitle' => '#151826',
126 ],
127 'lawyer' => [
128 'color' => '#e74c3c',
129 'main' => '#444444',
130 'colorTitle' => '#4e4353',
131 ],
132 'real-estate' => [
133 'color' => '#f74c3c',
134 'main' => '#1a2e39',
135 'secondary' => '#1a2e39',
136 'base' => true,
137 ],
138 'restaurant' => [
139 'color' => '#e6125d',
140 'main' => '#444444',
141 'colorTitle' => '#222222',
142 ],
143 'shipping' => [
144 'color' => '#ff0000',
145 'main' => '#444444',
146 'colorTitle' => '#2c2c2c',
147 ],
148 'agency' => [
149 'color' => '#fe6466',
150 'main' => '#a49da6',
151 'colorTitle' => '#383339',
152 ],
153 'music' => [
154 'color' => '#fe6476',
155 'main' => '#999999',
156 'colorTitle' => '#2f2f2f',
157 ],
158 'wedding' => [
159 'color' => '#d65779',
160 'main' => '#444444',
161 'colorTitle' => '#222222',
162 ],
163 'twentyThird' => [
164 'color' => '#A861AB',
165 ],
166 'photography' => [
167 'color' => '#333333',
168 'main' => '#444444',
169 'colorTitle' => '#333333',
170 'base' => true,
171 'baseInSettings' => false,
172 ],
173 ];
174
175 $event = new Event('landing', 'onGetThemeColors', [
176 'colors' => $colors
177 ]);
178 $event->send();
179 foreach ($event->getResults() as $result)
180 {
181 if ($result->getType() != \Bitrix\Main\EventResult::ERROR)
182 {
183 if (($modified = $result->getModified()))
184 {
185 if (isset($modified['colors']))
186 {
187 $colors = $modified['colors'];
188 }
189 }
190 }
191 }
192
193 if (
194 !is_array($colors) ||
195 empty($colors)
196 )
197 {
198 $colors = [
199 '1construction' => [
200 'color' => '#f7b70b',
201 'base' => true
202 ]
203 ];
204 }
205
206 return $colors;
207 }
208
213 public static function getAllColorCodes(): array
214 {
215 $colors = self::getColorCodes();
216 $allColors = [];
217 foreach ($colors as $colorItem)
218 {
219 if (isset($colorItem['color']))
220 {
221 $allColors[] = $colorItem['color'];
222 }
223 }
224 return $allColors;
225 }
226
231 public static function getStartColorCodes(): array
232 {
233 $colors = self::getColorCodes();
234 $startColors = [];
235 foreach ($colors as $colorItem)
236 {
237 if (isset($colorItem['base']) && $colorItem['base'] === true)
238 {
239 $startColors[] = $colorItem['color'];
240 }
241 }
242 return $startColors;
243 }
244
250 protected static function getThemeCodeByColor(string $hexColor): ?string
251 {
252 $colors = self::getColorCodes();
253 foreach($colors as $code => $color)
254 {
255 if($color['color'] === $hexColor)
256 {
257 return $code;
258 }
259 }
260
261 return null;
262 }
263
268 public function enabled()
269 {
270 if ($this->issetCustomExec())
271 {
272 return true;
273 }
274
275 if ($this->isPage())
276 {
277 if (
278 $this->fields['CODE']->getValue()
279 && !$this->fields['COLOR']->getValue()
280 )
281 {
282 return true;
283 }
284 return $this->fields['USE']->getValue() === 'Y';
285 }
286
287 return true;
288 }
289
294 protected static function convertHexToRgb(string $hexColor): array
295 {
296 if ($hexColor[0] !== '#')
297 {
298 $hexColor = '#'.$hexColor;
299 }
300 if (strlen($hexColor) === 4)
301 {
302 $hexColor =
303 $hexColor[0] . $hexColor[1] . $hexColor[1] . $hexColor[2] . $hexColor[2] . $hexColor[3] . $hexColor[3];
304 }
305 if (strlen($hexColor) !== 7)
306 {
307 $hexColor = self::DEFAULT_COLOR;
308 }
309
310 return [
311 hexdec(substr($hexColor, 1, 2)),
312 hexdec(substr($hexColor, 3, 2)),
313 hexdec(substr($hexColor, 5, 2)),
314 ];
315 }
316
323 protected static function convertRgbToHsl(int $red, int $green, int $blue): array
324 {
325 $red /= 255;
326 $green /= 255;
327 $blue /= 255;
328 $max = max($red, $green, $blue);
329 $min = min($red, $green, $blue);
330 $lightness = ($max + $min) / 2;
331 $d = $max - $min;
332 if($d == 0)
333 {
334 $hue = $saturation = 0;
335 }
336 else
337 {
338 $hue = 0;
339 $saturation = $d / (1 - abs(2 * $lightness - 1));
340 switch($max)
341 {
342 case $red:
343 $hue = 60 * fmod((($green - $blue) / $d), 6);
344 if ($blue > $green)
345 {
346 $hue += 360;
347 }
348 break;
349
350 case $green:
351 $hue = 60 * (($blue - $red) / $d + 2);
352 break;
353
354 case $blue:
355 $hue = 60 * (($red - $green) / $d + 4);
356 break;
357
358 default:
359 break;
360 }
361 }
362
363 return [
364 round($hue, 2),
365 round($saturation, 2) * 100,
366 round($lightness, 2) * 100,
367 ];
368 }
369
374 public function exec(): void
375 {
376 $defaultColors = self::getColorCodes();
377
378 // get color from request or from settings
379 $request = Application::getInstance()->getContext()->getRequest();
380 if ($request->get('color'))
381 {
382 $colorHex = $request->get('color');
383 }
384 elseif (
385 ($themeCodeFromRequest = $request->get('theme'))
386 && array_key_exists($themeCodeFromRequest, $defaultColors)
387 )
388 {
389 $themeCode = $themeCodeFromRequest;
390 $colorHex = $defaultColors[$themeCodeFromRequest]['color'];
391 }
392 else
393 {
394 $colorHex = HtmlFilter::encode(trim($this->fields['COLOR']->getValue()));
395 if (!$colorHex)
396 {
397 $themeCode = HtmlFilter::encode(trim($this->fields['CODE']->getValue()));
398 $colorHex = $themeCode ? $defaultColors[$themeCode]['color'] : self::DEFAULT_COLOR;
399 }
400 }
401
402 if (!is_string($colorHex))
403 {
404 $colorHex = '';
405 }
406 $colorHex = self::prepareColor($colorHex);
407
408 $restrictionCode = Restriction\Hook::getRestrictionCodeByHookCode('THEME');
409 if (
410 !Restriction\Manager::isAllowed($restrictionCode)
411 && !self::getThemeCodeByColor($colorHex)
412 )
413 {
414 $colorHex = self::DEFAULT_COLOR;
415 }
416
417 // print
418 $rgbColor = self::convertHexToRgb($colorHex);
419 $rgbTemplate = $rgbColor[0] . ', ' . $rgbColor[1] . ', ' . $rgbColor[2];
420 $hslColor = self::convertRgbToHsl($rgbColor[0], $rgbColor[1], $rgbColor[2]);
421
422 if (
423 isset($themeCode)
424 || ($themeCode = self::getThemeCodeByColor($colorHex))
425 )
426 {
427 $colorMain = $defaultColors[$themeCode]['main'] ?? null;
428 if ($defaultColors[$themeCode]['secondary'] ?? null)
429 {
430 $colorSecondary = $defaultColors[$themeCode]['secondary'];
431 }
432 if ($defaultColors[$themeCode]['colorTitle'] ?? null)
433 {
434 $colorTitle = $defaultColors[$themeCode]['colorTitle'];
435 }
436 }
437 $colorMain = $colorMain ?? 'hsl('.$hslColor[0].', 20%, 20%)';
438 $colorSecondary = $colorSecondary ?? 'hsl('.$hslColor[0].', 20%, 80%)';
439 $colorTitle = $colorTitle ?? $colorMain;
440
441 if ($hslColor[2] > 60)
442 {
443 $colorStrictInverseFromPrimary = '#000000';
444 }
445 else
446 {
447 $colorStrictInverseFromPrimary = '#ffffff';
448 }
449
450 Asset::getInstance()->addString(
451 '<style type="text/css">
452 :root {
453 --primary: ' . $colorHex . ' !important' .';
454 --primary-darken-1: hsl(' . $hslColor[0] . ', ' . $hslColor[1] . '%, ' . min($hslColor[2] - 2, 100) . '%)' . ';
455 --primary-darken-2: hsl(' . $hslColor[0] . ', ' . $hslColor[1] . '%, ' . min($hslColor[2] - 5, 100) . '%)' . ';
456 --primary-darken-3: hsl(' . $hslColor[0] . ', ' . $hslColor[1] . '%, ' . min($hslColor[2] - 10, 100) . '%)' . ';
457 --primary-lighten-1: hsl(' . $hslColor[0] . ', ' . $hslColor[1] . '%, ' . max($hslColor[2] + 10, 0) . '%)' . ';
458 --primary-opacity-0: rgba('.$rgbTemplate.', 0);
459 --primary-opacity-0_05: rgba('.$rgbTemplate.', 0.05);
460 --primary-opacity-0_1: rgba('.$rgbTemplate.', 0.1);
461 --primary-opacity-0_15: rgba('.$rgbTemplate.', 0.15);
462 --primary-opacity-0_2: rgba('.$rgbTemplate.', 0.2);
463 --primary-opacity-0_25: rgba('.$rgbTemplate.', 0.25);
464 --primary-opacity-0_3: rgba('.$rgbTemplate.', 0.3);
465 --primary-opacity-0_35: rgba('.$rgbTemplate.', 0.35);
466 --primary-opacity-0_4: rgba('.$rgbTemplate.', 0.4);
467 --primary-opacity-0_45: rgba('.$rgbTemplate.', 0.45);
468 --primary-opacity-0_5: rgba('.$rgbTemplate.', 0.5);
469 --primary-opacity-0_55: rgba('.$rgbTemplate.', 0.55);
470 --primary-opacity-0_6: rgba('.$rgbTemplate.', 0.6);
471 --primary-opacity-0_65: rgba('.$rgbTemplate.', 0.65);
472 --primary-opacity-0_7: rgba('.$rgbTemplate.', 0.7);
473 --primary-opacity-0_75: rgba('.$rgbTemplate.', 0.75);
474 --primary-opacity-0_8: rgba('.$rgbTemplate.', 0.8);
475 --primary-opacity-0_85: rgba('.$rgbTemplate.', 0.85);
476 --primary-opacity-0_9: rgba('.$rgbTemplate.', 0.9);
477 --primary-opacity-0_95: rgba('.$rgbTemplate.', 0.95);
478 --theme-color-main: ' . $colorMain . ';
479 --theme-color-secondary: ' . $colorSecondary . ';
480 --theme-color-title: ' . $colorTitle . ';
481 --theme-color-strict-inverse: ' . $colorStrictInverseFromPrimary . ';
482 }
483 </style>',
484 false,
485 AssetLocation::BEFORE_CSS
486 );
487 }
488
489 public static function prepareColor(string $color): string
490 {
491 $color = HtmlFilter::encode(trim($color));
492
493 if($color[0] !== '#')
494 {
495 $color = '#' . $color;
496 }
497 if(!self::isHex($color))
498 {
499 return self::DEFAULT_COLOR;
500 }
501 if(mb_strlen($color) === 4)
502 {
503 $color = $color[0] . $color[1] . $color[1] . $color[2] . $color[2] . $color[3] . $color[3];
504 }
505
506 return $color;
507 }
508
509 public static function isHex(string $color): bool
510 {
511 return (bool)preg_match('/^#([\da-f]{3}){1,2}$/i', $color);
512 }
513}
static prepareColor(string $color)
Definition theme.php:489
static convertHexToRgb(string $hexColor)
Definition theme.php:294
static isHex(string $color)
Definition theme.php:509
static getThemeCodeByColor(string $hexColor)
Definition theme.php:250
static convertRgbToHsl(int $red, int $green, int $blue)
Definition theme.php:323
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29