sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

[web] menu tree

This commit is contained in:
Martin Geno 2016-09-12 20:34:09 +02:00
parent f1e7bacbfc
commit b5409b101a
5 changed files with 47 additions and 16 deletions

View File

@ -64,7 +64,7 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
logger = logger.WithField("hID", website.ID)
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
for _, item := range menus {
if item.Shorturl == "" && menu == nil {
@ -74,19 +74,24 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
menu = item
}
}
menus = modul.BuildMenuTree(menus)
page := &modul.Page{WebsiteID: website.ID, MenuID: menu.ID}
dbconnection.Where(page).Find(page)
logger.Info(page.ID)
unsafe := blackfriday.MarkdownCommon([]byte(page.Content))
page.Content = string(bluemonday.UGCPolicy().SanitizeBytes(unsafe))
i := TemplateInfo{
Host: host,
URL: url,
Page: page,
Menu: menus,
Website: website,
Host: host,
URL: url,
Page: page,
Menu: menus,
}
t, err := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl"))
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, _ := template.ParseGlob(fmt.Sprintf("%s/%d/%s/%s", config.Webroot, website.ID, "tmpl", "*.tmpl"))
logger.Info("")
t.Execute(w, i)
}

View File

@ -4,8 +4,9 @@ import modul "dev.sum7.de/sum7/warehost/modul/web"
//TemplateInfo Information send to Template
type TemplateInfo struct {
Host string
URL string
Page *modul.Page
Menu []*modul.Menu
Host string
URL string
Website *modul.Website
Page *modul.Page
Menu []*modul.Menu
}

View File

@ -4,6 +4,6 @@ package main
// MODUL variables
const (
MODULWEB = false
MODULWEB = true
MODULHOST = false
)

View File

@ -53,7 +53,7 @@ type Menu struct {
Name string `gorm:"type:varchar(255);column:name" json:"name"`
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"`
Menu []*Menu `gorm:"foreignkey:Menu" json:"menu"`
Children []*Menu `json:"menu"`
}
// TableName of struct
@ -71,6 +71,31 @@ type Page struct {
// TableName of struct
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
func SyncModels(dbconnection *gorm.DB) {
dbconnection.AutoMigrate(&Website{}, &Domain{}, &Manager{}, &Media{}, &Menu{}, &Page{})

@ -1 +1 @@
Subproject commit 9e6988ddfbfe1957ba24fd556c738ab03c4e5791
Subproject commit 58aab8dfd889cc4b5101524e8340c0f1321d7faa