blog.go 6.1 KB

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