<?php namespace Lazalka; use Bitrix\Main\Data\Cache; use Bitrix\Main\Loader; class CatalogFaq { public const IBLOCK_CODE = 'lz_catalog_faq'; public const PROP = 'PAGE_URL'; private static $iblockId; private static $propId; private static $registered = false; public static function register(): void { if (self::$registered) { return; } self::$registered = true; AddEventHandler('iblock', 'OnBeforeIBlockElementAdd', [self::class, 'onBeforeSave']); AddEventHandler('iblock', 'OnBeforeIBlockElementUpdate', [self::class, 'onBeforeSave']); AddEventHandler('iblock', 'OnAfterIBlockElementAdd', [self::class, 'onAfterSave']); AddEventHandler('iblock', 'OnAfterIBlockElementUpdate', [self::class, 'onAfterSave']); AddEventHandler('iblock', 'OnAfterIBlockElementDelete', [self::class, 'onAfterSave']); } public static function onBeforeSave(&$fields): void { $iblockId = self::getIblockId(); if (!$iblockId || (int)($fields['IBLOCK_ID'] ?? 0) !== $iblockId) { return; } if (empty($fields['PROPERTY_VALUES']) || !is_array($fields['PROPERTY_VALUES'])) { return; } $propId = self::getPropId(); foreach ($fields['PROPERTY_VALUES'] as $key => &$values) { if ((string)$key !== self::PROP && (string)$key !== (string)$propId) { continue; } self::normalizeValueNode($values); } unset($values); } public static function onAfterSave($fields): void { $iblockId = self::getIblockId(); $id = (int)($fields['IBLOCK_ID'] ?? 0); if (!$iblockId || $id !== $iblockId) { return; } Cache::clearCache(true, '/lazalka/catalog_faq'); if (class_exists('\CBitrixComponent')) { \CBitrixComponent::clearComponentCache('bitrix:catalog'); } } public static function isPaginationPage(): bool { return strpos((string)($_SERVER['REQUEST_URI'] ?? ''), 'PAGEN') !== false; } public static function getItemsForCurrentPage(): array { if (self::isPaginationPage()) { return []; } $ids = self::getElementIdsForCurrentPage(); if (!$ids) { return []; } if (!Loader::includeModule('iblock')) { return []; } $iblockId = self::getIblockId(); if (!$iblockId) { return []; } $result = []; $res = \CIBlockElement::GetList( ['SORT' => 'ASC', 'ID' => 'ASC'], [ 'IBLOCK_ID' => $iblockId, 'ID' => $ids, 'ACTIVE' => 'Y', 'CHECK_PERMISSIONS' => 'N', ], false, false, ['ID', 'NAME', 'PREVIEW_TEXT', 'PREVIEW_TEXT_TYPE', 'SORT'] ); while ($row = $res->Fetch()) { $result[] = $row; } return $result; } public static function normalizePath($path): string { $path = trim(html_entity_decode((string)$path, ENT_QUOTES | ENT_HTML5, 'UTF-8')); if ($path === '') { return ''; } if (preg_match('#^https?://#i', $path)) { $parsed = parse_url($path); $path = $parsed['path'] ?? ''; } else { $parsed = parse_url($path); if (is_array($parsed) && isset($parsed['path'])) { $path = $parsed['path']; } } $path = rawurldecode(str_replace('\\', '/', $path)); $path = preg_replace('#/+#', '/', $path); if ($path === '' || $path === '/') { return ''; } if ($path[0] !== '/') { $path = '/' . $path; } if (substr($path, -1) !== '/') { $path .= '/'; } return $path; } private static function getElementIdsForCurrentPage(): array { $ids = []; foreach (self::getCandidatePaths() as $path) { foreach (self::getElementIdsByPath($path) as $id) { $ids[$id] = $id; } } return array_values($ids); } private static function getCandidatePaths(): array { $paths = []; $add = static function ($value) use (&$paths) { $normalized = self::normalizePath($value); if ($normalized === '') { return; } $paths[$normalized] = $normalized; $trimmed = rtrim($normalized, '/'); if ($trimmed !== '') { $paths[$trimmed] = $trimmed; } }; global $APPLICATION; if (is_object($APPLICATION)) { $add($APPLICATION->GetCurPage(false)); if (method_exists($APPLICATION, 'GetCurDir')) { $add($APPLICATION->GetCurDir()); } } $add($_SERVER['REQUEST_URI'] ?? ''); if (Loader::includeModule('aspro.smartseo') && class_exists('\\Aspro\\Smartseo\\General\\Smartseo')) { try { $data = \Aspro\Smartseo\General\Smartseo::getCurrentData(); if (is_array($data)) { foreach (['URL_NEW', 'NEW_URL', 'URL_REAL', 'REAL_URL'] as $key) { if (!empty($data[$key])) { $add($data[$key]); } } } } catch (\Throwable $e) { } } return array_values($paths); } private static function getElementIdsByPath(string $path): array { $iblockId = self::getIblockId(); if (!$iblockId || $path === '') { return []; } $cache = Cache::createInstance(); $cacheKey = md5($iblockId . '|' . $path); if ($cache->initCache(300, $cacheKey, '/lazalka/catalog_faq')) { $vars = $cache->getVars(); return is_array($vars) ? $vars : []; } if (!$cache->startDataCache()) { return []; } if (!Loader::includeModule('iblock')) { $cache->abortDataCache(); return []; } $ids = []; $res = \CIBlockElement::GetList( ['SORT' => 'ASC', 'ID' => 'ASC'], [ 'IBLOCK_ID' => $iblockId, 'ACTIVE' => 'Y', 'CHECK_PERMISSIONS' => 'N', 'PROPERTY_' . self::PROP => $path, ], false, false, ['ID'] ); while ($row = $res->Fetch()) { $ids[] = (int)$row['ID']; } $cache->endDataCache($ids); return $ids; } private static function getIblockId(): int { if (self::$iblockId !== null) { return self::$iblockId; } self::$iblockId = 0; if (!Loader::includeModule('iblock')) { return 0; } $row = \CIBlock::GetList([], ['CODE' => self::IBLOCK_CODE, 'CHECK_PERMISSIONS' => 'N'])->Fetch(); if ($row) { self::$iblockId = (int)$row['ID']; } return self::$iblockId; } private static function getPropId(): int { if (self::$propId !== null) { return self::$propId; } self::$propId = 0; $iblockId = self::getIblockId(); if (!$iblockId || !Loader::includeModule('iblock')) { return 0; } $row = \CIBlockProperty::GetList([], ['IBLOCK_ID' => $iblockId, 'CODE' => self::PROP])->Fetch(); if ($row) { self::$propId = (int)$row['ID']; } return self::$propId; } private static function normalizeValueNode(&$node): void { if (is_string($node)) { $node = self::normalizePath($node); return; } if (!is_array($node)) { return; } if (array_key_exists('VALUE', $node) && !is_array($node['VALUE'])) { $node['VALUE'] = self::normalizePath($node['VALUE']); return; } foreach ($node as &$child) { self::normalizeValueNode($child); } unset($child); } }
[Error] 
Class "Lazalka\CatalogFaq" not found (0)
/home/bitrix/ext_www/lazalka.ru/local/php_interface/init.php:680
#0: include_once
	/home/bitrix/ext_www/lazalka.ru/bitrix/modules/main/include.php:137
#1: require_once(string)
	/home/bitrix/ext_www/lazalka.ru/bitrix/modules/main/include/prolog_before.php:19
#2: require_once(string)
	/home/bitrix/ext_www/lazalka.ru/bitrix/modules/main/include/prolog.php:10
#3: require_once(string)
	/home/bitrix/ext_www/lazalka.ru/bitrix/header.php:2
#4: require(string)
	/home/bitrix/ext_www/lazalka.ru/statii/index.php:2
#5: include_once(string)
	/home/bitrix/ext_www/lazalka.ru/bitrix/modules/main/include/urlrewrite.php:128
#6: include_once(string)
	/home/bitrix/ext_www/lazalka.ru/bitrix/urlrewrite.php:2
----------