blog.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package fetdata
  2. import (
  3. "math"
  4. "strings"
  5. "golang-fave/engine/wrapper"
  6. "golang-fave/utils"
  7. )
  8. type BlogPagination struct {
  9. Num string
  10. Link string
  11. Current bool
  12. }
  13. type Blog struct {
  14. wrap *wrapper.Wrapper
  15. category *BlogCategory
  16. post *BlogPost
  17. posts []*BlogPost
  18. postsCount int
  19. postsPerPage int
  20. postsMaxPage int
  21. postsCurrPage int
  22. pagination []*BlogPagination
  23. paginationPrev *BlogPagination
  24. paginationNext *BlogPagination
  25. bufferCats map[string][]*BlogCategory
  26. }
  27. func (this *Blog) init() {
  28. if this == nil {
  29. return
  30. }
  31. sql_nums := `
  32. SELECT
  33. COUNT(*)
  34. FROM
  35. blog_posts
  36. WHERE
  37. active = 1
  38. ;
  39. `
  40. sql_rows := `
  41. SELECT
  42. id,
  43. user,
  44. name,
  45. alias,
  46. briefly,
  47. content,
  48. UNIX_TIMESTAMP(datetime) as datetime,
  49. active
  50. FROM
  51. blog_posts
  52. WHERE
  53. active = 1
  54. ORDER BY
  55. id DESC
  56. LIMIT ?, ?;
  57. `
  58. // Category selected
  59. if this.category != nil {
  60. var cat_ids []string
  61. if rows, err := this.wrap.DB.Query(
  62. `SELECT
  63. node.id
  64. FROM
  65. blog_cats AS node,
  66. blog_cats AS parent
  67. WHERE
  68. node.lft BETWEEN parent.lft AND parent.rgt AND
  69. node.id > 1 AND
  70. parent.id = ?
  71. GROUP BY
  72. node.id
  73. ORDER BY
  74. node.lft ASC
  75. ;`,
  76. this.category.Id(),
  77. ); err == nil {
  78. defer rows.Close()
  79. for rows.Next() {
  80. var cat_id string
  81. if err := rows.Scan(&cat_id); err == nil {
  82. cat_ids = append(cat_ids, cat_id)
  83. }
  84. }
  85. }
  86. sql_nums = `
  87. SELECT
  88. COUNT(*)
  89. FROM
  90. (
  91. SELECT
  92. COUNT(*)
  93. FROM
  94. blog_posts
  95. LEFT JOIN blog_cat_post_rel ON blog_cat_post_rel.post_id = blog_posts.id
  96. WHERE
  97. blog_posts.active = 1 AND
  98. blog_cat_post_rel.category_id IN (` + strings.Join(cat_ids, ", ") + `)
  99. GROUP BY
  100. blog_posts.id
  101. ) AS tbl
  102. ;
  103. `
  104. sql_rows = `
  105. SELECT
  106. blog_posts.id,
  107. blog_posts.user,
  108. blog_posts.name,
  109. blog_posts.alias,
  110. blog_posts.briefly,
  111. blog_posts.content,
  112. UNIX_TIMESTAMP(blog_posts.datetime) AS datetime,
  113. blog_posts.active
  114. FROM
  115. blog_posts
  116. LEFT JOIN blog_cat_post_rel ON blog_cat_post_rel.post_id = blog_posts.id
  117. WHERE
  118. blog_posts.active = 1 AND
  119. blog_cat_post_rel.category_id IN (` + strings.Join(cat_ids, ", ") + `)
  120. GROUP BY
  121. blog_posts.id
  122. ORDER BY
  123. blog_posts.id DESC
  124. LIMIT ?, ?;
  125. `
  126. }
  127. if err := this.wrap.DB.QueryRow(sql_nums).Scan(&this.postsCount); err == nil {
  128. // TODO: to control panel settings
  129. this.postsPerPage = 5
  130. this.postsMaxPage = int(math.Ceil(float64(this.postsCount) / float64(this.postsPerPage)))
  131. this.postsCurrPage = this.wrap.GetCurrentPage(this.postsMaxPage)
  132. offset := this.postsCurrPage*this.postsPerPage - this.postsPerPage
  133. if rows, err := this.wrap.DB.Query(sql_rows, offset, this.postsPerPage); err == nil {
  134. defer rows.Close()
  135. for rows.Next() {
  136. row := utils.MySql_blog_post{}
  137. if err := rows.Scan(&row.A_id, &row.A_user, &row.A_name, &row.A_alias, &row.A_briefly, &row.A_content, &row.A_datetime, &row.A_active); err == nil {
  138. this.posts = append(this.posts, &BlogPost{object: &row})
  139. }
  140. }
  141. }
  142. }
  143. // Build pagination
  144. for i := 1; i <= this.postsMaxPage; i++ {
  145. link := this.wrap.R.URL.Path
  146. if i > 1 {
  147. link = link + "?p=" + utils.IntToStr(i)
  148. }
  149. this.pagination = append(this.pagination, &BlogPagination{
  150. Num: utils.IntToStr(i),
  151. Link: link,
  152. Current: i == this.postsCurrPage,
  153. })
  154. }
  155. // Pagination prev/next
  156. if this.postsMaxPage > 1 {
  157. link := this.wrap.R.URL.Path
  158. if this.postsCurrPage-1 > 1 {
  159. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.postsCurrPage-1)
  160. }
  161. this.paginationPrev = &BlogPagination{
  162. Num: utils.IntToStr(this.postsCurrPage - 1),
  163. Link: link,
  164. Current: this.postsCurrPage <= 1,
  165. }
  166. if this.postsCurrPage >= 1 && this.postsCurrPage < this.postsMaxPage {
  167. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.postsCurrPage+1)
  168. } else {
  169. link = this.wrap.R.URL.Path + "?p=" + utils.IntToStr(this.postsMaxPage)
  170. }
  171. this.paginationNext = &BlogPagination{
  172. Num: utils.IntToStr(this.postsCurrPage + 1),
  173. Link: link,
  174. Current: this.postsCurrPage >= this.postsMaxPage,
  175. }
  176. }
  177. }
  178. func (this *Blog) Category() *BlogCategory {
  179. if this == nil {
  180. return nil
  181. }
  182. return this.category
  183. }
  184. func (this *Blog) Post() *BlogPost {
  185. if this == nil {
  186. return nil
  187. }
  188. return this.post
  189. }
  190. func (this *Blog) HavePosts() bool {
  191. if this == nil {
  192. return false
  193. }
  194. if len(this.posts) <= 0 {
  195. return false
  196. }
  197. return true
  198. }
  199. func (this *Blog) Posts() []*BlogPost {
  200. if this == nil {
  201. return []*BlogPost{}
  202. }
  203. return this.posts
  204. }
  205. func (this *Blog) PostsCount() int {
  206. if this == nil {
  207. return 0
  208. }
  209. return this.postsCount
  210. }
  211. func (this *Blog) PostsPerPage() int {
  212. if this == nil {
  213. return 0
  214. }
  215. return this.postsPerPage
  216. }
  217. func (this *Blog) PostsMaxPage() int {
  218. if this == nil {
  219. return 0
  220. }
  221. return this.postsMaxPage
  222. }
  223. func (this *Blog) PostsCurrPage() int {
  224. if this == nil {
  225. return 0
  226. }
  227. return this.postsCurrPage
  228. }
  229. func (this *Blog) Pagination() []*BlogPagination {
  230. if this == nil {
  231. return []*BlogPagination{}
  232. }
  233. return this.pagination
  234. }
  235. func (this *Blog) PaginationPrev() *BlogPagination {
  236. if this == nil {
  237. return nil
  238. }
  239. return this.paginationPrev
  240. }
  241. func (this *Blog) PaginationNext() *BlogPagination {
  242. if this == nil {
  243. return nil
  244. }
  245. return this.paginationNext
  246. }
  247. func (this *Blog) Categories(mlvl int) []*BlogCategory {
  248. if this == nil {
  249. return []*BlogCategory{}
  250. }
  251. if this.bufferCats == nil {
  252. this.bufferCats = map[string][]*BlogCategory{}
  253. }
  254. key := ""
  255. where := ``
  256. if mlvl > 0 {
  257. where += `AND tbl.depth <= ` + utils.IntToStr(mlvl)
  258. }
  259. if _, ok := this.bufferCats[key]; !ok {
  260. var cats []*BlogCategory
  261. if rows, err := this.wrap.DB.Query(`
  262. SELECT
  263. tbl.*
  264. FROM
  265. (
  266. SELECT
  267. node.id,
  268. node.user,
  269. node.name,
  270. node.alias,
  271. node.lft,
  272. node.rgt,
  273. (COUNT(parent.id) - 1) AS depth
  274. FROM
  275. blog_cats AS node,
  276. blog_cats AS parent
  277. WHERE
  278. node.lft BETWEEN parent.lft AND parent.rgt
  279. GROUP BY
  280. node.id
  281. ORDER BY
  282. node.lft ASC
  283. ) AS tbl
  284. WHERE
  285. tbl.id > 1
  286. ` + where + `
  287. ;
  288. `); err == nil {
  289. defer rows.Close()
  290. for rows.Next() {
  291. row := utils.MySql_blog_category{}
  292. var Depth int
  293. if err := rows.Scan(&row.A_id, &row.A_user, &row.A_name, &row.A_alias, &row.A_lft, &row.A_rgt, &Depth); err == nil {
  294. cats = append(cats, &BlogCategory{object: &row, depth: Depth})
  295. }
  296. }
  297. }
  298. this.bufferCats[key] = cats
  299. }
  300. return this.bufferCats[key]
  301. }