events.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package views
  2. import (
  3. "reflect"
  4. "github.com/qor5/admin/activity"
  5. "github.com/qor5/admin/presets"
  6. "github.com/qor5/admin/presets/actions"
  7. "github.com/qor5/admin/publish"
  8. "github.com/qor5/web"
  9. "github.com/qor5/x/i18n"
  10. "github.com/sunfmin/reflectutils"
  11. "gorm.io/gorm"
  12. )
  13. const (
  14. PublishEvent = "publish_PublishEvent"
  15. RepublishEvent = "publish_RepublishEvent"
  16. UnpublishEvent = "publish_UnpublishEvent"
  17. switchVersionEvent = "publish_SwitchVersionEvent"
  18. SaveNewVersionEvent = "publish_SaveNewVersionEvent"
  19. DuplicateVersionEvent = "publish_DuplicateVersionEvent"
  20. renameVersionEvent = "publish_RenameVersionEvent"
  21. selectVersionsEvent = "publish_SelectVersionsEvent"
  22. afterDeleteVersionEvent = "publish_AfterDeleteVersionEvent"
  23. ActivityPublish = "Publish"
  24. ActivityRepublish = "Republish"
  25. ActivityUnPublish = "UnPublish"
  26. ParamScriptAfterPublish = "publish_param_script_after_publish"
  27. )
  28. func registerEventFuncs(db *gorm.DB, mb *presets.ModelBuilder, publisher *publish.Builder, ab *activity.ActivityBuilder) {
  29. mb.RegisterEventFunc(PublishEvent, publishAction(db, mb, publisher, ab, ActivityPublish))
  30. mb.RegisterEventFunc(RepublishEvent, publishAction(db, mb, publisher, ab, ActivityRepublish))
  31. mb.RegisterEventFunc(UnpublishEvent, unpublishAction(db, mb, publisher, ab, ActivityUnPublish))
  32. mb.RegisterEventFunc(switchVersionEvent, switchVersionAction(db, mb, publisher))
  33. mb.RegisterEventFunc(SaveNewVersionEvent, saveNewVersionAction(db, mb, publisher))
  34. mb.RegisterEventFunc(DuplicateVersionEvent, duplicateVersionAction(db, mb, publisher))
  35. mb.RegisterEventFunc(renameVersionEvent, renameVersionAction(db, mb, publisher, ab, ActivityUnPublish))
  36. mb.RegisterEventFunc(selectVersionsEvent, selectVersionsAction(db, mb, publisher, ab, ActivityUnPublish))
  37. mb.RegisterEventFunc(afterDeleteVersionEvent, afterDeleteVersionAction(db, mb, publisher))
  38. }
  39. func publishAction(db *gorm.DB, mb *presets.ModelBuilder, publisher *publish.Builder, ab *activity.ActivityBuilder, actionName string) web.EventFunc {
  40. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  41. paramID := ctx.R.FormValue(presets.ParamID)
  42. obj := mb.NewModel()
  43. obj, err = mb.Editing().Fetcher(obj, paramID, ctx)
  44. if err != nil {
  45. return
  46. }
  47. publisher.WithEventContext(ctx)
  48. err = publisher.Publish(obj)
  49. if err != nil {
  50. return
  51. }
  52. if ab != nil {
  53. if _, exist := ab.GetModelBuilder(obj); exist {
  54. ab.AddCustomizedRecord(actionName, false, ctx.R.Context(), obj)
  55. }
  56. }
  57. if script := ctx.R.FormValue(ParamScriptAfterPublish); script != "" {
  58. web.AppendVarsScripts(&r, script)
  59. } else {
  60. presets.ShowMessage(&r, "success", "")
  61. r.Reload = true
  62. }
  63. return
  64. }
  65. }
  66. func unpublishAction(db *gorm.DB, mb *presets.ModelBuilder, publisher *publish.Builder, ab *activity.ActivityBuilder, actionName string) web.EventFunc {
  67. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  68. paramID := ctx.R.FormValue(presets.ParamID)
  69. obj := mb.NewModel()
  70. obj, err = mb.Editing().Fetcher(obj, paramID, ctx)
  71. if err != nil {
  72. return
  73. }
  74. err = publisher.UnPublish(obj)
  75. if err != nil {
  76. return
  77. }
  78. if ab != nil {
  79. if _, exist := ab.GetModelBuilder(obj); exist {
  80. ab.AddCustomizedRecord(actionName, false, ctx.R.Context(), obj)
  81. }
  82. }
  83. presets.ShowMessage(&r, "success", "")
  84. r.Reload = true
  85. return
  86. }
  87. }
  88. func renameVersionAction(db *gorm.DB, mb *presets.ModelBuilder, publisher *publish.Builder, ab *activity.ActivityBuilder, actionName string) web.EventFunc {
  89. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  90. paramID := ctx.R.FormValue(presets.ParamID)
  91. obj := mb.NewModel()
  92. obj, err = mb.Editing().Fetcher(obj, paramID, ctx)
  93. if err != nil {
  94. return
  95. }
  96. name := ctx.R.FormValue("name")
  97. if err = reflectutils.Set(obj, "Version.VersionName", name); err != nil {
  98. return
  99. }
  100. if err = mb.Editing().Saver(obj, paramID, ctx); err != nil {
  101. return
  102. }
  103. msgr := i18n.MustGetModuleMessages(ctx.R, I18nPublishKey, Messages_en_US).(*Messages)
  104. presets.ShowMessage(&r, msgr.SuccessfullyRename, "")
  105. return
  106. }
  107. }
  108. func selectVersionsAction(db *gorm.DB, mb *presets.ModelBuilder, publisher *publish.Builder, ab *activity.ActivityBuilder, actionName string) web.EventFunc {
  109. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  110. var (
  111. msgr = i18n.MustGetModuleMessages(ctx.R, I18nPublishKey, Messages_en_US).(*Messages)
  112. )
  113. table, _, err := versionListTable(db, mb, msgr, ctx)
  114. if err != nil {
  115. return
  116. }
  117. r.UpdatePortals = append(r.UpdatePortals, &web.PortalUpdate{
  118. Name: "versions-list",
  119. Body: table,
  120. })
  121. return
  122. }
  123. }
  124. func afterDeleteVersionAction(db *gorm.DB, mb *presets.ModelBuilder, publisher *publish.Builder) web.EventFunc {
  125. return func(ctx *web.EventContext) (r web.EventResponse, err error) {
  126. qs := ctx.Queries()
  127. cs := mb.NewModel().(presets.SlugDecoder).PrimaryColumnValuesBySlug(ctx.R.FormValue("id"))
  128. deletedVersion := cs["version"]
  129. deletedID := qs.Get("id")
  130. currentSelectedID := qs.Get("current_selected_id")
  131. // switching version is one of the following in order that exists:
  132. // 1. current selected version
  133. // 2. next(older) version
  134. // 3. prev(newer) version
  135. switchingVersion := currentSelectedID
  136. if deletedID == currentSelectedID {
  137. versions, _ := findVersionItems(db, mb, ctx, deletedID)
  138. vO := reflect.ValueOf(versions).Elem()
  139. if vO.Len() == 0 {
  140. r.Reload = true
  141. return
  142. }
  143. version := vO.Index(0).Interface()
  144. if vO.Len() > 1 {
  145. hasOlderVersion := false
  146. for i := 0; i < vO.Len(); i++ {
  147. v := vO.Index(i).Interface()
  148. if v.(publish.VersionInterface).GetVersion() < deletedVersion {
  149. hasOlderVersion = true
  150. version = v
  151. break
  152. }
  153. }
  154. if !hasOlderVersion {
  155. version = vO.Index(vO.Len() - 1)
  156. }
  157. }
  158. switchingVersion = version.(presets.SlugEncoder).PrimarySlug()
  159. }
  160. web.AppendVarsScripts(&r,
  161. web.Plaid().
  162. EventFunc(switchVersionEvent).
  163. Query(presets.ParamID, switchingVersion).
  164. Query("selected", qs.Get("selected")).
  165. Query("page", qs.Get("page")).
  166. Query("no_msg", true).
  167. Go(),
  168. web.Plaid().
  169. EventFunc(actions.ReloadList).
  170. Go(),
  171. )
  172. return
  173. }
  174. }