jordi fita mas 44526b1efb Add the edit form for services
This one has an input to select the icon.  It makes no sense to choose
an icon only by name, thus a <select> is not appropriate, and had to
use a hidden input with a row of button to choose the icon from.  This
works now only because there are very few icons; we’ll need to choose
a different approach when there are many more icons.

Since now the icons have to be defined in CSS for both the public and
admin sections, i had to split it into a separate file that both sites
can use.  I considered the option to “include” that CSS with m4, like
i do for images in demo.sql, but it made everything too complicated
(e.g., having to call make for each change in the CSS), and decided to
load that CSS in a separate <link>.
2023-09-25 20:10:33 +02:00

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
}