fetdata.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package fetdata
  2. import (
  3. "bytes"
  4. "html"
  5. "html/template"
  6. "os"
  7. "time"
  8. "golang-fave/engine/basket"
  9. "golang-fave/engine/consts"
  10. "golang-fave/engine/utils"
  11. "golang-fave/engine/wrapper"
  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) IsUserLoggedIn() bool {
  86. if this.wrap.User == nil {
  87. this.wrap.LoadSessionUser()
  88. }
  89. return this.wrap.User != nil && this.wrap.User.A_id > 0
  90. }
  91. func (this *FERData) CurrentUser() *User {
  92. if this.wrap.User == nil {
  93. return &User{wrap: this.wrap}
  94. } else {
  95. return &User{wrap: this.wrap, object: this.wrap.User}
  96. }
  97. }
  98. func (this *FERData) Module() string {
  99. if this.is404 {
  100. return "404"
  101. }
  102. var mod string
  103. if this.wrap.CurrModule == "index" {
  104. mod = "index"
  105. } else if this.wrap.CurrModule == "blog" {
  106. if len(this.wrap.UrlArgs) == 3 && this.wrap.UrlArgs[0] == "blog" && this.wrap.UrlArgs[1] == "category" && this.wrap.UrlArgs[2] != "" {
  107. mod = "blog-category"
  108. } else if len(this.wrap.UrlArgs) == 2 && this.wrap.UrlArgs[0] == "blog" && this.wrap.UrlArgs[1] != "" {
  109. mod = "blog-post"
  110. } else {
  111. mod = "blog"
  112. }
  113. } else if this.wrap.CurrModule == "shop" {
  114. if len(this.wrap.UrlArgs) == 3 && this.wrap.UrlArgs[0] == "shop" && this.wrap.UrlArgs[1] == "category" && this.wrap.UrlArgs[2] != "" {
  115. mod = "shop-category"
  116. } else if len(this.wrap.UrlArgs) == 2 && this.wrap.UrlArgs[0] == "shop" && this.wrap.UrlArgs[1] != "" {
  117. mod = "shop-product"
  118. } else {
  119. mod = "shop"
  120. }
  121. }
  122. return mod
  123. }
  124. func (this *FERData) ModuleBlogEnabled() bool {
  125. return (*this.wrap.Config).Modules.Enabled.Blog != 0
  126. }
  127. func (this *FERData) ModuleShopEnabled() bool {
  128. return (*this.wrap.Config).Modules.Enabled.Shop != 0
  129. }
  130. func (this *FERData) DateTimeUnix() int {
  131. return int(time.Now().Unix())
  132. }
  133. func (this *FERData) DateTimeFormat(format string) string {
  134. return time.Unix(int64(time.Now().Unix()), 0).Format(format)
  135. }
  136. func (this *FERData) EscapeString(str string) string {
  137. return html.EscapeString(str)
  138. }
  139. func (this *FERData) cachedBlock(block string) template.HTML {
  140. tmpl, err := template.New(block + ".html").Funcs(utils.TemplateAdditionalFuncs()).ParseFiles(
  141. this.wrap.DTemplate + string(os.PathSeparator) + block + ".html",
  142. )
  143. if err != nil {
  144. return template.HTML(err.Error())
  145. }
  146. var tpl bytes.Buffer
  147. err = tmpl.Execute(&tpl, consts.TmplData{
  148. System: utils.GetTmplSystemData("", ""),
  149. Data: this,
  150. })
  151. if err != nil {
  152. return template.HTML(err.Error())
  153. }
  154. return template.HTML(string(tpl.Bytes()))
  155. }
  156. func (this *FERData) CachedBlock1() template.HTML {
  157. if data, ok := this.wrap.GetBlock1(); ok {
  158. return data
  159. }
  160. data := this.cachedBlock("cached-block-1")
  161. this.wrap.SetBlock1(data)
  162. return data
  163. }
  164. func (this *FERData) CachedBlock2() template.HTML {
  165. if data, ok := this.wrap.GetBlock2(); ok {
  166. return data
  167. }
  168. data := this.cachedBlock("cached-block-2")
  169. this.wrap.SetBlock2(data)
  170. return data
  171. }
  172. func (this *FERData) CachedBlock3() template.HTML {
  173. if data, ok := this.wrap.GetBlock3(); ok {
  174. return data
  175. }
  176. data := this.cachedBlock("cached-block-3")
  177. this.wrap.SetBlock3(data)
  178. return data
  179. }
  180. func (this *FERData) CachedBlock4() template.HTML {
  181. if data, ok := this.wrap.GetBlock4(); ok {
  182. return data
  183. }
  184. data := this.cachedBlock("cached-block-4")
  185. this.wrap.SetBlock4(data)
  186. return data
  187. }
  188. func (this *FERData) CachedBlock5() template.HTML {
  189. if data, ok := this.wrap.GetBlock5(); ok {
  190. return data
  191. }
  192. data := this.cachedBlock("cached-block-5")
  193. this.wrap.SetBlock5(data)
  194. return data
  195. }
  196. func (this *FERData) ShopBasketProductsCount() int {
  197. return this.wrap.ShopBasket.ProductsCount(&basket.SBParam{
  198. R: this.wrap.R,
  199. DB: this.wrap.DB,
  200. Host: this.wrap.CurrHost,
  201. Config: this.wrap.Config,
  202. SessionId: this.wrap.GetSessionId(),
  203. })
  204. }
  205. func (this *FERData) ImagePlaceholderHref() string {
  206. return utils.GetImagePlaceholderSrc()
  207. }
  208. func (this *FERData) ShopOrderRequiredLastName() bool {
  209. return (*this.wrap.Config).Shop.Orders.RequiredFields.LastName != 0
  210. }
  211. func (this *FERData) ShopOrderRequiredFirstName() bool {
  212. return (*this.wrap.Config).Shop.Orders.RequiredFields.FirstName != 0
  213. }
  214. func (this *FERData) ShopOrderRequiredMiddleName() bool {
  215. return (*this.wrap.Config).Shop.Orders.RequiredFields.MiddleName != 0
  216. }
  217. func (this *FERData) ShopOrderRequiredMobilePhone() bool {
  218. return (*this.wrap.Config).Shop.Orders.RequiredFields.MobilePhone != 0
  219. }
  220. func (this *FERData) ShopOrderRequiredEmailAddress() bool {
  221. return (*this.wrap.Config).Shop.Orders.RequiredFields.EmailAddress != 0
  222. }
  223. func (this *FERData) ShopOrderRequiredDelivery() bool {
  224. return (*this.wrap.Config).Shop.Orders.RequiredFields.Delivery != 0
  225. }
  226. func (this *FERData) ShopOrderRequiredComment() bool {
  227. return (*this.wrap.Config).Shop.Orders.RequiredFields.Comment != 0
  228. }