fetdata.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package fetdata
  2. import (
  3. "bytes"
  4. "html"
  5. "html/template"
  6. "os"
  7. "time"
  8. "golang-fave/consts"
  9. "golang-fave/engine/basket"
  10. "golang-fave/engine/wrapper"
  11. "golang-fave/utils"
  12. )
  13. type FERData struct {
  14. wrap *wrapper.Wrapper
  15. is404 bool
  16. Page *Page
  17. Blog *Blog
  18. Shop *Shop
  19. }
  20. func New(wrap *wrapper.Wrapper, is404 bool, drow interface{}, duser *utils.MySql_user) *FERData {
  21. var d_Page *Page
  22. var d_Blog *Blog
  23. var d_Shop *Shop
  24. var preUser *User
  25. if duser != nil {
  26. preUser = &User{wrap: wrap, object: duser}
  27. }
  28. if wrap.CurrModule == "index" {
  29. if o, ok := drow.(*utils.MySql_page); ok {
  30. d_Page = &Page{wrap: wrap, object: o, user: preUser}
  31. }
  32. } else if wrap.CurrModule == "blog" {
  33. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  34. if o, ok := drow.(*utils.MySql_blog_category); ok {
  35. d_Blog = &Blog{wrap: wrap, category: (&BlogCategory{wrap: wrap, object: o, user: preUser}).load(nil)}
  36. d_Blog.load()
  37. }
  38. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "blog" && wrap.UrlArgs[1] != "" {
  39. if o, ok := drow.(*utils.MySql_blog_post); ok {
  40. d_Blog = &Blog{wrap: wrap, post: (&BlogPost{wrap: wrap, object: o, user: preUser}).load()}
  41. }
  42. } else {
  43. d_Blog = &Blog{wrap: wrap}
  44. d_Blog.load()
  45. }
  46. } else if wrap.CurrModule == "shop" {
  47. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  48. if o, ok := drow.(*utils.MySql_shop_category); ok {
  49. d_Shop = &Shop{wrap: wrap, category: (&ShopCategory{wrap: wrap, object: o, user: preUser}).load(nil)}
  50. d_Shop.load()
  51. }
  52. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {
  53. if o, ok := drow.(*utils.MySql_shop_product); ok {
  54. d_Shop = &Shop{wrap: wrap, product: (&ShopProduct{wrap: wrap, object: o, user: preUser}).load()}
  55. }
  56. } else {
  57. d_Shop = &Shop{wrap: wrap}
  58. d_Shop.load()
  59. }
  60. }
  61. if d_Blog == nil {
  62. d_Blog = &Blog{wrap: wrap}
  63. }
  64. if d_Shop == nil {
  65. d_Shop = &Shop{wrap: wrap}
  66. }
  67. fer := &FERData{
  68. wrap: wrap,
  69. is404: is404,
  70. Page: d_Page,
  71. Blog: d_Blog,
  72. Shop: d_Shop,
  73. }
  74. return fer
  75. }
  76. func (this *FERData) RequestURI() string {
  77. return this.wrap.R.RequestURI
  78. }
  79. func (this *FERData) RequestURL() string {
  80. return this.wrap.R.URL.Path
  81. }
  82. func (this *FERData) RequestGET() string {
  83. return utils.ExtractGetParams(this.wrap.R.RequestURI)
  84. }
  85. func (this *FERData) Module() string {
  86. if this.is404 {
  87. return "404"
  88. }
  89. var mod string
  90. if this.wrap.CurrModule == "index" {
  91. mod = "index"
  92. } else if this.wrap.CurrModule == "blog" {
  93. if len(this.wrap.UrlArgs) == 3 && this.wrap.UrlArgs[0] == "blog" && this.wrap.UrlArgs[1] == "category" && this.wrap.UrlArgs[2] != "" {
  94. mod = "blog-category"
  95. } else if len(this.wrap.UrlArgs) == 2 && this.wrap.UrlArgs[0] == "blog" && this.wrap.UrlArgs[1] != "" {
  96. mod = "blog-post"
  97. } else {
  98. mod = "blog"
  99. }
  100. } else if this.wrap.CurrModule == "shop" {
  101. if len(this.wrap.UrlArgs) == 3 && this.wrap.UrlArgs[0] == "shop" && this.wrap.UrlArgs[1] == "category" && this.wrap.UrlArgs[2] != "" {
  102. mod = "shop-category"
  103. } else if len(this.wrap.UrlArgs) == 2 && this.wrap.UrlArgs[0] == "shop" && this.wrap.UrlArgs[1] != "" {
  104. mod = "shop-product"
  105. } else {
  106. mod = "shop"
  107. }
  108. }
  109. return mod
  110. }
  111. func (this *FERData) DateTimeUnix() int {
  112. return int(time.Now().Unix())
  113. }
  114. func (this *FERData) DateTimeFormat(format string) string {
  115. return time.Unix(int64(time.Now().Unix()), 0).Format(format)
  116. }
  117. func (this *FERData) EscapeString(str string) string {
  118. return html.EscapeString(str)
  119. }
  120. func (this *FERData) cachedBlock(block string) template.HTML {
  121. tmpl, err := template.New(block + ".html").Funcs(utils.TemplateAdditionalFuncs()).ParseFiles(
  122. this.wrap.DTemplate + string(os.PathSeparator) + block + ".html",
  123. )
  124. if err != nil {
  125. return template.HTML(err.Error())
  126. }
  127. var tpl bytes.Buffer
  128. err = tmpl.Execute(&tpl, consts.TmplData{
  129. System: utils.GetTmplSystemData("", ""),
  130. Data: this,
  131. })
  132. if err != nil {
  133. return template.HTML(err.Error())
  134. }
  135. return template.HTML(string(tpl.Bytes()))
  136. }
  137. func (this *FERData) CachedBlock1() template.HTML {
  138. if data, ok := this.wrap.GetBlock1(); ok {
  139. return data
  140. }
  141. data := this.cachedBlock("cached-block-1")
  142. this.wrap.SetBlock1(data)
  143. return data
  144. }
  145. func (this *FERData) CachedBlock2() template.HTML {
  146. if data, ok := this.wrap.GetBlock2(); ok {
  147. return data
  148. }
  149. data := this.cachedBlock("cached-block-2")
  150. this.wrap.SetBlock2(data)
  151. return data
  152. }
  153. func (this *FERData) CachedBlock3() template.HTML {
  154. if data, ok := this.wrap.GetBlock3(); ok {
  155. return data
  156. }
  157. data := this.cachedBlock("cached-block-3")
  158. this.wrap.SetBlock3(data)
  159. return data
  160. }
  161. func (this *FERData) CachedBlock4() template.HTML {
  162. if data, ok := this.wrap.GetBlock4(); ok {
  163. return data
  164. }
  165. data := this.cachedBlock("cached-block-4")
  166. this.wrap.SetBlock4(data)
  167. return data
  168. }
  169. func (this *FERData) CachedBlock5() template.HTML {
  170. if data, ok := this.wrap.GetBlock5(); ok {
  171. return data
  172. }
  173. data := this.cachedBlock("cached-block-5")
  174. this.wrap.SetBlock5(data)
  175. return data
  176. }
  177. func (this *FERData) ShopBasketProductsCount() int {
  178. return this.wrap.ShopBasket.ProductsCount(&basket.SBParam{
  179. R: this.wrap.R,
  180. DB: this.wrap.DB,
  181. Host: this.wrap.CurrHost,
  182. Config: this.wrap.Config,
  183. SessionId: this.wrap.GetSessionId(),
  184. })
  185. }
  186. func (this *FERData) ImagePlaceholderHref() string {
  187. return utils.GetImagePlaceholderSrc()
  188. }
  189. func (this *FERData) ShopOrderRequiredLastName() bool {
  190. return (*this.wrap.Config).Shop.Orders.RequiredFields.LastName != 0
  191. }
  192. func (this *FERData) ShopOrderRequiredFirstName() bool {
  193. return (*this.wrap.Config).Shop.Orders.RequiredFields.FirstName != 0
  194. }
  195. func (this *FERData) ShopOrderRequiredMiddleName() bool {
  196. return (*this.wrap.Config).Shop.Orders.RequiredFields.MiddleName != 0
  197. }
  198. func (this *FERData) ShopOrderRequiredMobilePhone() bool {
  199. return (*this.wrap.Config).Shop.Orders.RequiredFields.MobilePhone != 0
  200. }
  201. func (this *FERData) ShopOrderRequiredEmailAddress() bool {
  202. return (*this.wrap.Config).Shop.Orders.RequiredFields.EmailAddress != 0
  203. }
  204. func (this *FERData) ShopOrderRequiredDelivery() bool {
  205. return (*this.wrap.Config).Shop.Orders.RequiredFields.Delivery != 0
  206. }
  207. func (this *FERData) ShopOrderRequiredComment() bool {
  208. return (*this.wrap.Config).Shop.Orders.RequiredFields.Comment != 0
  209. }