Page management, step 3: section types with their own settings; the front page as sections

Migration 083 gives the section types that need more than blocks their
own tables (hero, slides, countdown, stats, paths, featured cards, the
Community blog's slots, pinned posts and feeds) and turns the front page
into Home's sections: the hero, then its bands in order. Any section can
be hidden. The Front page editor, its tables and the Community page's
are gone; /api/front-page and /api/community-blog fold into the page's
sections (sectionData.js). The admin engine reads and writes a child
collection's own side tables and gated nested collections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aidan Dona 2026-10-17 00:34:56 -05:00
parent f33c0008fe
commit f1a0b06ac2
46 changed files with 1525 additions and 1339 deletions

View file

@ -0,0 +1,435 @@
-- ═══════════════════════════════════════════════════════════════
-- 083 SECTION SETTINGS, AND THE FRONT PAGE AS SECTIONS
--
-- A section type that needs more than its blocks keeps its settings
-- in a table of its own, one row per section (and its lists in
-- tables under the section), so it can go on any page with content
-- of its own. The admin engine reads and writes them as the section's
-- own side tables (admin-crud.js, a child's extensions):
--
-- section_hero a hero: words, buttons, backdrop, feature
-- section_slides its photos
-- section_countdown a countdown: the pinned event, or the next
-- one of a scope and type
-- section_stats a stats band's numbers
-- section_paths a pathfinder's paths
-- section_path_actions each path's links
-- section_featured featured cards' columns (the cards are
-- featured_cards owned by 'page_section')
-- section_blog a Community blog's headings and slots
-- section_blog_posts its pinned posts of ours
-- section_feeds the feeds its community posts come from
-- (its pinned community posts are
-- featured_cards owned by 'page_section')
--
-- page_sections gets is_hidden (any section can be left out without
-- deleting it, as the front page's bands could), and a title of up
-- to 60 characters on the front page, which has no side nav; 30
-- elsewhere. The table is rebuilt for that.
--
-- Then what was the front page's own (front_page and its lists, 012
-- to 074) becomes the home page's sections: the hero first, then
-- each band in its order, hidden ones kept hidden; and the Community
-- page's blog settings (community_page) move into its blog section.
-- Those tables go. A timeline section whose view is 'carousel' is
-- the rail of featured moments the front page showed; the full
-- timeline otherwise.
-- ═══════════════════════════════════════════════════════════════
CREATE TABLE page_sections_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
page_id TEXT NOT NULL REFERENCES site_pages (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
anchor TEXT NOT NULL
CONSTRAINT "A section's URL ID can use only lowercase letters, numbers and hyphens, eg. leadership."
CHECK (anchor <> '' AND anchor NOT GLOB '*[^a-z0-9-]*'),
title TEXT NOT NULL
CONSTRAINT "A section title can be 30 characters at most (it's listed in the page's side nav), 60 on the front page."
CHECK (length(title) <= CASE WHEN page_id = 'home' THEN 60 ELSE 30 END),
blurb TEXT,
accent TEXT CHECK (accent IN ('teal', 'deep', 'green', 'amber')),
preset TEXT CHECK (preset IN ('community_blog', 'group_map', 'region_list', 'partner_list',
'events', 'timeline', 'people_directory',
'feedback_form', 'request_form', 'data_request_form', 'giving', 'qr_maker',
'hero', 'countdown', 'calendar', 'stats', 'featured_cards', 'connect')),
-- An events preset's filter and first view.
scope_id TEXT REFERENCES event_scopes (id) ON DELETE SET NULL,
event_type TEXT CHECK (event_type IN ('retreat', 'class', 'workshop', 'meeting', 'other')),
view TEXT CHECK (view IN ('carousel', 'grid', 'map')),
-- Organization cards (067): the kinds listed by themselves.
org_kinds TEXT CHECK (org_kinds IN ('partner', 'ministry', 'partner,ministry')),
is_hidden INTEGER NOT NULL DEFAULT 0 CHECK (is_hidden IN (0, 1)),
UNIQUE (page_id, anchor)
) STRICT;
INSERT INTO page_sections_new
(id, page_id, sort_order, anchor, title, blurb, accent, preset, scope_id, event_type, view, org_kinds)
SELECT id, page_id, sort_order, anchor, title, blurb, accent, preset, scope_id, event_type, view, org_kinds
FROM page_sections;
PRAGMA legacy_alter_table = ON;
DROP TABLE page_sections;
ALTER TABLE page_sections_new RENAME TO page_sections;
PRAGMA legacy_alter_table = OFF;
CREATE INDEX page_sections_page_idx ON page_sections (page_id, sort_order);
-- ── The settings tables ──────────────────────────────────────────
CREATE TABLE section_hero (
section_id INTEGER PRIMARY KEY REFERENCES page_sections (id) ON DELETE CASCADE,
eyebrow TEXT,
headline TEXT,
subhead TEXT,
primary_label TEXT,
primary_url TEXT,
secondary_label TEXT,
secondary_url TEXT,
backdrop TEXT NOT NULL DEFAULT 'brand' CHECK (backdrop IN ('brand', 'photos')),
slide_seconds INTEGER NOT NULL DEFAULT 7 CHECK (slide_seconds BETWEEN 3 AND 60),
feature TEXT NOT NULL DEFAULT 'none' CHECK (feature IN ('none', 'livestream', 'video', 'event')),
livestream_url TEXT,
livestream_title TEXT,
video_url TEXT,
video_title TEXT,
video_captions TEXT,
video_still TEXT,
event_id TEXT REFERENCES events (id) ON DELETE SET NULL
) STRICT;
CREATE TABLE section_slides (
id INTEGER PRIMARY KEY AUTOINCREMENT,
section_id INTEGER NOT NULL REFERENCES page_sections (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
media TEXT NOT NULL,
alt TEXT,
caption TEXT,
link_url TEXT
) STRICT;
CREATE INDEX section_slides_section_idx ON section_slides (section_id, sort_order);
CREATE TABLE section_countdown (
section_id INTEGER PRIMARY KEY REFERENCES page_sections (id) ON DELETE CASCADE,
event_id TEXT REFERENCES events (id) ON DELETE SET NULL,
scope_id TEXT REFERENCES event_scopes (id) ON DELETE SET NULL,
event_type TEXT CHECK (event_type IN ('retreat', 'class', 'workshop', 'meeting', 'other'))
) STRICT;
CREATE TABLE section_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
section_id INTEGER NOT NULL REFERENCES page_sections (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
label TEXT NOT NULL,
source TEXT NOT NULL DEFAULT 'manual'
CHECK (source IN ('manual', 'years_since', 'regions', 'groups',
'partners', 'events_held', 'retreats_held',
'people', 'awards_given')),
value TEXT,
suffix TEXT,
note TEXT
) STRICT;
CREATE INDEX section_stats_section_idx ON section_stats (section_id, sort_order);
CREATE TABLE section_paths (
id INTEGER PRIMARY KEY AUTOINCREMENT,
section_id INTEGER NOT NULL REFERENCES page_sections (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
label TEXT NOT NULL,
icon TEXT,
blurb TEXT
) STRICT;
CREATE INDEX section_paths_section_idx ON section_paths (section_id, sort_order);
CREATE TABLE section_path_actions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path_id INTEGER NOT NULL REFERENCES section_paths (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
label TEXT NOT NULL,
description TEXT,
url TEXT NOT NULL
) STRICT;
CREATE INDEX section_path_actions_path_idx ON section_path_actions (path_id, sort_order);
CREATE TABLE section_featured (
section_id INTEGER PRIMARY KEY REFERENCES page_sections (id) ON DELETE CASCADE,
columns INTEGER CHECK (columns IN (2, 3, 4))
) STRICT;
CREATE TABLE section_blog (
section_id INTEGER PRIMARY KEY REFERENCES page_sections (id) ON DELETE CASCADE,
internal_title TEXT,
internal_slots INTEGER NOT NULL DEFAULT 3 CHECK (internal_slots BETWEEN 0 AND 12),
external_title TEXT,
external_slots INTEGER NOT NULL DEFAULT 3 CHECK (external_slots BETWEEN 0 AND 12),
external_url TEXT,
external_label TEXT
) STRICT;
CREATE TABLE section_blog_posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
section_id INTEGER NOT NULL REFERENCES page_sections (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
post_id TEXT NOT NULL REFERENCES blog_posts (id) ON DELETE CASCADE
) STRICT;
CREATE INDEX section_blog_posts_section_idx ON section_blog_posts (section_id, sort_order);
CREATE TABLE section_feeds (
id INTEGER PRIMARY KEY AUTOINCREMENT,
section_id INTEGER NOT NULL REFERENCES page_sections (id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 0,
url TEXT NOT NULL,
label TEXT
) STRICT;
CREATE INDEX section_feeds_section_idx ON section_feeds (section_id, sort_order);
-- ── featured_cards: owned by a section ───────────────────────────
CREATE TABLE featured_cards_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
owner_kind TEXT NOT NULL CHECK (owner_kind IN ('page_section', 'organization')),
owner_id TEXT NOT NULL,
sort_order INTEGER NOT NULL DEFAULT 0,
kind TEXT NOT NULL DEFAULT 'link'
CHECK (kind IN ('record', 'link', 'video')),
ref_kind TEXT CHECK (ref_kind IN ('event', 'organization', 'person', 'team', 'award')),
ref_id TEXT,
url TEXT,
eyebrow TEXT,
title TEXT,
blurb TEXT,
image TEXT,
size TEXT CHECK (size IN ('normal', 'wide', 'tall', 'large')),
CHECK ((ref_kind IS NULL) = (ref_id IS NULL)),
CONSTRAINT "A record card needs a record to point at."
CHECK (kind != 'record' OR ref_kind IS NOT NULL),
CONSTRAINT "A link or video card needs a URL."
CHECK (kind = 'record' OR url IS NOT NULL)
) STRICT;
-- ── The Community blog into its section ──────────────────────────
INSERT INTO section_blog (section_id, internal_title, internal_slots, external_title, external_slots, external_url, external_label)
SELECT s.id, c.internal_title, c.internal_slots, c.external_title, c.external_slots, c.external_url, c.external_label
FROM page_sections s, community_page c
WHERE s.page_id = 'community' AND s.preset = 'community_blog' AND c.id = 'community';
INSERT INTO section_blog_posts (section_id, sort_order, post_id)
SELECT s.id, p.sort_order, p.post_id
FROM page_sections s JOIN community_page_pinned_posts p ON p.page_id = 'community'
WHERE s.page_id = 'community' AND s.preset = 'community_blog';
INSERT INTO section_feeds (section_id, sort_order, url, label)
SELECT s.id, f.sort_order, f.url, f.label
FROM page_sections s JOIN community_page_feeds f ON f.page_id = 'community'
WHERE s.page_id = 'community' AND s.preset = 'community_blog';
INSERT INTO featured_cards_new
(owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image, size)
SELECT 'page_section', CAST(s.id AS TEXT), f.sort_order, f.kind, f.ref_kind, f.ref_id, f.url, f.eyebrow, f.title, f.blurb, f.image, f.size
FROM featured_cards f
JOIN page_sections s ON s.page_id = 'community' AND s.preset = 'community_blog'
WHERE f.owner_kind = 'community_page'
AND s.id = (SELECT min(id) FROM page_sections WHERE page_id = 'community' AND preset = 'community_blog');
-- ── The front page into the home page's sections ─────────────────
-- The hero first.
INSERT INTO page_sections (page_id, sort_order, anchor, title, preset)
VALUES ('home', 0, 'hero', 'Welcome', 'hero');
INSERT INTO section_hero
(section_id, eyebrow, headline, subhead, primary_label, primary_url, secondary_label, secondary_url,
backdrop, slide_seconds, feature, livestream_url, livestream_title, video_url, video_title,
video_captions, video_still, event_id)
SELECT (SELECT id FROM page_sections WHERE page_id = 'home' AND anchor = 'hero'),
eyebrow, headline, subhead, primary_label, primary_url, secondary_label, secondary_url,
hero_backdrop, slide_seconds, hero_feature, livestream_url, livestream_title, video_url, video_title,
video_captions, video_still, hero_event_id
FROM front_page WHERE id = 'home';
INSERT INTO section_slides (section_id, sort_order, media, alt, caption, link_url)
SELECT (SELECT id FROM page_sections WHERE page_id = 'home' AND anchor = 'hero'), sort_order, media, alt, caption, link_url
FROM front_page_slides WHERE page_id = 'home';
-- A featured band, hidden, when the front page has cards but no band
-- for them, so they aren't lost.
INSERT INTO front_page_sections (page_id, sort_order, section, is_hidden)
SELECT 'home', 99, 'featured', 1
WHERE NOT EXISTS (SELECT 1 FROM front_page_sections WHERE page_id = 'home' AND section = 'featured')
AND EXISTS (SELECT 1 FROM featured_cards WHERE owner_kind = 'front_page');
-- Each band, in its order: its section type, anchor and heading.
INSERT INTO page_sections (page_id, sort_order, anchor, title, blurb, preset, scope_id, event_type, view, is_hidden)
SELECT 'home',
b.sort_order + 1,
CASE b.section
WHEN 'countdown' THEN 'next-up'
WHEN 'stats' THEN 'numbers'
WHEN 'timeline' THEN 'story'
ELSE b.section
END,
substr(COALESCE(b.title, CASE b.section
WHEN 'countdown' THEN 'Next up'
WHEN 'retreats' THEN 'National Retreats'
WHEN 'calendar' THEN 'What''s on'
WHEN 'stats' THEN 'By the numbers'
WHEN 'timeline' THEN 'Our story so far'
WHEN 'connect' THEN 'Find your way in'
ELSE 'Featured'
END), 1, 60),
b.blurb,
CASE b.section
WHEN 'retreats' THEN 'events'
WHEN 'featured' THEN 'featured_cards'
ELSE b.section
END,
CASE WHEN b.section = 'retreats' THEN 'national' END,
CASE WHEN b.section = 'retreats' THEN 'retreat' END,
CASE WHEN b.section IN ('retreats', 'timeline') THEN 'carousel' END,
b.is_hidden
FROM front_page_sections b
WHERE b.page_id = 'home'
ORDER BY b.sort_order;
-- The retreats band's link through to the Retreats page, as a button
-- above its carousel.
INSERT INTO content_blocks (owner_kind, owner_id, slot, sort_order, type, text, href)
SELECT 'page_section', CAST(id AS TEXT), 'body', 0, 'button', 'All retreats', '/retreats'
FROM page_sections WHERE page_id = 'home' AND anchor = 'retreats';
INSERT INTO section_countdown (section_id, event_id, scope_id, event_type)
SELECT (SELECT id FROM page_sections WHERE page_id = 'home' AND preset = 'countdown'),
countdown_event_id, countdown_scope_id, countdown_event_type
FROM front_page
WHERE id = 'home' AND EXISTS (SELECT 1 FROM page_sections WHERE page_id = 'home' AND preset = 'countdown');
INSERT INTO section_stats (section_id, sort_order, label, source, value, suffix, note)
SELECT (SELECT id FROM page_sections WHERE page_id = 'home' AND preset = 'stats'),
sort_order, label, source, value, suffix, note
FROM front_page_stats
WHERE page_id = 'home' AND EXISTS (SELECT 1 FROM page_sections WHERE page_id = 'home' AND preset = 'stats');
INSERT INTO section_paths (id, section_id, sort_order, label, icon, blurb)
SELECT id, (SELECT id FROM page_sections WHERE page_id = 'home' AND preset = 'connect'),
sort_order, label, icon, blurb
FROM front_page_paths
WHERE page_id = 'home' AND EXISTS (SELECT 1 FROM page_sections WHERE page_id = 'home' AND preset = 'connect');
INSERT INTO section_path_actions (path_id, sort_order, label, description, url)
SELECT a.path_id, a.sort_order, a.label, a.description, a.url
FROM front_page_path_actions a
WHERE a.path_id IN (SELECT id FROM section_paths);
INSERT INTO section_featured (section_id, columns)
SELECT (SELECT id FROM page_sections WHERE page_id = 'home' AND preset = 'featured_cards'), featured_columns
FROM front_page
WHERE id = 'home' AND EXISTS (SELECT 1 FROM page_sections WHERE page_id = 'home' AND preset = 'featured_cards');
INSERT INTO featured_cards_new
(owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image, size)
SELECT 'page_section', (SELECT CAST(id AS TEXT) FROM page_sections WHERE page_id = 'home' AND preset = 'featured_cards'),
sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image, size
FROM featured_cards
WHERE owner_kind = 'front_page'
AND EXISTS (SELECT 1 FROM page_sections WHERE page_id = 'home' AND preset = 'featured_cards');
-- An organization's own cards (072) come along as they are.
INSERT INTO featured_cards_new
(id, owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image, size)
SELECT id, owner_kind, owner_id, sort_order, kind, ref_kind, ref_id, url, eyebrow, title, blurb, image, size
FROM featured_cards WHERE owner_kind = 'organization';
-- Drafts of what's gone: the Front page editor is the home page's now.
DELETE FROM drafts WHERE entity = 'front_page';
-- ── What's gone ──────────────────────────────────────────────────
-- The cleanup triggers on events, people, teams, awards and
-- organizations name featured_cards; legacy mode leaves them alone
-- through the rename.
PRAGMA legacy_alter_table = ON;
DROP TABLE featured_cards;
ALTER TABLE featured_cards_new RENAME TO featured_cards;
PRAGMA legacy_alter_table = OFF;
CREATE INDEX featured_cards_owner_idx ON featured_cards (owner_kind, owner_id, sort_order);
DROP TABLE front_page_path_actions;
DROP TABLE front_page_paths;
DROP TABLE front_page_stats;
DROP TABLE front_page_slides;
DROP TABLE front_page_sections;
DROP TABLE front_page;
DROP TABLE community_page_pinned_posts;
DROP TABLE community_page_feeds;
DROP TABLE community_page;
-- ── Triggers, last ───────────────────────────────────────────────
-- A section's blocks and its featured cards go with it.
CREATE TRIGGER page_sections_cleanup
AFTER DELETE ON page_sections
BEGIN
DELETE FROM content_blocks WHERE owner_kind = 'page_section' AND owner_id = CAST(old.id AS TEXT);
DELETE FROM featured_cards WHERE owner_kind = 'page_section' AND owner_id = CAST(old.id AS TEXT);
END;
CREATE TRIGGER featured_cards_owner_exists
BEFORE INSERT ON featured_cards
BEGIN
SELECT CASE
WHEN new.owner_kind = 'organization'
AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.owner_id)
THEN RAISE(ABORT, 'featured_cards: no such organization')
WHEN new.owner_kind = 'page_section'
AND NOT EXISTS (SELECT 1 FROM page_sections WHERE CAST(id AS TEXT) = new.owner_id)
THEN RAISE(ABORT, 'featured_cards: no such page section')
END;
END;
CREATE TRIGGER featured_cards_ref_exists
BEFORE INSERT ON featured_cards
BEGIN
SELECT CASE
WHEN new.ref_kind = 'event'
AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such event')
WHEN new.ref_kind = 'organization'
AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such organization')
WHEN new.ref_kind = 'person'
AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such person')
WHEN new.ref_kind = 'team'
AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such team')
WHEN new.ref_kind = 'award'
AND NOT EXISTS (SELECT 1 FROM awards WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such award')
END;
END;
CREATE TRIGGER featured_cards_ref_exists_update
BEFORE UPDATE OF ref_kind, ref_id ON featured_cards
BEGIN
SELECT CASE
WHEN new.ref_kind = 'event'
AND NOT EXISTS (SELECT 1 FROM events WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such event')
WHEN new.ref_kind = 'organization'
AND NOT EXISTS (SELECT 1 FROM organizations WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such organization')
WHEN new.ref_kind = 'person'
AND NOT EXISTS (SELECT 1 FROM people WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such person')
WHEN new.ref_kind = 'team'
AND NOT EXISTS (SELECT 1 FROM teams WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such team')
WHEN new.ref_kind = 'award'
AND NOT EXISTS (SELECT 1 FROM awards WHERE id = new.ref_id)
THEN RAISE(ABORT, 'featured_cards: no such award')
END;
END;