It made no sense to have a file upload in each form that needs a media, because to reuse an existing media users would need to upload the exact same file again; this is very unusual and unfriendly. A better option is to have a “centralized” media section, where people can upload files there, and then have a picker to select from there. Ideally, there would be an upload option in the picker, but i did not add it yet. I’ve split the content from the media because i want users to have the option to update a media, for instance when they need to upload a reduced or cropped version of the same photo, without an edit they would need to upload the file as a new media and then update all places where the old version was used. And i did not want to trouble people that uploads the same photo twice: without the separate relation, doing so would throw a constraint error. I do not believe there is any security problem to have all companies link their media to the same file, as they were already readable by everyone and could upload the data from a different company to their own; in other words, it is not worse than it was now.
83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package carousel
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"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
|
|
CarouselName string
|
|
ID int
|
|
Caption *form.L10nInput
|
|
}
|
|
|
|
func newSlideL10nForm(f *slideForm, loc *locale.Locale) *slideL10nForm {
|
|
return &slideL10nForm{
|
|
Locale: loc,
|
|
CarouselName: f.CarouselName,
|
|
ID: f.ID,
|
|
Caption: f.Caption.L10nInput(),
|
|
}
|
|
}
|
|
|
|
func (l10n *slideL10nForm) FillFromDatabase(ctx context.Context, conn *database.Conn) error {
|
|
row := conn.QueryRow(ctx, fmt.Sprintf(`
|
|
select coalesce(i18n.caption, '') as l10n_caption
|
|
from %[1]s_carousel as carousel
|
|
left join %[1]s_carousel_i18n as i18n on carousel.media_id = i18n.media_id and i18n.lang_tag = $1
|
|
where carousel.media_id = $2
|
|
`, l10n.CarouselName), 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, "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(), fmt.Sprintf("select translate_%s_carousel_slide($1, $2, $3)", l10n.CarouselName), l10n.ID, l10n.Locale.Language, l10n.Caption)
|
|
httplib.Redirect(w, r, "/admin/"+l10n.CarouselName, 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
|
|
}
|