forked from zzdboy/GoCMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.go
489 lines (405 loc) · 16.4 KB
/
category.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// +----------------------------------------------------------------------
// | GoCMS 0.1
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.6574.com.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zzdboy <zzdboy1616@163.com>
// +----------------------------------------------------------------------
package models
//栏目管理
import "strconv"
import "html/template"
import "encoding/json"
import "github.com/revel/revel"
type Category struct {
Id int64 `xorm:"pk"`
Pid int64 `xorm:"int(11)"`
Type int64 `xorm:"int(11)"`
Name string `xorm:"varchar(255)"`
Enname string `xorm:"varchar(255)"`
Desc string `xorm:"text"`
Url string `xorm:"varchar(100)"`
Hits int64 `xorm:"int(11)"`
Setting string `xorm:"text"`
SettingText map[string]interface{} `xorm:"- <- ->"`
Order int64 `xorm:"int(11)"`
Ismenu int64 `xorm:"default 1"`
}
//根据Id获取栏目信息
func (c *Category) GetById(Id int64) *Category {
category := new(Category)
has, err := DB_Read.Id(Id).Get(category)
if err != nil {
revel.WARN.Println(has)
revel.WARN.Printf("错误: %v", err)
} else {
SettingText := make(map[string]interface{})
if len(category.Setting) > 0 {
err = json.Unmarshal([]byte(category.Setting), &SettingText)
if err != nil {
revel.WARN.Printf("错误: %v", err)
} else {
category.SettingText = SettingText
}
}
}
return category
}
//添加栏目
func (c *Category) Save() bool {
category := new(Category)
category.Pid = c.Pid
category.Type = c.Type
category.Name = c.Name
category.Enname = c.Enname
category.Desc = c.Desc
category.Url = c.Url
category.Hits = 0
category.Setting = c.Setting
category.Order = c.Order
category.Ismenu = c.Ismenu
has, err := DB_Write.Insert(category)
if err != nil {
revel.WARN.Println(has)
revel.WARN.Printf("错误: %v", err)
return false
}
return true
}
//编辑栏目
func (c *Category) Edit(Id int64) bool {
category := new(Category)
if c.Pid > 0 {
category.Pid = c.Pid
}
if len(c.Name) > 0 {
category.Name = c.Name
}
if len(c.Enname) > 0 {
category.Enname = c.Enname
}
category.Type = c.Type
if len(c.Url) > 0 {
category.Url = c.Url
}
if len(c.Desc) > 0 {
category.Desc = c.Desc
}
if len(c.Setting) > 0 {
category.Setting = c.Setting
}
if c.Order > 0 {
category.Order = c.Order
}
if c.Ismenu > 0 {
category.Ismenu = c.Ismenu
}
has, err := DB_Write.Id(Id).Update(category)
if err != nil {
revel.WARN.Println(has)
revel.WARN.Printf("错误: %v", err)
return false
}
return true
}
//快速进入 搜索
func (c *Category) GetCateNameHtml(CateName string) string {
categorys := make([]*Category, 0)
Html := ""
if len(CateName) > 0 {
DB_Read.Where(" name like '%" + CateName + "%'").Find(&categorys)
for _, category := range categorys {
Html += "<li>"
if category.Type == 0 {
Html += "<a href=\"/Content/list/" + strconv.FormatInt(category.Id, 10) + "/\">" + category.Name + "</a>"
} else {
Html += "<a href=\"/Content/addContent/" + strconv.FormatInt(category.Id, 10) + "/\">" + category.Name + "</a>"
}
Html += "</li>"
}
}
return Html
}
//获取所有菜单
//返回HTML
func (c *Category) GetCateGoryHtml(Admin_Info *Admin) template.HTML {
categorys := make([]*Category, 0)
DB_Read.Asc("order").Find(&categorys)
//初始化菜单Map
category_list := make(map[int64][]*Category)
for _, category := range categorys {
if _, ok := category_list[category.Pid]; !ok {
category_list[category.Pid] = make([]*Category, 0)
}
category_list[category.Pid] = append(category_list[category.Pid], category)
}
Html := ""
for _, category := range category_list[0] {
Html += "<tr>"
Html += "<td align='center'>" + strconv.FormatInt(category.Id, 10) + "</td>"
if Admin_Info.Lang == "zh-cn" {
Html += "<td ><b>" + category.Name + "</b>"
} else {
Html += "<td ><b>" + category.Enname + "</b>"
}
if category.Ismenu == 0 {
Html += " <img title='不在导航显示' src='/public/img/icon/gear_disable.png'>"
}
Html += "</td>"
Html += "<td >"
if category.Type == 0 {
if Admin_Info.Lang == "zh-cn" {
Html += "内部栏目"
} else {
Html += "Column"
}
} else {
if Admin_Info.Lang == "zh-cn" {
Html += "<font color=\"blue\">单网页</font>"
} else {
Html += "<font color=\"blue\">Single page</font>"
}
}
Html += "</td>"
Html += "<td align='center'>" + strconv.FormatInt(category.Hits, 10) + "</td>"
Html += "<td align='center'>" + category.Url + "</td>"
Html += "<td align=\"center\">"
if Admin_Info.Lang == "zh-cn" {
Html += "<a href=\"/Category/add/" + strconv.FormatInt(category.Id, 10) + "/\">添加子菜单</a> |"
Html += "<a href=\"/Category/edit/" + strconv.FormatInt(category.Id, 10) + "/\">修改</a> |"
Html += "<a onclick=\"delete_cate(" + strconv.FormatInt(category.Id, 10) + ")\" href=\"javascript:;\">删除</a>"
} else {
Html += "<a href=\"/Category/add/" + strconv.FormatInt(category.Id, 10) + "/\">Add Submenu</a> |"
Html += "<a href=\"/Category/edit/" + strconv.FormatInt(category.Id, 10) + "/\">Edit</a> |"
Html += "<a onclick=\"delete_cate(" + strconv.FormatInt(category.Id, 10) + ")\" href=\"javascript:;\">Delete</a>"
}
Html += "</td>"
Html += "</tr>"
for _, category_second := range category_list[category.Id] {
Html += "<tr>"
Html += "<td align='center'>" + strconv.FormatInt(category_second.Id, 10) + "</td>"
Html += "<td >  ⊢ " + category_second.Name
if category_second.Ismenu == 0 {
Html += "<img title='不在导航显示' src='/public/img/icon/gear_disable.png'>"
}
Html += "</td>"
Html += "<td >"
if category_second.Type == 0 {
if Admin_Info.Lang == "zh-cn" {
Html += "内部栏目"
} else {
Html += "Column"
}
} else {
if Admin_Info.Lang == "zh-cn" {
Html += "<font color=\"blue\">单网页</font>"
} else {
Html += "<font color=\"blue\">Single page</font>"
}
}
Html += "</td>"
Html += "<td align='center'>" + strconv.FormatInt(category_second.Hits, 10) + "</td>"
Html += "<td align='center'>" + category_second.Url + "</td>"
Html += "<td align=\"center\">"
if Admin_Info.Lang == "zh-cn" {
Html += "<a href=\"/Category/add/" + strconv.FormatInt(category_second.Id, 10) + "/\">添加子菜单</a> |"
Html += "<a href=\"/Category/edit/" + strconv.FormatInt(category_second.Id, 10) + "/\">修改</a> |"
Html += "<a onclick=\"delete_cate(" + strconv.FormatInt(category_second.Id, 10) + ")\" href=\"javascript:;\">删除</a>"
} else {
Html += "<a href=\"/Category/add/" + strconv.FormatInt(category_second.Id, 10) + "/\">Add Submenu</a> |"
Html += "<a href=\"/Category/edit/" + strconv.FormatInt(category_second.Id, 10) + "/\">Edit</a> |"
Html += "<a onclick=\"delete_cate(" + strconv.FormatInt(category_second.Id, 10) + ")\" href=\"javascript:;\">Delete</a>"
}
Html += "</td>"
Html += "</tr>"
for _, category_last := range category_list[category_second.Id] {
Html += "<tr>"
Html += "<td align='center'>" + strconv.FormatInt(category_last.Id, 10) + "</td>"
Html += "<td >  ⊢ " + category_last.Name
if category_last.Ismenu == 0 {
Html += "<img title='不在导航显示' src='/public/img/icon/gear_disable.png'>"
}
Html += "</td>"
Html += "<td >"
if category_last.Type == 0 {
if Admin_Info.Lang == "zh-cn" {
Html += "内部栏目"
} else {
Html += "Column"
}
} else {
if Admin_Info.Lang == "zh-cn" {
Html += "<font color=\"blue\">单网页</font>"
} else {
Html += "<font color=\"blue\">Single page</font>"
}
}
Html += "</td>"
Html += "<td align='center'>" + strconv.FormatInt(category_last.Hits, 10) + "</td>"
Html += "<td align='center'>" + category_last.Url + "</td>"
Html += "<td align=\"center\">"
if Admin_Info.Lang == "zh-cn" {
//Html += "<a href=\"/Category/add/" + strconv.FormatInt(category_last.Id, 10) + "/\">添加子菜单</a> |"
Html += "<a href=\"/Category/edit/" + strconv.FormatInt(category_last.Id, 10) + "/\">修改</a> |"
Html += "<a onclick=\"delete_cate(" + strconv.FormatInt(category_last.Id, 10) + ")\" href=\"javascript:;\">删除</a>"
} else {
//Html += "<a href=\"/Category/add/" + strconv.FormatInt(category_last.Id, 10) + "/\">Add Submenu</a> |"
Html += "<a href=\"/Category/edit/" + strconv.FormatInt(category_last.Id, 10) + "/\">Edit</a> |"
Html += "<a onclick=\"delete_cate(" + strconv.FormatInt(category_last.Id, 10) + ")\" href=\"javascript:;\">Delete</a>"
}
Html += "</td>"
Html += "</tr>"
}
}
}
return template.HTML(Html)
}
//返回菜单树
func (c *Category) GetLeftTree() template.HTML {
categorys := make([]*Category, 0)
DB_Read.Asc("order").Find(&categorys)
//初始化菜单Map
category_list := make(map[int64][]*Category)
for _, category := range categorys {
if _, ok := category_list[category.Pid]; !ok {
category_list[category.Pid] = make([]*Category, 0)
}
category_list[category.Pid] = append(category_list[category.Pid], category)
}
Html := "<SCRIPT type=\"text/javascript\">var zNodes =["
for _, category := range category_list[0] {
if category.Type == 0 {
Html += "{ id:" + strconv.FormatInt(category.Id, 10) + ", pId:" + strconv.FormatInt(category.Pid, 10) + ", name:'" + category.Name + "', open:true},"
} else {
Html += "{ id:" + strconv.FormatInt(category.Id, 10) + ", pId:" + strconv.FormatInt(category.Pid, 10) + ", name:'" + category.Name + "', url:'/Content/addContent/" + strconv.FormatInt(category.Id, 10) + "/', target :'right', icon :'/public/img/file.gif', open:true},"
}
for _, category_second := range category_list[category.Id] {
if len(category_list[category_second.Id]) > 0 {
Html += "{ id:" + strconv.FormatInt(category_second.Id, 10) + ", pId:" + strconv.FormatInt(category_second.Pid, 10) + ", name:'" + category_second.Name + "', open:true},"
} else {
if category_second.Type == 0 {
Html += "{ id:" + strconv.FormatInt(category_second.Id, 10) + ", pId:" + strconv.FormatInt(category_second.Pid, 10) + ", name:'" + category_second.Name + "', url:'/Content/list/" + strconv.FormatInt(category_second.Id, 10) + "/', target :'right', icon :'/public/img/add_content.gif', open:true},"
} else {
Html += "{ id:" + strconv.FormatInt(category_second.Id, 10) + ", pId:" + strconv.FormatInt(category_second.Pid, 10) + ", name:'" + category_second.Name + "', url:'/Content/addContent/" + strconv.FormatInt(category_second.Id, 10) + "/', target :'right', icon :'/public/img/file.gif', open:true},"
}
}
for _, category_last := range category_list[category_second.Id] {
if category_last.Type == 0 {
Html += "{ id:" + strconv.FormatInt(category_last.Id, 10) + ", pId:" + strconv.FormatInt(category_last.Pid, 10) + ", name:'" + category_last.Name + "', url:'/Content/list/" + strconv.FormatInt(category_last.Id, 10) + "/', target :'right', icon :'/public/img/add_content.gif', open:true},"
} else {
Html += "{ id:" + strconv.FormatInt(category_last.Id, 10) + ", pId:" + strconv.FormatInt(category_last.Pid, 10) + ", name:'" + category_last.Name + "', url:'/Content/addContent/" + strconv.FormatInt(category_last.Id, 10) + "/', target :'right', icon :'/public/img/file.gif', open:true},"
}
}
}
}
Html += "];</SCRIPT>"
return template.HTML(Html)
}
//返回左侧导航
func (c *Category) GetLeftHtml() template.HTML {
categorys := make([]*Category, 0)
DB_Read.Asc("order").Find(&categorys)
//初始化菜单Map
category_list := make(map[int64][]*Category)
for _, category := range categorys {
if _, ok := category_list[category.Pid]; !ok {
category_list[category.Pid] = make([]*Category, 0)
}
category_list[category.Pid] = append(category_list[category.Pid], category)
}
Html := ""
for _, category := range category_list[0] {
if len(category_list[category.Id]) > 0 {
Html += "<li id='" + strconv.FormatInt(category.Id, 10) + "'>"
Html += "<div class=\"hitarea\"></div>"
Html += "<span class='folder'>" + category.Name + "</span>"
} else {
Html += "<li id='" + strconv.FormatInt(category.Id, 10) + "'>"
Html += "<div class=\"hitarea\"></div>"
Html += "<a href=\"/Content/list/" + strconv.FormatInt(category.Id, 10) + "/\" target=\"right\"><img alt=\"添加\" src=\"/public/img/add_content.gif\"></a> "
Html += "<a href='/Content/list/" + strconv.FormatInt(category.Id, 10) + "/' target='right' onclick='open_list(this)'>" + category.Name + "</a>"
Html += "</li>"
}
if len(category_list[category.Id]) > 0 {
for _, category_second := range category_list[category.Id] {
if len(category_list[category_second.Id]) > 0 {
Html += "<ul><span class=''> "
Html += "<li id='" + strconv.FormatInt(category_second.Id, 10) + "'>"
Html += "<div class=\"hitarea\"></div>"
Html += "<span class='folder'>" + category_second.Name + "</span>"
} else {
Html += "<ul><span class=''> "
Html += "<li id='" + strconv.FormatInt(category_second.Id, 10) + "'>"
Html += "<a href=\"/Content/list/" + strconv.FormatInt(category_second.Id, 10) + "/\" target=\"right\"><img alt=\"添加\" src=\"/public/img/add_content.gif\"></a> "
Html += "<a href='/Content/list/" + strconv.FormatInt(category_second.Id, 10) + "/' target='right' onclick='open_list(this)'>" + category_second.Name + "</a>"
Html += "</li>"
}
for _, category_last := range category_list[category_second.Id] {
Html += "<ul><span class=''> "
Html += "<li id='" + strconv.FormatInt(category_last.Id, 10) + "'>"
Html += "<a href=\"/Content/list/" + strconv.FormatInt(category_last.Id, 10) + "/\" target=\"right\"><img alt=\"添加\" src=\"/public/img/add_content.gif\"></a> "
Html += "<a href='/Content/list/" + strconv.FormatInt(category_last.Id, 10) + "/' target='right' onclick='open_list(this)'>" + category_last.Name + "</a>"
Html += "</span></ul>"
}
if len(category_list[category_second.Id]) > 0 {
Html += "</li></span></ul>"
} else {
Html += "</li></span></ul>"
}
}
}
if len(category_list[category.Id]) > 0 {
Html += "</li>"
}
}
return template.HTML(Html)
}
//返回菜单Option的HTML
func (c *Category) GetCateGoryOptionHtml(Id int64) template.HTML {
categorys := make([]*Category, 0)
DB_Read.Asc("order").Find(&categorys)
//初始化菜单Map
category_list := make(map[int64][]*Category)
for _, category := range categorys {
if _, ok := category_list[category.Pid]; !ok {
category_list[category.Pid] = make([]*Category, 0)
}
category_list[category.Pid] = append(category_list[category.Pid], category)
}
Html := ""
for _, category := range category_list[0] {
if category.Id == Id {
Html += "<option value=" + strconv.FormatInt(category.Id, 10) + " selected ><b>" + category.Name + "</b></option>"
} else {
Html += "<option value=" + strconv.FormatInt(category.Id, 10) + "><b>" + category.Name + "</b></option>"
}
for _, category_second := range category_list[category.Id] {
if category_second.Id == Id {
Html += "<option value=" + strconv.FormatInt(category_second.Id, 10) + " selected > ⊢" + category_second.Name + "</option>"
} else {
Html += "<option value=" + strconv.FormatInt(category_second.Id, 10) + "> ⊢" + category_second.Name + "</option>"
}
for _, category_last := range category_list[category_second.Id] {
if category_last.Id == Id {
Html += "<option value=" + strconv.FormatInt(category_last.Id, 10) + " selected >  ⊢" + category_last.Name + "</option>"
} else {
Html += "<option value=" + strconv.FormatInt(category_last.Id, 10) + ">  ⊢" + category_last.Name + "</option>"
}
}
}
}
return template.HTML(Html)
}
//删除栏目
func (c *Category) DelByID(Id int64) bool {
category := new(Category)
has, err := DB_Write.Id(Id).Delete(category)
if err != nil {
revel.WARN.Println(has)
revel.WARN.Printf("错误: %v", err)
return false
}
return true
}