[web] menu tree
This commit is contained in:
parent
f1e7bacbfc
commit
b5409b101a
|
@ -64,7 +64,7 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
||||||
logger = logger.WithField("hID", website.ID)
|
logger = logger.WithField("hID", website.ID)
|
||||||
|
|
||||||
var menus []*modul.Menu
|
var menus []*modul.Menu
|
||||||
dbconnection.Where("website = ? AND menu IS NULL", website.ID).Preload("Menu").Find(&menus)
|
dbconnection.Where("website = ?", website.ID).Preload("Menu").Find(&menus)
|
||||||
var menu *modul.Menu
|
var menu *modul.Menu
|
||||||
for _, item := range menus {
|
for _, item := range menus {
|
||||||
if item.Shorturl == "" && menu == nil {
|
if item.Shorturl == "" && menu == nil {
|
||||||
|
@ -74,19 +74,24 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
||||||
menu = item
|
menu = item
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
menus = modul.BuildMenuTree(menus)
|
||||||
page := &modul.Page{WebsiteID: website.ID, MenuID: menu.ID}
|
page := &modul.Page{WebsiteID: website.ID, MenuID: menu.ID}
|
||||||
dbconnection.Where(page).Find(page)
|
dbconnection.Where(page).Find(page)
|
||||||
logger.Info(page.ID)
|
|
||||||
|
|
||||||
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
|
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
|
||||||
page.Content = string(bluemonday.UGCPolicy().SanitizeBytes(unsafe))
|
page.Content = string(bluemonday.UGCPolicy().SanitizeBytes(unsafe))
|
||||||
i := TemplateInfo{
|
i := TemplateInfo{
|
||||||
|
Website: website,
|
||||||
Host: host,
|
Host: host,
|
||||||
URL: url,
|
URL: url,
|
||||||
Page: page,
|
Page: page,
|
||||||
Menu: menus,
|
Menu: menus,
|
||||||
}
|
}
|
||||||
t, _ := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl"))
|
t, err := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl"))
|
||||||
logger.Info("")
|
logger.Info("done")
|
||||||
|
if err != nil {
|
||||||
|
w.Write([]byte(fmt.Sprintf("<h1>Error on rendering Template</h1>\n%s", err)))
|
||||||
|
} else {
|
||||||
t.Execute(w, i)
|
t.Execute(w, i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import modul "dev.sum7.de/sum7/warehost/modul/web"
|
||||||
type TemplateInfo struct {
|
type TemplateInfo struct {
|
||||||
Host string
|
Host string
|
||||||
URL string
|
URL string
|
||||||
|
Website *modul.Website
|
||||||
Page *modul.Page
|
Page *modul.Page
|
||||||
Menu []*modul.Menu
|
Menu []*modul.Menu
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,6 @@ package main
|
||||||
|
|
||||||
// MODUL variables
|
// MODUL variables
|
||||||
const (
|
const (
|
||||||
MODULWEB = false
|
MODULWEB = true
|
||||||
MODULHOST = false
|
MODULHOST = false
|
||||||
)
|
)
|
||||||
|
|
|
@ -53,7 +53,7 @@ type Menu struct {
|
||||||
Name string `gorm:"type:varchar(255);column:name" json:"name"`
|
Name string `gorm:"type:varchar(255);column:name" json:"name"`
|
||||||
Shorturl string `gorm:"type:varchar(255);column:url" json:"url"`
|
Shorturl string `gorm:"type:varchar(255);column:url" json:"url"`
|
||||||
ParentID sql.NullInt64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE SET NULL ON DELETE SET NULL;column:menu"`
|
ParentID sql.NullInt64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE SET NULL ON DELETE SET NULL;column:menu"`
|
||||||
Menu []*Menu `gorm:"foreignkey:Menu" json:"menu"`
|
Children []*Menu `json:"menu"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName of struct
|
// TableName of struct
|
||||||
|
@ -71,6 +71,31 @@ type Page struct {
|
||||||
// TableName of struct
|
// TableName of struct
|
||||||
func (Page) TableName() string { return "web_page" }
|
func (Page) TableName() string { return "web_page" }
|
||||||
|
|
||||||
|
// BuildMenuTree format a array of Menu to a Tree of Menu (with Children)
|
||||||
|
func BuildMenuTree(items []*Menu) []*Menu {
|
||||||
|
menumap := make(map[int64]*Menu)
|
||||||
|
var menuexit []*Menu
|
||||||
|
for _, item := range items {
|
||||||
|
if menumap[item.ID] == nil {
|
||||||
|
menumap[item.ID] = item
|
||||||
|
}
|
||||||
|
if menumap[item.ID].ID == -1 {
|
||||||
|
tmp := menumap[item.ID].Children
|
||||||
|
menumap[item.ID] = item
|
||||||
|
menumap[item.ID].Children = tmp
|
||||||
|
}
|
||||||
|
if !item.ParentID.Valid {
|
||||||
|
menuexit = append(menuexit, item)
|
||||||
|
} else {
|
||||||
|
if menumap[item.ParentID.Int64] == nil {
|
||||||
|
menumap[item.ParentID.Int64] = &Menu{ID: -1}
|
||||||
|
}
|
||||||
|
menumap[item.ParentID.Int64].Children = append(menumap[item.ParentID.Int64].Children, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return menuexit
|
||||||
|
}
|
||||||
|
|
||||||
// SyncModels to verify the database schema
|
// SyncModels to verify the database schema
|
||||||
func SyncModels(dbconnection *gorm.DB) {
|
func SyncModels(dbconnection *gorm.DB) {
|
||||||
dbconnection.AutoMigrate(&Website{}, &Domain{}, &Manager{}, &Media{}, &Menu{}, &Page{})
|
dbconnection.AutoMigrate(&Website{}, &Domain{}, &Manager{}, &Media{}, &Menu{}, &Page{})
|
||||||
|
|
2
webroot
2
webroot
|
@ -1 +1 @@
|
||||||
Subproject commit 9e6988ddfbfe1957ba24fd556c738ab03c4e5791
|
Subproject commit 58aab8dfd889cc4b5101524e8340c0f1321d7faa
|
Reference in New Issue