model.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package seo
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "time"
  6. "github.com/qor5/admin/l10n"
  7. "github.com/qor5/admin/media/media_library"
  8. h "github.com/theplant/htmlgo"
  9. )
  10. // QorSEOSettingInterface support customize Seo model
  11. // @snippet_begin(QorSEOSettingInterface)
  12. type QorSEOSettingInterface interface {
  13. GetName() string
  14. SetName(string)
  15. GetSEOSetting() Setting
  16. SetSEOSetting(Setting)
  17. GetVariables() Variables
  18. SetVariables(Variables)
  19. GetLocale() string
  20. SetLocale(string)
  21. GetTitle() string
  22. GetDescription() string
  23. GetKeywords() string
  24. GetOpenGraphURL() string
  25. GetOpenGraphType() string
  26. GetOpenGraphImageURL() string
  27. GetOpenGraphImageFromMediaLibrary() media_library.MediaBox
  28. GetOpenGraphMetadata() []OpenGraphMetadata
  29. }
  30. // @snippet_end
  31. // QorSEOSetting default seo model
  32. type QorSEOSetting struct {
  33. Name string `gorm:"primary_key"`
  34. Setting Setting
  35. Variables Variables `sql:"type:text"`
  36. CreatedAt time.Time
  37. UpdatedAt time.Time
  38. DeletedAt *time.Time `gorm:"index"`
  39. l10n.Locale
  40. }
  41. // Setting defined meta's attributes
  42. type Setting struct {
  43. Title string `gorm:"size:4294967295"`
  44. Description string
  45. Keywords string
  46. OpenGraphURL string
  47. OpenGraphType string
  48. OpenGraphImageURL string
  49. OpenGraphImageFromMediaLibrary media_library.MediaBox
  50. OpenGraphMetadata []OpenGraphMetadata
  51. EnabledCustomize bool
  52. }
  53. // OpenGraphMetadata open graph meta data
  54. type OpenGraphMetadata struct {
  55. Property string
  56. Content string
  57. }
  58. func (s *QorSEOSetting) SetSEOSetting(setting Setting) {
  59. s.Setting = setting
  60. }
  61. // GetSEOSetting get seo setting
  62. func (s QorSEOSetting) GetSEOSetting() Setting {
  63. return s.Setting
  64. }
  65. // SetVariablesSetting set variables setting
  66. func (s *QorSEOSetting) SetVariables(setting Variables) {
  67. s.Variables = setting
  68. }
  69. // GetVariablesSetting get variables setting
  70. func (s QorSEOSetting) GetVariables() Variables {
  71. return s.Variables
  72. }
  73. // GetName get QorSeoSetting's name
  74. func (s QorSEOSetting) GetLocale() string {
  75. return s.Name
  76. }
  77. // SetName set QorSeoSetting's name
  78. func (s *QorSEOSetting) SetLocale(locale string) {
  79. s.LocaleCode = locale
  80. }
  81. // GetName get QorSeoSetting's name
  82. func (s QorSEOSetting) GetName() string {
  83. return s.Name
  84. }
  85. // SetName set QorSeoSetting's name
  86. func (s *QorSEOSetting) SetName(name string) {
  87. s.Name = name
  88. }
  89. func (s QorSEOSetting) GetOpenGraphURL() string {
  90. return s.Setting.OpenGraphURL
  91. }
  92. func (s QorSEOSetting) GetOpenGraphType() string {
  93. return s.Setting.OpenGraphType
  94. }
  95. func (s QorSEOSetting) GetOpenGraphImageURL() string {
  96. return s.Setting.OpenGraphImageURL
  97. }
  98. func (s QorSEOSetting) GetOpenGraphImageFromMediaLibrary() media_library.MediaBox {
  99. return s.Setting.OpenGraphImageFromMediaLibrary
  100. }
  101. func (s QorSEOSetting) GetOpenGraphMetadata() []OpenGraphMetadata {
  102. return s.Setting.OpenGraphMetadata
  103. }
  104. // GetTitle get Setting's title
  105. func (s QorSEOSetting) GetTitle() string {
  106. return s.Setting.Title
  107. }
  108. // GetDescription get Setting's description
  109. func (s QorSEOSetting) GetDescription() string {
  110. return s.Setting.Description
  111. }
  112. // GetKeywords get Setting's keywords
  113. func (s QorSEOSetting) GetKeywords() string {
  114. return s.Setting.Keywords
  115. }
  116. // Scan scan value from database into struct
  117. func (setting *Setting) Scan(value interface{}) error {
  118. if bytes, ok := value.([]byte); ok {
  119. json.Unmarshal(bytes, setting)
  120. } else if str, ok := value.(string); ok {
  121. json.Unmarshal([]byte(str), setting)
  122. } else if strs, ok := value.([]string); ok {
  123. for _, str := range strs {
  124. json.Unmarshal([]byte(str), setting)
  125. }
  126. }
  127. return nil
  128. }
  129. // Value get value from struct, and save into database
  130. func (setting Setting) Value() (driver.Value, error) {
  131. result, err := json.Marshal(setting)
  132. return string(result), err
  133. }
  134. func (setting Setting) IsEmpty() bool {
  135. return setting.Title == "" && setting.Description == "" && setting.Keywords == "" && setting.OpenGraphURL == "" && setting.OpenGraphType == "" && setting.OpenGraphImageURL == "" && setting.OpenGraphImageFromMediaLibrary.Url == "" && len(setting.OpenGraphMetadata) == 0
  136. }
  137. type Variables map[string]string
  138. // Scan scan value from database into struct
  139. func (setting *Variables) Scan(value interface{}) error {
  140. if bytes, ok := value.([]byte); ok {
  141. json.Unmarshal(bytes, setting)
  142. } else if str, ok := value.(string); ok {
  143. json.Unmarshal([]byte(str), setting)
  144. } else if strs, ok := value.([]string); ok {
  145. for _, str := range strs {
  146. json.Unmarshal([]byte(str), setting)
  147. }
  148. }
  149. return nil
  150. }
  151. // Value get value from struct, and save into database
  152. func (setting Variables) Value() (driver.Value, error) {
  153. result, err := json.Marshal(setting)
  154. return string(result), err
  155. }
  156. func (setting Setting) HTMLComponent(tags map[string]string) h.HTMLComponent {
  157. openGraphData := map[string]string{
  158. "og:title": setting.Title,
  159. "og:description": setting.Description,
  160. "og:url": setting.OpenGraphURL,
  161. "og:type": setting.OpenGraphType,
  162. "og:image": setting.OpenGraphImageURL,
  163. }
  164. for _, metavalue := range setting.OpenGraphMetadata {
  165. openGraphData[metavalue.Property] = metavalue.Content
  166. }
  167. for _, key := range []string{"og:url", "og:type", "og:image", "og:title", "og:description"} {
  168. if v := openGraphData[key]; v == "" {
  169. if v, ok := tags[key]; ok {
  170. openGraphData[key] = v
  171. }
  172. }
  173. }
  174. if openGraphData["og:type"] == "" {
  175. openGraphData["og:type"] = "website"
  176. }
  177. for key, value := range tags {
  178. if _, ok := openGraphData[key]; !ok {
  179. openGraphData[key] = value
  180. }
  181. }
  182. var openGraphDataComponents h.HTMLComponents
  183. for key, value := range openGraphData {
  184. openGraphDataComponents = append(openGraphDataComponents, h.Meta().Attr("property", key).Attr("name", key).Attr("content", value))
  185. }
  186. return h.HTMLComponents{
  187. h.Title(setting.Title),
  188. h.Meta().Attr("name", "description").Attr("content", setting.Description),
  189. h.Meta().Attr("name", "keywords").Attr("content", setting.Keywords),
  190. openGraphDataComponents,
  191. }
  192. }