Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
icon.php
1<?php
3
4use \Bitrix\Landing\Block;
5use \Bitrix\Landing\Node;
6use \Bitrix\Landing\Config;
7use \Bitrix\Landing\Manager;
8use \Bitrix\Landing\Assets;
9use \Bitrix\Landing\Internals\BlockTable;
10use \Bitrix\Main\IO\File;
11
12class Icon
13{
17 const CONTENT_ICON_FILE_NAME = 'content.css';
18
22 const RULE_ICON_FILE_NAME = 'style.css';
23
27 const ICON_FONT_FILE_NAME = 'font.woff';
28 const ICON_FONT_FILE_NAME_2 = 'font.woff2';
29
30 protected const VENDOR_UPDATES = [
31 'fa' => 'far',
32 ];
33 protected const ICON_UPDATES = [
34 'fa' => [
35 'fa-500px' => ['vendor' => 'fab'],
36 'fa-adn' => ['vendor' => 'fab'],
37 'fa-amazon' => ['vendor' => 'fab'],
38 'fa-android' => ['vendor' => 'fab'],
39 'fa-angellist' => ['vendor' => 'fab'],
40 'fa-apple' => ['vendor' => 'fab'],
41 'fa-bandcamp' => ['vendor' => 'fab'],
42 'fa-behance' => ['vendor' => 'fab'],
43 'fa-behance-square' => ['vendor' => 'fab'],
44 'fa-bitbucket' => ['vendor' => 'fab'],
45 'fa-bitbucket-square' => [
46 'vendor' => 'fab',
47 'content' => '\\f171',
48 ],
49 'fa-bitcoin' => [
50 'vendor' => 'fab',
51 'content' => '\\f379',
52 ],
53 'fa-black-tie' => ['vendor' => 'fab'],
54 'fa-bluetooth' => ['vendor' => 'fab'],
55 'fa-bluetooth-b' => ['vendor' => 'fab'],
56 'fa-btc' => ['vendor' => 'fab'],
57 'fa-buysellads' => ['vendor' => 'fab'],
58 'fa-cc-amex' => ['vendor' => 'fab'],
59 'fa-cc-diners-club' => ['vendor' => 'fab'],
60 'fa-cc-discover' => ['vendor' => 'fab'],
61 'fa-cc-jcb' => ['vendor' => 'fab'],
62 'fa-cc-mastercard' => ['vendor' => 'fab'],
63 'fa-cc-paypal' => ['vendor' => 'fab'],
64 'fa-cc-stripe' => ['vendor' => 'fab'],
65 'fa-cc-visa' => ['vendor' => 'fab'],
66 'fa-chrome' => ['vendor' => 'fab'],
67 'fa-codepen' => ['vendor' => 'fab'],
68 'fa-codiepie' => ['vendor' => 'fab'],
69 'fa-connectdevelop' => ['vendor' => 'fab'],
70 'fa-contao' => ['vendor' => 'fab'],
71 'fa-creative-commons' => ['vendor' => 'fab'],
72 'fa-css3' => ['vendor' => 'fab'],
73 'fa-dashcube' => ['vendor' => 'fab'],
74 'fa-delicious' => ['vendor' => 'fab'],
75 'fa-deviantart' => ['vendor' => 'fab'],
76 'fa-digg' => ['vendor' => 'fab'],
77 'fa-dribbble' => ['vendor' => 'fab'],
78 'fa-dropbox' => ['vendor' => 'fab'],
79 'fa-drupal' => ['vendor' => 'fab'],
80 'fa-edge' => ['vendor' => 'fab'],
81 'fa-eercast' => [
82 'vendor' => 'fab',
83 'content' => '\\f2da',
84 ],
85 'fa-empire' => ['vendor' => 'fab'],
86 'fa-envira' => ['vendor' => 'fab'],
87 'fa-etsy' => ['vendor' => 'fab'],
88 'fa-expeditedssl' => ['vendor' => 'fab'],
89 'fa-fa' => [
90 'vendor' => 'fab',
91 'content' => '\\f2b4',
92 ],
93 'fa-facebook' => ['vendor' => 'fab'],
94 'fa-facebook-f' => [
95 'vendor' => 'fab',
96 'content' => '\\f39e',
97 ],
98 'fa-facebook-official' => [
99 'vendor' => 'fab',
100 'content' => '\\f09a',
101 ],
102 'fa-facebook-square' => ['vendor' => 'fab'],
103 'fa-firefox' => ['vendor' => 'fab'],
104 'fa-first-order' => ['vendor' => 'fab'],
105 'fa-flickr' => ['vendor' => 'fab'],
106 'fa-font-awesome' => ['vendor' => 'fab'],
107 'fa-fonticons' => ['vendor' => 'fab'],
108 'fa-fort-awesome' => ['vendor' => 'fab'],
109 'fa-forumbee' => ['vendor' => 'fab'],
110 'fa-foursquare' => ['vendor' => 'fab'],
111 'fa-free-code-camp' => ['vendor' => 'fab'],
112 'fa-ge' => [
113 'vendor' => 'fab',
114 'content' => '\\f1d1',
115 ],
116 'fa-get-pocket' => ['vendor' => 'fab'],
117 'fa-gg' => ['vendor' => 'fab'],
118 'fa-gg-circle' => ['vendor' => 'fab'],
119 'fa-git' => ['vendor' => 'fab'],
120 'fa-git-square' => ['vendor' => 'fab'],
121 'fa-github' => ['vendor' => 'fab'],
122 'fa-github-alt' => ['vendor' => 'fab'],
123 'fa-github-square' => ['vendor' => 'fab'],
124 'fa-gitlab' => ['vendor' => 'fab'],
125 'fa-gittip' => [
126 'vendor' => 'fab',
127 'content' => '\\f184',
128 ],
129 'fa-glide' => ['vendor' => 'fab'],
130 'fa-glide-g' => ['vendor' => 'fab'],
131 'fa-google' => ['vendor' => 'fab'],
132 'fa-google-plus' => [
133 'vendor' => 'fab',
134 'content' => '\\f2b3',
135 ],
136 'fa-google-plus-circle' => [
137 'vendor' => 'fab',
138 'content' => '\\f2b3',
139 ],
140 'fa-google-plus-official' => [
141 'vendor' => 'fab',
142 'content' => '\\f2b3',
143 ],
144 'fa-google-plus-square' => ['vendor' => 'fab'],
145 'fa-google-wallet' => ['vendor' => 'fab'],
146 'fa-gratipay' => ['vendor' => 'fab'],
147 'fa-grav' => ['vendor' => 'fab'],
148 'fa-hacker-news' => ['vendor' => 'fab'],
149 'fa-houzz' => ['vendor' => 'fab'],
150 'fa-html5' => ['vendor' => 'fab'],
151 'fa-imdb' => ['vendor' => 'fab'],
152 'fa-instagram' => ['vendor' => 'fab'],
153 'fa-internet-explorer' => ['vendor' => 'fab'],
154 'fa-ioxhost' => ['vendor' => 'fab'],
155 'fa-joomla' => ['vendor' => 'fab'],
156 'fa-jsfiddle' => ['vendor' => 'fab'],
157 'fa-lastfm' => ['vendor' => 'fab'],
158 'fa-lastfm-square' => ['vendor' => 'fab'],
159 'fa-leanpub' => ['vendor' => 'fab'],
160 'fa-linkedin' => [
161 'vendor' => 'fab',
162 'content' => '\\f08c',
163 ],
164 'fa-linkedin-square' => [
165 'vendor' => 'fab',
166 'content' => '\\f08c',
167 ],
168 'fa-linode' => ['vendor' => 'fab'],
169 'fa-linux' => ['vendor' => 'fab'],
170 'fa-maxcdn' => ['vendor' => 'fab'],
171 'fa-meanpath' => [
172 'vendor' => 'fab',
173 'content' => '\\f2b4',
174 ],
175 'fa-medium' => ['vendor' => 'fab'],
176 'fa-meetup' => ['vendor' => 'fab'],
177 'fa-mixcloud' => ['vendor' => 'fab'],
178 'fa-modx' => ['vendor' => 'fab'],
179 'fa-odnoklassniki' => ['vendor' => 'fab'],
180 'fa-odnoklassniki-square' => ['vendor' => 'fab'],
181 'fa-opencart' => ['vendor' => 'fab'],
182 'fa-openid' => ['vendor' => 'fab'],
183 'fa-opera' => ['vendor' => 'fab'],
184 'fa-optin-monster' => ['vendor' => 'fab'],
185 'fa-pagelines' => ['vendor' => 'fab'],
186 'fa-paypal' => ['vendor' => 'fab'],
187 'fa-pied-piper' => ['vendor' => 'fab'],
188 'fa-pied-piper-alt' => ['vendor' => 'fab'],
189 'fa-pied-piper-pp' => ['vendor' => 'fab'],
190 'fa-pinterest' => ['vendor' => 'fab'],
191 'fa-pinterest-p' => ['vendor' => 'fab'],
192 'fa-pinterest-square' => ['vendor' => 'fab'],
193 'fa-product-hunt' => ['vendor' => 'fab'],
194 'fa-qq' => ['vendor' => 'fab'],
195 'fa-quora' => ['vendor' => 'fab'],
196 'fa-ra' => [
197 'vendor' => 'fab',
198 'content' => '\\f1d0',
199 ],
200 'fa-ravelry' => ['vendor' => 'fab'],
201 'fa-rebel' => ['vendor' => 'fab'],
202 'fa-reddit' => ['vendor' => 'fab'],
203 'fa-reddit-alien' => ['vendor' => 'fab'],
204 'fa-reddit-square' => ['vendor' => 'fab'],
205 'fa-renren' => ['vendor' => 'fab'],
206 'fa-resistance' => [
207 'vendor' => 'fab',
208 'content' => '\\f1d0',
209 ],
210 'fa-safari' => ['vendor' => 'fab'],
211 'fa-scribd' => ['vendor' => 'fab'],
212 'fa-sellsy' => ['vendor' => 'fab'],
213 'fa-shirtsinbulk' => ['vendor' => 'fab'],
214 'fa-simplybuilt' => ['vendor' => 'fab'],
215 'fa-skyatlas' => ['vendor' => 'fab'],
216 'fa-skype' => ['vendor' => 'fab'],
217 'fa-slack' => ['vendor' => 'fab'],
218 'fa-slideshare' => ['vendor' => 'fab'],
219 'fa-snapchat' => ['vendor' => 'fab'],
220 'fa-snapchat-ghost' => [
221 'vendor' => 'fab',
222 'content' => '\\f2ab',
223 ],
224 'fa-snapchat-square' => ['vendor' => 'fab'],
225 'fa-soundcloud' => ['vendor' => 'fab'],
226 'fa-spotify' => ['vendor' => 'fab'],
227 'fa-stack-exchange' => ['vendor' => 'fab'],
228 'fa-stack-overflow' => ['vendor' => 'fab'],
229 'fa-steam' => ['vendor' => 'fab'],
230 'fa-steam-square' => ['vendor' => 'fab'],
231 'fa-stumbleupon' => ['vendor' => 'fab'],
232 'fa-stumbleupon-circle' => ['vendor' => 'fab'],
233 'fa-superpowers' => ['vendor' => 'fab'],
234 'fa-telegram' => ['vendor' => 'fab'],
235 'fa-tencent-weibo' => ['vendor' => 'fab'],
236 'fa-themeisle' => ['vendor' => 'fab'],
237 'fa-trello' => ['vendor' => 'fab'],
238 'fa-tumblr' => ['vendor' => 'fab'],
239 'fa-tumblr-square' => ['vendor' => 'fab'],
240 'fa-twitch' => ['vendor' => 'fab'],
241 'fa-twitter' => ['vendor' => 'fab'],
242 'fa-twitter-square' => ['vendor' => 'fab'],
243 'fa-usb' => ['vendor' => 'fab'],
244 'fa-viacoin' => ['vendor' => 'fab'],
245 'fa-viadeo' => ['vendor' => 'fab'],
246 'fa-viadeo-square' => ['vendor' => 'fab'],
247 'fa-vimeo' => [
248 'vendor' => 'fab',
249 'content' => '\\f40a',
250 ],
251 'fa-vimeo-square' => ['vendor' => 'fab'],
252 'fa-vine' => ['vendor' => 'fab'],
253 'fa-vk' => ['vendor' => 'fab'],
254 'fa-wechat' => [
255 'vendor' => 'fab',
256 'content' => '\\f1d7',
257 ],
258 'fa-weibo' => ['vendor' => 'fab'],
259 'fa-weixin' => ['vendor' => 'fab'],
260 'fa-whatsapp' => ['vendor' => 'fab'],
261 'fa-wikipedia-w' => ['vendor' => 'fab'],
262 'fa-windows' => ['vendor' => 'fab'],
263 'fa-wordpress' => ['vendor' => 'fab'],
264 'fa-wpbeginner' => ['vendor' => 'fab'],
265 'fa-wpexplorer' => ['vendor' => 'fab'],
266 'fa-wpforms' => ['vendor' => 'fab'],
267 'fa-xing' => ['vendor' => 'fab'],
268 'fa-xing-square' => ['vendor' => 'fab'],
269 'fa-y-combinator' => ['vendor' => 'fab'],
270 'fa-y-combinator-square' => [
271 'vendor' => 'fab',
272 'content' => '\\f1d4',
273 ],
274 'fa-yahoo' => ['vendor' => 'fab'],
275 'fa-yc' => [
276 'vendor' => 'fab',
277 'content' => '\\f1d4',
278 ],
279 'fa-yc-square' => [
280 'vendor' => 'fab',
281 'content' => '\\f1d4',
282 ],
283 'fa-yelp' => ['vendor' => 'fab'],
284 'fa-yoast' => ['vendor' => 'fab'],
285 'fa-youtube' => ['vendor' => 'fab'],
286 'fa-youtube-play' => [
287 'vendor' => 'fab',
288 'content' => '\\f167',
289 ],
290 'fa-youtube-square' => [
291 'vendor' => 'fab',
292 'content' => '\\f431',
293 ],
294
295 'fa-plus' => ['content' => '\2b'],
296 'fa-usd' => ['content' => '\24'],
297 'fa-dollar' => ['content' => '\24'],
298 'fa-rupee' => ['content' => '\e1bc'],
299 'fa-inr' => ['content' => '\e1bc'],
300 'fa-wheelchair-alt' => ['content' => '\\e2ce',],
301 ],
302 ];
303
309 protected static function getIconsPath(string $vendorName): ?string
310 {
311 $iconSrc = Config::get('icon_src');
312 $iconSrc = Manager::getDocRoot() . $iconSrc . $vendorName;
313 if (is_dir($iconSrc))
314 {
315 return $iconSrc;
316 }
317
318 return null;
319 }
320
326 protected static function getIconsContentByVendor(string $vendorName): array
327 {
328 static $vendorContent = [];
329
330 if (!array_key_exists($vendorName, $vendorContent))
331 {
332 $vendorContent[$vendorName] = [];
333 $path = self::getIconsPath($vendorName);
334 if ($path)
335 {
336 $cssFileName = $path . '/' . self::CONTENT_ICON_FILE_NAME;
337 if (File::isFileExists($cssFileName))
338 {
339 $cssContent = File::getFileContents($cssFileName);
340 if ($cssContent)
341 {
342 $classPrefix = $vendorName;
343 $iconVendorsConfig = Config::get('icon_vendors_config');
344 if ($iconVendorsConfig && isset ($iconVendorsConfig[$vendorName]['class_prefix']))
345 {
346 $classPrefix = $iconVendorsConfig[$vendorName]['class_prefix'];
347 }
348 $found = preg_match_all(
349 '/\.(' . $classPrefix . '-[^:]+):{1,2}before\s*{\s*content:\s*["\']((?:\\\\.|[^\\\\])*?)["\'];\s*}/',
350 $cssContent,
351 $matches
352 );
353 if ($found)
354 {
355 foreach ($matches[1] as $i => $match)
356 {
357 $vendorContent[$vendorName][$match] = $matches[2][$i];
358 }
359 }
360 }
361 }
362 }
363 }
364
365 return $vendorContent[$vendorName];
366 }
367
374 protected static function getIconContentByClass(string $className, string $vendorName): ?string
375 {
376 $contentAll = self::getIconsContentByVendor($vendorName);
377
378 return $contentAll[$className] ?? null;
379 }
380
386 protected static function saveAssets(Block $block): void
387 {
388 $iconSrc = Config::get('icon_src');
389 $iconVendors = Config::get('icon_vendors');
390 $blockContent = $block->getContent();
391
392 if (!$iconSrc || !$iconVendors || !$blockContent)
393 {
394 return;
395 }
396
397 $assetsIcon = [];
398 $iconVendors = (array)$iconVendors;
399 $vendorsStr = '(' . implode('|', $iconVendors) . ')';
400 $prefixesStr = $vendorsStr;
401 // special vendors
402 $iconVendorsConfig = Config::get('icon_vendors_config');
403 if ($iconVendorsConfig && !empty($iconVendorsConfig))
404 {
405 $classPrefixes = $iconVendors;
406 foreach ($iconVendorsConfig as $vendor => $config)
407 {
408 $classPrefixes[array_search($vendor, $classPrefixes, true)] = $config['class_prefix'];
409 }
410 $prefixesStr = '(' . implode('|', array_unique($classPrefixes)) . ')';
411 }
412 $found = preg_match_all(
413 '/(?<=[\s"])' . $vendorsStr . '?\s*(' . $prefixesStr . '-([^\s"\/\\\]+))/s',
414 $blockContent,
415 $matches
416 );
417 if ($found)
418 {
419 foreach ($matches[0] as $i => $full)
420 {
421 $vendor = $matches[1][$i]
422 ? trim($matches[1][$i])
423 : trim($matches[3][$i]);
424 $class = trim($matches[2][$i]);
425 [$vendor, $class] = self::updateIconsBeforeSave($vendor, $class);
426 if (!isset($assetsIcon[$vendor]))
427 {
428 $assetsIcon[$vendor] = [];
429 }
430 $assetsIcon[$vendor][$class] = self::getIconContentByClass(
431 $class,
432 $vendor
433 );
434 if ($assetsIcon[$vendor][$class] === null)
435 {
436 unset($assetsIcon[$vendor][$class]);
437 }
438 if (!$assetsIcon[$vendor])
439 {
440 unset($assetsIcon[$vendor]);
441 }
442 }
443 }
444
445 $block->saveAssets([
446 'icon' => $assetsIcon
447 ]);
448 }
449
455 public static function processing(Block $block): void
456 {
457 // find assets always, because block can use icon not only as icon-node, but also just in html
458 self::saveAssets($block);
459 }
460
466 public static function processingLanding(int $landingId): void
467 {
468 $res = BlockTable::getList([
469 'select' => [
470 'ID'
471 ],
472 'filter' => [
473 'LID' => $landingId,
474 '=DELETED' => 'N'
475 ]
476 ]);
477 while ($row = $res->fetch())
478 {
479 $block = new Block($row['ID']);
480 self::processing($block);
481 $block->save();
482 }
483 }
484
490 public static function view(Block $block): void
491 {
492 $blockAssets = $block->getAssets();
493 if (isset($blockAssets['icon']) && !empty($blockAssets['icon']))
494 {
495 $assetsManager = Assets\Manager::getInstance();
496 $iconSrc = Config::get('icon_src');
497 if (!$iconSrc)
498 {
499 return;
500 }
501 $stylesString = '<style>';
502 $blockAssets['icon'] = self::updateIconsBeforeView($blockAssets['icon']);
503 foreach ($blockAssets['icon'] as $vendorName => $icons)
504 {
505 $fontFile = $iconSrc . $vendorName . '/' . self::ICON_FONT_FILE_NAME;
506 $assetsManager->addAsset($fontFile);
507 $fontFile2 = $iconSrc . $vendorName . '/' . self::ICON_FONT_FILE_NAME_2;
508 $assetsManager->addAsset($fontFile2);
509
510 $stylesFile = $iconSrc . $vendorName . '/' . self::RULE_ICON_FILE_NAME;
511 $assetsManager->addAsset($stylesFile);
512
513 foreach ($icons as $className => $content)
514 {
515 $stylesString .= '.' . $className . ':before{content:"' . $content . '";}';
516 }
517 }
518 $stylesString .= '</style>';
519 $assetsManager->addString($stylesString);
520 }
521 }
522
523 protected static function updateIconsBeforeView($iconData): array
524 {
525 $newIcons = $iconData;
526
527 foreach ($iconData as $vendor => $icons)
528 {
529 if (!isset(self::VENDOR_UPDATES[$vendor]))
530 {
531 continue;
532 }
533
534 $newVendor = self::VENDOR_UPDATES[$vendor];
535
536 foreach ($icons as $icon => $content)
537 {
538 if (isset(self::ICON_UPDATES[$vendor]))
539 {
540 $currVendor = self::ICON_UPDATES[$vendor][$icon]['vendor'] ?? $newVendor;
541 $newContent = self::ICON_UPDATES[$vendor][$icon]['content'] ?? $content;
542 $newIcons[$currVendor][$icon] = $newContent;
543
544 unset($newIcons[$vendor][$icon]);
545 if (empty($newIcons[$vendor]))
546 {
547 unset($newIcons[$vendor]);
548 }
549 }
550 }
551 }
552
553 return $newIcons;
554 }
555
556 protected static function updateIconsBeforeSave(string $vendor, string $class): array
557 {
558 $newVendor = self::VENDOR_UPDATES[$vendor] ?? $vendor;
559 $newClass = $class;
560 if (isset(self::ICON_UPDATES[$vendor]))
561 {
562 $newVendor = self::ICON_UPDATES[$vendor][$class]['vendor'] ?? $newVendor;
563 }
564
565 return [$newVendor, $newClass];
566 }
567}
static saveAssets(Block $block)
Definition icon.php:386
static updateIconsBeforeView($iconData)
Definition icon.php:523
static processingLanding(int $landingId)
Definition icon.php:466
static processing(Block $block)
Definition icon.php:455
static getIconsPath(string $vendorName)
Definition icon.php:309
static getIconContentByClass(string $className, string $vendorName)
Definition icon.php:374
static getIconsContentByVendor(string $vendorName)
Definition icon.php:326
static updateIconsBeforeSave(string $vendor, string $class)
Definition icon.php:556
saveAssets(array $assets)
Definition block.php:3067