Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
commentparser.php
1<?php
2
3namespace Bitrix\UI\Timeline;
4
7
9{
10 protected $parser;
11 protected $userFields;
12
13 public function __construct(array $userFields = [])
14 {
15 $this->userFields = $userFields;
16 }
17
18 public function setUserFields(array $userFields): CommentParser
19 {
20 $this->userFields = $userFields;
21
22 return $this;
23 }
24
25 public function getHtml(string $text): string
26 {
27 $rules = array(
28 "HTML" => "N",
29 "ALIGN" => "Y",
30 "ANCHOR" => "Y", "BIU" => "Y",
31 "IMG" => "Y", "QUOTE" => "Y",
32 "CODE" => "Y", "FONT" => "Y",
33 "LIST" => "Y", "SMILES" => "Y",
34 "NL2BR" => "Y", "MULTIPLE_BR" => "N",
35 "VIDEO" => "Y", "LOG_VIDEO" => "N",
36 "SHORT_ANCHOR" => "Y"
37 );
38
39 $parser = $this->getParser();
40
41 if($parser instanceof \blogTextParser)
42 {
43 $result = $parser->convert($text, [], $rules);
44 }
45 elseif($parser instanceof \forumTextParser)
46 {
47 $result = $parser->convert($text, $rules, "html", []);
48 }
49 elseif($parser instanceof \logTextParser)
50 {
51 $result = $parser->convert($text, [], $rules);
52 }
53 else
54 {
55 $result = $parser->convertText($text);
56 }
57
58 $result = \Bitrix\Main\Text\Emoji::decode($result);
59 $result = preg_replace('/\[[^\]]+\]/', '', $result);
60
61 return $result;
62 }
63
64 public function getText(string $text): string
65 {
66 $parser = $this->getParser();
67 if($parser instanceof \blogTextParser || $parser instanceof \forumTextParser)
68 {
69 $result = $parser::killAllTags($text);
70 }
71 else
72 {
73 $result = $parser::clearAllTags($text);
74 }
75
76 return preg_replace('/\[[^\]]+\]/', '', $result);
77 }
78
79 public function getMentionedUserIds(string $text): array
80 {
81 $mentionedUserIds = [];
82
83 if(preg_match_all("/\[user\s*=\s*([^\]]*)\](.+?)\[\/user\]/is" . BX_UTF_PCRE_MODIFIER, $text, $matches) && is_array($matches[1]))
84 {
85 $mentionedUserIds = $matches[1];
86 $mentionedUserIds = array_unique($mentionedUserIds);
87 foreach($mentionedUserIds as &$mentionedUserId)
88 {
89 $mentionedUserId = (int) $mentionedUserId;
90 }
91 }
92
93 return $mentionedUserIds;
94 }
95
96 protected function getParser(): \CTextParser
97 {
98 $languageId = Loc::getCurrentLang();
99 if($this->parser === null && Loader::includeModule('blog'))
100 {
101 $this->parser = new \blogTextParser($languageId);
102 }
103 if($this->parser === null && Loader::includeModule('forum'))
104 {
105 $this->parser = new \forumTextParser($languageId);
106 }
107 if($this->parser === null && Loader::includeModule('socialnetwork'))
108 {
109 $this->parser = new \logTextParser($languageId);
110 }
111 if($this->parser === null)
112 {
113 $this->parser = new \CTextParser();
114 }
115
116 if(is_array($this->userFields))
117 {
118 $this->parser->arUserfields = $this->userFields;
119 }
120
121 return $this->parser;
122 }
123}
__construct(array $userFields=[])