module_blog_categories_act_modify.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package modules
  2. import (
  3. "context"
  4. "errors"
  5. "golang-fave/engine/utils"
  6. "golang-fave/engine/wrapper"
  7. )
  8. func (this *Modules) blog_ActionCategoryAdd(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) (error, int64) {
  9. var lastID int64 = 0
  10. return wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  11. // Block rows
  12. if _, err := tx.Exec(ctx, "SELECT id FROM fave_blog_cats FOR UPDATE;"); err != nil {
  13. return err
  14. }
  15. // Process
  16. if _, err := tx.Exec(ctx, "SELECT @mr := rgt FROM fave_blog_cats WHERE id = ?;", utils.StrToInt(pf_parent)); err != nil {
  17. return err
  18. }
  19. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET rgt = rgt + 2 WHERE rgt > @mr;"); err != nil {
  20. return err
  21. }
  22. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET lft = lft + 2 WHERE lft > @mr;"); err != nil {
  23. return err
  24. }
  25. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET rgt = rgt + 2 WHERE id = ?;", utils.StrToInt(pf_parent)); err != nil {
  26. return err
  27. }
  28. res, err := tx.Exec(ctx, "INSERT INTO fave_blog_cats (id, user, name, alias, lft, rgt) VALUES (NULL, ?, ?, ?, @mr, @mr + 1);", wrap.User.A_id, pf_name, pf_alias)
  29. if err != nil {
  30. return err
  31. }
  32. lastID, err = res.LastInsertId()
  33. if err != nil {
  34. return err
  35. }
  36. return nil
  37. }), lastID
  38. }
  39. func (this *Modules) blog_ActionCategoryUpdate(wrap *wrapper.Wrapper, pf_id, pf_name, pf_alias, pf_parent string) error {
  40. parentId := this.blog_GetCategoryParentId(wrap, utils.StrToInt(pf_id))
  41. if utils.StrToInt(pf_parent) == parentId {
  42. // If parent not changed, just update category data
  43. return wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  44. // Process
  45. if _, err := tx.Exec(
  46. ctx,
  47. `UPDATE fave_blog_cats SET
  48. name = ?,
  49. alias = ?
  50. WHERE
  51. id > 1 AND
  52. id = ?
  53. ;`,
  54. pf_name,
  55. pf_alias,
  56. utils.StrToInt(pf_id),
  57. ); err != nil {
  58. return err
  59. }
  60. return nil
  61. })
  62. }
  63. // TODO: Fix parent change
  64. // Parent is changed, move category to new parent
  65. return wrap.DB.Transaction(wrap.R.Context(), func(ctx context.Context, tx *wrapper.Tx) error {
  66. // Block all rows
  67. if _, err := tx.Exec(ctx, "SELECT id FROM fave_blog_cats FOR UPDATE;"); err != nil {
  68. return err
  69. }
  70. var parentL int
  71. var parentR int
  72. if err := tx.QueryRow(ctx, `SELECT lft, rgt FROM fave_blog_cats WHERE id = ?;`, utils.StrToInt(pf_parent)).Scan(&parentL, &parentR); *wrap.LogCpError(&err) != nil {
  73. return err
  74. }
  75. var targetL int
  76. var targetR int
  77. if err := tx.QueryRow(ctx, `SELECT lft, rgt FROM fave_blog_cats WHERE id = ?;`, utils.StrToInt(pf_id)).Scan(&targetL, &targetR); *wrap.LogCpError(&err) != nil {
  78. return err
  79. }
  80. if !(targetL < parentL && targetR > parentR) {
  81. // Select data
  82. rows, err := tx.Query(ctx, "SELECT id, lft, rgt FROM fave_blog_cats WHERE lft >= ? and rgt <= ? ORDER BY lft ASC", targetL, targetR)
  83. if err != nil {
  84. return err
  85. }
  86. defer rows.Close()
  87. var rows_id []int
  88. var rows_lft []int
  89. var rows_rgt []int
  90. for rows.Next() {
  91. var row_id int
  92. var row_lft int
  93. var row_rgt int
  94. if err := rows.Scan(&row_id, &row_lft, &row_rgt); *wrap.LogCpError(&err) == nil {
  95. rows_id = append(rows_id, row_id)
  96. rows_lft = append(rows_lft, row_lft)
  97. rows_rgt = append(rows_rgt, row_rgt)
  98. }
  99. }
  100. if targetL > parentR {
  101. // From right to left
  102. // Shift
  103. step := targetR - targetL + 1
  104. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET lft = lft + ? WHERE lft > ? and lft < ?;", step, parentR, targetL); err != nil {
  105. return err
  106. }
  107. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET rgt = rgt + ? WHERE rgt > ? and rgt < ?;", step, parentR, targetL); err != nil {
  108. return err
  109. }
  110. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET rgt = rgt + ? WHERE id = ?;", step, utils.StrToInt(pf_parent)); err != nil {
  111. return err
  112. }
  113. // Update target rows
  114. for i, _ := range rows_id {
  115. new_lft := rows_lft[i] - (targetL - parentR)
  116. new_rgt := rows_rgt[i] - (targetL - parentR)
  117. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET lft = ?, rgt = ? WHERE id = ?;", new_lft, new_rgt, rows_id[i]); err != nil {
  118. return err
  119. }
  120. }
  121. } else {
  122. // From left to right
  123. // Shift
  124. step := targetR - targetL + 1
  125. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET lft = lft - ? WHERE lft > ? and lft < ?;", step, targetR, parentR); err != nil {
  126. return err
  127. }
  128. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET rgt = rgt - ? WHERE rgt > ? and rgt < ?;", step, targetR, parentR); err != nil {
  129. return err
  130. }
  131. // Update target rows
  132. for i, _ := range rows_id {
  133. new_lft := rows_lft[i] + (parentR - targetL - step)
  134. new_rgt := rows_rgt[i] + (parentR - targetL - step)
  135. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET lft = ?, rgt = ? WHERE id = ?;", new_lft, new_rgt, rows_id[i]); err != nil {
  136. return err
  137. }
  138. }
  139. }
  140. } else {
  141. // Trying to move category to they child as parent
  142. return errors.New("Category can't be moved inside here child")
  143. }
  144. // Update target cat data
  145. if _, err := tx.Exec(ctx, "UPDATE fave_blog_cats SET name = ?, alias = ? WHERE id = ?;", pf_name, pf_alias, utils.StrToInt(pf_id)); err != nil {
  146. return err
  147. }
  148. return nil
  149. })
  150. }
  151. func (this *Modules) RegisterAction_BlogCategoriesModify() *Action {
  152. return this.newAction(AInfo{
  153. WantDB: true,
  154. Mount: "blog-categories-modify",
  155. WantAdmin: true,
  156. }, func(wrap *wrapper.Wrapper) {
  157. pf_id := utils.Trim(wrap.R.FormValue("id"))
  158. pf_name := utils.Trim(wrap.R.FormValue("name"))
  159. pf_alias := utils.Trim(wrap.R.FormValue("alias"))
  160. pf_parent := utils.Trim(wrap.R.FormValue("parent"))
  161. if !utils.IsNumeric(pf_id) || !utils.IsNumeric(pf_parent) {
  162. wrap.MsgError(`Inner system error`)
  163. return
  164. }
  165. if pf_name == "" {
  166. wrap.MsgError(`Please specify category name`)
  167. return
  168. }
  169. if pf_alias == "" {
  170. pf_alias = utils.GenerateSingleAlias(pf_name)
  171. }
  172. if !utils.IsValidSingleAlias(pf_alias) {
  173. wrap.MsgError(`Please specify correct category alias`)
  174. return
  175. }
  176. // Set root category as default
  177. if pf_parent == "0" {
  178. pf_parent = "1"
  179. } else {
  180. // Check if parent category exists
  181. var parentId int
  182. err := wrap.DB.QueryRow(
  183. wrap.R.Context(),
  184. `SELECT
  185. id
  186. FROM
  187. fave_blog_cats
  188. WHERE
  189. id > 1 AND
  190. id = ?
  191. LIMIT 1;`,
  192. utils.StrToInt(pf_parent),
  193. ).Scan(&parentId)
  194. if *wrap.LogCpError(&err) != nil {
  195. wrap.MsgError(err.Error())
  196. return
  197. }
  198. }
  199. if pf_id == "0" {
  200. var err error = nil
  201. var lastID int64 = 0
  202. if err, lastID = this.blog_ActionCategoryAdd(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
  203. wrap.MsgError(err.Error())
  204. return
  205. }
  206. wrap.ResetCacheBlocks()
  207. wrap.Write(`window.location='/cp/blog/categories-modify/` + utils.Int64ToStr(lastID) + `/';`)
  208. } else {
  209. if err := this.blog_ActionCategoryUpdate(wrap, pf_id, pf_name, pf_alias, pf_parent); err != nil {
  210. wrap.MsgError(err.Error())
  211. return
  212. }
  213. wrap.ResetCacheBlocks()
  214. wrap.Write(`window.location='/cp/blog/categories-modify/` + pf_id + `/';`)
  215. }
  216. })
  217. }