publish operations

This commit is contained in:
2026-01-09 19:08:00 +02:00
parent 84cd04c94f
commit 0ed0106103
14 changed files with 519 additions and 106 deletions
+29 -30
View File
@@ -56,23 +56,23 @@
path: '<path d="M416 288h-95.1c-17.67 0-32 14.33-32 32s14.33 32 32 32H416c17.67 0 32-14.33 32-32S433.7 288 416 288zM544 32h-223.1c-17.67 0-32 14.33-32 32s14.33 32 32 32H544c17.67 0 32-14.33 32-32S561.7 32 544 32zM352 416h-32c-17.67 0-32 14.33-32 32s14.33 32 32 32h32c17.67 0 31.1-14.33 31.1-32S369.7 416 352 416zM480 160h-159.1c-17.67 0-32 14.33-32 32s14.33 32 32 32H480c17.67 0 32-14.33 32-32S497.7 160 480 160zM192.4 330.7L160 366.1V64.03C160 46.33 145.7 32 128 32S96 46.33 96 64.03v302L63.6 330.7c-6.312-6.883-14.94-10.38-23.61-10.38c-7.719 0-15.47 2.781-21.61 8.414c-13.03 11.95-13.9 32.22-1.969 45.27l87.1 96.09c12.12 13.26 35.06 13.26 47.19 0l87.1-96.09c11.94-13.05 11.06-33.31-1.969-45.27C224.6 316.8 204.4 317.7 192.4 330.7z"/>',
viewBox: "0 0 576 512",
},
"filter": {
filter: {
path: '<path d="M3.853 54.87C10.47 40.9 24.54 32 40 32H472C487.5 32 501.5 40.9 508.1 54.87C514.8 68.84 512.7 85.37 502.1 97.33L320 320.9V448C320 460.1 313.2 471.2 302.3 476.6C291.5 482 278.5 480.9 268.8 473.6L204.8 425.6C196.7 419.6 192 410.1 192 400V320.9L9.042 97.33C-.745 85.37-2.765 68.84 3.854 54.87L3.853 54.87z"/>',
viewBox: "0 0 512 512",
},
"calendar": {
calendar: {
path: '<path d="M96 32C96 14.33 110.3 0 128 0C145.7 0 160 14.33 160 32V64H288V32C288 14.33 302.3 0 320 0C337.7 0 352 14.33 352 32V64H400C426.5 64 448 85.49 448 112V160H0V112C0 85.49 21.49 64 48 64H96V32zM448 464C448 490.5 426.5 512 400 512H48C21.49 512 0 490.5 0 464V192H448V464z"/>',
viewBox: "0 0 448 512",
},
"pencil": {
pencil: {
path: '<path d="M421.7 220.3L188.5 453.4L154.6 419.5L158.1 416H112C103.2 416 96 408.8 96 400V353.9L92.51 357.4C87.78 362.2 84.31 368 82.42 374.4L59.44 452.6L137.6 429.6C143.1 427.7 149.8 424.2 154.6 419.5L188.5 453.4C178.1 463.8 165.2 471.5 151.1 475.6L30.77 511C22.35 513.5 13.24 511.2 7.03 504.1C.8198 498.8-1.502 489.7 .976 481.2L36.37 360.9C40.53 346.8 48.16 333.9 58.57 323.5L291.7 90.34L421.7 220.3zM492.7 58.75C517.7 83.74 517.7 124.3 492.7 149.3L444.3 197.7L314.3 67.72L362.7 19.32C387.7-5.678 428.3-5.678 453.3 19.32L492.7 58.75z"/>',
viewBox: "0 0 512 512",
},
"database": {
database: {
path: '<path d="M448 80V128C448 172.2 347.7 208 224 208C100.3 208 0 172.2 0 128V80C0 35.82 100.3 0 224 0C347.7 0 448 35.82 448 80zM393.2 214.7C413.1 207.3 433.1 197.8 448 186.1V288C448 332.2 347.7 368 224 368C100.3 368 0 332.2 0 288V186.1C14.93 197.8 34.02 207.3 54.85 214.7C99.66 230.7 159.5 240 224 240C288.5 240 348.3 230.7 393.2 214.7V214.7zM54.85 374.7C99.66 390.7 159.5 400 224 400C288.5 400 348.3 390.7 393.2 374.7C413.1 367.3 433.1 357.8 448 346.1V432C448 476.2 347.7 512 224 512C100.3 512 0 476.2 0 432V346.1C14.93 357.8 34.02 367.3 54.85 374.7z"/>',
viewBox: "0 0 448 512",
},
"dice": {
dice: {
path: '<path d="M447.1 224c0-12.56-4.781-25.13-14.35-34.76l-174.9-174.9C249.1 4.786 236.5 0 223.1 0C211.4 0 198.9 4.786 189.2 14.35L14.35 189.2C4.783 198.9-.0011 211.4-.0011 223.1c0 12.56 4.785 25.17 14.35 34.8l174.9 174.9c9.625 9.562 22.19 14.35 34.75 14.35s25.13-4.783 34.75-14.35l174.9-174.9C443.2 249.1 447.1 236.6 447.1 224zM96 248c-13.25 0-23.1-10.75-23.1-23.1s10.75-23.1 23.1-23.1S120 210.8 120 224S109.3 248 96 248zM224 376c-13.25 0-23.1-10.75-23.1-23.1s10.75-23.1 23.1-23.1s23.1 10.75 23.1 23.1S237.3 376 224 376zM224 248c-13.25 0-23.1-10.75-23.1-23.1s10.75-23.1 23.1-23.1S248 210.8 248 224S237.3 248 224 248zM224 120c-13.25 0-23.1-10.75-23.1-23.1s10.75-23.1 23.1-23.1s23.1 10.75 23.1 23.1S237.3 120 224 120zM352 248c-13.25 0-23.1-10.75-23.1-23.1s10.75-23.1 23.1-23.1s23.1 10.75 23.1 23.1S365.3 248 352 248zM591.1 192l-118.7 0c4.418 10.27 6.604 21.25 6.604 32.23c0 20.7-7.865 41.38-23.63 57.14l-136.2 136.2v46.37C320 490.5 341.5 512 368 512h223.1c26.5 0 47.1-21.5 47.1-47.1V240C639.1 213.5 618.5 192 591.1 192zM479.1 376c-13.25 0-23.1-10.75-23.1-23.1s10.75-23.1 23.1-23.1s23.1 10.75 23.1 23.1S493.2 376 479.1 376z"/>',
viewBox: "0 0 640 512",
},
@@ -81,7 +81,7 @@
path: '<path d="M506.3 417l-213.3-364c-16.33-28-57.54-28-73.98 0l-213.2 364C-10.59 444.9 9.849 480 42.74 480h426.6C502.1 480 522.6 445 506.3 417zM232 168c0-13.25 10.75-24 24-24S280 154.8 280 168v128c0 13.25-10.75 24-23.1 24S232 309.3 232 296V168zM256 416c-17.36 0-31.44-14.08-31.44-31.44c0-17.36 14.07-31.44 31.44-31.44s31.44 14.08 31.44 31.44C287.4 401.9 273.4 416 256 416z"/>',
viewBox: "0 0 512 512",
},
"eye": {
eye: {
path: '<path d="M279.6 160.4C282.4 160.1 285.2 160 288 160C341 160 384 202.1 384 256C384 309 341 352 288 352C234.1 352 192 309 192 256C192 253.2 192.1 250.4 192.4 247.6C201.7 252.1 212.5 256 224 256C259.3 256 288 227.3 288 192C288 180.5 284.1 169.7 279.6 160.4zM480.6 112.6C527.4 156 558.7 207.1 573.5 243.7C576.8 251.6 576.8 260.4 573.5 268.3C558.7 304 527.4 355.1 480.6 399.4C433.5 443.2 368.8 480 288 480C207.2 480 142.5 443.2 95.42 399.4C48.62 355.1 17.34 304 2.461 268.3C-.8205 260.4-.8205 251.6 2.461 243.7C17.34 207.1 48.62 156 95.42 112.6C142.5 68.84 207.2 32 288 32C368.8 32 433.5 68.84 480.6 112.6V112.6zM288 112C208.5 112 144 176.5 144 256C144 335.5 208.5 400 288 400C367.5 400 432 335.5 432 256C432 176.5 367.5 112 288 112z"/>',
viewBox: "0 0 576 512",
},
@@ -93,19 +93,19 @@
path: '<path d="M500.3 443.7l-119.7-119.7c27.22-40.41 40.65-90.9 33.46-144.7C401.8 87.79 326.8 13.32 235.2 1.723C99.01-15.51-15.51 99.01 1.724 235.2c11.6 91.64 86.08 166.7 177.6 178.9c53.8 7.189 104.3-6.236 144.7-33.46l119.7 119.7c15.62 15.62 40.95 15.62 56.57 0C515.9 484.7 515.9 459.3 500.3 443.7zM79.1 208c0-70.58 57.42-128 128-128s128 57.42 128 128c0 70.58-57.42 128-128 128S79.1 278.6 79.1 208z"/>',
viewBox: "0 0 512 512",
},
"expand": {
expand: {
path: '<path d="M128 32H32C14.31 32 0 46.31 0 64v96c0 17.69 14.31 32 32 32s32-14.31 32-32V96h64c17.69 0 32-14.31 32-32S145.7 32 128 32zM416 32h-96c-17.69 0-32 14.31-32 32s14.31 32 32 32h64v64c0 17.69 14.31 32 32 32s32-14.31 32-32V64C448 46.31 433.7 32 416 32zM128 416H64v-64c0-17.69-14.31-32-32-32s-32 14.31-32 32v96c0 17.69 14.31 32 32 32h96c17.69 0 32-14.31 32-32S145.7 416 128 416zM416 320c-17.69 0-32 14.31-32 32v64h-64c-17.69 0-32 14.31-32 32s14.31 32 32 32h96c17.69 0 32-14.31 32-32v-96C448 334.3 433.7 320 416 320z"/>',
viewBox: "0 0 448 512",
},
"compress": {
compress: {
path: '<path d="M128 320H32c-17.69 0-32 14.31-32 32s14.31 32 32 32h64v64c0 17.69 14.31 32 32 32s32-14.31 32-32v-96C160 334.3 145.7 320 128 320zM416 320h-96c-17.69 0-32 14.31-32 32v96c0 17.69 14.31 32 32 32s32-14.31 32-32v-64h64c17.69 0 32-14.31 32-32S433.7 320 416 320zM320 192h96c17.69 0 32-14.31 32-32s-14.31-32-32-32h-64V64c0-17.69-14.31-32-32-32s-32 14.31-32 32v96C288 177.7 302.3 192 320 192zM128 32C110.3 32 96 46.31 96 64v64H32C14.31 128 0 142.3 0 160s14.31 32 32 32h96c17.69 0 32-14.31 32-32V64C160 46.31 145.7 32 128 32z"/>',
viewBox: "0 0 448 512",
},
"check": {
check: {
path: '<path d="M438.6 105.4C451.1 117.9 451.1 138.1 438.6 150.6L182.6 406.6C170.1 419.1 149.9 419.1 137.4 406.6L9.372 278.6C-3.124 266.1-3.124 245.9 9.372 233.4C21.87 220.9 42.13 220.9 54.63 233.4L159.1 338.7L393.4 105.4C405.9 92.88 426.1 92.88 438.6 105.4H438.6z"/>',
viewBox: "0 0 448 512",
},
"close": {
close: {
path: '<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18 17.94 6M18 18 6.06 6"/>',
viewBox: "0 0 24 24",
},
@@ -113,7 +113,7 @@
path: '<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h14M5 12l4-4m-4 4 4 4"/>',
viewBox: "0 0 24 24",
},
"list": {
list: {
path: '<path stroke="currentColor" stroke-linecap="round" stroke-width="2" d="M9 8h10M9 12h10M9 16h10M4.99 8H5m-.02 4h.01m0 4H5"/>',
viewBox: "0 0 24 24",
},
@@ -121,33 +121,33 @@
path: '<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6h8m-8 6h8m-8 6h8M4 16a2 2 0 1 1 3.321 1.5L4 20h5M4 5l2-1v6m-2 0h4"/>',
viewBox: "0 0 24 24",
},
"italic": {
italic: {
path: '<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m8.874 19 6.143-14M6 19h6.33m-.66-14H18"/>',
viewBox: "0 0 24 24",
}
},
};
export let width = 16;
export let height = 16;
export let icon = "";
export let fill = "currentColor";
export let stroke = "currentColor";
let {
width = 16,
height = 16,
icon,
fill = "currentColor",
stroke = "currentColor",
} = $props();
let selectedIcon = icons[icon];
</script>
<svg
class="bi"
xmlns="http://www.w3.org/2000/svg"
{width}
{height}
viewBox={selectedIcon.viewBox}
aria-labelledby={icon}
role="presentation"
{stroke}
{fill}
class="bi"
xmlns="http://www.w3.org/2000/svg"
{width}
{height}
viewBox={selectedIcon.viewBox}
aria-labelledby={icon}
role="presentation"
{stroke}
{fill}
>
{@html selectedIcon.path}
</svg>
@@ -155,6 +155,5 @@
<style>
svg {
vertical-align: text-top;
}
</style>
@@ -0,0 +1,60 @@
<script>
import { getApp } from "../../app";
import { post } from "../../modules/remote";
import Icon from "./../../common/Icon.svelte";
let { record, status } = $props();
let date = $state(null);
const app = getApp();
function handlePublish() {
post(app.url("records/publish"), { id: record.id }, (data, err) => {
Turbo.visit(window.location.href);
});
}
function handleTrash() {
post(app.url("records/trash"), { id: record.id }, (data, err) => {
Turbo.visit(window.location.href);
});
}
function handleSchedule() {
post(
app.url("records/schedule"),
{ id: record.id, date: date },
(data, err) => {
Turbo.visit(window.location.href);
},
);
}
</script>
<div style="display: flex; gap:20px">
{#if status != "trashed"}
<button onclick={handlePublish}>Publish Now</button>
{/if}
<details class="dropdown">
<summary role="button" class="secondary"
><Icon icon="ellipsis-vertical"></Icon></summary
>
<ul>
<li>
<form onsubmit={handleSchedule}>
<fieldset role="group">
<input
bind:value={date}
type="datetime-local"
aria-label="Datetime local"
required
/>
<button>Schedule</button>
</fieldset>
</form>
</li>
<li><a href="#">View Revisions</a></li>
{#if status != "trashed"}
<li>
<button onclick={handleTrash}>Move to trash</button>
</li>
{/if}
</ul>
</details>
</div>
@@ -3,6 +3,8 @@
import { onMount } from "svelte";
import TextField from "./fields/TextField.svelte";
import LocaleChooser from "./LocaleChooser.svelte";
import RecordStatus from "./RecordStatus.svelte";
import PublishingOptions from "./PublishingOptions.svelte";
import { getSelectedLocales } from "./locale.svelte.js";
let { channel, user, data } = $props();
let selectedLocales = $state(getSelectedLocales());
@@ -16,42 +18,52 @@
<!-- <svelte:window on:beforeunload={beforeUnload} /> -->
<ChannelLayout {body} {channel} schemas={data.schemas} {user}></ChannelLayout>
{#snippet body()}
<LocaleChooser {channel} onLocaleChange={handleLocaleChange}
></LocaleChooser>
{#each data.fields as field}
<div style="display:flex;gap:20px;">
{#if field.type === "text"}
<TextField
{channel}
{record}
errors={data.validationErrors.filter(
(f) => f.id === field.fieldId && f.locale === "main",
)}
schemaField={field}
locale="main"
dataField={data.draftData.find(
(f) => f.id === field.id && f.locale === "main",
)}
></TextField>
{#if field.translatable}
{#each selectedLocales as locale (locale)}
<TextField
{channel}
{record}
errors={data.validationErrors.filter(
(f) =>
f.fieldId === field.id &&
f.locale === locale,
)}
schemaField={field}
{locale}
dataField={data.draftData.find(
(f) => f.id === field.id && f.locale === locale,
)}
></TextField>
{/each}
<RecordStatus {channel} {record} status={data.recordStatus}></RecordStatus>
<div style="display:flex;gap:20px;justify-content: space-between;">
<LocaleChooser {channel} onLocaleChange={handleLocaleChange}
></LocaleChooser>
<PublishingOptions {record} status={data.recordStatus}
></PublishingOptions>
</div>
<fieldset disabled={data.recordStatus === "trashed"}>
{#each data.fields as field}
<div style="display:flex;gap:20px;">
{#if field.type === "text"}
<TextField
{channel}
{record}
validationError={data.validationErrors.find(
(f) =>
f.fieldId === field.id && f.locale === "main",
)}
schemaField={field}
locale="main"
dataField={data.draftData.find(
(f) => f.id === field.id && f.locale === "main",
)}
></TextField>
{#if field.translatable}
{#each selectedLocales as locale (locale)}
<TextField
{channel}
{record}
validationError={data.validationErrors.find(
(f) =>
f.fieldId === field.id &&
f.locale === locale,
)}
schemaField={field}
{locale}
dataField={data.draftData.find(
(f) =>
f.id === field.id &&
f.locale === locale,
)}
></TextField>
{/each}
{/if}
{/if}
{/if}
</div>
{/each}
</div>
{/each}
</fieldset>
{/snippet}
@@ -0,0 +1,84 @@
<script>
import { getApp } from "../../app";
import { post } from "../../modules/remote";
let { record, status } = $props();
const app = getApp();
function handleUntrash() {
post(app.url("records/untrash"), { id: record.id }, (data, err) => {
Turbo.visit(window.location.href);
});
}
function handleUnschedule() {
post(app.url("records/unschedule"), { id: record.id }, (data, err) => {
Turbo.visit(window.location.href);
});
}
function handleUnpublish() {
post(app.url("records/unpublish"), { id: record.id }, (data, err) => {
Turbo.visit(window.location.href);
});
}
</script>
<div
class="record-status record-status-{status}"
style="display: flex; gap:20px"
>
{#if status === "trashed"}
<span>This record is Trashed</span>
<button onclick={handleUntrash}>Restore</button>
{:else if status === "scheduled_and_published"}
<div>
<div>
<span>
This record was published at {record.publishedAt}
</span>
<button onclick={handleUnpublish}>Unpublish</button>
</div>
<div>
<span>
It is scheduled to be republished at {record.scheduledAt}
</span>
<button onclick={handleUnschedule}>Unschedule</button>
</div>
</div>
{:else if status === "published"}
<span>
This record was published at {record.publishedAt}
</span>
<button onclick={handleUnpublish}>Cancel</button>
{:else if status === "scheduled"}
<span>
This record is scheduled to be published at {record.scheduledAt}
</span>
<button onclick={handleUnschedule}>Cancel</button>
{:else}
<span>This record is a draft. Not yet published</span>
{/if}
</div>
<style>
.record-status {
border: 1px solid;
border-radius: 7px;
padding: 10px 20px;
margin: 0 0 30px;
font-size: 20px;
}
.record-status-trashed {
border-color: red;
}
.record-status-scheduled_and_published {
border-color: blue;
}
.record-status-published {
border-color: green;
}
.record-status-scheduled {
border-color: orange;
}
.record-status-draft {
border-color: gray;
}
</style>
@@ -2,13 +2,14 @@
import { post } from "../../../modules/remote";
import { getApp } from "../../../app";
import { getLocaleName } from "../locale.svelte.js";
let { channel, record, schemaField, dataField, locale, errors } = $props();
let { channel, record, schemaField, dataField, locale, validationError } =
$props();
let originalValue = dataField?.value ?? schemaField.props.default;
let newValue = $state(originalValue);
let valuesChanged = $derived(newValue !== originalValue);
let errorMessage = $state("");
let validationErrors = $state(errors);
let hasErrors = $derived(validationErrors.length > 0);
// let validationErrorState = $state(validationError);
let hasError = $derived(!!validationError);
const app = getApp();
function save() {
@@ -28,7 +29,7 @@
if (err.isNotEmpty()) {
errorMessage = err.first();
} else {
validationErrors = data.validationErrors;
validationError = data.validationError;
originalValue = newValue;
}
},
@@ -119,11 +120,11 @@
onblur={handleBlur}
oncompositionstart={handleCompositionStart}
oncompositionend={handleCompositionEnd}
aria-invalid={hasErrors ? "true" : ""}
aria-invalid={hasError ? "true" : ""}
/>
{#if hasErrors}
{#if hasError}
<small id={schemaField.id + "-help"}
>{validationErrors[0].message}</small
>{validationError.message}</small
>
{:else if schemaField.help != ""}
<small id={schemaField.id + "-help"}>{schemaField.help}</small>
+2 -4
View File
@@ -4,10 +4,6 @@ use Carbon\Carbon;
class Record
{
/**
* @param RecordField[] $draftData
* @param RecordField[] $liveData
*/
public function __construct(
public string $id,
public string $schemaId,
@@ -15,6 +11,8 @@ class Record
public string $createdBy,
public ?Carbon $publishedAt,
public ?string $publishedBy,
public ?Carbon $scheduledAt,
public ?string $scheduledBy,
public ?Carbon $trashedAt,
public ?string $trashedBy,
) {}
+10
View File
@@ -0,0 +1,10 @@
<?php namespace Lucent\Core\Data;
enum RecordStatus: string
{
case TRASHED = "trashed";
case PUBLISHED = "published";
case DRAFT = "draft";
case SCHEDULED = "scheduled";
case SCHEDULED_AND_PUBLISHED = "scheduled_and_published";
}
+24
View File
@@ -3,10 +3,34 @@
use Carbon\Carbon;
use Lucent\Core\Data\RecordField;
use Lucent\Core\Data\RecordMode;
use Lucent\Core\Repository\RecordFieldRepo;
use stdClass;
class RecordFieldModule
{
public static function publish(string $recordId): void
{
$recordFields = RecordFieldRepo::findByRecordId($recordId);
$liveData = collect($recordFields)
->where("mode", RecordMode::DRAFT)
->values()
->map(function (RecordField $f) {
$f->mode = RecordMode::LIVE;
return $f;
})
->toArray();
DB::transaction(function () use ($recordField) {
RecordFieldRepo::deleteLiveByRecordId($recordId);
RecordFieldRepo::insertMany($liveData);
});
}
public static function unpublish(string $recordId): void
{
RecordFieldRepo::deleteLiveByRecordId($recordId);
}
public static function fromDb(stdClass $field): RecordField
{
return new RecordField(
+23 -14
View File
@@ -3,22 +3,23 @@
use Carbon\Carbon;
use Lucent\Core\Data\Record;
use Lucent\Core\Data\RecordField;
use Lucent\Core\Data\RecordStatus;
use stdClass;
class RecordModule
{
public static function updateField(
Record $record,
RecordField $field,
): Record {
$recordFields = collect($record->draftData)->filter(
fn(RecordField $rf) => !(
$rf->id == $field->id && $rf->locale == $field->locale
),
);
$recordFields->push($field);
$record->draftData = $recordFields->values()->toArray();
return $record;
public static function getStatus(Record $record): RecordStatus
{
if ($record->trashedBy) {
return RecordStatus::TRASHED;
} elseif ($record->publishedBy && $record->scheduledAt) {
return RecordStatus::SCHEDULED_AND_PUBLISHED;
} elseif ($record->publishedBy) {
return RecordStatus::PUBLISHED;
} elseif ($record->scheduledAt) {
return RecordStatus::SCHEDULED;
}
return RecordStatus::DRAFT;
}
public static function toDb(Record $record): array
@@ -30,11 +31,15 @@ class RecordModule
"created_by" => $record->createdBy,
"published_at" => empty($record->publishedAt)
? null
: record->publishedAt->toJSON(),
: $record->publishedAt->toJSON(),
"published_by" => $record->publishedBy,
"scheduled_at" => empty($record->scheduledAt)
? null
: $record->scheduledAt->toJSON(),
"scheduled_by" => $record->scheduledBy,
"trashed_at" => empty($record->trashedAt)
? null
: record->trashedAt->toJSON(),
: $record->trashedAt->toJSON(),
"trashed_by" => $record->trashedBy,
];
}
@@ -50,6 +55,10 @@ class RecordModule
? null
: Carbon::parse(data_get($data, "published_at")),
publishedBy: data_get($data, "published_by"),
scheduledAt: empty(data_get($data, "scheduled_at"))
? null
: Carbon::parse(data_get($data, "scheduled_at")),
scheduledBy: data_get($data, "scheduled_by"),
trashedAt: empty(data_get($data, "trashed_at"))
? null
: Carbon::parse(data_get($data, "trashed_at")),
+81 -12
View File
@@ -1,5 +1,6 @@
<?php namespace Lucent\Core\Record;
use Illuminate\Support\Facades\Validator;
use Lucent\Core\Data\RecordError;
use Lucent\Core\Data\RecordField;
use Lucent\Core\Data\RecordMode;
@@ -23,7 +24,7 @@ class RecordValidationModule
$errors = [];
foreach ($schemaFields as $schemaField) {
$res = static::validateField("main", $schemaField, $recordFields);
$errors = array_merge($errors, $res);
$errors[] = $res;
if ($schemaField->translatable) {
foreach ($locales as $locale) {
$res = static::validateField(
@@ -31,7 +32,7 @@ class RecordValidationModule
$schemaField,
$recordFields,
);
$errors = array_merge($errors, $res);
$errors[] = $res;
}
}
}
@@ -52,15 +53,27 @@ class RecordValidationModule
string $locale,
Field $schemaField,
array $recordFields,
): array {
): ?RecordError {
// General Validations
$error = static::validateRequired($locale, $schemaField, $recordFields);
if (!empty($error)) {
return $error;
}
// Type specific
$error = match ($schemaField->type) {
"text" => static::validateText(
$locale,
$schemaField,
$recordFields,
),
default => static::validateText(
$locale,
$schemaField,
$recordFields,
),
};
return collect([$error])
->filter(fn($err) => $err !== null)
->values()
->toArray();
return $error;
}
/**
@@ -77,11 +90,7 @@ class RecordValidationModule
if ($schemaField->required === false) {
return null;
}
$recordField = collect($recordFields)->first(
fn(RecordField $field) => $field->id === $schemaField->id &&
$field->locale === $locale &&
$field->mode === RecordMode::DRAFT,
);
$recordField = static::findField($recordFields, $schemaField, $locale);
if (empty($recordField) || empty($recordField->value)) {
return new RecordError(
@@ -93,4 +102,64 @@ class RecordValidationModule
return null;
}
/**
*
* @param Field $schemaField
* @param RecordField[] $recordFields
* @return ?RecordError
*/
public static function validateText(
string $locale,
Field $schemaField,
array $recordFields,
): ?RecordError {
$recordField = static::findField($recordFields, $schemaField, $locale);
if (empty($recordField)) {
// We have already checked for required fields so only validate existing
return null;
}
$rules = "";
if ($schemaField->props->min > 0) {
$rules = "min:" . $schemaField->props->min;
}
if ($schemaField->props->max > 0) {
$rules = "max:" . $schemaField->props->max;
}
if (empty($rules)) {
return null;
}
$validator = Validator::make(
["value" => $recordField->value],
[
"value" => $rules,
],
);
if ($validator->fails()) {
return new RecordError(
$schemaField->id,
$locale,
$validator->errors()->first(),
);
}
return null;
}
private static function findField(
array $recordFields,
Field $schemaField,
string $locale,
): ?RecordField {
return collect($recordFields)->first(
fn(RecordField $field) => $field->id === $schemaField->id &&
$field->locale === $locale &&
$field->mode === RecordMode::DRAFT,
);
}
}
+16
View File
@@ -2,6 +2,7 @@
use Illuminate\Support\Facades\DB;
use Lucent\Core\Data\RecordField;
use Lucent\Core\Data\RecordMode;
use Lucent\Core\Record\RecordFieldModule;
class RecordFieldRepo
@@ -15,6 +16,21 @@ class RecordFieldRepo
);
}
public static function insertMany(array $recordFields): void
{
DB::table(self::TABLE_NAME)->insert(
array_map(RecordFieldModule::toDb(...), $recordFields),
);
}
public static function deleteLiveByRecordId(string $recordId): void
{
DB::table(self::TABLE_NAME)
->where("record_id", $recordId)
->where("mode", RecordMode::LIVE->value)
->delete();
}
public static function delete(RecordField $field): void
{
DB::table(self::TABLE_NAME)
+7
View File
@@ -13,6 +13,13 @@ class RecordRepo
DB::table(self::TABLE_NAME)->insert(RecordModule::toDb($record));
}
public static function update(Record $record): void
{
DB::table(self::TABLE_NAME)
->where("id", $record->id)
->update(RecordModule::toDb($record));
}
public static function findOne(string $id): ?Record
{
return DB::table(self::TABLE_NAME)
+105 -2
View File
@@ -11,6 +11,7 @@ use Lucent\Core\Data\RecordField;
use Lucent\Core\Repository\FieldRepo;
use Lucent\Core\Repository\SchemaRepo;
use Lucent\Core\Repository\RecordFieldRepo;
use Lucent\Core\Record\RecordModule;
use Lucent\Id\Id;
use Lucent\LucentException;
use Lucent\Query\Operator\OperatorRegistry;
@@ -28,6 +29,7 @@ use Lucent\Schema\Validator\ValidatorException;
use Lucent\Svelte\Svelte;
use Lucent\ViewModel\ViewModel;
use Lucent\Core\Data\Record;
use Lucent\Core\Data\RecordStatus;
use Lucent\Core\Data\RecordMode;
use Illuminate\Support\Facades\DB;
use function Lucent\Response\fail;
@@ -265,6 +267,8 @@ class RecordController
$draftData,
);
$recordStatus = RecordModule::getStatus($record);
return Svelte::view(
view: "recordEdit",
title: "Edit Record",
@@ -276,6 +280,7 @@ class RecordController
"record" => toArray($record),
"draftData" => $draftData,
"validationErrors" => $validationErrors,
"recordStatus" => $recordStatus,
],
);
}
@@ -325,6 +330,8 @@ class RecordController
createdBy: $userId,
publishedAt: null,
publishedBy: null,
scheduledAt: null,
scheduledBy: null,
trashedAt: null,
trashedBy: null,
);
@@ -371,7 +378,7 @@ class RecordController
RecordFieldRepo::insert($recordField);
});
$validationErrors = RecordValidationModule::validateField(
$validationError = RecordValidationModule::validateField(
$locale,
$field,
[$recordField],
@@ -379,7 +386,7 @@ class RecordController
return ok([
"recordField" => toArray($recordField),
"validationErrors" => $validationErrors,
"validationError" => $validationError,
]);
}
@@ -487,4 +494,100 @@ class RecordController
}
return ok();
}
public function publish(Request $request)
{
$id = $request->input("id");
$record = RecordRepo::findOne($id);
$recordStatus = RecordModule::getStatus($record);
if ($recordStatus == RecordStatus::TRASHED) {
return fail("Record is in trash");
}
$userId = AuthModule::getCurrentUserId();
$record->publishedAt = now();
$record->publishedBy = $userId;
$record->scheduledAt = null;
$record->scheduledBy = null;
RecordFieldModule::publish($record->id);
RecordRepo::update($record);
return ok(toArray($record));
}
public function unpublish(Request $request)
{
$id = $request->input("id");
$record = RecordRepo::findOne($id);
$record->publishedAt = null;
$record->publishedBy = null;
RecordFieldModule::unpublish($record->id);
RecordRepo::update($record);
return ok(toArray($record));
}
public function schedule(Request $request)
{
$id = $request->input("id");
$date = $request->input("date");
$record = RecordRepo::findOne($id);
$recordStatus = RecordModule::getStatus($record);
$userId = AuthModule::getCurrentUserId();
if ($recordStatus == RecordStatus::TRASHED) {
return fail("Record is in trash");
}
$record->scheduledAt = Carbon::parse($date);
$record->scheduledBy = $userId;
RecordRepo::update($record);
return ok(toArray($record));
}
public function unschedule(Request $request)
{
$id = $request->input("id");
$record = RecordRepo::findOne($id);
$record->scheduledAt = null;
$record->scheduledBy = null;
RecordRepo::update($record);
return ok(toArray($record));
}
public function trash(Request $request)
{
$id = $request->input("id");
$record = RecordRepo::findOne($id);
$userId = AuthModule::getCurrentUserId();
$record->trashedAt = now();
$record->trashedBy = $userId;
$record->publishedAt = null;
$record->publishedBy = null;
$record->scheduledAt = null;
$record->scheduledBy = null;
RecordRepo::update($record);
return ok(toArray($record));
}
public function untrash(Request $request)
{
$id = $request->input("id");
$record = RecordRepo::findOne($id);
$record->trashedAt = null;
$record->trashedBy = null;
RecordRepo::update($record);
return ok(toArray($record));
}
}
+21
View File
@@ -76,6 +76,27 @@ Route::group(
RecordController::class,
"saveField",
]);
Route::post("records/publish", [
RecordController::class,
"publish",
]);
Route::post("records/unpublish", [
RecordController::class,
"unpublish",
]);
Route::post("records/schedule", [
RecordController::class,
"schedule",
]);
Route::post("records/unschedule", [
RecordController::class,
"unschedule",
]);
Route::post("records/trash", [RecordController::class, "trash"]);
Route::post("records/untrash", [
RecordController::class,
"untrash",
]);
});
//OLD