module_shop.go 48 KB

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