config.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package config
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. type Config struct {
  7. Engine struct {
  8. MainModule int
  9. Maintenance int
  10. }
  11. Blog struct {
  12. Pagination struct {
  13. Index int
  14. Category int
  15. }
  16. }
  17. Shop struct {
  18. Pagination struct {
  19. Index int
  20. Category int
  21. }
  22. Thumbnails struct {
  23. Thumbnail0 [3]int
  24. Thumbnail1 [3]int
  25. Thumbnail2 [3]int
  26. Thumbnail3 [3]int
  27. ThumbnailFull [3]int
  28. }
  29. Price struct {
  30. Format int
  31. Round int
  32. }
  33. Orders struct {
  34. RequiredFields struct {
  35. LastName int
  36. FirstName int
  37. MiddleName int
  38. MobilePhone int
  39. EmailAddress int
  40. Delivery int
  41. Comment int
  42. }
  43. NotifyEmail string
  44. Enabled int
  45. NewOrderEmailThemeCp string
  46. NewOrderEmailThemeUser string
  47. }
  48. CustomFields struct {
  49. Field1 struct {
  50. Enabled int
  51. Caption string
  52. }
  53. Field2 struct {
  54. Enabled int
  55. Caption string
  56. }
  57. }
  58. }
  59. API struct {
  60. XML struct {
  61. Enabled int
  62. Name string
  63. Company string
  64. Url string
  65. }
  66. }
  67. SMTP struct {
  68. Host string
  69. Port int
  70. Login string
  71. Password string
  72. }
  73. Modules struct {
  74. Enabled struct {
  75. Blog int
  76. Shop int
  77. }
  78. }
  79. }
  80. func ConfigNew() *Config {
  81. c := &Config{}
  82. c.configDefault()
  83. return c
  84. }
  85. func (this *Config) configDefault() {
  86. this.Engine.MainModule = 0
  87. this.Engine.Maintenance = 0
  88. this.Blog.Pagination.Index = 5
  89. this.Blog.Pagination.Category = 5
  90. this.Shop.Pagination.Index = 9
  91. this.Shop.Pagination.Category = 9
  92. this.Shop.Thumbnails.Thumbnail0[0] = 100
  93. this.Shop.Thumbnails.Thumbnail0[1] = 100
  94. this.Shop.Thumbnails.Thumbnail0[2] = 0
  95. this.Shop.Thumbnails.Thumbnail1[0] = 200
  96. this.Shop.Thumbnails.Thumbnail1[1] = 200
  97. this.Shop.Thumbnails.Thumbnail1[2] = 2
  98. this.Shop.Thumbnails.Thumbnail2[0] = 250
  99. this.Shop.Thumbnails.Thumbnail2[1] = 250
  100. this.Shop.Thumbnails.Thumbnail2[2] = 2
  101. this.Shop.Thumbnails.Thumbnail3[0] = 450
  102. this.Shop.Thumbnails.Thumbnail3[1] = 450
  103. this.Shop.Thumbnails.Thumbnail3[2] = 2
  104. this.Shop.Thumbnails.ThumbnailFull[0] = 1000
  105. this.Shop.Thumbnails.ThumbnailFull[1] = 800
  106. this.Shop.Thumbnails.ThumbnailFull[2] = 1
  107. this.Shop.Price.Format = 2
  108. this.Shop.Price.Round = 0
  109. this.Shop.Orders.RequiredFields.LastName = 1
  110. this.Shop.Orders.RequiredFields.FirstName = 1
  111. this.Shop.Orders.RequiredFields.MiddleName = 0
  112. this.Shop.Orders.RequiredFields.MobilePhone = 0
  113. this.Shop.Orders.RequiredFields.EmailAddress = 1
  114. this.Shop.Orders.RequiredFields.Delivery = 0
  115. this.Shop.Orders.RequiredFields.Comment = 0
  116. this.Shop.Orders.NotifyEmail = ""
  117. this.Shop.Orders.Enabled = 1
  118. this.Shop.Orders.NewOrderEmailThemeCp = "❤️ New order"
  119. this.Shop.Orders.NewOrderEmailThemeUser = "❤️ Thanks for your order"
  120. this.Shop.CustomFields.Field1.Enabled = 0
  121. this.Shop.CustomFields.Field1.Caption = "Custom field 1"
  122. this.Shop.CustomFields.Field2.Enabled = 0
  123. this.Shop.CustomFields.Field2.Caption = "Custom field 2"
  124. this.API.XML.Enabled = 0
  125. this.API.XML.Name = ""
  126. this.API.XML.Company = ""
  127. this.API.XML.Url = ""
  128. this.SMTP.Host = ""
  129. this.SMTP.Port = 587
  130. this.SMTP.Login = ""
  131. this.SMTP.Password = ""
  132. this.Modules.Enabled.Blog = 1
  133. this.Modules.Enabled.Shop = 1
  134. }
  135. func (this *Config) ConfigRead(file string) error {
  136. f, err := os.Open(file)
  137. if err != nil {
  138. return err
  139. }
  140. defer f.Close()
  141. dec := json.NewDecoder(f)
  142. return dec.Decode(this)
  143. }
  144. func (this *Config) ConfigWrite(file string) error {
  145. r, err := json.Marshal(this)
  146. if err != nil {
  147. return err
  148. }
  149. f, err := os.Create(file)
  150. if err != nil {
  151. return err
  152. }
  153. defer f.Close()
  154. _, err = f.WriteString(string(r))
  155. return err
  156. }