page.php 2.7 KB

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