WebOrbiton
v1.0.0.0

StocketBase

66 lines · 1.9 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. if (count(get_included_files()) === 1) {
  6. http_response_code(403);
  7. exit;
  8. }
  9. ​
  10. if (!Auth::hasRoleAtLeast(Auth::ROLE_STORE_MANAGER)) {
  11. exit;
  12. }
  13. ​
  14. $title = trim((string) ($_POST['title'] ?? ''));
  15. $pageId = (int) ($_POST['page_id'] ?? 0) ?: null;
  16. $status = ($_POST['status'] ?? 'draft') === 'published' ? 'published' : 'draft';
  17. $showInMenu = isset($_POST['show_in_menu']) ? 1 : 0;
  18. $blocksJson = BlockEditor::sanitize((string) ($_POST['blocks_json'] ?? '{"blocks":[]}'), true);
  19. ​
  20. if ($title === '') {
  21. exit;
  22. }
  23. ​
  24. require_once __DIR__ . '/../includes/slugger.php';
  25. ​
  26. $slug = Slugger::make($title);
  27. $db = Database::site();
  28. ​
  29. if ($pageId !== null) {
  30. $previousMenu = $db->prepare('SELECT show_in_menu FROM pages WHERE id = :id LIMIT 1');
  31. $previousMenu->execute(['id' => $pageId]);
  32. $wasInMenu = (int) $previousMenu->fetchColumn() === 1;
  33. ​
  34. $statement = $db->prepare(
  35. 'UPDATE pages SET title = :title, slug = :slug, content_blocks = :content, status = :status, show_in_menu = :show_in_menu WHERE id = :id'
  36. );
  37. $statement->execute([
  38. 'title' => $title,
  39. 'slug' => $slug,
  40. 'content' => $blocksJson,
  41. 'status' => $status,
  42. 'show_in_menu' => $showInMenu,
  43. 'id' => $pageId,
  44. ]);
  45. ​
  46. if ($showInMenu === 1 && !$wasInMenu) {
  47. SiteFront::appendToCustomMenu('page', $pageId);
  48. }
  49. } else {
  50. $statement = $db->prepare(
  51. 'INSERT INTO pages (author_id, title, slug, content_blocks, status, show_in_menu) VALUES (:author_id, :title, :slug, :content, :status, :show_in_menu)'
  52. );
  53. $statement->execute([
  54. 'author_id' => $currentUser['id'],
  55. 'title' => $title,
  56. 'slug' => $slug,
  57. 'content' => $blocksJson,
  58. 'status' => $status,
  59. 'show_in_menu' => $showInMenu,
  60. ]);
  61. ​
  62. if ($showInMenu === 1) {
  63. SiteFront::appendToCustomMenu('page', (int) $db->lastInsertId());
  64. }
  65. }
  66. ​