module_shop.go 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. package modules
  2. import (
  3. "html"
  4. "net/http"
  5. "strings"
  6. "golang-fave/assets"
  7. "golang-fave/consts"
  8. "golang-fave/engine/builder"
  9. "golang-fave/engine/fetdata"
  10. "golang-fave/engine/sqlw"
  11. "golang-fave/engine/wrapper"
  12. "golang-fave/utils"
  13. )
  14. func (this *Modules) shop_GetCurrencySelectOptions(wrap *wrapper.Wrapper, id int) string {
  15. result := ``
  16. rows, err := wrap.DB.Query(
  17. `SELECT
  18. id,
  19. code
  20. FROM
  21. shop_currencies
  22. ORDER BY
  23. id ASC
  24. ;`,
  25. )
  26. if err == nil {
  27. defer rows.Close()
  28. values := make([]string, 2)
  29. scan := make([]interface{}, len(values))
  30. for i := range values {
  31. scan[i] = &values[i]
  32. }
  33. idStr := utils.IntToStr(id)
  34. for rows.Next() {
  35. err = rows.Scan(scan...)
  36. if *wrap.LogCpError(&err) == nil {
  37. selected := ""
  38. if string(values[0]) == idStr {
  39. selected = " selected"
  40. }
  41. result += `<option title="` + html.EscapeString(string(values[1])) + `" value="` + html.EscapeString(string(values[0])) + `"` + selected + `>` + html.EscapeString(string(values[1])) + `</option>`
  42. }
  43. }
  44. }
  45. return result
  46. }
  47. func (this *Modules) shop_GetProductValuesInputs(wrap *wrapper.Wrapper, product_id int) string {
  48. result := ``
  49. rows, err := wrap.DB.Query(
  50. `SELECT
  51. shop_filters.id,
  52. shop_filters.name,
  53. shop_filters_values.id,
  54. shop_filters_values.name,
  55. IF(shop_filter_product_values.filter_value_id > 0, 1, 0) as selected
  56. FROM
  57. shop_filters_values
  58. LEFT JOIN shop_filters ON shop_filters.id = shop_filters_values.filter_id
  59. LEFT JOIN shop_filter_product_values ON
  60. shop_filter_product_values.filter_value_id = shop_filters_values.id AND
  61. shop_filter_product_values.product_id = ` + utils.IntToStr(product_id) + `
  62. LEFT JOIN (
  63. SELECT
  64. shop_filters_values.filter_id,
  65. shop_filter_product_values.product_id
  66. FROM
  67. shop_filter_product_values
  68. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  69. WHERE
  70. shop_filter_product_values.product_id = ` + utils.IntToStr(product_id) + `
  71. GROUP BY
  72. shop_filters_values.filter_id
  73. ) as filter_used ON filter_used.filter_id = shop_filters.id
  74. WHERE
  75. filter_used.filter_id IS NOT NULL
  76. ORDER BY
  77. shop_filters.name ASC,
  78. shop_filters_values.name ASC
  79. ;`,
  80. )
  81. filter_ids := []int{}
  82. filter_names := map[int]string{}
  83. filter_values := map[int][]string{}
  84. if err == nil {
  85. defer rows.Close()
  86. values := make([]string, 5)
  87. scan := make([]interface{}, len(values))
  88. for i := range values {
  89. scan[i] = &values[i]
  90. }
  91. for rows.Next() {
  92. err = rows.Scan(scan...)
  93. if *wrap.LogCpError(&err) == nil {
  94. filter_id := utils.StrToInt(string(values[0]))
  95. if !utils.InArrayInt(filter_ids, filter_id) {
  96. filter_ids = append(filter_ids, filter_id)
  97. }
  98. filter_names[filter_id] = html.EscapeString(string(values[1]))
  99. selected := ``
  100. if utils.StrToInt(string(values[4])) == 1 {
  101. selected = ` selected`
  102. }
  103. filter_values[filter_id] = append(filter_values[filter_id], `<option value="`+html.EscapeString(string(values[2]))+`"`+selected+`>`+html.EscapeString(string(values[3]))+`</option>`)
  104. }
  105. }
  106. }
  107. for _, filter_id := range filter_ids {
  108. result += `<div class="form-group" id="prod_attr_` + utils.IntToStr(filter_id) + `">` +
  109. `<div><b>` + filter_names[filter_id] + `</b></div>` +
  110. `<div class="position-relative">` +
  111. `<select class="selectpicker form-control" name="value.` + utils.IntToStr(filter_id) + `" autocomplete="off" required multiple>` +
  112. strings.Join(filter_values[filter_id], "") +
  113. `</select>` +
  114. `<button type="button" class="btn btn-danger btn-dynamic-remove" onclick="fave.ShopProductsRemove(this);">&times;</button>` +
  115. `</div>` +
  116. `</div>`
  117. }
  118. return result
  119. }
  120. func (this *Modules) shop_GetFilterValuesInputs(wrap *wrapper.Wrapper, filter_id int) string {
  121. result := ``
  122. rows, err := wrap.DB.Query(
  123. `SELECT
  124. id,
  125. name
  126. FROM
  127. shop_filters_values
  128. WHERE
  129. filter_id = ?
  130. ORDER BY
  131. name ASC
  132. ;`,
  133. filter_id,
  134. )
  135. if err == nil {
  136. defer rows.Close()
  137. values := make([]string, 2)
  138. scan := make([]interface{}, len(values))
  139. for i := range values {
  140. scan[i] = &values[i]
  141. }
  142. for rows.Next() {
  143. err = rows.Scan(scan...)
  144. if *wrap.LogCpError(&err) == nil {
  145. result += `<div class="form-group position-relative"><input class="form-control" type="text" name="value.` + html.EscapeString(string(values[0])) + `" value="` + html.EscapeString(string(values[1])) + `" placeholder="" autocomplete="off" required><button type="button" class="btn btn-danger btn-dynamic-remove" onclick="fave.ShopAttributesRemove(this);">&times;</button></div>`
  146. }
  147. }
  148. }
  149. return result
  150. }
  151. func (this *Modules) shop_GetAllAttributesSelectOptions(wrap *wrapper.Wrapper) string {
  152. result := ``
  153. rows, err := wrap.DB.Query(
  154. `SELECT
  155. id,
  156. name,
  157. filter
  158. FROM
  159. shop_filters
  160. ORDER BY
  161. name ASC
  162. ;`,
  163. )
  164. result += `<option title="&mdash;" value="0">&mdash;</option>`
  165. if err == nil {
  166. defer rows.Close()
  167. values := make([]string, 3)
  168. scan := make([]interface{}, len(values))
  169. for i := range values {
  170. scan[i] = &values[i]
  171. }
  172. for rows.Next() {
  173. err = rows.Scan(scan...)
  174. if *wrap.LogCpError(&err) == nil {
  175. result += `<option title="` + html.EscapeString(string(values[1])) + `" value="` + html.EscapeString(string(values[0])) + `">` + html.EscapeString(string(values[1])) + `</option>`
  176. }
  177. }
  178. }
  179. return result
  180. }
  181. func (this *Modules) shop_GetAllCurrencies(wrap *wrapper.Wrapper) map[int]string {
  182. result := map[int]string{}
  183. rows, err := wrap.DB.Query(
  184. `SELECT
  185. id,
  186. code
  187. FROM
  188. shop_currencies
  189. ORDER BY
  190. id ASC
  191. ;`,
  192. )
  193. if err == nil {
  194. defer rows.Close()
  195. values := make([]string, 2)
  196. scan := make([]interface{}, len(values))
  197. for i := range values {
  198. scan[i] = &values[i]
  199. }
  200. for rows.Next() {
  201. err = rows.Scan(scan...)
  202. if *wrap.LogCpError(&err) == nil {
  203. result[utils.StrToInt(string(values[0]))] = html.EscapeString(string(values[1]))
  204. }
  205. }
  206. }
  207. return result
  208. }
  209. func (this *Modules) shop_GetAllProductImages(wrap *wrapper.Wrapper, product_id int) string {
  210. result := ``
  211. rows, err := wrap.DB.Query(
  212. `SELECT
  213. id,
  214. product_id,
  215. filename
  216. FROM
  217. shop_product_images
  218. WHERE
  219. product_id = ?
  220. ORDER BY
  221. ord ASC
  222. ;`,
  223. product_id,
  224. )
  225. if err == nil {
  226. defer rows.Close()
  227. values := make([]string, 3)
  228. scan := make([]interface{}, len(values))
  229. for i := range values {
  230. scan[i] = &values[i]
  231. }
  232. for rows.Next() {
  233. err = rows.Scan(scan...)
  234. if *wrap.LogCpError(&err) == nil {
  235. result += `<div class="attached-img" data-id="` + html.EscapeString(string(values[0])) + `"><a href="/products/images/` + html.EscapeString(string(values[1])) + `/` + html.EscapeString(string(values[2])) + `" title="` + html.EscapeString(string(values[2])) + `" target="_blank"><img id="pimg_` + string(values[1]) + `_` + strings.Replace(string(values[2]), ".", "_", -1) + `" src="/products/images/` + string(values[1]) + `/thumb-0-` + string(values[2]) + `" onerror="WaitForFave(function(){fave.ShopProductsRetryImage(this, 'pimg_` + string(values[1]) + `_` + strings.Replace(string(values[2]), ".", "_", -1) + `');});" /></a><a class="remove" onclick="fave.ShopProductsDeleteImage(this, ` + html.EscapeString(string(values[1])) + `, '` + html.EscapeString(string(values[2])) + `');"><svg viewBox="1 1 11 14" width="10" height="12" class="sicon" version="1.1"><path fill-rule="evenodd" d="M11 2H9c0-.55-.45-1-1-1H5c-.55 0-1 .45-1 1H2c-.55 0-1 .45-1 1v1c0 .55.45 1 1 1v9c0 .55.45 1 1 1h7c.55 0 1-.45 1-1V5c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 12H3V5h1v8h1V5h1v8h1V5h1v8h1V5h1v9zm1-10H2V3h9v1z"></path></svg></a></div>`
  236. }
  237. }
  238. }
  239. return result
  240. }
  241. func (this *Modules) shop_GetSubProducts(wrap *wrapper.Wrapper, id int) string {
  242. result := ``
  243. rows, err := wrap.DB.Query(
  244. `SELECT
  245. id,
  246. name
  247. FROM
  248. shop_products
  249. WHERE
  250. parent_id = ?
  251. ORDER BY
  252. id DESC
  253. ;`,
  254. id,
  255. )
  256. if err == nil {
  257. defer rows.Close()
  258. values := make([]string, 2)
  259. scan := make([]interface{}, len(values))
  260. for i := range values {
  261. scan[i] = &values[i]
  262. }
  263. for rows.Next() {
  264. err = rows.Scan(scan...)
  265. if *wrap.LogCpError(&err) == nil {
  266. result += `<div><a href="/cp/` + wrap.CurrModule + `/modify/` + html.EscapeString(string(values[0])) + `/">` + html.EscapeString(string(values[1])) + ` ` + html.EscapeString(string(values[0])) + `</a> <a class="ico delete" title="Delete" href="javascript:fave.ActionDataTableDelete(this,'shop-detach','` + html.EscapeString(string(values[0])) + `','Are you sure want to detach product?');"><svg viewBox="0 0 16 16" width="16" height="16" class="sicon" version="1.1"><path fill-rule="evenodd" d="M11 2H9c0-.55-.45-1-1-1H5c-.55 0-1 .45-1 1H2c-.55 0-1 .45-1 1v1c0 .55.45 1 1 1v9c0 .55.45 1 1 1h7c.55 0 1-.45 1-1V5c.55 0 1-.45 1-1V3c0-.55-.45-1-1-1zm-1 12H3V5h1v8h1V5h1v8h1V5h1v8h1V5h1v9zm1-10H2V3h9v1z"></path></svg></a></div>`
  267. }
  268. }
  269. }
  270. return result
  271. }
  272. func (this *Modules) shop_GetParentProduct(wrap *wrapper.Wrapper, id int) string {
  273. result := ``
  274. rows, err := wrap.DB.Query(
  275. `SELECT
  276. id,
  277. name
  278. FROM
  279. shop_products
  280. WHERE
  281. id = ?
  282. ;`,
  283. id,
  284. )
  285. if err == nil {
  286. defer rows.Close()
  287. values := make([]string, 2)
  288. scan := make([]interface{}, len(values))
  289. for i := range values {
  290. scan[i] = &values[i]
  291. }
  292. for rows.Next() {
  293. err = rows.Scan(scan...)
  294. if *wrap.LogCpError(&err) == nil {
  295. result += `<div><a href="/cp/` + wrap.CurrModule + `/modify/` + html.EscapeString(string(values[0])) + `/">` + html.EscapeString(string(values[1])) + ` ` + html.EscapeString(string(values[0])) + `</a></div>`
  296. }
  297. }
  298. }
  299. return result
  300. }
  301. func (this *Modules) RegisterModule_Shop() *Module {
  302. return this.newModule(MInfo{
  303. WantDB: true,
  304. Mount: "shop",
  305. Name: "Shop",
  306. Order: 2,
  307. System: false,
  308. Icon: assets.SysSvgIconShop,
  309. Sub: &[]MISub{
  310. {Mount: "default", Name: "List of products", Show: true, Icon: assets.SysSvgIconList},
  311. {Mount: "add", Name: "Add new product", Show: true, Icon: assets.SysSvgIconPlus},
  312. {Mount: "modify", Name: "Modify product", Show: false},
  313. {Sep: true, Show: true},
  314. {Mount: "categories", Name: "List of categories", Show: true, Icon: assets.SysSvgIconList},
  315. {Mount: "categories-add", Name: "Add new category", Show: true, Icon: assets.SysSvgIconPlus},
  316. {Mount: "categories-modify", Name: "Modify category", Show: false},
  317. {Sep: true, Show: true},
  318. {Mount: "attributes", Name: "List of attributes", Show: true, Icon: assets.SysSvgIconList},
  319. {Mount: "attributes-add", Name: "Add new attribute", Show: true, Icon: assets.SysSvgIconPlus},
  320. {Mount: "attributes-modify", Name: "Modify attribute", Show: false},
  321. {Sep: true, Show: true},
  322. {Mount: "currencies", Name: "List of currencies", Show: true, Icon: assets.SysSvgIconList},
  323. {Mount: "currencies-add", Name: "Add new currency", Show: true, Icon: assets.SysSvgIconPlus},
  324. {Mount: "currencies-modify", Name: "Modify currency", Show: false},
  325. },
  326. }, func(wrap *wrapper.Wrapper) {
  327. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  328. // Shop category
  329. row := &utils.MySql_shop_category{}
  330. rou := &utils.MySql_user{}
  331. err := wrap.DB.QueryRow(`
  332. SELECT
  333. main.id,
  334. main.user,
  335. main.name,
  336. main.alias,
  337. main.lft,
  338. main.rgt,
  339. main.depth,
  340. parent.id AS parent_id,
  341. users.id,
  342. users.first_name,
  343. users.last_name,
  344. users.email,
  345. users.admin,
  346. users.active
  347. FROM
  348. (
  349. SELECT
  350. node.id,
  351. node.user,
  352. node.name,
  353. node.alias,
  354. node.lft,
  355. node.rgt,
  356. (COUNT(parent.id) - 1) AS depth
  357. FROM
  358. shop_cats AS node,
  359. shop_cats AS parent
  360. WHERE
  361. node.lft BETWEEN parent.lft AND parent.rgt
  362. GROUP BY
  363. node.id
  364. ORDER BY
  365. node.lft ASC
  366. ) AS main
  367. LEFT JOIN (
  368. SELECT
  369. node.id,
  370. node.user,
  371. node.name,
  372. node.alias,
  373. node.lft,
  374. node.rgt,
  375. (COUNT(parent.id) - 0) AS depth
  376. FROM
  377. shop_cats AS node,
  378. shop_cats AS parent
  379. WHERE
  380. node.lft BETWEEN parent.lft AND parent.rgt
  381. GROUP BY
  382. node.id
  383. ORDER BY
  384. node.lft ASC
  385. ) AS parent ON
  386. parent.depth = main.depth AND
  387. main.lft > parent.lft AND
  388. main.rgt < parent.rgt
  389. LEFT JOIN users ON users.id = main.user
  390. WHERE
  391. main.id > 1 AND
  392. main.alias = ?
  393. ORDER BY
  394. main.lft ASC
  395. ;`,
  396. wrap.UrlArgs[2],
  397. ).Scan(
  398. &row.A_id,
  399. &row.A_user,
  400. &row.A_name,
  401. &row.A_alias,
  402. &row.A_lft,
  403. &row.A_rgt,
  404. &row.A_depth,
  405. &row.A_parent,
  406. &rou.A_id,
  407. &rou.A_first_name,
  408. &rou.A_last_name,
  409. &rou.A_email,
  410. &rou.A_admin,
  411. &rou.A_active,
  412. )
  413. if err != nil && err != wrapper.ErrNoRows {
  414. // System error 500
  415. wrap.LogCpError(&err)
  416. utils.SystemErrorPageEngine(wrap.W, err)
  417. return
  418. } else if err == wrapper.ErrNoRows {
  419. // User error 404 page
  420. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  421. return
  422. }
  423. // Fix url
  424. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  425. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  426. return
  427. }
  428. // Render template
  429. wrap.RenderFrontEnd("shop-category", fetdata.New(wrap, false, row, rou), http.StatusOK)
  430. return
  431. } else if len(wrap.UrlArgs) >= 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "basket" && (wrap.UrlArgs[2] == "info" || wrap.UrlArgs[2] == "add" || wrap.UrlArgs[2] == "update" || wrap.UrlArgs[2] == "remove") {
  432. if wrap.UrlArgs[2] == "info" {
  433. wrap.W.WriteHeader(http.StatusOK)
  434. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  435. wrap.W.Write([]byte(wrap.ShopBasket.Info(wrap.CurrHost, wrap.GetSessionId(), wrap.DB, 1)))
  436. return
  437. } else if wrap.UrlArgs[2] == "plus" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  438. wrap.W.WriteHeader(http.StatusOK)
  439. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  440. wrap.W.Write([]byte(wrap.ShopBasket.Plus(wrap.CurrHost, wrap.GetSessionId(), wrap.DB, utils.StrToInt(wrap.UrlArgs[3]))))
  441. return
  442. } else if wrap.UrlArgs[2] == "minus" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  443. wrap.W.WriteHeader(http.StatusOK)
  444. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  445. wrap.W.Write([]byte(wrap.ShopBasket.Minus(wrap.CurrHost, wrap.GetSessionId(), wrap.DB, utils.StrToInt(wrap.UrlArgs[3]))))
  446. return
  447. } else if wrap.UrlArgs[2] == "remove" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  448. wrap.W.WriteHeader(http.StatusOK)
  449. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  450. wrap.W.Write([]byte(wrap.ShopBasket.Remove(wrap.CurrHost, wrap.GetSessionId(), wrap.DB, utils.StrToInt(wrap.UrlArgs[3]))))
  451. return
  452. }
  453. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {
  454. // Shop product
  455. row := &utils.MySql_shop_product{}
  456. rou := &utils.MySql_user{}
  457. err := wrap.DB.QueryRow(`
  458. SELECT
  459. shop_products.id,
  460. shop_products.parent_id,
  461. shop_products.user,
  462. shop_products.currency,
  463. shop_products.price,
  464. shop_products.name,
  465. shop_products.alias,
  466. shop_products.vendor,
  467. shop_products.quantity,
  468. shop_products.category,
  469. shop_products.briefly,
  470. shop_products.content,
  471. UNIX_TIMESTAMP(shop_products.datetime) as datetime,
  472. shop_products.active,
  473. users.id,
  474. users.first_name,
  475. users.last_name,
  476. users.email,
  477. users.admin,
  478. users.active
  479. FROM
  480. shop_products
  481. LEFT JOIN users ON users.id = shop_products.user
  482. WHERE
  483. shop_products.active = 1 and
  484. shop_products.alias = ?
  485. LIMIT 1;`,
  486. wrap.UrlArgs[1],
  487. ).Scan(
  488. &row.A_id,
  489. &row.A_parent,
  490. &row.A_user,
  491. &row.A_currency,
  492. &row.A_price,
  493. &row.A_name,
  494. &row.A_alias,
  495. &row.A_vendor,
  496. &row.A_quantity,
  497. &row.A_category,
  498. &row.A_briefly,
  499. &row.A_content,
  500. &row.A_datetime,
  501. &row.A_active,
  502. &rou.A_id,
  503. &rou.A_first_name,
  504. &rou.A_last_name,
  505. &rou.A_email,
  506. &rou.A_admin,
  507. &rou.A_active,
  508. )
  509. if err != nil && err != wrapper.ErrNoRows {
  510. // System error 500
  511. wrap.LogCpError(&err)
  512. utils.SystemErrorPageEngine(wrap.W, err)
  513. return
  514. } else if err == wrapper.ErrNoRows {
  515. // User error 404 page
  516. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  517. return
  518. }
  519. // Fix url
  520. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  521. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  522. return
  523. }
  524. // Render template
  525. wrap.RenderFrontEnd("shop-product", fetdata.New(wrap, false, row, rou), http.StatusOK)
  526. return
  527. } else if len(wrap.UrlArgs) == 1 && wrap.UrlArgs[0] == "shop" {
  528. // Shop
  529. // Fix url
  530. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  531. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  532. return
  533. }
  534. // Render template
  535. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  536. return
  537. } else if (*wrap.Config).Engine.MainModule == 2 {
  538. // Render template
  539. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  540. return
  541. }
  542. // User error 404 page
  543. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  544. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  545. content := ""
  546. sidebar := ""
  547. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  548. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  549. {Name: "List of products"},
  550. })
  551. // Load currencies
  552. currencies := this.shop_GetAllCurrencies(wrap)
  553. content += builder.DataTable(
  554. wrap,
  555. "shop_products",
  556. "id",
  557. "DESC",
  558. &[]builder.DataTableRow{
  559. {
  560. DBField: "id",
  561. },
  562. {
  563. DBField: "name",
  564. NameInTable: "Product / URL",
  565. CallBack: func(values *[]string) string {
  566. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + ` ` + html.EscapeString((*values)[0]) + `</a>`
  567. alias := html.EscapeString((*values)[2])
  568. parent := ``
  569. if (*values)[7] != "" {
  570. parent = `<div class="parent">&uarr;<small><a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[7] + `/">` + html.EscapeString((*values)[8]) + ` ` + (*values)[7] + `</a></small></div>`
  571. }
  572. return `<div>` + name + `</div><div><small>/shop/` + alias + `/</small></div>` + parent
  573. },
  574. },
  575. {
  576. DBField: "alias",
  577. },
  578. {
  579. DBField: "currency",
  580. },
  581. {
  582. DBField: "price",
  583. NameInTable: "Price",
  584. Classes: "d-none d-md-table-cell",
  585. CallBack: func(values *[]string) string {
  586. return `<div>` + utils.Float64ToStr(utils.StrToFloat64((*values)[4])) + `</div>` +
  587. `<div><small>` + currencies[utils.StrToInt((*values)[3])] + `</small></div>`
  588. },
  589. },
  590. {
  591. DBField: "datetime",
  592. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  593. NameInTable: "Date / Time",
  594. Classes: "d-none d-lg-table-cell",
  595. CallBack: func(values *[]string) string {
  596. t := int64(utils.StrToInt((*values)[5]))
  597. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  598. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  599. },
  600. },
  601. {
  602. DBField: "active",
  603. NameInTable: "Active",
  604. Classes: "d-none d-sm-table-cell",
  605. CallBack: func(values *[]string) string {
  606. return builder.CheckBox(utils.StrToInt((*values)[6]))
  607. },
  608. },
  609. {
  610. DBField: "parent_id",
  611. },
  612. {
  613. DBField: "pname",
  614. DBExp: "spp.name",
  615. },
  616. },
  617. func(values *[]string) string {
  618. return builder.DataTableAction(&[]builder.DataTableActionRow{
  619. {
  620. Icon: assets.SysSvgIconView,
  621. Href: `/shop/` + (*values)[2] + `/`,
  622. Hint: "View",
  623. Target: "_blank",
  624. },
  625. {
  626. Icon: assets.SysSvgIconEdit,
  627. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  628. Hint: "Edit",
  629. },
  630. {
  631. Icon: assets.SysSvgIconRemove,
  632. Href: "javascript:fave.ActionDataTableDelete(this,'shop-delete','" +
  633. (*values)[0] + "','Are you sure want to delete product?');",
  634. Hint: "Delete",
  635. Classes: "delete",
  636. },
  637. })
  638. },
  639. "/cp/"+wrap.CurrModule+"/",
  640. func() (int, error) {
  641. var count int
  642. return count, wrap.DB.QueryRow(
  643. "SELECT COUNT(*) FROM `shop_products`;",
  644. ).Scan(&count)
  645. },
  646. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  647. return wrap.DB.Query(
  648. `SELECT
  649. shop_products.id,
  650. shop_products.name,
  651. shop_products.alias,
  652. shop_products.currency,
  653. shop_products.price,
  654. UNIX_TIMESTAMP(`+"`shop_products`.`datetime`"+`) AS datetime,
  655. shop_products.active,
  656. shop_products.parent_id,
  657. spp.name AS pname
  658. FROM
  659. shop_products
  660. LEFT JOIN shop_products AS spp ON spp.id = shop_products.parent_id
  661. ORDER BY
  662. shop_products.id DESC
  663. LIMIT ?, ?;`,
  664. limit_offset,
  665. pear_page,
  666. )
  667. },
  668. true,
  669. )
  670. } else if wrap.CurrSubModule == "categories" {
  671. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  672. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  673. {Name: "List of categories"},
  674. })
  675. content += builder.DataTable(
  676. wrap,
  677. "shop_cats",
  678. "id",
  679. "ASC",
  680. &[]builder.DataTableRow{
  681. {
  682. DBField: "id",
  683. },
  684. {
  685. DBField: "user",
  686. },
  687. {
  688. DBField: "name",
  689. NameInTable: "Category",
  690. CallBack: func(values *[]string) string {
  691. depth := utils.StrToInt((*values)[4]) - 1
  692. if depth < 0 {
  693. depth = 0
  694. }
  695. sub := strings.Repeat("&mdash; ", depth)
  696. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  697. return `<div>` + name + `</div>`
  698. },
  699. },
  700. {
  701. DBField: "alias",
  702. },
  703. {
  704. DBField: "depth",
  705. },
  706. },
  707. func(values *[]string) string {
  708. return builder.DataTableAction(&[]builder.DataTableActionRow{
  709. {
  710. Icon: assets.SysSvgIconView,
  711. Href: `/shop/category/` + (*values)[3] + `/`,
  712. Hint: "View",
  713. Target: "_blank",
  714. },
  715. {
  716. Icon: assets.SysSvgIconEdit,
  717. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  718. Hint: "Edit",
  719. },
  720. {
  721. Icon: assets.SysSvgIconRemove,
  722. Href: "javascript:fave.ActionDataTableDelete(this,'shop-categories-delete','" +
  723. (*values)[0] + "','Are you sure want to delete category?');",
  724. Hint: "Delete",
  725. Classes: "delete",
  726. },
  727. })
  728. },
  729. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  730. nil,
  731. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  732. return wrap.DB.Query(
  733. `SELECT
  734. node.id,
  735. node.user,
  736. node.name,
  737. node.alias,
  738. (COUNT(parent.id) - 1) AS depth
  739. FROM
  740. shop_cats AS node,
  741. shop_cats AS parent
  742. WHERE
  743. node.lft BETWEEN parent.lft AND parent.rgt AND
  744. node.id > 1
  745. GROUP BY
  746. node.id
  747. ORDER BY
  748. node.lft ASC
  749. ;`,
  750. )
  751. },
  752. false,
  753. )
  754. } else if wrap.CurrSubModule == "attributes" {
  755. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  756. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  757. {Name: "List of attributes"},
  758. })
  759. content += builder.DataTable(
  760. wrap,
  761. "shop_filters",
  762. "id",
  763. "DESC",
  764. &[]builder.DataTableRow{
  765. {
  766. DBField: "id",
  767. },
  768. {
  769. DBField: "name",
  770. NameInTable: "Name",
  771. CallBack: func(values *[]string) string {
  772. name := `<a href="/cp/` + wrap.CurrModule + `/attributes-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  773. return `<div>` + name + `</div><div><small>` + html.EscapeString((*values)[2]) + `</small></div>`
  774. },
  775. },
  776. {
  777. DBField: "filter",
  778. },
  779. },
  780. func(values *[]string) string {
  781. return builder.DataTableAction(&[]builder.DataTableActionRow{
  782. {
  783. Icon: assets.SysSvgIconEdit,
  784. Href: "/cp/" + wrap.CurrModule + "/attributes-modify/" + (*values)[0] + "/",
  785. Hint: "Edit",
  786. },
  787. {
  788. Icon: assets.SysSvgIconRemove,
  789. Href: "javascript:fave.ActionDataTableDelete(this,'shop-attributes-delete','" +
  790. (*values)[0] + "','Are you sure want to delete attribute?');",
  791. Hint: "Delete",
  792. Classes: "delete",
  793. },
  794. })
  795. },
  796. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  797. nil,
  798. nil,
  799. true,
  800. )
  801. } else if wrap.CurrSubModule == "currencies" {
  802. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  803. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  804. {Name: "List of currencies"},
  805. })
  806. content += builder.DataTable(
  807. wrap,
  808. "shop_currencies",
  809. "id",
  810. "DESC",
  811. &[]builder.DataTableRow{
  812. {
  813. DBField: "id",
  814. },
  815. {
  816. DBField: "name",
  817. NameInTable: "Name",
  818. CallBack: func(values *[]string) string {
  819. name := `<a href="/cp/` + wrap.CurrModule + `/currencies-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + ` (` + (*values)[3] + `, ` + (*values)[4] + `)</a>`
  820. return `<div>` + name + `</div>`
  821. },
  822. },
  823. {
  824. DBField: "coefficient",
  825. NameInTable: "Coefficient",
  826. Classes: "d-none d-md-table-cell",
  827. CallBack: func(values *[]string) string {
  828. return utils.Float64ToStrF(utils.StrToFloat64((*values)[2]), "%.4f")
  829. },
  830. },
  831. {
  832. DBField: "code",
  833. },
  834. {
  835. DBField: "symbol",
  836. },
  837. },
  838. func(values *[]string) string {
  839. return builder.DataTableAction(&[]builder.DataTableActionRow{
  840. {
  841. Icon: assets.SysSvgIconEdit,
  842. Href: "/cp/" + wrap.CurrModule + "/currencies-modify/" + (*values)[0] + "/",
  843. Hint: "Edit",
  844. },
  845. {
  846. Icon: assets.SysSvgIconRemove,
  847. Href: "javascript:fave.ActionDataTableDelete(this,'shop-currencies-delete','" +
  848. (*values)[0] + "','Are you sure want to delete currency?');",
  849. Hint: "Delete",
  850. Classes: "delete",
  851. },
  852. })
  853. },
  854. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  855. nil,
  856. nil,
  857. true,
  858. )
  859. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  860. if wrap.CurrSubModule == "add" {
  861. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  862. {Name: "Add new product"},
  863. })
  864. } else {
  865. if len(wrap.UrlArgs) >= 3 && utils.IsNumeric(wrap.UrlArgs[2]) {
  866. content += `<div class="product-copy"><a title="Duplicate product" href="javascript:fave.ShopProductsDuplicate(this, ` + wrap.UrlArgs[2] + `);">` + assets.SysSvgIconCopy + `</a></div>`
  867. content += `<div class="product-another"><a title="Duplicate product and attach" href="javascript:fave.ShopProductsDuplicateWithAttach(this, ` + wrap.UrlArgs[2] + `);">` + assets.SysSvgIconPlus + `</a></div>`
  868. }
  869. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  870. {Name: "Modify product"},
  871. })
  872. }
  873. data := utils.MySql_shop_product{
  874. A_id: 0,
  875. A_user: 0,
  876. A_currency: 0,
  877. A_price: 0,
  878. A_gname: "",
  879. A_name: "",
  880. A_alias: "",
  881. A_vendor: "",
  882. A_quantity: 0,
  883. A_category: 0,
  884. A_briefly: "",
  885. A_content: "",
  886. A_datetime: 0,
  887. A_active: 0,
  888. }
  889. if wrap.CurrSubModule == "modify" {
  890. if len(wrap.UrlArgs) != 3 {
  891. return "", "", ""
  892. }
  893. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  894. return "", "", ""
  895. }
  896. err := wrap.DB.QueryRow(`
  897. SELECT
  898. id,
  899. parent_id,
  900. user,
  901. currency,
  902. price,
  903. gname,
  904. name,
  905. alias,
  906. vendor,
  907. quantity,
  908. category,
  909. briefly,
  910. content,
  911. active
  912. FROM
  913. shop_products
  914. WHERE
  915. id = ?
  916. LIMIT 1;`,
  917. utils.StrToInt(wrap.UrlArgs[2]),
  918. ).Scan(
  919. &data.A_id,
  920. &data.A_parent,
  921. &data.A_user,
  922. &data.A_currency,
  923. &data.A_price,
  924. &data.A_gname,
  925. &data.A_name,
  926. &data.A_alias,
  927. &data.A_vendor,
  928. &data.A_quantity,
  929. &data.A_category,
  930. &data.A_briefly,
  931. &data.A_content,
  932. &data.A_active,
  933. )
  934. if *wrap.LogCpError(&err) != nil {
  935. return "", "", ""
  936. }
  937. }
  938. if data.A_parent_id() > 0 {
  939. content += `<style>.product-another{display:none}</style>`
  940. }
  941. // All product current categories
  942. var selids []int
  943. if data.A_id > 0 {
  944. rows, err := wrap.DB.Query("SELECT category_id FROM shop_cat_product_rel WHERE product_id = ?;", data.A_id)
  945. if err == nil {
  946. defer rows.Close()
  947. values := make([]int, 1)
  948. scan := make([]interface{}, len(values))
  949. for i := range values {
  950. scan[i] = &values[i]
  951. }
  952. for rows.Next() {
  953. err = rows.Scan(scan...)
  954. if *wrap.LogCpError(&err) == nil {
  955. selids = append(selids, int(values[0]))
  956. }
  957. }
  958. }
  959. }
  960. // Sub products
  961. sub_products := ``
  962. if data.A_id >= 1 && data.A_parent_id() <= 0 {
  963. sub_products = this.shop_GetSubProducts(wrap, data.A_id)
  964. }
  965. btn_caption := "Add"
  966. if wrap.CurrSubModule == "modify" {
  967. btn_caption = "Save"
  968. }
  969. content += builder.DataForm(wrap, []builder.DataFormField{
  970. {
  971. Kind: builder.DFKHidden,
  972. Name: "action",
  973. Value: "shop-modify",
  974. },
  975. {
  976. Kind: builder.DFKHidden,
  977. Name: "id",
  978. Value: utils.IntToStr(data.A_id),
  979. },
  980. {
  981. Kind: builder.DFKText,
  982. CallBack: func(field *builder.DataFormField) string {
  983. if data.A_id >= 1 && data.A_parent_id() <= 0 {
  984. return `<div class="form-group nf">` +
  985. `<div class="row">` +
  986. `<div class="col-md-3">` +
  987. `<label>Sub products</label>` +
  988. `</div>` +
  989. `<div class="col-md-9">` +
  990. `<div class="list-wrapper">` +
  991. sub_products +
  992. `<div><a href="javascript:fave.ShopAttachProduct(` + utils.IntToStr(data.A_id) + `);"><b>Attach product</b></a></div>` +
  993. `</div>` +
  994. `</div>` +
  995. `</div>` +
  996. `</div>`
  997. }
  998. return ""
  999. },
  1000. },
  1001. {
  1002. Kind: builder.DFKText,
  1003. CallBack: func(field *builder.DataFormField) string {
  1004. if data.A_id >= 1 && data.A_parent_id() != 0 {
  1005. return `<div class="form-group nf">` +
  1006. `<div class="row">` +
  1007. `<div class="col-md-3">` +
  1008. `<label>Parent</label>` +
  1009. `</div>` +
  1010. `<div class="col-md-9">` +
  1011. `<div class="list-wrapper">` +
  1012. this.shop_GetParentProduct(wrap, data.A_parent_id()) +
  1013. `</div>` +
  1014. `</div>` +
  1015. `</div>` +
  1016. `</div>`
  1017. }
  1018. return ""
  1019. },
  1020. },
  1021. {
  1022. Kind: builder.DFKText,
  1023. CallBack: func(field *builder.DataFormField) string {
  1024. if data.A_id >= 1 && data.A_parent_id() <= 0 && sub_products != "" {
  1025. return `<div class="form-group nf">` +
  1026. `<div class="row">` +
  1027. `<div class="col-md-3">` +
  1028. `<label for="lbl_gname">Group name</label>` +
  1029. `</div>` +
  1030. `<div class="col-md-9">` +
  1031. `<div><input class="form-control" type="text" id="lbl_gname" name="gname" value="` + html.EscapeString(data.A_gname) + `" maxlength="255" autocomplete="off"></div>` +
  1032. `</div>` +
  1033. `</div>` +
  1034. `</div>`
  1035. }
  1036. return ""
  1037. },
  1038. },
  1039. {
  1040. Kind: builder.DFKText,
  1041. Caption: "Product name",
  1042. Name: "name",
  1043. Value: data.A_name,
  1044. Required: true,
  1045. Min: "1",
  1046. Max: "255",
  1047. },
  1048. {
  1049. Kind: builder.DFKText,
  1050. Caption: "Product price",
  1051. Name: "price",
  1052. Value: "0",
  1053. CallBack: func(field *builder.DataFormField) string {
  1054. return `<div class="form-group n6">` +
  1055. `<div class="row">` +
  1056. `<div class="col-md-3">` +
  1057. `<label for="lbl_price">Product price</label>` +
  1058. `</div>` +
  1059. `<div class="col-md-9">` +
  1060. `<div>` +
  1061. `<div class="row">` +
  1062. `<div class="col-md-8">` +
  1063. `<div><input class="form-control" type="number" step="0.01" id="lbl_price" name="price" value="` + utils.Float64ToStr(data.A_price) + `" placeholder="" autocomplete="off" required></div>` +
  1064. `<div class="d-md-none mb-3"></div>` +
  1065. `</div>` +
  1066. `<div class="col-md-4">` +
  1067. `<select class="selectpicker form-control" id="lbl_currency" name="currency" data-live-search="true">` +
  1068. this.shop_GetCurrencySelectOptions(wrap, data.A_currency) +
  1069. `</select>` +
  1070. `</div>` +
  1071. `</div>` +
  1072. `</div>` +
  1073. `</div>` +
  1074. `</div>` +
  1075. `</div>`
  1076. },
  1077. },
  1078. {
  1079. Kind: builder.DFKText,
  1080. Caption: "Product alias",
  1081. Name: "alias",
  1082. Value: data.A_alias,
  1083. Hint: "Example: mobile-phone",
  1084. Max: "255",
  1085. },
  1086. {
  1087. Kind: builder.DFKText,
  1088. Caption: "Vendor/Count",
  1089. Name: "vendor",
  1090. Value: "0",
  1091. CallBack: func(field *builder.DataFormField) string {
  1092. return `<div class="form-group n8">` +
  1093. `<div class="row">` +
  1094. `<div class="col-md-3">` +
  1095. `<label for="lbl_vendor">Vendor/Count</label>` +
  1096. `</div>` +
  1097. `<div class="col-md-9">` +
  1098. `<div>` +
  1099. `<div class="row">` +
  1100. `<div class="col-md-8">` +
  1101. `<div><input class="form-control" type="text" id="lbl_vendor" name="vendor" value="` + html.EscapeString(data.A_vendor) + `" placeholder="" autocomplete="off"></div>` +
  1102. `<div class="d-md-none mb-3"></div>` +
  1103. `</div>` +
  1104. `<div class="col-md-4">` +
  1105. `<input class="form-control" type="number" step="1" id="lbl_quantity" name="quantity" value="` + utils.IntToStr(data.A_quantity) + `" placeholder="" autocomplete="off">` +
  1106. `</div>` +
  1107. `</div>` +
  1108. `</div>` +
  1109. `</div>` +
  1110. `</div>` +
  1111. `</div>`
  1112. },
  1113. },
  1114. {
  1115. Kind: builder.DFKText,
  1116. Caption: "Category",
  1117. Name: "category",
  1118. Value: "0",
  1119. CallBack: func(field *builder.DataFormField) string {
  1120. return `<div class="form-group n9">` +
  1121. `<div class="row">` +
  1122. `<div class="col-md-3">` +
  1123. `<label for="lbl_category">Category</label>` +
  1124. `</div>` +
  1125. `<div class="col-md-9">` +
  1126. `<div>` +
  1127. `<select class="selectpicker form-control" id="lbl_category" name="category" data-live-search="true">` +
  1128. `<option title="Nothing selected" value="0">&mdash;</option>` +
  1129. this.shop_GetCategorySelectOptions(wrap, 0, data.A_category, []int{}) +
  1130. `</select>` +
  1131. `</div>` +
  1132. `</div>` +
  1133. `</div>` +
  1134. `</div>`
  1135. },
  1136. },
  1137. {
  1138. Kind: builder.DFKText,
  1139. Caption: "Categories",
  1140. Name: "cats",
  1141. Value: "0",
  1142. CallBack: func(field *builder.DataFormField) string {
  1143. return `<div class="form-group n10">` +
  1144. `<div class="row">` +
  1145. `<div class="col-md-3">` +
  1146. `<label for="lbl_cats">Categories</label>` +
  1147. `</div>` +
  1148. `<div class="col-md-9">` +
  1149. `<div>` +
  1150. `<select class="selectpicker form-control" id="lbl_cats" name="cats[]" data-live-search="true" multiple>` +
  1151. this.shop_GetCategorySelectOptions(wrap, 0, 0, selids) +
  1152. `</select>` +
  1153. `</div>` +
  1154. `</div>` +
  1155. `</div>` +
  1156. `</div>`
  1157. },
  1158. },
  1159. {
  1160. Kind: builder.DFKText,
  1161. Caption: "Attributes",
  1162. Name: "",
  1163. Value: "",
  1164. CallBack: func(field *builder.DataFormField) string {
  1165. return `<div class="form-group n11">` +
  1166. `<div class="row">` +
  1167. `<div class="col-md-3">` +
  1168. `<label>Attributes</label>` +
  1169. `</div>` +
  1170. `<div class="col-md-9">` +
  1171. `<div class="list-wrapper">` +
  1172. `<div id="list">` +
  1173. this.shop_GetProductValuesInputs(wrap, data.A_id) +
  1174. `</div>` +
  1175. `<div class="list-button position-relative">` +
  1176. `<select class="selectpicker form-control" id="lbl_attributes" data-live-search="true" onchange="fave.ShopProductsAdd();">` +
  1177. this.shop_GetAllAttributesSelectOptions(wrap) +
  1178. `</select>` +
  1179. `</div>` +
  1180. `</div>` +
  1181. `</div>` +
  1182. `</div>` +
  1183. `</div>`
  1184. },
  1185. },
  1186. {
  1187. Kind: builder.DFKTextArea,
  1188. Caption: "Briefly",
  1189. Name: "briefly",
  1190. Value: data.A_briefly,
  1191. Classes: "briefly wysiwyg",
  1192. },
  1193. {
  1194. Kind: builder.DFKTextArea,
  1195. Caption: "Product content",
  1196. Name: "content",
  1197. Value: data.A_content,
  1198. Classes: "wysiwyg",
  1199. },
  1200. {
  1201. Kind: builder.DFKText,
  1202. Caption: "Product images",
  1203. Name: "",
  1204. Value: "",
  1205. CallBack: func(field *builder.DataFormField) string {
  1206. if data.A_id == 0 {
  1207. return ``
  1208. }
  1209. return `<div class="form-group n14">` +
  1210. `<div class="row">` +
  1211. `<div class="col-md-3">` +
  1212. `<label>Product images</label>` +
  1213. `</div>` +
  1214. `<div class="col-md-9">` +
  1215. `<div class="list-wrapper">` +
  1216. `<div id="list-images">` +
  1217. this.shop_GetAllProductImages(wrap, data.A_id) +
  1218. `</div>` +
  1219. `<div id="img-upload-block" class="list-button position-relative">` +
  1220. `<div id="upload-msg">Uploading...</div>` +
  1221. `<input class="form-control ignore-lost-data" type="file" id="file" name="file" onchange="fave.ShopProductsUploadImage('shop-upload-image', ` + utils.IntToStr(data.A_id) + `, 'file');" style="font-size:12px;" multiple />` +
  1222. `</div>` +
  1223. `</div>` +
  1224. `</div>` +
  1225. `</div>` +
  1226. `</div>` +
  1227. `<script>WaitForFave(function(){Sortable.create(document.getElementById('list-images'),{animation:0,onEnd:function(evt){var orderData=[];$('#list-images div.attached-img').each(function(i,v){orderData.push({Id:$(v).data('id'),Order:i+1});});$('#list-images').addClass('loading');fave.ShopProductsImageReorder('shop-images-reorder',{Items:orderData});},});});</script>`
  1228. },
  1229. },
  1230. {
  1231. Kind: builder.DFKCheckBox,
  1232. Caption: "Active",
  1233. Name: "active",
  1234. Value: utils.IntToStr(data.A_active),
  1235. },
  1236. {
  1237. Kind: builder.DFKSubmit,
  1238. Value: btn_caption,
  1239. Target: "add-edit-button",
  1240. },
  1241. })
  1242. if wrap.CurrSubModule == "add" {
  1243. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1244. } else {
  1245. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1246. }
  1247. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  1248. if wrap.CurrSubModule == "categories-add" {
  1249. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1250. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1251. {Name: "Add new category"},
  1252. })
  1253. } else {
  1254. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1255. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1256. {Name: "Modify category"},
  1257. })
  1258. }
  1259. data := utils.MySql_shop_category{
  1260. A_id: 0,
  1261. A_user: 0,
  1262. A_name: "",
  1263. A_alias: "",
  1264. A_lft: 0,
  1265. A_rgt: 0,
  1266. }
  1267. if wrap.CurrSubModule == "categories-modify" {
  1268. if len(wrap.UrlArgs) != 3 {
  1269. return "", "", ""
  1270. }
  1271. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1272. return "", "", ""
  1273. }
  1274. err := wrap.DB.QueryRow(`
  1275. SELECT
  1276. id,
  1277. user,
  1278. name,
  1279. alias,
  1280. lft,
  1281. rgt
  1282. FROM
  1283. shop_cats
  1284. WHERE
  1285. id = ?
  1286. LIMIT 1;`,
  1287. utils.StrToInt(wrap.UrlArgs[2]),
  1288. ).Scan(
  1289. &data.A_id,
  1290. &data.A_user,
  1291. &data.A_name,
  1292. &data.A_alias,
  1293. &data.A_lft,
  1294. &data.A_rgt,
  1295. )
  1296. if *wrap.LogCpError(&err) != nil {
  1297. return "", "", ""
  1298. }
  1299. }
  1300. btn_caption := "Add"
  1301. if wrap.CurrSubModule == "categories-modify" {
  1302. btn_caption = "Save"
  1303. }
  1304. parentId := 0
  1305. if wrap.CurrSubModule == "categories-modify" {
  1306. parentId = this.shop_GetCategoryParentId(wrap, data.A_id)
  1307. }
  1308. content += builder.DataForm(wrap, []builder.DataFormField{
  1309. {
  1310. Kind: builder.DFKHidden,
  1311. Name: "action",
  1312. Value: "shop-categories-modify",
  1313. },
  1314. {
  1315. Kind: builder.DFKHidden,
  1316. Name: "id",
  1317. Value: utils.IntToStr(data.A_id),
  1318. },
  1319. {
  1320. Kind: builder.DFKText,
  1321. Caption: "Parent",
  1322. Name: "parent",
  1323. Value: "0",
  1324. CallBack: func(field *builder.DataFormField) string {
  1325. return `<div class="form-group n2">` +
  1326. `<div class="row">` +
  1327. `<div class="col-md-3">` +
  1328. `<label for="lbl_parent">Parent</label>` +
  1329. `</div>` +
  1330. `<div class="col-md-9">` +
  1331. `<div>` +
  1332. `<select class="selectpicker form-control" id="lbl_parent" name="parent" data-live-search="true">` +
  1333. `<option title="Nothing selected" value="0">&mdash;</option>` +
  1334. this.shop_GetCategorySelectOptions(wrap, data.A_id, parentId, []int{}) +
  1335. `</select>` +
  1336. `</div>` +
  1337. `</div>` +
  1338. `</div>` +
  1339. `</div>`
  1340. },
  1341. },
  1342. {
  1343. Kind: builder.DFKText,
  1344. Caption: "Name",
  1345. Name: "name",
  1346. Value: data.A_name,
  1347. Required: true,
  1348. Min: "1",
  1349. Max: "255",
  1350. },
  1351. {
  1352. Kind: builder.DFKText,
  1353. Caption: "Alias",
  1354. Name: "alias",
  1355. Value: data.A_alias,
  1356. Hint: "Example: popular-products",
  1357. Max: "255",
  1358. },
  1359. {
  1360. Kind: builder.DFKSubmit,
  1361. Value: btn_caption,
  1362. Target: "add-edit-button",
  1363. },
  1364. })
  1365. if wrap.CurrSubModule == "categories-add" {
  1366. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1367. } else {
  1368. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1369. }
  1370. } else if wrap.CurrSubModule == "attributes-add" || wrap.CurrSubModule == "attributes-modify" {
  1371. if wrap.CurrSubModule == "attributes-add" {
  1372. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1373. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1374. {Name: "Add new attribute"},
  1375. })
  1376. } else {
  1377. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1378. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1379. {Name: "Modify attribute"},
  1380. })
  1381. }
  1382. data := utils.MySql_shop_filter{
  1383. A_id: 0,
  1384. A_name: "",
  1385. A_filter: "",
  1386. }
  1387. if wrap.CurrSubModule == "attributes-modify" {
  1388. if len(wrap.UrlArgs) != 3 {
  1389. return "", "", ""
  1390. }
  1391. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1392. return "", "", ""
  1393. }
  1394. err := wrap.DB.QueryRow(`
  1395. SELECT
  1396. id,
  1397. name,
  1398. filter
  1399. FROM
  1400. shop_filters
  1401. WHERE
  1402. id = ?
  1403. LIMIT 1;`,
  1404. utils.StrToInt(wrap.UrlArgs[2]),
  1405. ).Scan(
  1406. &data.A_id,
  1407. &data.A_name,
  1408. &data.A_filter,
  1409. )
  1410. if *wrap.LogCpError(&err) != nil {
  1411. return "", "", ""
  1412. }
  1413. }
  1414. btn_caption := "Add"
  1415. if wrap.CurrSubModule == "attributes-modify" {
  1416. btn_caption = "Save"
  1417. }
  1418. content += builder.DataForm(wrap, []builder.DataFormField{
  1419. {
  1420. Kind: builder.DFKHidden,
  1421. Name: "action",
  1422. Value: "shop-attributes-modify",
  1423. },
  1424. {
  1425. Kind: builder.DFKHidden,
  1426. Name: "id",
  1427. Value: utils.IntToStr(data.A_id),
  1428. },
  1429. {
  1430. Kind: builder.DFKText,
  1431. Caption: "Attribute name",
  1432. Name: "name",
  1433. Value: data.A_name,
  1434. Required: true,
  1435. Min: "1",
  1436. Max: "255",
  1437. },
  1438. {
  1439. Kind: builder.DFKText,
  1440. Caption: "Attribute in filter",
  1441. Name: "filter",
  1442. Value: data.A_filter,
  1443. Required: true,
  1444. Min: "1",
  1445. Max: "255",
  1446. },
  1447. {
  1448. Kind: builder.DFKText,
  1449. Caption: "Attribute values",
  1450. Name: "",
  1451. Value: "",
  1452. CallBack: func(field *builder.DataFormField) string {
  1453. return `<div class="form-group n4">` +
  1454. `<div class="row">` +
  1455. `<div class="col-md-3">` +
  1456. `<label>Attribute values</label>` +
  1457. `</div>` +
  1458. `<div class="col-md-9">` +
  1459. `<div class="list-wrapper">` +
  1460. `<div id="list">` +
  1461. this.shop_GetFilterValuesInputs(wrap, data.A_id) +
  1462. `</div>` +
  1463. `<div class="list-button"><button type="button" class="btn btn-success" onclick="fave.ShopAttributesAdd();">Add attribute value</button></div>` +
  1464. `</div>` +
  1465. `</div>` +
  1466. `</div>` +
  1467. `</div>`
  1468. },
  1469. },
  1470. {
  1471. Kind: builder.DFKSubmit,
  1472. Value: btn_caption,
  1473. Target: "add-edit-button",
  1474. },
  1475. })
  1476. if wrap.CurrSubModule == "attributes-add" {
  1477. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1478. } else {
  1479. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1480. }
  1481. } else if wrap.CurrSubModule == "currencies-add" || wrap.CurrSubModule == "currencies-modify" {
  1482. if wrap.CurrSubModule == "currencies-add" {
  1483. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1484. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1485. {Name: "Add new currency"},
  1486. })
  1487. } else {
  1488. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1489. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1490. {Name: "Modify currency"},
  1491. })
  1492. }
  1493. data := utils.MySql_shop_currency{
  1494. A_id: 0,
  1495. A_name: "",
  1496. A_coefficient: 0,
  1497. A_code: "",
  1498. A_symbol: "",
  1499. }
  1500. if wrap.CurrSubModule == "currencies-modify" {
  1501. if len(wrap.UrlArgs) != 3 {
  1502. return "", "", ""
  1503. }
  1504. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1505. return "", "", ""
  1506. }
  1507. err := wrap.DB.QueryRow(`
  1508. SELECT
  1509. id,
  1510. name,
  1511. coefficient,
  1512. code,
  1513. symbol
  1514. FROM
  1515. shop_currencies
  1516. WHERE
  1517. id = ?
  1518. LIMIT 1;`,
  1519. utils.StrToInt(wrap.UrlArgs[2]),
  1520. ).Scan(
  1521. &data.A_id,
  1522. &data.A_name,
  1523. &data.A_coefficient,
  1524. &data.A_code,
  1525. &data.A_symbol,
  1526. )
  1527. if *wrap.LogCpError(&err) != nil {
  1528. return "", "", ""
  1529. }
  1530. }
  1531. btn_caption := "Add"
  1532. if wrap.CurrSubModule == "currencies-modify" {
  1533. btn_caption = "Save"
  1534. }
  1535. content += builder.DataForm(wrap, []builder.DataFormField{
  1536. {
  1537. Kind: builder.DFKHidden,
  1538. Name: "action",
  1539. Value: "shop-currencies-modify",
  1540. },
  1541. {
  1542. Kind: builder.DFKHidden,
  1543. Name: "id",
  1544. Value: utils.IntToStr(data.A_id),
  1545. },
  1546. {
  1547. Kind: builder.DFKText,
  1548. Caption: "Currency name",
  1549. Name: "name",
  1550. Value: data.A_name,
  1551. Required: true,
  1552. Min: "1",
  1553. Max: "255",
  1554. },
  1555. {
  1556. Kind: builder.DFKText,
  1557. Caption: "Currency coefficient",
  1558. Name: "coefficient",
  1559. Value: "0",
  1560. CallBack: func(field *builder.DataFormField) string {
  1561. return `<div class="form-group n3">` +
  1562. `<div class="row">` +
  1563. `<div class="col-md-3">` +
  1564. `<label for="lbl_coefficient">Currency coefficient</label>` +
  1565. `</div>` +
  1566. `<div class="col-md-9">` +
  1567. `<div><input class="form-control" type="number" step="0.0001" id="lbl_coefficient" name="coefficient" value="` + utils.Float64ToStrF(data.A_coefficient, "%.4f") + `" placeholder="" autocomplete="off" required></div>` +
  1568. `</div>` +
  1569. `</div>` +
  1570. `</div>`
  1571. },
  1572. },
  1573. {
  1574. Kind: builder.DFKText,
  1575. Caption: "Currency code",
  1576. Name: "code",
  1577. Value: data.A_code,
  1578. Required: true,
  1579. Min: "1",
  1580. Max: "10",
  1581. },
  1582. {
  1583. Kind: builder.DFKText,
  1584. Caption: "Currency symbol",
  1585. Name: "symbol",
  1586. Value: data.A_symbol,
  1587. Required: true,
  1588. Min: "1",
  1589. Max: "5",
  1590. },
  1591. {
  1592. Kind: builder.DFKSubmit,
  1593. Value: btn_caption,
  1594. Target: "add-edit-button",
  1595. },
  1596. })
  1597. if wrap.CurrSubModule == "currencies-add" {
  1598. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1599. } else {
  1600. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1601. }
  1602. }
  1603. return this.getSidebarModules(wrap), content, sidebar
  1604. })
  1605. }