2022-08-11 15:26:55 +00:00
|
|
|
package renderer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/icons"
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
2022-10-17 03:02:19 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/emailing"
|
2022-08-11 15:26:55 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
2022-12-05 19:06:22 +00:00
|
|
|
"github.com/coreos/go-oidc"
|
2022-08-11 15:26:55 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Renderer struct {
|
|
|
|
TemplatesDir string
|
|
|
|
GlobalConfig *viper.Viper
|
|
|
|
ThemeConfig *viper.Viper
|
2022-10-17 03:02:19 +00:00
|
|
|
Mailer *emailing.Mailer
|
2022-08-11 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewRenderer(global *viper.Viper, templates_dir string) *Renderer {
|
|
|
|
theme := viper.New()
|
|
|
|
theme.SetConfigName("config")
|
|
|
|
theme.AddConfigPath(templates_dir)
|
|
|
|
err := theme.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("fatal error config file: %w", err))
|
|
|
|
}
|
|
|
|
return &Renderer{
|
|
|
|
TemplatesDir: templates_dir,
|
|
|
|
GlobalConfig: global,
|
|
|
|
ThemeConfig: theme,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (renderer *Renderer) Render(name string, w http.ResponseWriter, r *http.Request, files []string, state RenderState) {
|
2022-12-05 19:06:22 +00:00
|
|
|
|
2022-08-11 15:26:55 +00:00
|
|
|
genericFiles := renderer.ThemeConfig.GetStringSlice("views.generic.files")
|
|
|
|
|
|
|
|
prefixed_files := []string{}
|
|
|
|
for _, f := range genericFiles {
|
|
|
|
prefixed_files = append(prefixed_files, renderer.templateFile(f))
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
prefixed_files = append(prefixed_files, renderer.templateFile(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
t := template.New(name).Funcs(
|
|
|
|
template.FuncMap{
|
|
|
|
"timeFrom": TimeFrom,
|
2022-12-05 19:06:22 +00:00
|
|
|
"timeFormat": TimeFormat,
|
2022-08-11 15:26:55 +00:00
|
|
|
"genderISO5218": GenderISO5218,
|
|
|
|
"dict": Dict,
|
2022-08-12 12:53:54 +00:00
|
|
|
"json": JSON,
|
2023-03-10 11:16:12 +00:00
|
|
|
"rawjson": RawJSON,
|
|
|
|
"unescapeHTML": UnescapeHTML,
|
2022-10-17 03:02:19 +00:00
|
|
|
"walkingLength": WalkingLength,
|
|
|
|
"divideFloat64": Divide[float64],
|
|
|
|
"divideInt": Divide[int],
|
2022-08-11 15:26:55 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
t = template.Must(t.ParseFiles(prefixed_files...))
|
2022-10-31 09:38:05 +00:00
|
|
|
|
2022-08-11 15:26:55 +00:00
|
|
|
err := t.ExecuteTemplate(w, "main", state)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (renderer *Renderer) RenderNoLayout(name string, w http.ResponseWriter, r *http.Request, files []string, state RenderState) {
|
2022-12-05 19:06:22 +00:00
|
|
|
|
2022-08-11 15:26:55 +00:00
|
|
|
prefixed_files := []string{}
|
|
|
|
for _, f := range files {
|
|
|
|
prefixed_files = append(prefixed_files, renderer.templateFile(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
t := template.New(name).Funcs(
|
|
|
|
template.FuncMap{
|
|
|
|
"timeFrom": TimeFrom,
|
2022-12-05 19:06:22 +00:00
|
|
|
"timeFormat": TimeFormat,
|
2022-08-11 15:26:55 +00:00
|
|
|
"genderISO5218": GenderISO5218,
|
|
|
|
"dict": Dict,
|
2022-08-12 12:53:54 +00:00
|
|
|
"json": JSON,
|
2023-03-10 11:16:12 +00:00
|
|
|
"rawjson": RawJSON,
|
|
|
|
"unsescapeHTML": UnescapeHTML,
|
2022-10-17 03:02:19 +00:00
|
|
|
"divideFloat64": Divide[float64],
|
|
|
|
"divideInt": Divide[int],
|
2022-08-11 15:26:55 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
t = template.Must(t.ParseFiles(prefixed_files...))
|
|
|
|
err := t.ExecuteTemplate(w, "main", state)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) templateFile(file string) string {
|
|
|
|
return r.TemplatesDir + file
|
|
|
|
}
|
|
|
|
|
|
|
|
type RenderState struct {
|
|
|
|
icons.IconSet
|
|
|
|
LayoutState
|
2022-12-05 19:06:22 +00:00
|
|
|
UserID string
|
|
|
|
UserClaims map[string]any
|
|
|
|
Group storage.Group
|
|
|
|
Roles any
|
|
|
|
ViewState any // This is a state specific to a given view
|
2022-08-11 15:26:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewState(r *http.Request, themeConfig *viper.Viper, menuState string) RenderState {
|
|
|
|
iconset := themeConfig.GetStringMapString("icons.svg")
|
2022-12-05 19:06:22 +00:00
|
|
|
|
2022-08-11 15:26:55 +00:00
|
|
|
// Get State elements from Request
|
2022-12-05 19:06:22 +00:00
|
|
|
var userid string
|
|
|
|
var claims map[string]any
|
|
|
|
|
|
|
|
u := r.Context().Value(identification.IdtokenKey)
|
|
|
|
if u != nil {
|
|
|
|
if current_user_token, ok := u.(*oidc.IDToken); ok {
|
|
|
|
userid = current_user_token.Subject
|
|
|
|
}
|
|
|
|
|
|
|
|
c := r.Context().Value(identification.ClaimsKey)
|
|
|
|
if c != nil {
|
|
|
|
if current_user_claims, ok := c.(map[string]any); ok {
|
|
|
|
claims = current_user_claims
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-08-11 15:26:55 +00:00
|
|
|
|
|
|
|
g := r.Context().Value(identification.GroupKey)
|
|
|
|
if g == nil {
|
|
|
|
return RenderState{
|
|
|
|
IconSet: icons.NewIconSet(iconset),
|
|
|
|
LayoutState: LayoutState{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var roles map[string]any
|
|
|
|
|
|
|
|
ro, ok := r.Context().Value(identification.RolesKey).(map[string]any)
|
|
|
|
if ok {
|
|
|
|
roles = ro
|
|
|
|
}
|
|
|
|
|
|
|
|
group := g.(storage.Group)
|
|
|
|
|
|
|
|
modules := group.Data["modules"].(map[string]any)
|
|
|
|
|
|
|
|
ls := LayoutState{
|
|
|
|
AdministrationState: AdministrationState{
|
2022-10-17 03:02:19 +00:00
|
|
|
Display: modules["administration"] != nil && modules["administration"].(bool),
|
2022-08-11 15:26:55 +00:00
|
|
|
Active: menuState == administrationMenu,
|
|
|
|
},
|
2022-09-05 05:25:05 +00:00
|
|
|
|
2022-08-11 15:26:55 +00:00
|
|
|
MenuItems: []MenuItem{
|
|
|
|
{
|
|
|
|
Title: "Tableau de bord",
|
|
|
|
Link: "/app/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == dashboardMenu,
|
2022-08-11 15:26:55 +00:00
|
|
|
Icon: "hero:outline/home",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["beneficiaries"] != nil && modules["beneficiaries"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Bénéficiaires",
|
|
|
|
Link: "/app/beneficiaries/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == beneficiariesMenu,
|
2022-08-11 15:26:55 +00:00
|
|
|
Icon: "hero:outline/user-group",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["journeys"] != nil && modules["journeys"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Déplacements",
|
|
|
|
Link: "/app/journeys/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == journeysMenu,
|
2022-08-12 12:53:54 +00:00
|
|
|
Icon: "hero:outline/map",
|
2022-08-11 15:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["vehicles"] != nil && modules["vehicles"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
2022-08-12 12:53:54 +00:00
|
|
|
Title: "Véhicules partagés",
|
2022-08-11 15:26:55 +00:00
|
|
|
Link: "/app/vehicles/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == vehiclesMenu,
|
2022-09-05 05:25:05 +00:00
|
|
|
Icon: "tabler-icons:car",
|
2022-08-11 15:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["vehicles_management"] != nil && modules["vehicles_management"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Gestion des véhicules",
|
|
|
|
Link: "/app/vehicles-management/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == vehiclesmanagementMenu,
|
2022-08-12 12:53:54 +00:00
|
|
|
Icon: "hero:outline/briefcase",
|
2022-08-11 15:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["agenda"] != nil && modules["agenda"].(bool) {
|
2022-08-11 15:26:55 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
2022-09-05 05:25:05 +00:00
|
|
|
Title: "Agenda dispositifs",
|
|
|
|
Link: "/app/agenda/",
|
2022-10-17 03:02:19 +00:00
|
|
|
Active: menuState == agendaMenu,
|
2022-08-12 12:53:54 +00:00
|
|
|
Icon: "hero:outline/calendar",
|
2022-08-11 15:26:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-08 11:24:06 +00:00
|
|
|
if modules["group_module"] != nil && modules["group_module"].(bool) {
|
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Groupes / Communautés",
|
|
|
|
Link: "/app/group_module/",
|
|
|
|
Active: menuState == groupMenu,
|
|
|
|
Icon: "hero:outline/group_module",
|
|
|
|
})
|
|
|
|
|
2022-10-31 09:38:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:21:08 +00:00
|
|
|
if modules["support"] != nil && modules["support"].(bool) {
|
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Support",
|
|
|
|
Link: "/app/support/",
|
|
|
|
Active: menuState == commentMenu,
|
|
|
|
Icon: "hero:outline/support",
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:02:19 +00:00
|
|
|
if modules["directory"] != nil && modules["directory"].(bool) {
|
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Répertoire solutions",
|
|
|
|
Link: "/app/directory/",
|
|
|
|
Active: menuState == directoryMenu,
|
|
|
|
Icon: "hero:outline/document-text",
|
|
|
|
})
|
|
|
|
}
|
2022-12-14 11:18:11 +00:00
|
|
|
if modules["conseillers"] != nil && modules["conseillers"].(bool) {
|
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
|
|
|
Title: "Conseillers",
|
|
|
|
Link: "/app/conseillers/",
|
2023-01-17 07:31:07 +00:00
|
|
|
Active: menuState == membersMenu,
|
2022-12-14 11:18:11 +00:00
|
|
|
Icon: "hero:outline/user-group",
|
|
|
|
})
|
|
|
|
}
|
2024-05-02 07:54:51 +00:00
|
|
|
if modules["solidarity_service"] != nil && modules["solidarity_service"].(bool) {
|
2024-04-19 13:54:38 +00:00
|
|
|
ls.MenuItems = append(ls.MenuItems, MenuItem{
|
2024-11-22 16:26:32 +00:00
|
|
|
Title: "Transport solidaire",
|
2024-05-02 07:54:51 +00:00
|
|
|
Link: "/app/solidarity_service/",
|
|
|
|
Active: menuState == solidarityserviceMenu,
|
2024-04-19 13:54:38 +00:00
|
|
|
Icon: "hero:outline/map",
|
|
|
|
})
|
|
|
|
}
|
2022-08-11 15:26:55 +00:00
|
|
|
return RenderState{
|
|
|
|
IconSet: icons.NewIconSet(iconset),
|
|
|
|
Group: group,
|
|
|
|
Roles: roles,
|
2022-12-05 19:06:22 +00:00
|
|
|
UserID: userid,
|
|
|
|
UserClaims: claims,
|
2022-08-11 15:26:55 +00:00
|
|
|
LayoutState: ls,
|
|
|
|
}
|
|
|
|
}
|