models.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. LocalizeFromModelID uint
  121. }
  122. func (c *Container) PrimarySlug() string {
  123. if !l10nON {
  124. return fmt.Sprintf("%v", c.ID)
  125. }
  126. return fmt.Sprintf("%v_%v", c.ID, c.LocaleCode)
  127. }
  128. func (c *Container) PrimaryColumnValuesBySlug(slug string) map[string]string {
  129. segs := strings.Split(slug, "_")
  130. if !l10nON {
  131. if len(segs) != 1 {
  132. panic("wrong slug")
  133. }
  134. return map[string]string{
  135. "id": segs[0],
  136. }
  137. }
  138. if len(segs) != 2 {
  139. panic("wrong slug")
  140. }
  141. return map[string]string{
  142. "id": segs[0],
  143. "locale_code": segs[1],
  144. }
  145. }
  146. func (*Container) TableName() string {
  147. return "page_builder_containers"
  148. }
  149. type DemoContainer struct {
  150. gorm.Model
  151. ModelName string
  152. ModelID uint
  153. l10n.Locale
  154. }
  155. func (c *DemoContainer) PrimarySlug() string {
  156. if !l10nON {
  157. return fmt.Sprintf("%v", c.ID)
  158. }
  159. return fmt.Sprintf("%v_%v", c.ID, c.LocaleCode)
  160. }
  161. func (c *DemoContainer) PrimaryColumnValuesBySlug(slug string) map[string]string {
  162. segs := strings.Split(slug, "_")
  163. if !l10nON {
  164. if len(segs) != 1 {
  165. panic("wrong slug")
  166. }
  167. return map[string]string{
  168. "id": segs[0],
  169. }
  170. }
  171. if len(segs) != 2 {
  172. panic("wrong slug")
  173. }
  174. return map[string]string{
  175. "id": segs[0],
  176. "locale_code": segs[1],
  177. }
  178. }
  179. func (*DemoContainer) TableName() string {
  180. return "page_builder_demo_containers"
  181. }
  182. type Template struct {
  183. gorm.Model
  184. Name string
  185. Description string
  186. l10n.Locale
  187. }
  188. func (t *Template) GetID() uint {
  189. return t.ID
  190. }
  191. func (t *Template) PrimarySlug() string {
  192. if !l10nON {
  193. return fmt.Sprintf("%v", t.ID)
  194. }
  195. return fmt.Sprintf("%v_%v", t.ID, t.LocaleCode)
  196. }
  197. func (t *Template) PrimaryColumnValuesBySlug(slug string) map[string]string {
  198. segs := strings.Split(slug, "_")
  199. if !l10nON {
  200. if len(segs) != 1 {
  201. panic("wrong slug")
  202. }
  203. return map[string]string{
  204. "id": segs[0],
  205. }
  206. }
  207. if len(segs) != 2 {
  208. panic("wrong slug")
  209. }
  210. return map[string]string{
  211. "id": segs[0],
  212. "locale_code": segs[1],
  213. }
  214. }
  215. func (*Template) TableName() string {
  216. return "page_builder_templates"
  217. }
  218. const templateVersion = "tpl"
  219. func (t *Template) Page() *Page {
  220. return &Page{
  221. Model: t.Model,
  222. Title: t.Name,
  223. Slug: "",
  224. Status: publish.Status{
  225. Status: publish.StatusDraft,
  226. OnlineUrl: "",
  227. },
  228. Schedule: publish.Schedule{},
  229. Version: publish.Version{
  230. Version: templateVersion,
  231. },
  232. Locale: t.Locale,
  233. }
  234. }