mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 10:35:30 +00:00
Save button
This commit is contained in:
@@ -1,18 +1,27 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers/functions"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/middleware"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
const (
|
||||
URL_ALMANACH_EDIT = "edit/"
|
||||
TEMPLATE_ALMANACH_EDIT = "/almanach/edit/"
|
||||
URL_ALMANACH_EDIT = "edit/"
|
||||
TEMPLATE_ALMANACH_EDIT = "/almanach/edit/"
|
||||
preferredSeriesRelationType = "Bevorzugter Reihentitel"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -35,6 +44,7 @@ func (p *AlmanachEditPage) Setup(router *router.Router[*core.RequestEvent], app
|
||||
rg := router.Group(URL_ALMANACH)
|
||||
rg.BindFunc(middleware.IsAdminOrEditor())
|
||||
rg.GET(URL_ALMANACH_EDIT, p.GET(engine, app))
|
||||
rg.POST(URL_ALMANACH_EDIT+"save", p.POSTSave(engine, app))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -60,6 +70,10 @@ func (p *AlmanachEditPage) GET(engine *templating.Engine, app core.App) HandleFu
|
||||
data["abbrs"] = abbrs
|
||||
}
|
||||
|
||||
if msg := e.Request.URL.Query().Get("saved_message"); msg != "" {
|
||||
data["success"] = msg
|
||||
}
|
||||
|
||||
return engine.Response200(e, p.Template, data, p.Layout)
|
||||
}
|
||||
}
|
||||
@@ -97,3 +111,481 @@ func NewAlmanachEditResult(app core.App, id string, filters BeitraegeFilterParam
|
||||
Prev: prev,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *AlmanachEditPage) POSTSave(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return func(e *core.RequestEvent) error {
|
||||
id := e.Request.PathValue("id")
|
||||
req := templating.NewRequest(e)
|
||||
|
||||
payload := almanachEditPayload{}
|
||||
if err := e.BindBody(&payload); err != nil {
|
||||
return e.JSON(http.StatusBadRequest, map[string]any{
|
||||
"error": "Ungültige Formulardaten.",
|
||||
})
|
||||
}
|
||||
|
||||
if err := req.CheckCSRF(payload.CSRFToken); err != nil {
|
||||
return e.JSON(http.StatusBadRequest, map[string]any{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
entry, err := dbmodels.Entries_MusenalmID(app, id)
|
||||
if err != nil {
|
||||
return e.JSON(http.StatusNotFound, map[string]any{
|
||||
"error": "Band wurde nicht gefunden.",
|
||||
})
|
||||
}
|
||||
|
||||
if err := payload.Validate(); err != nil {
|
||||
return e.JSON(http.StatusBadRequest, map[string]any{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
if payload.LastEdited != "" {
|
||||
lastEdited, err := types.ParseDateTime(payload.LastEdited)
|
||||
if err != nil {
|
||||
return e.JSON(http.StatusBadRequest, map[string]any{
|
||||
"error": "Ungültiger Bearbeitungszeitstempel.",
|
||||
})
|
||||
}
|
||||
if !entry.Updated().Time().Equal(lastEdited.Time()) {
|
||||
return e.JSON(http.StatusConflict, map[string]any{
|
||||
"error": "Der Eintrag wurde inzwischen geändert. Bitte Seite neu laden.",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
user := req.User()
|
||||
if err := app.RunInTransaction(func(tx core.App) error {
|
||||
if err := applyEntryChanges(tx, entry, &payload, user); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := applyItemsChanges(tx, entry, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := applySeriesRelations(tx, entry, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := applyAgentRelations(tx, entry, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
app.Logger().Error("Failed to save almanach entry", "entry_id", entry.Id, "error", err)
|
||||
return e.JSON(http.StatusInternalServerError, map[string]any{
|
||||
"error": "Speichern fehlgeschlagen.",
|
||||
})
|
||||
}
|
||||
|
||||
freshEntry, err := dbmodels.Entries_MusenalmID(app, id)
|
||||
if err == nil {
|
||||
entry = freshEntry
|
||||
}
|
||||
|
||||
updatedInfo := map[string]string{
|
||||
"raw": entry.Updated().Time().Format(time.RFC3339Nano),
|
||||
"date": functions.GermanDate(entry.Updated()),
|
||||
"time": functions.GermanTime(entry.Updated()),
|
||||
}
|
||||
if user != nil && strings.TrimSpace(user.Name) != "" {
|
||||
updatedInfo["user"] = user.Name
|
||||
}
|
||||
|
||||
return e.JSON(http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"message": "Änderungen gespeichert.",
|
||||
"updated": updatedInfo,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type almanachEditPayload struct {
|
||||
CSRFToken string `json:"csrf_token"`
|
||||
LastEdited string `json:"last_edited"`
|
||||
Entry almanachEntryPayload `json:"entry"`
|
||||
Languages []string `json:"languages"`
|
||||
Places []string `json:"places"`
|
||||
Items []almanachItemPayload `json:"items"`
|
||||
DeletedItemIDs []string `json:"deleted_item_ids"`
|
||||
SeriesRelations []almanachRelationPayload `json:"series_relations"`
|
||||
NewSeriesRelations []almanachNewRelationPayload `json:"new_series_relations"`
|
||||
DeletedSeriesRelationIDs []string `json:"deleted_series_relation_ids"`
|
||||
AgentRelations []almanachRelationPayload `json:"agent_relations"`
|
||||
NewAgentRelations []almanachNewRelationPayload `json:"new_agent_relations"`
|
||||
DeletedAgentRelationIDs []string `json:"deleted_agent_relation_ids"`
|
||||
}
|
||||
|
||||
type almanachEntryPayload struct {
|
||||
PreferredTitle string `json:"preferred_title"`
|
||||
Title string `json:"title"`
|
||||
ParallelTitle string `json:"parallel_title"`
|
||||
Subtitle string `json:"subtitle"`
|
||||
VariantTitle string `json:"variant_title"`
|
||||
Incipit string `json:"incipit"`
|
||||
ResponsibilityStatement string `json:"responsibility_statement"`
|
||||
PublicationStatement string `json:"publication_statement"`
|
||||
PlaceStatement string `json:"place_statement"`
|
||||
Edition string `json:"edition"`
|
||||
Annotation string `json:"annotation"`
|
||||
EditComment string `json:"edit_comment"`
|
||||
Extent string `json:"extent"`
|
||||
Dimensions string `json:"dimensions"`
|
||||
References string `json:"references"`
|
||||
Status string `json:"status"`
|
||||
Year *int `json:"year"`
|
||||
}
|
||||
|
||||
type almanachItemPayload struct {
|
||||
ID string `json:"id"`
|
||||
Owner string `json:"owner"`
|
||||
Identifier string `json:"identifier"`
|
||||
Location string `json:"location"`
|
||||
Media []string `json:"media"`
|
||||
Annotation string `json:"annotation"`
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
|
||||
type almanachRelationPayload struct {
|
||||
ID string `json:"id"`
|
||||
TargetID string `json:"target_id"`
|
||||
Type string `json:"type"`
|
||||
Uncertain bool `json:"uncertain"`
|
||||
}
|
||||
|
||||
type almanachNewRelationPayload struct {
|
||||
TargetID string `json:"target_id"`
|
||||
Type string `json:"type"`
|
||||
Uncertain bool `json:"uncertain"`
|
||||
}
|
||||
|
||||
func (payload *almanachEditPayload) Validate() error {
|
||||
payload.Entry.Status = strings.TrimSpace(payload.Entry.Status)
|
||||
if strings.TrimSpace(payload.Entry.PreferredTitle) == "" {
|
||||
return fmt.Errorf("Kurztitel ist erforderlich.")
|
||||
}
|
||||
if payload.Entry.Year == nil {
|
||||
return fmt.Errorf("Jahr muss angegeben werden.")
|
||||
}
|
||||
if payload.Entry.Status == "" || !slices.Contains(dbmodels.EDITORSTATE_VALUES, payload.Entry.Status) {
|
||||
return fmt.Errorf("Ungültiger Status.")
|
||||
}
|
||||
for _, relation := range payload.SeriesRelations {
|
||||
if err := validateRelationType(relation.Type, dbmodels.SERIES_RELATIONS); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, relation := range payload.NewSeriesRelations {
|
||||
if err := validateRelationType(relation.Type, dbmodels.SERIES_RELATIONS); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, relation := range payload.AgentRelations {
|
||||
if err := validateRelationType(relation.Type, dbmodels.AGENT_RELATIONS); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, relation := range payload.NewAgentRelations {
|
||||
if err := validateRelationType(relation.Type, dbmodels.AGENT_RELATIONS); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
hasPreferred := false
|
||||
for _, relation := range payload.SeriesRelations {
|
||||
if strings.TrimSpace(relation.Type) == preferredSeriesRelationType {
|
||||
hasPreferred = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasPreferred {
|
||||
for _, relation := range payload.NewSeriesRelations {
|
||||
if strings.TrimSpace(relation.Type) == preferredSeriesRelationType {
|
||||
hasPreferred = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !hasPreferred {
|
||||
return fmt.Errorf("Mindestens ein bevorzugter Reihentitel muss verknüpft sein.")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRelationType(value string, allowed []string) error {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return fmt.Errorf("Ungültiger Beziehungstyp.")
|
||||
}
|
||||
if !slices.Contains(allowed, value) {
|
||||
return fmt.Errorf("Ungültiger Beziehungstyp.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sanitizeStrings(values []string) []string {
|
||||
seen := map[string]struct{}{}
|
||||
cleaned := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[value]; ok {
|
||||
continue
|
||||
}
|
||||
seen[value] = struct{}{}
|
||||
cleaned = append(cleaned, value)
|
||||
}
|
||||
return cleaned
|
||||
}
|
||||
|
||||
func applyEntryChanges(tx core.App, entry *dbmodels.Entry, payload *almanachEditPayload, user *dbmodels.FixedUser) error {
|
||||
entry.SetPreferredTitle(strings.TrimSpace(payload.Entry.PreferredTitle))
|
||||
entry.SetTitleStmt(strings.TrimSpace(payload.Entry.Title))
|
||||
entry.SetParallelTitle(strings.TrimSpace(payload.Entry.ParallelTitle))
|
||||
entry.SetSubtitleStmt(strings.TrimSpace(payload.Entry.Subtitle))
|
||||
entry.SetVariantTitle(strings.TrimSpace(payload.Entry.VariantTitle))
|
||||
entry.SetIncipitStmt(strings.TrimSpace(payload.Entry.Incipit))
|
||||
entry.SetResponsibilityStmt(strings.TrimSpace(payload.Entry.ResponsibilityStatement))
|
||||
entry.SetPublicationStmt(strings.TrimSpace(payload.Entry.PublicationStatement))
|
||||
entry.SetPlaceStmt(strings.TrimSpace(payload.Entry.PlaceStatement))
|
||||
entry.SetEdition(strings.TrimSpace(payload.Entry.Edition))
|
||||
entry.SetAnnotation(strings.TrimSpace(payload.Entry.Annotation))
|
||||
entry.SetComment(strings.TrimSpace(payload.Entry.EditComment))
|
||||
entry.SetExtent(strings.TrimSpace(payload.Entry.Extent))
|
||||
entry.SetDimensions(strings.TrimSpace(payload.Entry.Dimensions))
|
||||
entry.SetReferences(strings.TrimSpace(payload.Entry.References))
|
||||
entry.SetYear(*payload.Entry.Year)
|
||||
entry.SetEditState(payload.Entry.Status)
|
||||
entry.SetLanguage(sanitizeStrings(payload.Languages))
|
||||
entry.SetPlaces(sanitizeStrings(payload.Places))
|
||||
if user != nil {
|
||||
entry.SetEditor(user.Id)
|
||||
}
|
||||
return tx.Save(entry)
|
||||
}
|
||||
|
||||
func applyItemsChanges(tx core.App, entry *dbmodels.Entry, payload *almanachEditPayload) error {
|
||||
var itemsCollection *core.Collection
|
||||
getItemsCollection := func() (*core.Collection, error) {
|
||||
if itemsCollection != nil {
|
||||
return itemsCollection, nil
|
||||
}
|
||||
collection, err := tx.FindCollectionByNameOrId(dbmodels.ITEMS_TABLE)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
itemsCollection = collection
|
||||
return itemsCollection, nil
|
||||
}
|
||||
|
||||
for _, itemPayload := range payload.Items {
|
||||
itemID := strings.TrimSpace(itemPayload.ID)
|
||||
var item *dbmodels.Item
|
||||
if itemID != "" {
|
||||
record, err := tx.FindRecordById(dbmodels.ITEMS_TABLE, itemID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
item = dbmodels.NewItem(record)
|
||||
if item.Entry() != entry.Id {
|
||||
return fmt.Errorf("Exemplar %s gehört zu einem anderen Eintrag.", itemID)
|
||||
}
|
||||
} else {
|
||||
collection, err := getItemsCollection()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
item = dbmodels.NewItem(core.NewRecord(collection))
|
||||
}
|
||||
|
||||
item.SetEntry(entry.Id)
|
||||
item.SetOwner(strings.TrimSpace(itemPayload.Owner))
|
||||
item.SetIdentifier(strings.TrimSpace(itemPayload.Identifier))
|
||||
item.SetLocation(strings.TrimSpace(itemPayload.Location))
|
||||
item.SetAnnotation(strings.TrimSpace(itemPayload.Annotation))
|
||||
item.SetUri(strings.TrimSpace(itemPayload.URI))
|
||||
item.SetMedia(sanitizeStrings(itemPayload.Media))
|
||||
|
||||
if err := tx.Save(item); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, id := range payload.DeletedItemIDs {
|
||||
itemID := strings.TrimSpace(id)
|
||||
if itemID == "" {
|
||||
continue
|
||||
}
|
||||
record, err := tx.FindRecordById(dbmodels.ITEMS_TABLE, itemID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
item := dbmodels.NewItem(record)
|
||||
if item.Entry() != entry.Id {
|
||||
continue
|
||||
}
|
||||
if err := tx.Delete(record); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func applySeriesRelations(tx core.App, entry *dbmodels.Entry, payload *almanachEditPayload) error {
|
||||
tableName := dbmodels.RelationTableName(dbmodels.ENTRIES_TABLE, dbmodels.SERIES_TABLE)
|
||||
var collection *core.Collection
|
||||
getCollection := func() (*core.Collection, error) {
|
||||
if collection != nil {
|
||||
return collection, nil
|
||||
}
|
||||
col, err := tx.FindCollectionByNameOrId(tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
collection = col
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
for _, relation := range payload.SeriesRelations {
|
||||
relationID := strings.TrimSpace(relation.ID)
|
||||
if relationID == "" {
|
||||
continue
|
||||
}
|
||||
record, err := tx.FindRecordById(tableName, relationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proxy := dbmodels.NewREntriesSeries(record)
|
||||
if proxy.Entry() != entry.Id {
|
||||
return fmt.Errorf("Relation %s gehört zu einem anderen Eintrag.", relationID)
|
||||
}
|
||||
proxy.SetEntry(entry.Id)
|
||||
proxy.SetSeries(strings.TrimSpace(relation.TargetID))
|
||||
proxy.SetType(strings.TrimSpace(relation.Type))
|
||||
proxy.SetUncertain(relation.Uncertain)
|
||||
if err := tx.Save(proxy); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, relationID := range payload.DeletedSeriesRelationIDs {
|
||||
relationID = strings.TrimSpace(relationID)
|
||||
if relationID == "" {
|
||||
continue
|
||||
}
|
||||
record, err := tx.FindRecordById(tableName, relationID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
proxy := dbmodels.NewREntriesSeries(record)
|
||||
if proxy.Entry() != entry.Id {
|
||||
continue
|
||||
}
|
||||
if err := tx.Delete(record); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, relation := range payload.NewSeriesRelations {
|
||||
targetID := strings.TrimSpace(relation.TargetID)
|
||||
if targetID == "" {
|
||||
continue
|
||||
}
|
||||
col, err := getCollection()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proxy := dbmodels.NewREntriesSeries(core.NewRecord(col))
|
||||
proxy.SetEntry(entry.Id)
|
||||
proxy.SetSeries(targetID)
|
||||
proxy.SetType(strings.TrimSpace(relation.Type))
|
||||
proxy.SetUncertain(relation.Uncertain)
|
||||
if err := tx.Save(proxy); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyAgentRelations(tx core.App, entry *dbmodels.Entry, payload *almanachEditPayload) error {
|
||||
tableName := dbmodels.RelationTableName(dbmodels.ENTRIES_TABLE, dbmodels.AGENTS_TABLE)
|
||||
var collection *core.Collection
|
||||
getCollection := func() (*core.Collection, error) {
|
||||
if collection != nil {
|
||||
return collection, nil
|
||||
}
|
||||
col, err := tx.FindCollectionByNameOrId(tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
collection = col
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
for _, relation := range payload.AgentRelations {
|
||||
relationID := strings.TrimSpace(relation.ID)
|
||||
if relationID == "" {
|
||||
continue
|
||||
}
|
||||
record, err := tx.FindRecordById(tableName, relationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proxy := dbmodels.NewREntriesAgents(record)
|
||||
if proxy.Entry() != entry.Id {
|
||||
return fmt.Errorf("Relation %s gehört zu einem anderen Eintrag.", relationID)
|
||||
}
|
||||
proxy.SetEntry(entry.Id)
|
||||
proxy.SetAgent(strings.TrimSpace(relation.TargetID))
|
||||
proxy.SetType(strings.TrimSpace(relation.Type))
|
||||
proxy.SetUncertain(relation.Uncertain)
|
||||
if err := tx.Save(proxy); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, relationID := range payload.DeletedAgentRelationIDs {
|
||||
relationID = strings.TrimSpace(relationID)
|
||||
if relationID == "" {
|
||||
continue
|
||||
}
|
||||
record, err := tx.FindRecordById(tableName, relationID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
proxy := dbmodels.NewREntriesAgents(record)
|
||||
if proxy.Entry() != entry.Id {
|
||||
continue
|
||||
}
|
||||
if err := tx.Delete(record); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, relation := range payload.NewAgentRelations {
|
||||
targetID := strings.TrimSpace(relation.TargetID)
|
||||
if targetID == "" {
|
||||
continue
|
||||
}
|
||||
col, err := getCollection()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proxy := dbmodels.NewREntriesAgents(core.NewRecord(col))
|
||||
proxy.SetEntry(entry.Id)
|
||||
proxy.SetAgent(targetID)
|
||||
proxy.SetType(strings.TrimSpace(relation.Type))
|
||||
proxy.SetUncertain(relation.Uncertain)
|
||||
if err := tx.Save(proxy); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -80,17 +80,16 @@ type AlmanachResult struct {
|
||||
<div class="">
|
||||
<div class="font-bold text-sm mb-1"><i class="ri-calendar-line"></i> Zuletzt bearbeitet</div>
|
||||
<div>
|
||||
<div class="px-1.5 py-0.5 rounded-xs bg-gray-200 w-fit">
|
||||
{{ GermanDate $model.result.Entry.Updated }},
|
||||
{{ GermanTime
|
||||
$model.result.Entry.Updated
|
||||
}}h
|
||||
<div class="px-1.5 py-0.5 rounded-xs bg-gray-200 w-fit" id="almanach-updated-stamp">
|
||||
<span id="almanach-updated-date">{{ GermanDate $model.result.Entry.Updated }}</span>,
|
||||
<span id="almanach-updated-time">{{ GermanTime $model.result.Entry.Updated }}</span>h
|
||||
</div>
|
||||
<div
|
||||
class="px-1.5 py-0.5 rounded-xs mt-1.5 bg-gray-200 w-fit {{ if not $model.result.User }}hidden{{ end }}"
|
||||
id="almanach-updated-user">
|
||||
<i class="ri-user-line mr-1"></i>
|
||||
<span id="almanach-updated-user-name">{{- if $model.result.User -}}{{ $model.result.User.Name }}{{- end -}}</span>
|
||||
</div>
|
||||
{{- if $model.result.User -}}
|
||||
<div class="px-1.5 py-0.5 rounded-xs mt-1.5 bg-gray-200 w-fit">
|
||||
<i class="ri-user-line mr-1"></i> {{- $model.result.User.Name -}}
|
||||
</div>
|
||||
{{- end -}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -100,10 +99,17 @@ type AlmanachResult struct {
|
||||
|
||||
<div class="container-normal mx-auto mt-4 !px-0">
|
||||
{{ template "_usermessage" $model }}
|
||||
<form class="w-full flex gap-8 dbform" id="changealmanachform" x-target="changealmanachform user-message almanach-header-data" hx-boost="false" method="POST">
|
||||
<form
|
||||
class="w-full dbform"
|
||||
id="changealmanachform"
|
||||
x-target="changealmanachform user-message almanach-header-data"
|
||||
hx-boost="false"
|
||||
method="POST"
|
||||
data-save-endpoint="/almanach/{{ $model.result.Entry.MusenalmID }}/edit/save">
|
||||
<input type="hidden" name="csrf_token" value="{{ $model.csrf_token }}" />
|
||||
<input type="hidden" name="last_edited" value="{{ $model.result.Entry.Updated }}" />
|
||||
|
||||
<div class="flex gap-8">
|
||||
<!-- Left Column -->
|
||||
<div class="flex-1 flex flex-col gap-4">
|
||||
<!-- Kurztitel -->
|
||||
@@ -553,6 +559,7 @@ type AlmanachResult struct {
|
||||
<multi-select-simple
|
||||
id="places"
|
||||
name="places[]"
|
||||
value='[{{- range $i, $place := $model.result.Places -}}{{- if $i }},{{ end -}}"{{ $place.Id }}"{{- end -}}]'
|
||||
placeholder="Orte suchen..."
|
||||
data-toggle-label='<i class="ri-add-circle-line"></i>'
|
||||
data-empty-text="Keine Orte ausgewählt..."
|
||||
@@ -561,8 +568,8 @@ type AlmanachResult struct {
|
||||
data-result-key="places"
|
||||
data-minchars="1"
|
||||
data-limit="15"
|
||||
data-initial-options='[{{- range $i, $place := $model.result.Places -}}{{- if $i }},{{ end -}}{"id":"{{ $place.Id }}","name":{{ printf "%q" $place.Name }},"additional_data":{{ printf "%q" $place.Pseudonyms }}}}{{- end -}}]'
|
||||
data-initial-values='[{{- range $i, $place := $model.result.Places -}}{{- if $i }},{{ end -}}"{{ $place.Id }}"{{- end -}}]'>
|
||||
data-initial-options='[{{- range $i, $place := $model.result.Places -}}{{- if $i }},{{ end -}}{{ printf "{\"id\":%q,\"name\":%q,\"additional_data\":%q}" $place.Id $place.Name $place.Pseudonyms }}{{- end -}}]'
|
||||
data-initial-values='[{{- range $i, $place := $model.result.Places -}}{{- if $i }},{{ end -}}{{ printf "%q" $place.Id }}{{- end -}}]'>
|
||||
</multi-select-simple>
|
||||
</div>
|
||||
</div>
|
||||
@@ -655,6 +662,7 @@ type AlmanachResult struct {
|
||||
<label for="languages" class="inputlabel">Sprachen</label>
|
||||
<multi-select-simple
|
||||
id="languages"
|
||||
name="languages[]"
|
||||
show-create-button="false"
|
||||
placeholder="Sprachen suchen..."
|
||||
data-toggle-label='<i class="ri-add-circle-line"></i>'
|
||||
@@ -887,6 +895,14 @@ type AlmanachResult struct {
|
||||
</div>
|
||||
</div>
|
||||
<!-- End Right Column -->
|
||||
</div>
|
||||
<div class="w-full flex items-end justify-between gap-4 mt-6 flex-wrap">
|
||||
<p id="almanach-save-feedback" class="text-sm text-gray-600" aria-live="polite"></p>
|
||||
<button type="button" class="submitbutton flex items-center gap-2 self-end" data-role="almanach-save">
|
||||
<i class="ri-save-line"></i>
|
||||
<span>Speichern</span>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</almanach-edit-page>
|
||||
|
||||
@@ -1,12 +1,25 @@
|
||||
const PREFERRED_SERIES_RELATION = "Bevorzugter Reihentitel";
|
||||
|
||||
export class AlmanachEditPage extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
this._pendingAgent = null;
|
||||
this._form = null;
|
||||
this._saveButton = null;
|
||||
this._statusEl = null;
|
||||
this._saveEndpoint = "";
|
||||
this._isSaving = false;
|
||||
this._handleSaveClick = this._handleSaveClick.bind(this);
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this._initForm();
|
||||
this._initPlaces();
|
||||
this._initSaveHandling();
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
this._teardownSaveHandling();
|
||||
}
|
||||
|
||||
_initForm() {
|
||||
@@ -31,20 +44,383 @@ export class AlmanachEditPage extends HTMLElement {
|
||||
}
|
||||
}
|
||||
|
||||
_initPlaces() {
|
||||
const placesSelect = this.querySelector("#places");
|
||||
if (!placesSelect) {
|
||||
_initPlaces() {
|
||||
const placesSelect = this.querySelector("#places");
|
||||
if (!placesSelect) {
|
||||
return;
|
||||
}
|
||||
const applyInitial = () => {
|
||||
const initialPlaces = this._parseJSONAttr(placesSelect, "data-initial-options") || [];
|
||||
const initialPlaceIds = this._parseJSONAttr(placesSelect, "data-initial-values") || [];
|
||||
if (initialPlaces.length > 0 && typeof placesSelect.setOptions === "function") {
|
||||
placesSelect.setOptions(initialPlaces);
|
||||
}
|
||||
if (initialPlaceIds.length > 0) {
|
||||
placesSelect.value = initialPlaceIds;
|
||||
if (typeof placesSelect.captureInitialSelection === "function") {
|
||||
placesSelect.captureInitialSelection();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof placesSelect.setOptions === "function") {
|
||||
applyInitial();
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof window.customElements?.whenDefined === "function") {
|
||||
window.customElements.whenDefined("multi-select-simple").then(() => {
|
||||
requestAnimationFrame(() => applyInitial());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_initSaveHandling() {
|
||||
this._teardownSaveHandling();
|
||||
this._form = this.querySelector("#changealmanachform");
|
||||
this._saveButton = this.querySelector("[data-role='almanach-save']");
|
||||
this._statusEl = this.querySelector("#almanach-save-feedback");
|
||||
if (!this._form || !this._saveButton) {
|
||||
return;
|
||||
}
|
||||
const initialPlaces = this._parseJSONAttr(placesSelect, "data-initial-options") || [];
|
||||
const initialPlaceIds = this._parseJSONAttr(placesSelect, "data-initial-values") || [];
|
||||
this._saveEndpoint = this._form.getAttribute("data-save-endpoint") || this._deriveSaveEndpoint();
|
||||
this._saveButton.addEventListener("click", this._handleSaveClick);
|
||||
}
|
||||
|
||||
if (initialPlaces.length > 0 && typeof placesSelect.setOptions === "function") {
|
||||
placesSelect.setOptions(initialPlaces);
|
||||
_teardownSaveHandling() {
|
||||
if (this._saveButton) {
|
||||
this._saveButton.removeEventListener("click", this._handleSaveClick);
|
||||
}
|
||||
if (initialPlaceIds.length > 0) {
|
||||
placesSelect.value = initialPlaceIds;
|
||||
this._saveButton = null;
|
||||
this._statusEl = null;
|
||||
}
|
||||
|
||||
_deriveSaveEndpoint() {
|
||||
if (!window?.location?.pathname) {
|
||||
return "/almanach/save";
|
||||
}
|
||||
const path = window.location.pathname.endsWith("/")
|
||||
? window.location.pathname.slice(0, -1)
|
||||
: window.location.pathname;
|
||||
return `${path}/save`;
|
||||
}
|
||||
|
||||
async _handleSaveClick(event) {
|
||||
event.preventDefault();
|
||||
if (this._isSaving) {
|
||||
return;
|
||||
}
|
||||
this._clearStatus();
|
||||
let payload;
|
||||
try {
|
||||
payload = this._buildPayload();
|
||||
} catch (error) {
|
||||
this._showStatus(error instanceof Error ? error.message : String(error), "error");
|
||||
return;
|
||||
}
|
||||
this._setSavingState(true);
|
||||
try {
|
||||
const response = await fetch(this._saveEndpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
let data = null;
|
||||
try {
|
||||
data = await response.clone().json();
|
||||
} catch {
|
||||
data = null;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const message = data?.error || `Speichern fehlgeschlagen (${response.status}).`;
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
await this._reloadForm(data?.message || "Änderungen gespeichert.");
|
||||
this._clearStatus();
|
||||
} catch (error) {
|
||||
this._showStatus(error instanceof Error ? error.message : "Speichern fehlgeschlagen.", "error");
|
||||
} finally {
|
||||
this._setSavingState(false);
|
||||
}
|
||||
}
|
||||
|
||||
_buildPayload() {
|
||||
if (!this._form) {
|
||||
throw new Error("Formular konnte nicht gefunden werden.");
|
||||
}
|
||||
const formData = new FormData(this._form);
|
||||
const entry = {
|
||||
preferred_title: this._readValue(formData, "preferred_title"),
|
||||
title: this._readValue(formData, "title"),
|
||||
parallel_title: this._readValue(formData, "paralleltitle"),
|
||||
subtitle: this._readValue(formData, "subtitle"),
|
||||
variant_title: this._readValue(formData, "varianttitle"),
|
||||
incipit: this._readValue(formData, "incipit"),
|
||||
responsibility_statement: this._readValue(formData, "responsibility_statement"),
|
||||
publication_statement: this._readValue(formData, "publication_statement"),
|
||||
place_statement: this._readValue(formData, "place_statement"),
|
||||
edition: this._readValue(formData, "edition"),
|
||||
annotation: this._readValue(formData, "annotation"),
|
||||
edit_comment: this._readValue(formData, "edit_comment"),
|
||||
extent: this._readValue(formData, "extent"),
|
||||
dimensions: this._readValue(formData, "dimensions"),
|
||||
references: this._readValue(formData, "refs"),
|
||||
status: this._readValue(formData, "type"),
|
||||
};
|
||||
|
||||
if (!entry.preferred_title) {
|
||||
throw new Error("Kurztitel ist erforderlich.");
|
||||
}
|
||||
|
||||
const yearValue = this._readValue(formData, "year");
|
||||
if (yearValue === "") {
|
||||
throw new Error("Jahr muss angegeben werden (0 ist erlaubt).");
|
||||
}
|
||||
const parsedYear = Number.parseInt(yearValue, 10);
|
||||
if (Number.isNaN(parsedYear)) {
|
||||
throw new Error("Jahr ist ungültig.");
|
||||
}
|
||||
entry.year = parsedYear;
|
||||
|
||||
const languages = formData.getAll("languages[]").map((value) => value.trim()).filter(Boolean);
|
||||
const places = formData.getAll("places[]").map((value) => value.trim()).filter(Boolean);
|
||||
const { items, removedIds } = this._collectItems(formData);
|
||||
const {
|
||||
relations: seriesRelations,
|
||||
deleted: deletedSeriesRelationIds,
|
||||
} = this._collectRelations(formData, {
|
||||
prefix: "entries_series",
|
||||
targetField: "series",
|
||||
});
|
||||
const newSeriesRelations = this._collectNewRelations("entries_series");
|
||||
const hasPreferredSeries = [...seriesRelations, ...newSeriesRelations].some(
|
||||
(relation) => relation.type === PREFERRED_SERIES_RELATION,
|
||||
);
|
||||
if (!hasPreferredSeries) {
|
||||
throw new Error("Mindestens ein bevorzugter Reihentitel muss verknüpft sein.");
|
||||
}
|
||||
|
||||
const {
|
||||
relations: agentRelations,
|
||||
deleted: deletedAgentRelationIds,
|
||||
} = this._collectRelations(formData, {
|
||||
prefix: "entries_agents",
|
||||
targetField: "agent",
|
||||
});
|
||||
const newAgentRelations = this._collectNewRelations("entries_agents");
|
||||
|
||||
return {
|
||||
csrf_token: this._readValue(formData, "csrf_token"),
|
||||
last_edited: this._readValue(formData, "last_edited"),
|
||||
entry,
|
||||
languages,
|
||||
places,
|
||||
items,
|
||||
deleted_item_ids: removedIds,
|
||||
series_relations: seriesRelations,
|
||||
new_series_relations: newSeriesRelations,
|
||||
deleted_series_relation_ids: deletedSeriesRelationIds,
|
||||
agent_relations: agentRelations,
|
||||
new_agent_relations: newAgentRelations,
|
||||
deleted_agent_relation_ids: deletedAgentRelationIds,
|
||||
};
|
||||
}
|
||||
|
||||
_collectItems(formData) {
|
||||
const ids = formData.getAll("items_id[]").map((value) => value.trim());
|
||||
const owners = formData.getAll("items_owner[]");
|
||||
const identifiers = formData.getAll("items_identifier[]");
|
||||
const locations = formData.getAll("items_location[]");
|
||||
const media = formData.getAll("items_media[]");
|
||||
const annotations = formData.getAll("items_annotation[]");
|
||||
const uris = formData.getAll("items_uri[]");
|
||||
const removed = new Set(
|
||||
formData
|
||||
.getAll("items_removed[]")
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean),
|
||||
);
|
||||
const items = [];
|
||||
for (let index = 0; index < ids.length; index += 1) {
|
||||
const id = ids[index] || "";
|
||||
if (id && removed.has(id)) {
|
||||
continue;
|
||||
}
|
||||
const owner = (owners[index] || "").trim();
|
||||
const identifier = (identifiers[index] || "").trim();
|
||||
const location = (locations[index] || "").trim();
|
||||
const annotation = (annotations[index] || "").trim();
|
||||
const uri = (uris[index] || "").trim();
|
||||
const mediaValue = (media[index] || "").trim();
|
||||
const hasValues = id || owner || identifier || location || annotation || uri || mediaValue;
|
||||
if (!hasValues) {
|
||||
continue;
|
||||
}
|
||||
items.push({
|
||||
id,
|
||||
owner,
|
||||
identifier,
|
||||
location,
|
||||
annotation,
|
||||
uri,
|
||||
media: mediaValue ? [mediaValue] : [],
|
||||
});
|
||||
}
|
||||
return {
|
||||
items,
|
||||
removedIds: Array.from(removed),
|
||||
};
|
||||
}
|
||||
|
||||
_collectRelations(formData, { prefix, targetField }) {
|
||||
const relations = [];
|
||||
const deleted = [];
|
||||
for (const [key, value] of formData.entries()) {
|
||||
if (!key.startsWith(`${prefix}_type[`)) {
|
||||
continue;
|
||||
}
|
||||
const relationKey = key.slice(key.indexOf("[") + 1, -1);
|
||||
const targetKey = `${prefix}_${targetField}[${relationKey}]`;
|
||||
const relationIdKey = `${prefix}_id[${relationKey}]`;
|
||||
const deleteKey = `${prefix}_delete[${relationKey}]`;
|
||||
const uncertainKey = `${prefix}_uncertain[${relationKey}]`;
|
||||
const targetId = (formData.get(targetKey) || "").trim();
|
||||
if (!targetId) {
|
||||
continue;
|
||||
}
|
||||
const relationId = (formData.get(relationIdKey) || relationKey).trim();
|
||||
if (formData.has(deleteKey)) {
|
||||
if (relationId) {
|
||||
deleted.push(relationId);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
relations.push({
|
||||
id: relationId,
|
||||
target_id: targetId,
|
||||
type: (value || "").trim(),
|
||||
uncertain: formData.has(uncertainKey),
|
||||
});
|
||||
}
|
||||
return { relations, deleted };
|
||||
}
|
||||
|
||||
_collectNewRelations(prefix) {
|
||||
const editor = this.querySelector(`relations-editor[data-prefix='${prefix}']`);
|
||||
if (!editor) {
|
||||
return [];
|
||||
}
|
||||
const newRows = editor.querySelectorAll("[data-role='relation-add-row'] [data-rel-row]");
|
||||
const relations = [];
|
||||
newRows.forEach((row) => {
|
||||
const idInput = row.querySelector(`input[name='${prefix}_new_id']`);
|
||||
const typeInput = row.querySelector(`select[name='${prefix}_new_type']`);
|
||||
const uncertainInput = row.querySelector(`input[name='${prefix}_new_uncertain']`);
|
||||
if (!idInput) {
|
||||
return;
|
||||
}
|
||||
const targetId = idInput.value.trim();
|
||||
if (!targetId) {
|
||||
return;
|
||||
}
|
||||
relations.push({
|
||||
target_id: targetId,
|
||||
type: (typeInput?.value || "").trim(),
|
||||
uncertain: Boolean(uncertainInput?.checked),
|
||||
});
|
||||
});
|
||||
return relations;
|
||||
}
|
||||
|
||||
_readValue(formData, field) {
|
||||
const value = formData.get(field);
|
||||
return value ? String(value).trim() : "";
|
||||
}
|
||||
|
||||
_setSavingState(isSaving) {
|
||||
this._isSaving = isSaving;
|
||||
if (!this._saveButton) {
|
||||
return;
|
||||
}
|
||||
this._saveButton.disabled = isSaving;
|
||||
const label = this._saveButton.querySelector("span");
|
||||
if (label) {
|
||||
label.textContent = isSaving ? "Speichern..." : "Speichern";
|
||||
}
|
||||
}
|
||||
|
||||
_clearStatus() {
|
||||
if (!this._statusEl) {
|
||||
return;
|
||||
}
|
||||
this._statusEl.textContent = "";
|
||||
this._statusEl.classList.remove("text-red-700", "text-green-700");
|
||||
}
|
||||
|
||||
_showStatus(message, type) {
|
||||
if (!this._statusEl) {
|
||||
return;
|
||||
}
|
||||
this._clearStatus();
|
||||
this._statusEl.textContent = message;
|
||||
if (type === "success") {
|
||||
this._statusEl.classList.add("text-green-700");
|
||||
} else if (type === "error") {
|
||||
this._statusEl.classList.add("text-red-700");
|
||||
}
|
||||
}
|
||||
|
||||
async _reloadForm(successMessage) {
|
||||
this._teardownSaveHandling();
|
||||
const targetUrl = new URL(window.location.href);
|
||||
if (successMessage) {
|
||||
targetUrl.searchParams.set("saved_message", successMessage);
|
||||
} else {
|
||||
targetUrl.searchParams.delete("saved_message");
|
||||
}
|
||||
|
||||
const response = await fetch(targetUrl.toString(), {
|
||||
headers: {
|
||||
"X-Requested-With": "fetch",
|
||||
},
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error("Formular konnte nicht aktualisiert werden.");
|
||||
}
|
||||
|
||||
const html = await response.text();
|
||||
const parser = new DOMParser();
|
||||
const doc = parser.parseFromString(html, "text/html");
|
||||
|
||||
const newForm = doc.querySelector("#changealmanachform");
|
||||
const currentForm = this.querySelector("#changealmanachform");
|
||||
if (!newForm || !currentForm) {
|
||||
throw new Error("Formular konnte nicht geladen werden.");
|
||||
}
|
||||
currentForm.replaceWith(newForm);
|
||||
this._form = newForm;
|
||||
|
||||
const newMessage = doc.querySelector("#user-message");
|
||||
const currentMessage = this.querySelector("#user-message");
|
||||
if (newMessage && currentMessage) {
|
||||
currentMessage.replaceWith(newMessage);
|
||||
}
|
||||
|
||||
const newHeader = doc.querySelector("#almanach-header-data");
|
||||
const currentHeader = this.querySelector("#almanach-header-data");
|
||||
if (newHeader && currentHeader) {
|
||||
currentHeader.replaceWith(newHeader);
|
||||
}
|
||||
|
||||
this._initForm();
|
||||
this._initPlaces();
|
||||
this._initSaveHandling();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -506,6 +506,14 @@ export class MultiSelectSimple extends HTMLElement {
|
||||
this._updateRootElementStateClasses();
|
||||
}
|
||||
|
||||
captureInitialSelection() {
|
||||
this._initialValue = [...this._value];
|
||||
this._initialOrder = [...this._value];
|
||||
this._removedIds.clear();
|
||||
this._initialCaptured = true;
|
||||
this._renderSelectedItems();
|
||||
}
|
||||
|
||||
_synchronizeHiddenSelect() {
|
||||
if (!this.hiddenSelect) return;
|
||||
this.hiddenSelect.innerHTML = "";
|
||||
|
||||
Reference in New Issue
Block a user