Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
landing.php
1<?php
3
4use \Bitrix\Landing\Block;
5use \Bitrix\Landing\Hook;
6use \Bitrix\Landing\Landing as LandingCore;
7use \Bitrix\Landing\Landing\Cache;
8
10{
16 protected function getMetadata(array $ids)
17 {
18 $images = [];
19 $res = \Bitrix\Landing\Internals\HookDataTable::getList([
20 'select' => [
21 'VALUE', 'ENTITY_ID', 'CODE'
22 ],
23 'filter' => [
24 '=HOOK' => 'METAOG',
25 '=CODE' => ['IMAGE', 'DESCRIPTION'],
26 '=PUBLIC' => Hook::getEditMode() ? 'N' : 'Y',
27 '=ENTITY_TYPE' => Hook::ENTITY_TYPE_LANDING,
28 'ENTITY_ID' => $ids
29 ]
30 ]);
31 while ($row = $res->fetch())
32 {
33 if (!isset($images[$row['ENTITY_ID']]))
34 {
35 $images[$row['ENTITY_ID']] = [];
36 }
37 if ($row['CODE'] == 'IMAGE')
38 {
39 if (intval($row['VALUE']) > 0)
40 {
41 $row['VALUE'] = \Bitrix\Landing\File::getFilePath($row['VALUE']);
42 }
43 }
44 $images[$row['ENTITY_ID']][$row['CODE']] = $row['VALUE'];
45 }
46
47 return $images;
48 }
49
54 protected function getSearchQuery()
55 {
56 static $currentRequest = null;
57
58 if ($currentRequest === null)
59 {
60 $context = \Bitrix\Main\Application::getInstance()->getContext();
61 $currentRequest = $context->getRequest();
62 }
63
64 return trim($currentRequest->get('q'));
65 }
66
73 protected function getSearchSnippet(string $query, string $content): string
74 {
75 $isUtf = defined('BX_UTF') && BX_UTF === true;
76
77 if (!$isUtf)
78 {
79 [$content, $query] = \Bitrix\Main\Text\Encoding::convertEncoding(
80 [$content, $query], SITE_CHARSET, 'UTF-8'
81 );
82 }
83
84 $phrases = explode(' ', $query);
85 \trimArr($phrases, true);
86 $newContent = '';
87 $snippetOffset = 50;
88
89 foreach ($phrases as $phrase)
90 {
91 $phrasePos = mb_strpos(mb_strtolower($content), mb_strtolower($phrase));
92 if ($phrasePos === false)
93 {
94 continue;
95 }
96 $newContent .= (($phrasePos > $snippetOffset) ? ' ...' : ' ') .
97 mb_substr(
98 $content,
99 ($phrasePos > $snippetOffset) ? $phrasePos - $snippetOffset : 0,
100 $phrasePos + mb_strlen($phrase) + $snippetOffset
101 ) .
102 '... ';
103 }
104
105 if (!$isUtf)
106 {
107 $newContent = \Bitrix\Main\Text\Encoding::convertEncoding(
108 $newContent, 'UTF-8', SITE_CHARSET
109 );
110 }
111
112 return $newContent;
113 }
114
119 public function getElementListData()
120 {
121 $this->seo->clear();
122 $needPreviewPicture = false;
123 $needPreviewText = false;
124 $needLink = true;//always
125
126 // select
127 $select = $this->getPreparedSelectFields();
128 if (empty($select))
129 {
130 return [];
131 }
132
133 // filter
134 $filter = $this->getInternalFilter();
135 $contextFilter = $this->getOptionsValue('context_filter');
136 $cache = $this->getOptionsValue('cache');
137 if (empty($filter))
138 {
139 $filter = [];
140 }
141 if (isset($contextFilter['SITE_ID']))
142 {
143 $filter['SITE_ID'] = $contextFilter['SITE_ID'];
144 }
145 if (isset($contextFilter['LANDING_ACTIVE']))
146 {
147 $filter['=ACTIVE'] = $contextFilter['LANDING_ACTIVE'];
148 }
149 $filter['==AREAS.ID'] = null;
150
151 // select, order
152 $order = [];
153 $select[] = 'ID';
154 $rawOrder = $this->getOrder();
155 if (isset($rawOrder['by']) && isset($rawOrder['order']))
156 {
157 $order[$rawOrder['by']] = $rawOrder['order'];
158 if (!in_array($rawOrder['by'], $select))
159 {
160 $select[] = $rawOrder['by'];
161 }
162 }
163 foreach ($select as $i => $code)
164 {
165 if ($code == 'IMAGE')
166 {
167 $needPreviewPicture = true;
168 unset($select[$i]);
169 }
170 else if ($code == 'DESCRIPTION')
171 {
172 $needPreviewText = true;
173 unset($select[$i]);
174 }
175 else if ($code == 'LINK')
176 {
177 $needLink = true;
178 unset($select[$i]);
179 }
180 }
181
182 // limit
183 $limit = $this->getLimit();
184 if ($limit <= 0)
185 {
186 $limit = 10;
187 }
188
189 $searchContent = [];
190 $query = $this->getSearchQuery();
191 if ($query)
192 {
193 if ($cache instanceof \CPHPCache)
194 {
195 $cache->abortDataCache();
196 }
197
198 if (mb_strlen($query) < 3)
199 {
200 return [];
201 }
202
203 // search in blocks
204 $blockFilter = [];
205 if (isset($filter['SITE_ID']))
206 {
207 $blockFilter['LANDING.SITE_ID'] = $filter['SITE_ID'];
208 }
209 $blocks = Block::search(
210 $query,
211 $blockFilter,
212 ['LID', 'ID', 'SEARCH_CONTENT']
213 );
214 $landingBlocksIds = [];
215 foreach ($blocks as $block)
216 {
217 $searchContent[$block['LID']] = $this->getSearchSnippet($query, $block['SEARCH_CONTENT']);
218 if (!$searchContent[$block['LID']])
219 {
220 unset($searchContent[$block['LID']]);
221 }
222 $landingBlocksIds[] = $block['LID'];
223 }
224
225 // merge filter with search query
226 $filter[] = [
227 'LOGIC' => 'OR',
228 'TITLE' => '%' . $query . '%',
229 '*%SEARCH_CONTENT' => $query,
230 'ID' => $landingBlocksIds ? $landingBlocksIds : [-1]
231 ];
232 }
233
234 // get data
235 $result = [];
236 $res = LandingCore::getList([
237 'select' => $select,
238 'filter' => $filter,
239 'order' => $order,
240 'limit' => $limit
241 ]);
242 while ($row = $res->fetch())
243 {
244 Cache::register($row['ID']);
245 $result[$row['ID']] = [
246 'TITLE' => \htmlspecialcharsbx($row['TITLE'])
247 ];
248 }
249
250 // get meta data
251 $metaData = [];
252 if (
253 $needPreviewPicture ||
254 $needPreviewText
255 )
256 {
257 $metaData = $this->getMetadata(
258 array_keys($result)
259 );
260 }
261
262 // and feel result data with meta data
263 foreach ($result as $id => &$item)
264 {
265 if (
266 $needPreviewPicture &&
267 isset($metaData[$id]['IMAGE'])
268 )
269 {
270 $item['IMAGE'] = [
271 'src' => $metaData[$id]['IMAGE'],
272 'alt' => $item['TITLE'] ?? ''
273 ];
274 }
275 if ($needPreviewText)
276 {
277 if (isset($searchContent[$id]))
278 {
279 $item['DESCRIPTION'] = \htmlspecialcharsbx($searchContent[$id]);
280 }
281 else if (isset($metaData[$id]['DESCRIPTION']))
282 {
283 $item['DESCRIPTION'] = \htmlspecialcharsbx($metaData[$id]['DESCRIPTION']);
284 }
285 }
286 if ($needLink)
287 {
288 $item['LINK'] = '#landing' . $id;
289 }
290 if ($query)
291 {
292 $item['_GET'] = [
293 'q' => $query
294 ];
295 }
296 }
297
298 return array_values($result);
299 }
300
306 public function getElementData($element)
307 {
308 $this->seo->clear();
309
310 $element = intval($element);
311 if ($element <= 0)
312 {
313 return [];
314 }
315
316 // select
317 $select = $this->getPreparedSelectFields();
318 if (empty($select))
319 {
320 return [];
321 }
322
323 // filter
324 $filter = $this->getInternalFilter();
325 if (empty($filter))
326 {
327 return [];
328 }
329 $filter['ID'] = $element;
330 $select[] = 'ID';
331
332 // get data
333 $res = LandingCore::getList([
334 'select' => $select,
335 'filter' => $filter,
336 ]);
337 $row = $res->fetch();
338 if (empty($row))
339 {
340 return [];
341 }
342
343 Cache::register($row['ID']);
344 $this->seo->setTitle($row['TITLE']);
345
346 return [$row];
347 }
348}
getSearchSnippet(string $query, string $content)
Definition landing.php:73
static getEditMode()
Definition hook.php:241
const ENTITY_TYPE_LANDING
Definition hook.php:25
static getList(array $parameters=array())