blog.go 8.0 KB

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