forked from laysense/laysite
100 lines
3.8 KiB
PHP
100 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use PDO;
|
|
use RuntimeException;
|
|
|
|
final class ReusableBlocks
|
|
{
|
|
public static function sources(PDO $pdo): array
|
|
{
|
|
return $pdo->query('SELECT r.main_id, s.key, s.title, p.slug, p.name
|
|
FROM section_reuse r JOIN sections s ON s.page_slug = r.page_slug AND s.key = r.section_key
|
|
JOIN pages p ON p.slug = r.page_slug WHERE r.main_id IS NOT NULL ORDER BY p.name, s.position')->fetchAll();
|
|
}
|
|
|
|
public static function source(PDO $pdo, string $id, bool $draft): ?array
|
|
{
|
|
if ($draft) {
|
|
$query = $pdo->prepare('SELECT s.* FROM section_reuse r JOIN sections s
|
|
ON s.page_slug = r.page_slug AND s.key = r.section_key WHERE r.main_id = ?');
|
|
$query->execute([$id]);
|
|
if ($section = $query->fetch()) {
|
|
return $section + ['main_id' => $id];
|
|
}
|
|
}
|
|
$query = $pdo->prepare('SELECT payload FROM published_blocks WHERE main_id = ?');
|
|
$query->execute([$id]);
|
|
$payload = $query->fetchColumn();
|
|
|
|
return $payload === false ? null : json_decode($payload, true, flags: JSON_THROW_ON_ERROR);
|
|
}
|
|
|
|
public static function materialize(array $mirror, array $source): array
|
|
{
|
|
$snapshot = array_replace($mirror, array_intersect_key($source, array_flip([
|
|
'title', 'lead', 'style', 'feed', 'alignments', 'presentation', 'body',
|
|
])));
|
|
$presentation = json_decode($snapshot['presentation'], true, flags: JSON_THROW_ON_ERROR);
|
|
// The mirror has its own outer anchor and a local namespace for IDs in the copied content.
|
|
unset($presentation['section']['id']);
|
|
foreach (['title', 'lead', 'body'] as $field) {
|
|
if (isset($presentation[$field]['id'])) {
|
|
$presentation[$field]['id'] = $snapshot['key'] . '-' . $presentation[$field]['id'];
|
|
}
|
|
}
|
|
$snapshot['presentation'] = CanvasStyle::encode($presentation);
|
|
$snapshot['body'] = Html::scopedFragment($snapshot['body'], $snapshot['key']);
|
|
|
|
return $snapshot;
|
|
}
|
|
|
|
public static function resolve(PDO $pdo, array $page, bool $draft): array
|
|
{
|
|
$sources = [];
|
|
foreach ($page['sections'] as &$section) {
|
|
if (!isset($section['mirror_id'])) {
|
|
continue;
|
|
}
|
|
$id = $section['mirror_id'];
|
|
if (!array_key_exists($id, $sources)) {
|
|
$sources[$id] = self::source($pdo, $id, $draft);
|
|
}
|
|
$source = $sources[$id];
|
|
if (!$source) {
|
|
continue;
|
|
}
|
|
$section = self::materialize($section, $source);
|
|
if ($draft && isset($source['page_slug'])) {
|
|
$query = $pdo->prepare('SELECT page_slug, section_key FROM section_reuse WHERE main_id = ?');
|
|
$query->execute([$id]);
|
|
$section['mirror_source'] = $query->fetch() ?: null;
|
|
}
|
|
}
|
|
unset($section);
|
|
|
|
return $page;
|
|
}
|
|
|
|
public static function set(PDO $pdo, string $slug, string $key, ?string $main = null, ?string $mirror = null): void
|
|
{
|
|
$pdo->prepare('DELETE FROM section_reuse WHERE page_slug = ? AND section_key = ?')->execute([$slug, $key]);
|
|
if ($main !== null || $mirror !== null) {
|
|
$pdo->prepare('INSERT INTO section_reuse (page_slug, section_key, main_id, mirror_id) VALUES (?, ?, ?, ?)')
|
|
->execute([$slug, $key, $main, $mirror]);
|
|
}
|
|
}
|
|
|
|
public static function restore(PDO $pdo, string $slug, array $section): void
|
|
{
|
|
if (isset($section['mirror_id'])) {
|
|
self::set($pdo, $slug, $section['key'], mirror: $section['mirror_id']);
|
|
} elseif (isset($section['main_id'])) {
|
|
self::set($pdo, $slug, $section['key'], main: $section['main_id']);
|
|
}
|
|
}
|
|
}
|