blog.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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() *Blog {
  29. if this == nil {
  30. return this
  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. return this
  267. }
  268. func (this *Blog) Category() *BlogCategory {
  269. if this == nil {
  270. return nil
  271. }
  272. return this.category
  273. }
  274. func (this *Blog) Post() *BlogPost {
  275. if this == nil {
  276. return nil
  277. }
  278. return this.post
  279. }
  280. func (this *Blog) HavePosts() bool {
  281. if this == nil {
  282. return false
  283. }
  284. if len(this.posts) <= 0 {
  285. return false
  286. }
  287. return true
  288. }
  289. func (this *Blog) Posts() []*BlogPost {
  290. if this == nil {
  291. return []*BlogPost{}
  292. }
  293. return this.posts
  294. }
  295. func (this *Blog) PostsCount() int {
  296. if this == nil {
  297. return 0
  298. }
  299. return this.postsCount
  300. }
  301. func (this *Blog) PostsPerPage() int {
  302. if this == nil {
  303. return 0
  304. }
  305. return this.postsPerPage
  306. }
  307. func (this *Blog) PostsMaxPage() int {
  308. if this == nil {
  309. return 0
  310. }
  311. return this.postsMaxPage
  312. }
  313. func (this *Blog) PostsCurrPage() int {
  314. if this == nil {
  315. return 0
  316. }
  317. return this.postsCurrPage
  318. }
  319. func (this *Blog) Pagination() []*BlogPagination {
  320. if this == nil {
  321. return []*BlogPagination{}
  322. }
  323. return this.pagination
  324. }
  325. func (this *Blog) PaginationPrev() *BlogPagination {
  326. if this == nil {
  327. return nil
  328. }
  329. return this.paginationPrev
  330. }
  331. func (this *Blog) PaginationNext() *BlogPagination {
  332. if this == nil {
  333. return nil
  334. }
  335. return this.paginationNext
  336. }
  337. func (this *Blog) Categories(mlvl int) []*BlogCategory {
  338. if this == nil {
  339. return []*BlogCategory{}
  340. }
  341. if this.bufferCats == nil {
  342. this.bufferCats = map[string][]*BlogCategory{}
  343. }
  344. key := ""
  345. where := ``
  346. if mlvl > 0 {
  347. where += `AND tbl.depth <= ` + utils.IntToStr(mlvl)
  348. }
  349. if _, ok := this.bufferCats[key]; !ok {
  350. var cats []*BlogCategory
  351. if rows, err := this.wrap.DB.Query(`
  352. SELECT
  353. tbl.*
  354. FROM
  355. (
  356. SELECT
  357. node.id,
  358. node.user,
  359. node.name,
  360. node.alias,
  361. node.lft,
  362. node.rgt,
  363. (COUNT(parent.id) - 1) AS depth
  364. FROM
  365. blog_cats AS node,
  366. blog_cats AS parent
  367. WHERE
  368. node.lft BETWEEN parent.lft AND parent.rgt
  369. GROUP BY
  370. node.id
  371. ORDER BY
  372. node.lft ASC
  373. ) AS tbl
  374. WHERE
  375. tbl.id > 1
  376. ` + where + `
  377. ;
  378. `); err == nil {
  379. defer rows.Close()
  380. for rows.Next() {
  381. row := utils.MySql_blog_category{}
  382. var Depth int
  383. 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 {
  384. cats = append(cats, &BlogCategory{object: &row, depth: Depth})
  385. }
  386. }
  387. }
  388. this.bufferCats[key] = cats
  389. }
  390. return this.bufferCats[key]
  391. }