config.go 3.4 KB

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