camper/pkg/services/l10n.go

76 lines
2.3 KiB
Go

/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* SPDX-License-Identifier: AGPL-3.0-only
*/
package services
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 serviceL10nForm struct {
Locale *locale.Locale
ID int
Name *form.L10nInput
Description *form.L10nInput
}
func newServiceL10nForm(f *serviceForm, loc *locale.Locale) *serviceL10nForm {
return &serviceL10nForm{
Locale: loc,
ID: f.ID,
Name: f.Name.L10nInput(),
Description: f.Description.L10nInput(),
}
}
func (l10n *serviceL10nForm) FillFromDatabase(ctx context.Context, conn *database.Conn) error {
row := conn.QueryRow(ctx, `
select coalesce(i18n.name, '') as l10n_name
, coalesce(i18n.description, '') as l10n_description
from service
left join service_i18n as i18n on service.service_id = i18n.service_id and i18n.lang_tag = $1
where service.service_id = $2
`, l10n.Locale.Language, l10n.ID)
return row.Scan(&l10n.Name.Val, &l10n.Description.Val)
}
func (l10n *serviceL10nForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
template.MustRenderAdmin(w, r, user, company, "services/l10n.gohtml", l10n)
}
func editServiceL10n(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, l10n *serviceL10nForm) {
if ok, err := form.Handle(l10n, w, r, user); err != nil {
return
} else if !ok {
l10n.MustRender(w, r, user, company)
return
}
conn.MustExec(r.Context(), "select translate_service($1, $2, $3, $4)", l10n.ID, l10n.Locale.Language, l10n.Name, l10n.Description)
httplib.Redirect(w, r, "/admin/services", http.StatusSeeOther)
}
func (l10n *serviceL10nForm) Parse(r *http.Request) error {
if err := r.ParseForm(); err != nil {
return err
}
l10n.Name.FillValue(r)
l10n.Description.FillValue(r)
return nil
}
func (l10n *serviceL10nForm) Valid(l *locale.Locale) bool {
v := form.NewValidator(l)
v.CheckRequired(&l10n.Name.Input, l.GettextNoop("Name can not be empty."))
return v.AllOK
}