Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
utils.php
1<?php
3
4use \Bitrix\Landing\Manager;
5use \Bitrix\Landing\Error;
6use \Bitrix\Landing\PublicActionResult;
7use \Bitrix\Main\Loader;
8use \Bitrix\Main\UrlPreview\UrlPreview;
9
10class Utils
11{
15 const TYPE_CATALOG = 'catalog';
16
20 const TYPE_CATALOG_ELEMENT = 'element';
21
25 const TYPE_CATALOG_SECTION = 'section';
26
30 const TYPE_CATALOG_ALL = 'all';
31
37 public static function getUrlPreview($url)
38 {
39 $result = new PublicActionResult();
40
41 if (is_string($url) && $url !== '')
42 {
43 $urlPreview = new UrlPreview();
44 $result->setResult(
45 $urlPreview->getMetadataByUrl($url)
46 );
47 }
48 else
49 {
50 $error = new Error;
51 $error->addError('EMPTY_URL', 'Empty URL');
52 $result->setError($error);
53 }
54
55 return $result;
56 }
57
66 public static function catalogSearch($query = null, $type = self::TYPE_CATALOG_ALL, $iblock = null, $siteId = null)
67 {
68 $publicResult = new PublicActionResult();
69
70 if (!$iblock)
71 {
72 \Bitrix\Landing\Hook::setEditMode(true);
73 $settings = \Bitrix\Landing\Hook\Page\Settings::getDataForSite(
74 $siteId
75 );
76 $iblockId = $settings['IBLOCK_ID'];
77 }
78 else
79 {
80 $iblockId = $iblock;
81 }
82
83 if (!Loader::includeModule('iblock'))
84 {
85 $publicResult->setResult([]);
86 return $publicResult;
87 }
88
89 $data = [];
90
91 // make some magic
92 $filter = [
93 'IBLOCK_ID' => $iblockId,
94 array_merge(
95 ['LOGIC' => 'AND'],
96 array_map(function($fragment) {
97 return ['%NAME' => trim($fragment)];
98 }, explode(' ', trim($query)))
99 )
100 ];
101
102 // search in all catalog or in element
103 if (
104 $type === self::TYPE_CATALOG_ALL ||
105 $type === self::TYPE_CATALOG_ELEMENT)
106 {
107 $order = [];
108 $groupBy = false;
109 $navParams = ['nTopCount' => 50];
110 $select = [
111 'ID', 'NAME', 'IBLOCK_SECTION_ID', 'DETAIL_PICTURE'
112 ];
113
114 $result = \CIBlockElement::getList(
115 $order, $filter, $groupBy, $navParams, $select
116 );
117 while ($item = $result->fetch())
118 {
119 $chain = [];
120 static::makeCatalogEntityNavChain(
121 $item['IBLOCK_SECTION_ID'],
122 $chain
123 );
124 $data[] = [
125 'name' => $item['NAME'],
126 'id' => $item['ID'],
127 'image' => \CFile::getPath($item['DETAIL_PICTURE']),
128 'type' => self::TYPE_CATALOG,
129 'subType' => self::TYPE_CATALOG_ELEMENT,
130 'chain' => $chain
131 ];
132 }
133 }
134 // search in all catalog or in section
135 if (
136 $type === self::TYPE_CATALOG_ALL ||
137 $type === self::TYPE_CATALOG_SECTION
138 )
139 {
140 $order = [];
141 $select = [
142 'ID', 'NAME', 'IBLOCK_SECTION_ID', 'DETAIL_PICTURE'
143 ];
144 $filter = [
145 'IBLOCK_ID' => $iblockId, '%NAME' => trim($query)
146 ];
147 $count = false;
148
149 $sectResult = \CIBlockSection::GetList($order, $filter, $count, $select);
150 while ($item = $sectResult->Fetch())
151 {
152 $chain = [];
153 static::makeCatalogEntityNavChain(
154 $item['IBLOCK_SECTION_ID'],
155 $chain
156 );
157 $data[] = [
158 'name' => trim($item['NAME']),
159 'id' => $item['ID'],
160 'image' => '',
161 'type' => self::TYPE_CATALOG,
162 'subType' => self::TYPE_CATALOG_SECTION,
163 'chain' => !empty($chain) ? $chain : ['/']
164 ];
165 }
166 }
167
168
169 $publicResult->setResult($data);
170
171 return $publicResult;
172 }
173
174
181 protected static function makeCatalogEntityNavChain($sectionId, array &$chain)
182 {
183 if ($sectionId !== null)
184 {
185 $section = self::getCatalogEntity(
186 $sectionId,
187 self::TYPE_CATALOG_SECTION
188 );
189 array_unshift($chain, $section['name']);
190 static::makeCatalogEntityNavChain(
191 $section['sectionId'],
192 $chain
193 );
194 }
195 }
196
203 protected static function getCatalogEntity($entityId, $entityType)
204 {
205 $result = null;
206 $entityId = (int)$entityId;
207
208 if (Loader::includeModule('iblock'))
209 {
210 $isElement = $entityType == self::TYPE_CATALOG_ELEMENT;
211 if ($isElement)
212 {
213 $res = \CIBlockElement::getList(
214 array(),
215 array(
216 'ID' => $entityId,
217 'CHECK_PERMISSIONS' => 'Y'
218 ),
219 false,
220 array(
221 'nTopCount' => 1
222 ),
223 array(
224 'ID', 'NAME', 'DETAIL_PICTURE', 'IBLOCK_SECTION_ID'
225 )
226 );
227 }
228 else
229 {
230 $res = \CIBlockSection::getList(
231 array(),
232 array(
233 'ID' => $entityId,
234 'CHECK_PERMISSIONS' => 'Y'
235 ),
236 false,
237 array(
238 'ID', 'NAME', 'IBLOCK_SECTION_ID'
239 ),
240 array(
241 'nTopCount' => 1
242 )
243 );
244 }
245 if ($entity = $res->fetch())
246 {
247 $chain = array();
248 static::makeCatalogEntityNavChain(
249 $entity['IBLOCK_SECTION_ID'],
250 $chain
251 );
252 $result = array(
253 'id' => $entity['ID'],
254 'name' => $entity['NAME'],
255 'sectionId' => $entity['IBLOCK_SECTION_ID'],
256 'image' => $isElement
257 ? \CFile::getPath($entity['DETAIL_PICTURE'])
258 : '',
259 'type' => self::TYPE_CATALOG,
260 'subType' => $entityType,
261 'chain' => $chain
262 );
263 }
264 }
265
266 return $result;
267 }
268
269
275 public static function getCatalogElement($elementId)
276 {
277 $response = new PublicActionResult();
278
279 $element = self::getCatalogEntity(
280 $elementId,
281 self::TYPE_CATALOG_ELEMENT
282 );
283
284 if ($element)
285 {
286 $response->setResult($element);
287 }
288 else
289 {
290 $response->setError(new Error());
291 }
292
293 return $response;
294 }
295
301 public static function getCatalogSection($sectionId)
302 {
303 $response = new PublicActionResult();
304
305 $element = self::getCatalogEntity(
306 $sectionId,
307 self::TYPE_CATALOG_SECTION
308 );
309
310 if ($element)
311 {
312 $response->setResult($element);
313 }
314 else
315 {
316 $response->setError(new Error());
317 }
318
319 return $response;
320 }
321
328 public static function getIblockURL($elementId, $urlType)
329 {
330 static $urls = array();
331 static $settings = array();
332
333 $elementId = (int)$elementId;
334 $key = (string)$urlType . '_' . $elementId;
335
336 if (isset($urls[$key]))
337 {
338 return $urls[$key];
339 }
340
341 if (!\Bitrix\Main\Loader::includeModule('iblock'))
342 {
343 return $urls[$key];
344 }
345
346 if (empty($settings))
347 {
348 \Bitrix\Landing\Hook::setEditMode(true);
349 $settings = \Bitrix\Landing\Hook\Page\Settings::getDataForSite();
350 }
351
352 $urls[$key] = '#system_catalog';
353 $iblockId = $settings['IBLOCK_ID'];
354
355 // build url
356 if ($urlType == 'detail' || $urlType == 'element')
357 {
358 // element additional info
359 $res = \Bitrix\Iblock\ElementTable::getList(array(
360 'select' => array(
361 'ID', 'CODE', 'IBLOCK_SECTION_ID'
362 ),
363 'filter' => array(
364 'ID' => $elementId,
365 'IBLOCK_ID' => $iblockId
366 )
367 ));
368 if (!($element = $res->fetch()))
369 {
370 return $urls[$key];
371 }
372 // build url
373 $urls[$key] .= 'item/' . $element['CODE'] . '/';
374 }
375 elseif ($urlType == 'section')
376 {
377 $res = \CIBlockSection::getNavChain(
378 $iblockId,
379 $elementId
380 );
381 while ($row = $res->fetch())
382 {
383 $urls[$key] .= $row['CODE'] . '/';
384 }
385 }
386
387 return $urls[$key];
388 }
389
396 public static function checkMultiFeature(array $code, array $params = [])
397 {
398 $result = new PublicActionResult();
399
400 $result->setResult(Manager::checkMultiFeature(
401 (array)$code,
402 $params
403 ));
404
405 return $result;
406 }
407
413 public static function saveSettings(array $settings)
414 {
415 static $internal = true;
416
417 $result = new PublicActionResult();
418 $result->setResult(true);
419 $allowedKeys = ['google_images_key'];
420
421 foreach ($settings as $key => $value)
422 {
423 if (in_array($key, $allowedKeys))
424 {
425 \Bitrix\Main\Config\Option::set('landing', $key, $value);
426 }
427 }
428
429 return $result;
430 }
431
437 public static function isTrue($value)
438 {
439 static $internal = true;
440
441 if (
442 $value === 'true' ||
443 $value === true ||
444 (int)$value === 1
445 )
446 {
447 return true;
448 }
449
450 return false;
451 }
452
453 public static function getUserOption(string $name): ?PublicActionResult
454 {
455 $whiteList = ['color_field_recent_colors'];
456
457 $response = new PublicActionResult();
458 if (in_array($name, $whiteList, true))
459 {
460 $response->setResult(\CUserOptions::getOption('landing', $name, null));
461 }
462 else
463 {
464 $error = new Error;
465 $error->addError('WRONG_OPTION', 'Option name is not allowed');
466 $response->setError($error);
467 }
468
469 return $response;
470 }
471}
addError($code, $message='')
Definition error.php:18
static checkMultiFeature(array $features, array $params=[])
Definition manager.php:802
static makeCatalogEntityNavChain($sectionId, array &$chain)
Definition utils.php:181
static getIblockURL($elementId, $urlType)
Definition utils.php:328
static getCatalogElement($elementId)
Definition utils.php:275
static getCatalogSection($sectionId)
Definition utils.php:301
static checkMultiFeature(array $code, array $params=[])
Definition utils.php:396
static saveSettings(array $settings)
Definition utils.php:413
static catalogSearch($query=null, $type=self::TYPE_CATALOG_ALL, $iblock=null, $siteId=null)
Definition utils.php:66
static getCatalogEntity($entityId, $entityType)
Definition utils.php:203
static getUserOption(string $name)
Definition utils.php:453