blog.go 8.6 KB

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