models.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package pagebuilder
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "github.com/qor5/admin/l10n"
  8. "github.com/qor5/admin/publish"
  9. "github.com/qor5/admin/seo"
  10. "gorm.io/gorm"
  11. )
  12. type Page struct {
  13. gorm.Model
  14. Title string
  15. Slug string
  16. CategoryID uint
  17. SEO seo.Setting
  18. publish.Status
  19. publish.Schedule
  20. publish.Version
  21. l10n.Locale
  22. }
  23. func (p *Page) GetID() uint {
  24. return p.ID
  25. }
  26. func (*Page) TableName() string {
  27. return "page_builder_pages"
  28. }
  29. var l10nON bool
  30. func (p *Page) L10nON() {
  31. l10nON = true
  32. return
  33. }
  34. func (p *Page) PrimarySlug() string {
  35. if !l10nON {
  36. return fmt.Sprintf("%v_%v", p.ID, p.Version.Version)
  37. }
  38. return fmt.Sprintf("%v_%v_%v", p.ID, p.Version.Version, p.LocaleCode)
  39. }
  40. func (p *Page) PrimaryColumnValuesBySlug(slug string) map[string]string {
  41. segs := strings.Split(slug, "_")
  42. if !l10nON {
  43. if len(segs) != 2 {
  44. panic("wrong slug")
  45. }
  46. return map[string]string{
  47. "id": segs[0],
  48. "version": segs[1],
  49. }
  50. }
  51. if len(segs) != 3 {
  52. panic("wrong slug")
  53. }
  54. return map[string]string{
  55. "id": segs[0],
  56. "version": segs[1],
  57. "locale_code": segs[2],
  58. }
  59. }
  60. func (p *Page) PermissionRN() []string {
  61. rn := []string{"pages", strconv.Itoa(int(p.ID)), p.Version.Version}
  62. if l10nON {
  63. rn = append(rn, p.LocaleCode)
  64. }
  65. return rn
  66. }
  67. func (p *Page) GetCategory(db *gorm.DB) (category Category, err error) {
  68. err = db.Where("id = ? AND locale_code = ?", p.CategoryID, p.LocaleCode).First(&category).Error
  69. if errors.Is(err, gorm.ErrRecordNotFound) {
  70. err = nil
  71. }
  72. return
  73. }
  74. type Category struct {
  75. gorm.Model
  76. Name string
  77. Path string
  78. Description string
  79. IndentLevel int `gorm:"-"`
  80. l10n.Locale
  81. }
  82. func (c *Category) PrimarySlug() string {
  83. if !l10nON {
  84. return fmt.Sprintf("%v", c.ID)
  85. }
  86. return fmt.Sprintf("%v_%v", c.ID, c.LocaleCode)
  87. }
  88. func (c *Category) PrimaryColumnValuesBySlug(slug string) map[string]string {
  89. segs := strings.Split(slug, "_")
  90. if !l10nON {
  91. if len(segs) != 1 {
  92. panic("wrong slug")
  93. }
  94. return map[string]string{
  95. "id": segs[0],
  96. }
  97. }
  98. if len(segs) != 2 {
  99. panic("wrong slug")
  100. }
  101. return map[string]string{
  102. "id": segs[0],
  103. "locale_code": segs[1],
  104. }
  105. }
  106. func (*Category) TableName() string {
  107. return "page_builder_categories"
  108. }
  109. type Container struct {
  110. gorm.Model
  111. PageID uint
  112. PageVersion string
  113. ModelName string
  114. ModelID uint
  115. DisplayOrder float64
  116. Shared bool
  117. Hidden bool
  118. DisplayName string
  119. l10n.Locale
  120. }
  121. func (c *Container) PrimarySlug() string {
  122. if !l10nON {
  123. return fmt.Sprintf("%v", c.ID)
  124. }
  125. return fmt.Sprintf("%v_%v", c.ID, c.LocaleCode)
  126. }
  127. func (c *Container) PrimaryColumnValuesBySlug(slug string) map[string]string {
  128. segs := strings.Split(slug, "_")
  129. if !l10nON {
  130. if len(segs) != 1 {
  131. panic("wrong slug")
  132. }
  133. return map[string]string{
  134. "id": segs[0],
  135. }
  136. }
  137. if len(segs) != 2 {
  138. panic("wrong slug")
  139. }
  140. return map[string]string{
  141. "id": segs[0],
  142. "locale_code": segs[1],
  143. }
  144. }
  145. func (*Container) TableName() string {
  146. return "page_builder_containers"
  147. }
  148. type DemoContainer struct {
  149. gorm.Model
  150. ModelName string
  151. ModelID uint
  152. l10n.Locale
  153. }
  154. func (c *DemoContainer) PrimarySlug() string {
  155. if !l10nON {
  156. return fmt.Sprintf("%v", c.ID)
  157. }
  158. return fmt.Sprintf("%v_%v", c.ID, c.LocaleCode)
  159. }
  160. func (c *DemoContainer) PrimaryColumnValuesBySlug(slug string) map[string]string {
  161. segs := strings.Split(slug, "_")
  162. if !l10nON {
  163. if len(segs) != 1 {
  164. panic("wrong slug")
  165. }
  166. return map[string]string{
  167. "id": segs[0],
  168. }
  169. }
  170. if len(segs) != 2 {
  171. panic("wrong slug")
  172. }
  173. return map[string]string{
  174. "id": segs[0],
  175. "locale_code": segs[1],
  176. }
  177. }
  178. func (*DemoContainer) TableName() string {
  179. return "page_builder_demo_containers"
  180. }
  181. type Template struct {
  182. gorm.Model
  183. Name string
  184. Description string
  185. l10n.Locale
  186. }
  187. func (t *Template) GetID() uint {
  188. return t.ID
  189. }
  190. func (t *Template) PrimarySlug() string {
  191. if !l10nON {
  192. return fmt.Sprintf("%v", t.ID)
  193. }
  194. return fmt.Sprintf("%v_%v", t.ID, t.LocaleCode)
  195. }
  196. func (t *Template) PrimaryColumnValuesBySlug(slug string) map[string]string {
  197. segs := strings.Split(slug, "_")
  198. if !l10nON {
  199. if len(segs) != 1 {
  200. panic("wrong slug")
  201. }
  202. return map[string]string{
  203. "id": segs[0],
  204. }
  205. }
  206. if len(segs) != 2 {
  207. panic("wrong slug")
  208. }
  209. return map[string]string{
  210. "id": segs[0],
  211. "locale_code": segs[1],
  212. }
  213. }
  214. func (*Template) TableName() string {
  215. return "page_builder_templates"
  216. }
  217. const templateVersion = "tpl"
  218. func (t *Template) Page() *Page {
  219. return &Page{
  220. Model: t.Model,
  221. Title: t.Name,
  222. Slug: "",
  223. Status: publish.Status{
  224. Status: publish.StatusDraft,
  225. OnlineUrl: "",
  226. },
  227. Schedule: publish.Schedule{},
  228. Version: publish.Version{
  229. Version: templateVersion,
  230. },
  231. Locale: t.Locale,
  232. }
  233. }