/* * SPDX-FileCopyrightText: 2023 jordi fita mas * SPDX-License-Identifier: AGPL-3.0-only */ package home import ( "context" "net/http" "dev.tandem.ws/tandem/camper/pkg/auth" "dev.tandem.ws/tandem/camper/pkg/database" "dev.tandem.ws/tandem/camper/pkg/form" httplib "dev.tandem.ws/tandem/camper/pkg/http" "dev.tandem.ws/tandem/camper/pkg/locale" "dev.tandem.ws/tandem/camper/pkg/template" ) type slideL10nForm struct { Locale *locale.Locale ID int Caption *form.L10nInput } func newSlideL10nForm(f *slideForm, loc *locale.Locale) *slideL10nForm { return &slideL10nForm{ Locale: loc, ID: f.ID, Caption: f.Caption.L10nInput(), } } func (l10n *slideL10nForm) FillFromDatabase(ctx context.Context, conn *database.Conn) error { row := conn.QueryRow(ctx, ` select coalesce(i18n.caption, '') as l10n_caption from home_carousel left join home_carousel_i18n as i18n on home_carousel.media_id = i18n.media_id and i18n.lang_tag = $1 where home_carousel.media_id = $2 `, l10n.Locale.Language, l10n.ID) return row.Scan(&l10n.Caption.Val) } func (l10n *slideL10nForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) { template.MustRenderAdmin(w, r, user, company, "home/carousel/l10n.gohtml", l10n) } func editSlideL10n(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, l10n *slideL10nForm) { if err := l10n.Parse(r); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } if err := user.VerifyCSRFToken(r); err != nil { http.Error(w, err.Error(), http.StatusForbidden) return } if !l10n.Valid(user.Locale) { if !httplib.IsHTMxRequest(r) { w.WriteHeader(http.StatusUnprocessableEntity) } l10n.MustRender(w, r, user, company) return } conn.MustExec(r.Context(), "select translate_home_carousel_slide($1, $2, $3)", l10n.ID, l10n.Locale.Language, l10n.Caption) httplib.Redirect(w, r, "/admin/home", http.StatusSeeOther) } func (l10n *slideL10nForm) Parse(r *http.Request) error { if err := r.ParseForm(); err != nil { return err } l10n.Caption.FillValue(r) return nil } func (l10n *slideL10nForm) Valid(l *locale.Locale) bool { v := form.NewValidator(l) return v.AllOK }