settings.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package pagebuilder
  2. import (
  3. "fmt"
  4. "net/url"
  5. "os"
  6. "github.com/qor5/admin/note"
  7. "github.com/qor5/admin/presets"
  8. "github.com/qor5/admin/presets/actions"
  9. "github.com/qor5/admin/publish"
  10. pv "github.com/qor5/admin/publish/views"
  11. "github.com/qor5/admin/utils"
  12. . "github.com/qor5/ui/vuetify"
  13. vx "github.com/qor5/ui/vuetifyx"
  14. "github.com/qor5/web"
  15. "github.com/qor5/x/i18n"
  16. h "github.com/theplant/htmlgo"
  17. "gorm.io/gorm"
  18. )
  19. func settings(db *gorm.DB, pm *presets.ModelBuilder) presets.FieldComponentFunc {
  20. return func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  21. mi := field.ModelInfo
  22. p := obj.(*Page)
  23. c := &Category{}
  24. db.First(c, "id = ?", p.CategoryID)
  25. overview := vx.DetailInfo(
  26. vx.DetailColumn(
  27. vx.DetailField(vx.OptionalText(p.Title).ZeroLabel("No Title")).Label("Title"),
  28. vx.DetailField(vx.OptionalText(c.Path).ZeroLabel("No Category")).Label("Category"),
  29. ),
  30. vx.DetailColumn(
  31. vx.DetailField(vx.OptionalText(p.Slug).ZeroLabel("No Slug")).Label("Slug"),
  32. ),
  33. )
  34. var start, end, se string
  35. if p.GetScheduledStartAt() != nil {
  36. start = p.GetScheduledStartAt().Format("2006-01-02 15:04")
  37. }
  38. if p.GetScheduledEndAt() != nil {
  39. end = p.GetScheduledEndAt().Format("2006-01-02 15:04")
  40. }
  41. if start != "" || end != "" {
  42. se = start + " ~ " + end
  43. }
  44. var publishURL string
  45. if p.GetStatus() == publish.StatusOnline {
  46. var err error
  47. publishURL, err = url.JoinPath(os.Getenv("PUBLISH_URL"), p.getAccessUrl(p.GetOnlineUrl()))
  48. if err != nil {
  49. panic(err)
  50. }
  51. }
  52. pageState := vx.DetailInfo(
  53. vx.DetailColumn(
  54. vx.DetailField(vx.OptionalText(p.GetStatus()).ZeroLabel("No State")).Label("State"),
  55. vx.DetailField(h.A(h.Text(publishURL)).Href(publishURL).Target("_blank").Class("text-truncate")).Label("URL"),
  56. vx.DetailField(vx.OptionalText(se).ZeroLabel("No Set")).Label("SchedulePublishTime"),
  57. ),
  58. )
  59. var notes []note.QorNote
  60. ri := p.PrimarySlug()
  61. rt := pm.Info().Label()
  62. db.Where("resource_type = ? and resource_id = ?", rt, ri).
  63. Order("id DESC").Find(&notes)
  64. if len(notes) > 0 {
  65. userID, _ := note.GetUserData(ctx)
  66. userNote := note.UserNote{UserID: userID, ResourceType: rt, ResourceID: ri}
  67. db.Where(userNote).FirstOrCreate(&userNote)
  68. if userNote.Number != int64(len(notes)) {
  69. userNote.Number = int64(len(notes))
  70. db.Save(&userNote)
  71. }
  72. }
  73. var notesSetcion h.HTMLComponent
  74. if len(notes) > 0 {
  75. s := VContainer()
  76. for _, n := range notes {
  77. s.AppendChildren(VRow(VCardText(h.Text(n.Content)).Class("pb-0")))
  78. s.AppendChildren(VRow(VCardText(h.Text(fmt.Sprintf("%v - %v", n.Creator, n.CreatedAt.Format("2006-01-02 15:04:05 MST")))).Class("pt-0")))
  79. }
  80. notesSetcion = s
  81. }
  82. var editBtn h.HTMLComponent
  83. var pageStateBtn h.HTMLComponent
  84. var seoBtn h.HTMLComponent
  85. pvMsgr := i18n.MustGetModuleMessages(ctx.R, pv.I18nPublishKey, utils.Messages_en_US).(*pv.Messages)
  86. if p.GetStatus() == publish.StatusDraft {
  87. editBtn = VBtn("Edit").Depressed(true).
  88. Attr("@click", web.POST().
  89. EventFunc(actions.Edit).
  90. Query(presets.ParamOverlay, actions.Dialog).
  91. Query(presets.ParamID, p.PrimarySlug()).
  92. URL(mi.PresetsPrefix()+"/pages").Go(),
  93. )
  94. seoBtn = VBtn("Edit").Depressed(true).
  95. Attr("@click", web.POST().
  96. EventFunc(editSEODialogEvent).
  97. Query(presets.ParamOverlay, actions.Drawer).
  98. Query(presets.ParamID, p.PrimarySlug()).
  99. URL(mi.PresetsPrefix()+"/pages").Go(),
  100. )
  101. }
  102. if p.GetStatus() == publish.StatusOnline {
  103. pageStateBtn = VBtn(pvMsgr.Unpublish).Depressed(true).Class("mr-2").Attr("@click", fmt.Sprintf(`locals.action="%s";locals.commonConfirmDialog = true`, pv.UnpublishEvent))
  104. } else {
  105. pageStateBtn = VBtn("Schedule Publish").Depressed(true).
  106. Attr("@click", web.POST().
  107. EventFunc(schedulePublishDialogEvent).
  108. Query(presets.ParamOverlay, actions.Dialog).
  109. Query(presets.ParamID, p.PrimarySlug()).
  110. URL(mi.PresetsPrefix()+"/pages").Go(),
  111. )
  112. }
  113. seoState := "Default"
  114. if p.SEO.EnabledCustomize {
  115. seoState = "Customized"
  116. }
  117. seo := vx.DetailInfo(
  118. vx.DetailColumn(
  119. vx.DetailField(vx.OptionalText(seoState)).Label("State"),
  120. ),
  121. )
  122. return VContainer(
  123. VRow(
  124. VCol(
  125. vx.Card(overview).HeaderTitle("Overview").
  126. Actions(
  127. h.If(editBtn != nil, editBtn),
  128. ).Class("mb-4 rounded-lg").Outlined(true),
  129. vx.Card(pageState).HeaderTitle("Page State").
  130. Actions(
  131. h.If(pageStateBtn != nil, pageStateBtn),
  132. ).Class("mb-4 rounded-lg").Outlined(true),
  133. vx.Card(seo).HeaderTitle("SEO").
  134. Actions(
  135. h.If(seoBtn != nil, seoBtn),
  136. ).Class("mb-4 rounded-lg").Outlined(true),
  137. ).Cols(8),
  138. VCol(
  139. vx.Card(notesSetcion).HeaderTitle("Notes").
  140. Actions(
  141. VBtn("Create").Depressed(true).
  142. Attr("@click", web.POST().
  143. EventFunc(createNoteDialogEvent).
  144. Query(presets.ParamOverlay, actions.Dialog).
  145. Query(presets.ParamID, p.PrimarySlug()).
  146. URL(mi.PresetsPrefix()+"/pages").Go(),
  147. ),
  148. ).Class("mb-4 rounded-lg").Outlined(true),
  149. ).Cols(4),
  150. ),
  151. )
  152. }
  153. }
  154. func templateSettings(db *gorm.DB, pm *presets.ModelBuilder) presets.FieldComponentFunc {
  155. return func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  156. p := obj.(*Template)
  157. overview := vx.DetailInfo(
  158. vx.DetailColumn(
  159. vx.DetailField(vx.OptionalText(p.Name)).Label("Title"),
  160. vx.DetailField(vx.OptionalText(p.Description)).Label("Description"),
  161. ),
  162. )
  163. editBtn := VBtn("Edit").Depressed(true).
  164. Attr("@click", web.POST().
  165. EventFunc(actions.Edit).
  166. Query(presets.ParamOverlay, actions.Dialog).
  167. Query(presets.ParamID, p.PrimarySlug()).
  168. URL(pm.Info().ListingHref()).Go(),
  169. )
  170. return VContainer(
  171. VRow(
  172. VCol(
  173. vx.Card(overview).HeaderTitle("Overview").
  174. Actions(
  175. h.If(editBtn != nil, editBtn),
  176. ).Class("mb-4 rounded-lg").Outlined(true),
  177. ).Cols(8),
  178. ),
  179. )
  180. }
  181. }