page.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. use App\Settings;
  3. use App\Models\Page;
  4. use App\Models\SeoMeta;
  5. use App\Models\Faq;
  6. // Получение страницы по slug
  7. $pageModel = new Page();
  8. $seoMetaModel = new SeoMeta();
  9. $faqModel = new Faq();
  10. // Получаем slug из URL
  11. $slug = $slug ?? $_GET['slug'] ?? '';
  12. // Находим страницу по slug или главную страницу если slug пустой
  13. if (empty($slug)) {
  14. // Главная страница
  15. $currentPage = \db()->fetchOne("SELECT * FROM pages WHERE is_homepage = 1");
  16. } else {
  17. // Обычная страница по slug
  18. $currentPage = \db()->fetchOne("SELECT * FROM pages WHERE slug = ?", [$slug]);
  19. }
  20. // Если страница не найдена, возвращаем 404
  21. if (!$currentPage) {
  22. http_response_code(404);
  23. die('404 - Page not found');
  24. }
  25. // Получаем данные из настроек
  26. $content = (new Settings('content'))->getAll();
  27. // Получаем SEO данные для страницы
  28. $seo = $seoMetaModel->getForRecord('page', $currentPage['id']) ?: [];
  29. // Контент страницы
  30. $pageContent = $currentPage['content'] ?? '';
  31. // Получаем топ контент если есть
  32. $topContent = $currentPage['top_content'] ?? '';
  33. // Подготавливаем содержание для контента
  34. $headings = getContainsByText($pageContent);
  35. $pageContent = addIdToHInText($pageContent, $headings);
  36. // Получаем FAQ для этой страницы
  37. $faqItems = $faqModel->getByMorphable('page', $currentPage['id']);
  38. // Данные для домена
  39. $currentDomain = getCurrentDomain();
  40. $currentUrl = getCurrentUrl();
  41. // Определение типа пользователя
  42. $ip = getUserIp();
  43. $isGoogleBot = (strpos(gethostbyaddr($ip), 'google') !== false)
  44. ? true
  45. : false;
  46. $isBingBot = (strpos(gethostbyaddr($ip), 'bing') !== false)
  47. ? true
  48. : false;
  49. $isSearchBot = $isGoogleBot || $isBingBot;
  50. // Определяем лейаут страницы
  51. $layout = \App\Enums\PageLayout::tryFrom($currentPage['layout'] ?? 'default') ?? \App\Enums\PageLayout::Default;
  52. $layoutFile = $layout->viewPath();
  53. return ViewRender($layoutFile, [
  54. 'content' => $content,
  55. 'seo' => $seo,
  56. 'faqItems' => $faqItems,
  57. 'pageContent' => $pageContent,
  58. 'pageData' => $currentPage,
  59. 'topContent' => $topContent,
  60. 'headings' => $headings,
  61. 'currentDomain' => $currentDomain,
  62. 'currentUrl' => $currentUrl,
  63. 'isHomepage' => !empty($currentPage['is_homepage']),
  64. 'isSearchBot' => $isSearchBot,
  65. 'userIp' => $ip,
  66. ]);