schedule.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package views
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/qor5/admin/presets"
  7. "github.com/qor5/admin/publish"
  8. . "github.com/qor5/ui/vuetify"
  9. vx "github.com/qor5/ui/vuetifyx"
  10. "github.com/qor5/web"
  11. "github.com/qor5/x/i18n"
  12. "github.com/sunfmin/reflectutils"
  13. h "github.com/theplant/htmlgo"
  14. )
  15. func ScheduleEditFunc() presets.FieldComponentFunc {
  16. return func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  17. s, ok := obj.(publish.ScheduleInterface)
  18. if !ok {
  19. return nil
  20. }
  21. msgr := i18n.MustGetModuleMessages(ctx.R, I18nPublishKey, Messages_en_US).(*Messages)
  22. var start, end string
  23. if s.GetScheduledStartAt() != nil {
  24. start = s.GetScheduledStartAt().Format("2006-01-02 15:04")
  25. }
  26. if s.GetScheduledEndAt() != nil {
  27. end = s.GetScheduledEndAt().Format("2006-01-02 15:04")
  28. }
  29. publishedAt, unpublishedAt := "", ""
  30. if s.GetPublishedAt() != nil {
  31. publishedAt = s.GetPublishedAt().Format("2006-01-02 15:04")
  32. }
  33. if s.GetUnPublishedAt() != nil {
  34. unpublishedAt = s.GetUnPublishedAt().Format("2006-01-02 15:04")
  35. }
  36. return h.Div(
  37. VCard(
  38. h.If(s.GetStatus() != "",
  39. h.Div(
  40. VRow(
  41. VCol(
  42. h.Text(msgr.ActualPublishTime),
  43. ).Cols(4),
  44. VCol(
  45. VRow(
  46. VCol(
  47. h.If(publishedAt == "", h.Text(fmt.Sprintf("%v: %v ", msgr.PublishedAt, msgr.NotSet))).Else(h.Text(fmt.Sprintf("%v: %v ", msgr.PublishedAt, publishedAt))),
  48. ).Cols(6),
  49. VCol(
  50. h.If(unpublishedAt == "", h.Text(fmt.Sprintf("%v: %v ", msgr.UnPublishedAt, msgr.NotSet))).Else(h.Text(fmt.Sprintf("%v: %v ", msgr.UnPublishedAt, unpublishedAt))),
  51. ).Cols(6),
  52. ).NoGutters(true).Attr(`style="width: 100%"`),
  53. ).Cols(8).Class("text--secondary"),
  54. ).NoGutters(true),
  55. h.Div(
  56. VTooltip(
  57. web.Slot(
  58. VIcon("publish").Attr("v-bind", "attrs", "v-on", "on"),
  59. ).Name("activator").Scope("{ on, attrs }"),
  60. h.Span(strings.ReplaceAll(msgr.PublishScheduleTip, "{SchedulePublishTime}", msgr.SchedulePublishTime)),
  61. ).Bottom(true).MaxWidth(285),
  62. ).Class("v-expansion-panel-header__icon"),
  63. ).Class("v-expansion-panel-header"),
  64. ),
  65. VExpansionPanels(
  66. VExpansionPanel(
  67. VExpansionPanelHeader(
  68. VRow(
  69. VCol(
  70. h.Text(msgr.SchedulePublishTime),
  71. ).Cols(4),
  72. VCol(
  73. VFadeTransition(
  74. h.Span(msgr.WhenDoYouWantToPublish).Attr("v-if", "open"),
  75. VRow(
  76. VCol(
  77. h.If(start == "", h.Text(fmt.Sprintf("%v: %v ", msgr.ScheduledStartAt, msgr.NotSet))).Else(h.Text(fmt.Sprintf("%v: %v ", msgr.ScheduledStartAt, start))),
  78. ).Cols(6),
  79. VCol(
  80. h.If(end == "", h.Text(fmt.Sprintf("%v: %v ", msgr.ScheduledEndAt, msgr.NotSet))).Else(h.Text(fmt.Sprintf("%v: %v ", msgr.ScheduledEndAt, end))),
  81. ).Cols(6),
  82. ).NoGutters(true).Attr("v-else").Attr(`style="width: 100%"`),
  83. ).LeaveAbsolute(true),
  84. ).Cols(8).Class("text--secondary"),
  85. ).NoGutters(true),
  86. ).Attr("v-slot", "{ open }"),
  87. VExpansionPanelContent(
  88. VRow(
  89. VCol(
  90. vx.VXDateTimePicker().FieldName("ScheduledStartAt").Label(msgr.ScheduledStartAt).Value(start).
  91. TimePickerProps(vx.TimePickerProps{Format: "24hr", Scrollable: true}).
  92. ClearText(msgr.DateTimePickerClearText).OkText(msgr.DateTimePickerOkText),
  93. //h.RawHTML(fmt.Sprintf(`<vx-datetimepicker label="ScheduledStartAt" value="%s" v-field-name='"ScheduledStartAt"'> </vx-datetimepicker>`, start)),
  94. ).Cols(6),
  95. VCol(
  96. vx.VXDateTimePicker().FieldName("ScheduledEndAt").Label(msgr.ScheduledEndAt).Value(end).
  97. TimePickerProps(vx.TimePickerProps{Format: "24hr", Scrollable: true}).
  98. ClearText(msgr.DateTimePickerClearText).OkText(msgr.DateTimePickerOkText),
  99. //h.RawHTML(fmt.Sprintf(`<vx-datetimepicker label="ScheduledEndAt" value="%s" v-field-name='"ScheduledEndAt"'> </vx-datetimepicker>`, end)),
  100. ).Cols(6),
  101. ),
  102. ),
  103. ),
  104. ).Flat(true).Hover(true),
  105. ),
  106. h.Br(),
  107. )
  108. }
  109. }
  110. func ScheduleEditSetterFunc(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
  111. _, exist := ctx.R.Form["ScheduledStartAt"]
  112. if exist {
  113. s := ctx.R.FormValue("ScheduledStartAt")
  114. if err = setTime(obj, "ScheduledStartAt", s); err != nil {
  115. return
  116. }
  117. }
  118. _, exist = ctx.R.Form["ScheduledEndAt"]
  119. if exist {
  120. e := ctx.R.FormValue("ScheduledEndAt")
  121. if err = setTime(obj, "ScheduledEndAt", e); err != nil {
  122. return
  123. }
  124. }
  125. return
  126. }
  127. var timeFormat = "2006-01-02 15:04:05"
  128. func setTime(obj interface{}, fieldName string, val string) (err error) {
  129. if val == "" {
  130. err = reflectutils.Set(obj, fieldName, nil)
  131. } else {
  132. startAt, err1 := time.ParseInLocation(timeFormat, fmt.Sprintf("%v:00", val), time.Local)
  133. if err1 == nil && !startAt.IsZero() {
  134. err = reflectutils.Set(obj, fieldName, startAt)
  135. }
  136. }
  137. return
  138. }