forked from dionyself/golang-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
122 lines (111 loc) · 3.57 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package admins
import (
"strconv"
"github.com/chuongnx/beego"
"github.com/chuongnx/beego/context"
"github.com/chuongnx/beego/orm"
"github.com/chuongnx/golang-cms/core/block"
"github.com/chuongnx/golang-cms/core/defaults"
"github.com/chuongnx/golang-cms/core/lib/cache"
database "github.com/chuongnx/golang-cms/core/lib/db"
"github.com/chuongnx/golang-cms/core/template"
"github.com/chuongnx/golang-cms/utils"
mobiledetect "github.com/chuongnx/gomobiledetect"
)
// BaseController Extendable
type BaseController struct {
beego.Controller
db orm.Ormer
cache cache.CACHE
}
// ConfigPage receives template name and makes basic config to render it
func (CTRL *BaseController) ConfigPage(page string) {
CTRL.GetDB()
CTRL.GetCache()
theme := template.GetActiveTheme(false)
CTRL.Layout = theme[0] + "/" + "layout-admin.html"
device := CTRL.Ctx.Input.GetData("device_type").(string)
CTRL.LayoutSections = make(map[string]string)
CTRL.LayoutSections["Head"] = theme[0] + "/" + "partial/html_head_" + device + ".html"
CTRL.TplName = theme[0] + "/" + page
CTRL.Data["Theme"] = theme[0]
CTRL.Data["Style"] = theme[1]
CTRL.Data["ModuleMenu"] = CTRL.GetModuleMenu()
CTRL.GetBlocks()
//CTRL.GetActiveModule()
//CTRL.GetActiveCategory()
//CTRL.GetActiveAds()
}
func (CTRL *BaseController) GetBlocks() map[string]string {
// TODO : get blocks and set block content
// loadedBlocks := CTRL.cache.GetMap(fmt.Sprintf("activeblocks/%s/%s", module, session_id) , expirationTime)
loadedBlocks := make(map[string]string)
ActiveBlocks := block.GetActiveBlocks(false)
for _, CurentBlock := range ActiveBlocks {
cblock := block.Blocks[CurentBlock]
cblockSectionName := "Block_" + strconv.Itoa(cblock.GetPosition())
cblockSectionData := cblockSectionName + "_Data"
loadedBlocks[cblockSectionName] = cblock.GetTemplatePath()
CTRL.Data[cblockSectionData] = cblock.GetContent()
}
CTRL.LayoutSections = utils.MergeMaps(CTRL.LayoutSections, loadedBlocks)
return CTRL.LayoutSections
}
func (CTRL *BaseController) GetActiveAds() map[string]string {
// loadedAds := CTRL.cache.GetMap(fmt.Sprintf("activeAds/%s/%s", category, session_id) , expirationTime)
return make(map[string]string)
}
// GetCache set the cache connector into our controller
func (CTRL *BaseController) GetCache() {
CTRL.cache = cache.MainCache
}
// GetDB set the orm connector into our controller
// if repication activated we use slave to Slave
func (CTRL *BaseController) GetDB(db ...string) orm.Ormer {
CTRL.db = database.MainDatabase.Orm
if len(db) > 0 {
if db[0] == "master" {
db[0] = "default"
}
CTRL.db.Using(db[0])
}
return CTRL.db
}
// GetModuleMenu retrieves menu
func (CTRL *BaseController) GetModuleMenu() string {
output := defaults.GetDefaultMenu()
return output
}
// GetContent gets contents
func (CTRL *BaseController) GetContent() string {
output := defaults.GetDefaultMenu()
return output
}
// DetectUserAgent detects device type and set it into a cookie
var DetectUserAgent = func(ctx *context.Context) {
deviceDetector := mobiledetect.NewMobileDetect(ctx.Request, nil)
ctx.Request.ParseForm()
device := ""
if len(ctx.Request.Form["device_type"]) != 0 {
device = ctx.Request.Form["device_type"][0]
}
if device == "" {
device = ctx.Input.Cookie("Device-Type")
}
if device == "" {
if deviceDetector.IsMobile() {
device = "mobile"
}
if deviceDetector.IsTablet() {
device = "tablet"
}
if device == "" {
device = beego.AppConfig.String("DefaultDevice")
if device == "" {
device = "desktop"
}
}
}
ctx.Output.Cookie("Device-Type", device)
ctx.Input.SetData("device_type", device)
}