Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
link.php
1<?php
2namespace Bitrix\Landing\Node;
3
5use \Bitrix\Main\Application;
6
8{
13 public static function getHandlerJS()
14 {
15 return 'BX.Landing.Node.Link';
16 }
17
23 protected static function isAllowedTarget($target)
24 {
25 return in_array($target, array('_self', '_blank', '_popup'));
26 }
27
32 protected static function allowedAttrs()
33 {
34 return array('data-embed', 'data-url');
35 }
36
41 protected static function isFrame(): bool
42 {
43 static $isIframe = null;
44
45 if ($isIframe === null)
46 {
47 $request = Application::getInstance()->getContext()->getRequest();
48 $isIframe = $request->get('IFRAME') == 'Y';
49 }
50
51 return $isIframe;
52 }
53
61 public static function saveNode(\Bitrix\Landing\Block $block, $selector, array $data): array
62 {
63 $result = [];
64 $manifest = $block->getManifest();
65 $globalSkipContent = false;
66 if ($manifest['nodes'][$selector]['skipContent'] ?? false)
67 {
68 $globalSkipContent = true;
69 }
70
71 $doc = $block->getDom();
72 $resultList = $doc->querySelectorAll($selector);
73 $valueBefore = static::getNode($block, $selector);
74 $isIframe = self::isFrame();
75
76 foreach ($data as $pos => $value)
77 {
78 $text = (isset($value['text']) && is_string($value['text'])) ? trim($value['text']) : '';
79 $href = (isset($value['href']) && is_string($value['href'])) ? trim($value['href']) : '';
80 $query = (isset($value['query']) && is_string($value['query'])) ? trim($value['query']) : '';
81 $target = (isset($value['target']) && is_string($value['target'])) ? trim(mb_strtolower($value['target'])) : '';
82 $attrs = isset($value['attrs']) ? (array)$value['attrs'] : array();
83 $skipContent = $globalSkipContent || (isset($value['skipContent']) ? (boolean)$value['skipContent'] : false);
84 $result[$pos]['attrs'] = [];
85
86 if ($query)
87 {
88 $href .= (mb_strpos($href, '?') === false && !$isIframe) ? '?' : '&';
89 $href .= $query;
90 }
91
92 if (isset($value['text']) && !$text)
93 {
94 $text = '&nbsp;';
95 }
96
97 if (isset($resultList[$pos]))
98 {
99 if (
100 $text &&
101 !$skipContent &&
102 trim($resultList[$pos]->getTextContent()) != ''
103 )
104 {
105 $text = \htmlspecialcharsbx($text);
106 $result[$pos]['content'] = $text;
107 $resultList[$pos]->setInnerHTML($text);
108 }
109
110 if ($href != '')
111 {
112 $result[$pos]['attrs']['href'] = $href;
113 $resultList[$pos]->setAttribute('href', $href);
114 }
115
116 if (self::isAllowedTarget($target))
117 {
118 $result[$pos]['attrs']['target'] = $target;
119 $resultList[$pos]->setAttribute('target', $target);
120 }
121
122 $allowedAttrs = self::allowedAttrs();
123 if (!empty($attrs))
124 {
125 foreach ($attrs as $code => $val)
126 {
127 if ($val && in_array($code, $allowedAttrs))
128 {
129 $result[$pos]['attrs'][$code] = $val;
130 $resultList[$pos]->setAttribute($code, $val);
131 }
132 }
133 }
134 else
135 {
136 foreach ($allowedAttrs as $attr)
137 {
138 $resultList[$pos]->removeAttribute($attr);
139 }
140 }
141
142 if (History::isActive())
143 {
144 $history = new History($block->getLandingId(), History::ENTITY_TYPE_LANDING);
145 $history->push('EDIT_LINK', [
146 'block' => $block,
147 'selector' => $selector,
148 'position' => (int)$pos,
149 'valueBefore' => $valueBefore[$pos],
150 'valueAfter' => $value,
151 ]);
152 }
153 }
154 }
155
156 return $result;
157 }
158
165 public static function getNode(\Bitrix\Landing\Block $block, $selector)
166 {
167 $data = array();
168 $doc = $block->getDom();
169 $manifest = $block->getManifest();
170 $resultList = $doc->querySelectorAll($selector);
171
172 foreach ($resultList as $pos => $res)
173 {
174 $data[$pos] = array(
175 'href' => $res->getAttribute('href'),
176 'target' => $res->getAttribute('target'),
177 'attrs' => array(
178 'data-embed' => $res->getAttribute('data-embed'),
179 'data-url' => $res->getAttribute('data-url')
180 )
181 );
182 if (
183 !isset($manifest['nodes'][$selector]['skipContent']) ||
184 $manifest['nodes'][$selector]['skipContent'] !== true
185 )
186 {
187 $text = \htmlspecialcharsback($res->getInnerHTML());
188 $text = str_replace('&amp;nbsp;', '', $text);
189 $data[$pos]['text'] = $text;
190 }
191 }
192
193 return $data;
194 }
195
202 public static function getSearchableNode($block, $selector)
203 {
204 $searchContent = [];
205
206 $nodes = self::getNode($block, $selector);
207 foreach ($nodes as $node)
208 {
209 if (!isset($node['text']))
210 {
211 continue;
212 }
213 $node['text'] = self::prepareSearchContent($node['text']);
214 if ($node['text'] && !in_array($node['text'], $searchContent))
215 {
216 $searchContent[] = $node['text'];
217 }
218 }
219
220 return $searchContent;
221 }
222
227 protected static function validateFieldDefinition(array $field)
228 {
229 $result = parent::validateFieldDefinition($field);
230 if (empty($result))
231 {
232 return null;
233 }
234
235 $field['actions'] = static::prepareActions($field);
236 if (empty($field['actions']))
237 {
238 return null;
239 }
240
241 $result['actions'] = $field['actions'];
242 return $result;
243 }
244
249 protected static function prepareActions(array $field)
250 {
251 if (empty($field['actions']) || !is_array($field['actions']))
252 {
253 return null;
254 }
255 $result = [];
256 $dublicate = [];
257 foreach ($field['actions'] as $row)
258 {
259 if (empty($row) || !is_array($row))
260 {
261 continue;
262 }
263 $row = array_change_key_case($row, CASE_LOWER);
264
265 $row['name'] = static::prepareStringValue($row, 'name');
266 $row['type'] = static::prepareStringValue($row, 'type');
267 if (empty($row['name']) || empty($row['type']))
268 {
269 continue;
270 }
271 if (isset($dublicate[$row['type']]))
272 {
273 continue;
274 }
275
276 $result[] = [
277 'type' => $row['type'],
278 'name' => $row['name']
279 ];
280 $dublicate[$row['type']] = true;
281 }
282
283 return (!empty($result) ? $result : null);
284 }
285}
static prepareSearchContent($value)
Definition node.php:51