validate(['order' => 'required|array', 'order.*' => 'integer'])['order']; foreach ($ids as $sortOrder => $sectionId) { PageSection::where('page_id', $page->id) ->where('id', $sectionId) ->update(['sort_order' => $sortOrder]); } $this->flushCache($page); return response()->json(['ok' => true]); } // Добавляет блок из библиотеки на страницу (тип block) public function attachBlock(Request $request, Page $page): RedirectResponse { $data = $request->validate(['block_id' => 'required|exists:blocks,id']); $maxOrder = PageSection::where('page_id', $page->id)->max('sort_order') ?? -1; PageSection::create([ 'page_id' => $page->id, 'type' => 'block', 'block_id' => $data['block_id'], 'sort_order' => $maxOrder + 1, ]); $this->flushCache($page); return redirect()->route('admin.pages.edit', $page) ->with('success', 'Блок добавлен на страницу.'); } // Добавляет пустую текстовую секцию (тип content) public function addContent(Page $page): RedirectResponse { $maxOrder = PageSection::where('page_id', $page->id)->max('sort_order') ?? -1; PageSection::create([ 'page_id' => $page->id, 'type' => 'content', 'content' => '', 'sort_order' => $maxOrder + 1, ]); $this->flushCache($page); return redirect()->route('admin.pages.edit', $page) ->with('success', 'Текстовая область добавлена.'); } // Сохраняет HTML-контент текстовой секции public function updateContent(Request $request, Page $page, PageSection $section): JsonResponse { abort_if($section->page_id !== $page->id || $section->type !== 'content', 403); $section->update(['content' => $request->input('content', '')]); $this->flushCache($page); return response()->json(['ok' => true]); } // Удаляет секцию (любого типа) и перенумеровывает остальные public function destroy(Page $page, PageSection $section): RedirectResponse { abort_if($section->page_id !== $page->id, 403); $section->delete(); // Перенумеровываем порядок без дырок $sections = PageSection::where('page_id', $page->id)->orderBy('sort_order')->get(); foreach ($sections as $i => $s) { $s->update(['sort_order' => $i]); } $this->flushCache($page); return redirect()->route('admin.pages.edit', $page) ->with('success', 'Секция удалена.'); } // Сброс кеша публичной страницы и её секций private function flushCache(Page $page): void { Cache::forget('page.'.$page->slug); Cache::forget('page_sections.'.$page->slug); } }