module_shop.go 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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] == "plus" || wrap.UrlArgs[2] == "minus" || 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.Header().Set("Content-Type", "application/json; charset=utf-8")
  436. wrap.W.Write([]byte(wrap.ShopBasket.Info(wrap.CurrHost, wrap.GetSessionId(), wrap.DB, 1)))
  437. return
  438. } else if wrap.UrlArgs[2] == "plus" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  439. wrap.W.WriteHeader(http.StatusOK)
  440. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  441. wrap.W.Header().Set("Content-Type", "application/json; charset=utf-8")
  442. wrap.W.Write([]byte(wrap.ShopBasket.Plus(wrap.CurrHost, wrap.GetSessionId(), wrap.DB, utils.StrToInt(wrap.UrlArgs[3]))))
  443. return
  444. } else if wrap.UrlArgs[2] == "minus" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  445. wrap.W.WriteHeader(http.StatusOK)
  446. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  447. wrap.W.Header().Set("Content-Type", "application/json; charset=utf-8")
  448. wrap.W.Write([]byte(wrap.ShopBasket.Minus(wrap.CurrHost, wrap.GetSessionId(), wrap.DB, utils.StrToInt(wrap.UrlArgs[3]))))
  449. return
  450. } else if wrap.UrlArgs[2] == "remove" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  451. wrap.W.WriteHeader(http.StatusOK)
  452. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  453. wrap.W.Header().Set("Content-Type", "application/json; charset=utf-8")
  454. wrap.W.Write([]byte(wrap.ShopBasket.Remove(wrap.CurrHost, wrap.GetSessionId(), wrap.DB, utils.StrToInt(wrap.UrlArgs[3]))))
  455. return
  456. }
  457. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {
  458. // Shop product
  459. row := &utils.MySql_shop_product{}
  460. rou := &utils.MySql_user{}
  461. err := wrap.DB.QueryRow(`
  462. SELECT
  463. shop_products.id,
  464. shop_products.parent_id,
  465. shop_products.user,
  466. shop_products.currency,
  467. shop_products.price,
  468. shop_products.name,
  469. shop_products.alias,
  470. shop_products.vendor,
  471. shop_products.quantity,
  472. shop_products.category,
  473. shop_products.briefly,
  474. shop_products.content,
  475. UNIX_TIMESTAMP(shop_products.datetime) as datetime,
  476. shop_products.active,
  477. users.id,
  478. users.first_name,
  479. users.last_name,
  480. users.email,
  481. users.admin,
  482. users.active
  483. FROM
  484. shop_products
  485. LEFT JOIN users ON users.id = shop_products.user
  486. WHERE
  487. shop_products.active = 1 and
  488. shop_products.alias = ?
  489. LIMIT 1;`,
  490. wrap.UrlArgs[1],
  491. ).Scan(
  492. &row.A_id,
  493. &row.A_parent,
  494. &row.A_user,
  495. &row.A_currency,
  496. &row.A_price,
  497. &row.A_name,
  498. &row.A_alias,
  499. &row.A_vendor,
  500. &row.A_quantity,
  501. &row.A_category,
  502. &row.A_briefly,
  503. &row.A_content,
  504. &row.A_datetime,
  505. &row.A_active,
  506. &rou.A_id,
  507. &rou.A_first_name,
  508. &rou.A_last_name,
  509. &rou.A_email,
  510. &rou.A_admin,
  511. &rou.A_active,
  512. )
  513. if err != nil && err != wrapper.ErrNoRows {
  514. // System error 500
  515. wrap.LogCpError(&err)
  516. utils.SystemErrorPageEngine(wrap.W, err)
  517. return
  518. } else if err == wrapper.ErrNoRows {
  519. // User error 404 page
  520. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  521. return
  522. }
  523. // Fix url
  524. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  525. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  526. return
  527. }
  528. // Render template
  529. wrap.RenderFrontEnd("shop-product", fetdata.New(wrap, false, row, rou), http.StatusOK)
  530. return
  531. } else if len(wrap.UrlArgs) == 1 && wrap.UrlArgs[0] == "shop" {
  532. // Shop
  533. // Fix url
  534. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  535. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  536. return
  537. }
  538. // Render template
  539. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  540. return
  541. } else if (*wrap.Config).Engine.MainModule == 2 {
  542. // Render template
  543. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  544. return
  545. }
  546. // User error 404 page
  547. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  548. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  549. content := ""
  550. sidebar := ""
  551. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  552. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  553. {Name: "List of products"},
  554. })
  555. // Load currencies
  556. currencies := this.shop_GetAllCurrencies(wrap)
  557. content += builder.DataTable(
  558. wrap,
  559. "shop_products",
  560. "id",
  561. "DESC",
  562. &[]builder.DataTableRow{
  563. {
  564. DBField: "id",
  565. },
  566. {
  567. DBField: "name",
  568. NameInTable: "Product / URL",
  569. CallBack: func(values *[]string) string {
  570. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + ` ` + html.EscapeString((*values)[0]) + `</a>`
  571. alias := html.EscapeString((*values)[2])
  572. parent := ``
  573. if (*values)[7] != "" {
  574. parent = `<div class="parent">&uarr;<small><a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[7] + `/">` + html.EscapeString((*values)[8]) + ` ` + (*values)[7] + `</a></small></div>`
  575. }
  576. return `<div>` + name + `</div><div><small>/shop/` + alias + `/</small></div>` + parent
  577. },
  578. },
  579. {
  580. DBField: "alias",
  581. },
  582. {
  583. DBField: "currency",
  584. },
  585. {
  586. DBField: "price",
  587. NameInTable: "Price",
  588. Classes: "d-none d-md-table-cell",
  589. CallBack: func(values *[]string) string {
  590. return `<div>` + utils.Float64ToStr(utils.StrToFloat64((*values)[4])) + `</div>` +
  591. `<div><small>` + currencies[utils.StrToInt((*values)[3])] + `</small></div>`
  592. },
  593. },
  594. {
  595. DBField: "datetime",
  596. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  597. NameInTable: "Date / Time",
  598. Classes: "d-none d-lg-table-cell",
  599. CallBack: func(values *[]string) string {
  600. t := int64(utils.StrToInt((*values)[5]))
  601. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  602. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  603. },
  604. },
  605. {
  606. DBField: "active",
  607. NameInTable: "Active",
  608. Classes: "d-none d-sm-table-cell",
  609. CallBack: func(values *[]string) string {
  610. return builder.CheckBox(utils.StrToInt((*values)[6]))
  611. },
  612. },
  613. {
  614. DBField: "parent_id",
  615. },
  616. {
  617. DBField: "pname",
  618. DBExp: "spp.name",
  619. },
  620. },
  621. func(values *[]string) string {
  622. return builder.DataTableAction(&[]builder.DataTableActionRow{
  623. {
  624. Icon: assets.SysSvgIconView,
  625. Href: `/shop/` + (*values)[2] + `/`,
  626. Hint: "View",
  627. Target: "_blank",
  628. },
  629. {
  630. Icon: assets.SysSvgIconEdit,
  631. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  632. Hint: "Edit",
  633. },
  634. {
  635. Icon: assets.SysSvgIconRemove,
  636. Href: "javascript:fave.ActionDataTableDelete(this,'shop-delete','" +
  637. (*values)[0] + "','Are you sure want to delete product?');",
  638. Hint: "Delete",
  639. Classes: "delete",
  640. },
  641. })
  642. },
  643. "/cp/"+wrap.CurrModule+"/",
  644. func() (int, error) {
  645. var count int
  646. return count, wrap.DB.QueryRow(
  647. "SELECT COUNT(*) FROM `shop_products`;",
  648. ).Scan(&count)
  649. },
  650. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  651. return wrap.DB.Query(
  652. `SELECT
  653. shop_products.id,
  654. shop_products.name,
  655. shop_products.alias,
  656. shop_products.currency,
  657. shop_products.price,
  658. UNIX_TIMESTAMP(`+"`shop_products`.`datetime`"+`) AS datetime,
  659. shop_products.active,
  660. shop_products.parent_id,
  661. spp.name AS pname
  662. FROM
  663. shop_products
  664. LEFT JOIN shop_products AS spp ON spp.id = shop_products.parent_id
  665. ORDER BY
  666. shop_products.id DESC
  667. LIMIT ?, ?;`,
  668. limit_offset,
  669. pear_page,
  670. )
  671. },
  672. true,
  673. )
  674. } else if wrap.CurrSubModule == "categories" {
  675. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  676. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  677. {Name: "List of categories"},
  678. })
  679. content += builder.DataTable(
  680. wrap,
  681. "shop_cats",
  682. "id",
  683. "ASC",
  684. &[]builder.DataTableRow{
  685. {
  686. DBField: "id",
  687. },
  688. {
  689. DBField: "user",
  690. },
  691. {
  692. DBField: "name",
  693. NameInTable: "Category",
  694. CallBack: func(values *[]string) string {
  695. depth := utils.StrToInt((*values)[4]) - 1
  696. if depth < 0 {
  697. depth = 0
  698. }
  699. sub := strings.Repeat("&mdash; ", depth)
  700. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  701. return `<div>` + name + `</div>`
  702. },
  703. },
  704. {
  705. DBField: "alias",
  706. },
  707. {
  708. DBField: "depth",
  709. },
  710. },
  711. func(values *[]string) string {
  712. return builder.DataTableAction(&[]builder.DataTableActionRow{
  713. {
  714. Icon: assets.SysSvgIconView,
  715. Href: `/shop/category/` + (*values)[3] + `/`,
  716. Hint: "View",
  717. Target: "_blank",
  718. },
  719. {
  720. Icon: assets.SysSvgIconEdit,
  721. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  722. Hint: "Edit",
  723. },
  724. {
  725. Icon: assets.SysSvgIconRemove,
  726. Href: "javascript:fave.ActionDataTableDelete(this,'shop-categories-delete','" +
  727. (*values)[0] + "','Are you sure want to delete category?');",
  728. Hint: "Delete",
  729. Classes: "delete",
  730. },
  731. })
  732. },
  733. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  734. nil,
  735. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  736. return wrap.DB.Query(
  737. `SELECT
  738. node.id,
  739. node.user,
  740. node.name,
  741. node.alias,
  742. (COUNT(parent.id) - 1) AS depth
  743. FROM
  744. shop_cats AS node,
  745. shop_cats AS parent
  746. WHERE
  747. node.lft BETWEEN parent.lft AND parent.rgt AND
  748. node.id > 1
  749. GROUP BY
  750. node.id
  751. ORDER BY
  752. node.lft ASC
  753. ;`,
  754. )
  755. },
  756. false,
  757. )
  758. } else if wrap.CurrSubModule == "attributes" {
  759. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  760. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  761. {Name: "List of attributes"},
  762. })
  763. content += builder.DataTable(
  764. wrap,
  765. "shop_filters",
  766. "id",
  767. "DESC",
  768. &[]builder.DataTableRow{
  769. {
  770. DBField: "id",
  771. },
  772. {
  773. DBField: "name",
  774. NameInTable: "Name",
  775. CallBack: func(values *[]string) string {
  776. name := `<a href="/cp/` + wrap.CurrModule + `/attributes-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  777. return `<div>` + name + `</div><div><small>` + html.EscapeString((*values)[2]) + `</small></div>`
  778. },
  779. },
  780. {
  781. DBField: "filter",
  782. },
  783. },
  784. func(values *[]string) string {
  785. return builder.DataTableAction(&[]builder.DataTableActionRow{
  786. {
  787. Icon: assets.SysSvgIconEdit,
  788. Href: "/cp/" + wrap.CurrModule + "/attributes-modify/" + (*values)[0] + "/",
  789. Hint: "Edit",
  790. },
  791. {
  792. Icon: assets.SysSvgIconRemove,
  793. Href: "javascript:fave.ActionDataTableDelete(this,'shop-attributes-delete','" +
  794. (*values)[0] + "','Are you sure want to delete attribute?');",
  795. Hint: "Delete",
  796. Classes: "delete",
  797. },
  798. })
  799. },
  800. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  801. nil,
  802. nil,
  803. true,
  804. )
  805. } else if wrap.CurrSubModule == "currencies" {
  806. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  807. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  808. {Name: "List of currencies"},
  809. })
  810. content += builder.DataTable(
  811. wrap,
  812. "shop_currencies",
  813. "id",
  814. "DESC",
  815. &[]builder.DataTableRow{
  816. {
  817. DBField: "id",
  818. },
  819. {
  820. DBField: "name",
  821. NameInTable: "Name",
  822. CallBack: func(values *[]string) string {
  823. name := `<a href="/cp/` + wrap.CurrModule + `/currencies-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + ` (` + (*values)[3] + `, ` + (*values)[4] + `)</a>`
  824. return `<div>` + name + `</div>`
  825. },
  826. },
  827. {
  828. DBField: "coefficient",
  829. NameInTable: "Coefficient",
  830. Classes: "d-none d-md-table-cell",
  831. CallBack: func(values *[]string) string {
  832. return utils.Float64ToStrF(utils.StrToFloat64((*values)[2]), "%.4f")
  833. },
  834. },
  835. {
  836. DBField: "code",
  837. },
  838. {
  839. DBField: "symbol",
  840. },
  841. },
  842. func(values *[]string) string {
  843. return builder.DataTableAction(&[]builder.DataTableActionRow{
  844. {
  845. Icon: assets.SysSvgIconEdit,
  846. Href: "/cp/" + wrap.CurrModule + "/currencies-modify/" + (*values)[0] + "/",
  847. Hint: "Edit",
  848. },
  849. {
  850. Icon: assets.SysSvgIconRemove,
  851. Href: "javascript:fave.ActionDataTableDelete(this,'shop-currencies-delete','" +
  852. (*values)[0] + "','Are you sure want to delete currency?');",
  853. Hint: "Delete",
  854. Classes: "delete",
  855. },
  856. })
  857. },
  858. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  859. nil,
  860. nil,
  861. true,
  862. )
  863. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  864. if wrap.CurrSubModule == "add" {
  865. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  866. {Name: "Add new product"},
  867. })
  868. } else {
  869. if len(wrap.UrlArgs) >= 3 && utils.IsNumeric(wrap.UrlArgs[2]) {
  870. content += `<div class="product-copy"><a title="Duplicate product" href="javascript:fave.ShopProductsDuplicate(this, ` + wrap.UrlArgs[2] + `);">` + assets.SysSvgIconCopy + `</a></div>`
  871. content += `<div class="product-another"><a title="Duplicate product and attach" href="javascript:fave.ShopProductsDuplicateWithAttach(this, ` + wrap.UrlArgs[2] + `);">` + assets.SysSvgIconPlus + `</a></div>`
  872. }
  873. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  874. {Name: "Modify product"},
  875. })
  876. }
  877. data := utils.MySql_shop_product{
  878. A_id: 0,
  879. A_user: 0,
  880. A_currency: 0,
  881. A_price: 0,
  882. A_gname: "",
  883. A_name: "",
  884. A_alias: "",
  885. A_vendor: "",
  886. A_quantity: 0,
  887. A_category: 0,
  888. A_briefly: "",
  889. A_content: "",
  890. A_datetime: 0,
  891. A_active: 0,
  892. }
  893. if wrap.CurrSubModule == "modify" {
  894. if len(wrap.UrlArgs) != 3 {
  895. return "", "", ""
  896. }
  897. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  898. return "", "", ""
  899. }
  900. err := wrap.DB.QueryRow(`
  901. SELECT
  902. id,
  903. parent_id,
  904. user,
  905. currency,
  906. price,
  907. gname,
  908. name,
  909. alias,
  910. vendor,
  911. quantity,
  912. category,
  913. briefly,
  914. content,
  915. active
  916. FROM
  917. shop_products
  918. WHERE
  919. id = ?
  920. LIMIT 1;`,
  921. utils.StrToInt(wrap.UrlArgs[2]),
  922. ).Scan(
  923. &data.A_id,
  924. &data.A_parent,
  925. &data.A_user,
  926. &data.A_currency,
  927. &data.A_price,
  928. &data.A_gname,
  929. &data.A_name,
  930. &data.A_alias,
  931. &data.A_vendor,
  932. &data.A_quantity,
  933. &data.A_category,
  934. &data.A_briefly,
  935. &data.A_content,
  936. &data.A_active,
  937. )
  938. if *wrap.LogCpError(&err) != nil {
  939. return "", "", ""
  940. }
  941. }
  942. if data.A_parent_id() > 0 {
  943. content += `<style>.product-another{display:none}</style>`
  944. }
  945. // All product current categories
  946. var selids []int
  947. if data.A_id > 0 {
  948. rows, err := wrap.DB.Query("SELECT category_id FROM shop_cat_product_rel WHERE product_id = ?;", data.A_id)
  949. if err == nil {
  950. defer rows.Close()
  951. values := make([]int, 1)
  952. scan := make([]interface{}, len(values))
  953. for i := range values {
  954. scan[i] = &values[i]
  955. }
  956. for rows.Next() {
  957. err = rows.Scan(scan...)
  958. if *wrap.LogCpError(&err) == nil {
  959. selids = append(selids, int(values[0]))
  960. }
  961. }
  962. }
  963. }
  964. // Sub products
  965. sub_products := ``
  966. if data.A_id >= 1 && data.A_parent_id() <= 0 {
  967. sub_products = this.shop_GetSubProducts(wrap, data.A_id)
  968. }
  969. btn_caption := "Add"
  970. if wrap.CurrSubModule == "modify" {
  971. btn_caption = "Save"
  972. }
  973. content += builder.DataForm(wrap, []builder.DataFormField{
  974. {
  975. Kind: builder.DFKHidden,
  976. Name: "action",
  977. Value: "shop-modify",
  978. },
  979. {
  980. Kind: builder.DFKHidden,
  981. Name: "id",
  982. Value: utils.IntToStr(data.A_id),
  983. },
  984. {
  985. Kind: builder.DFKText,
  986. CallBack: func(field *builder.DataFormField) string {
  987. if data.A_id >= 1 && data.A_parent_id() <= 0 {
  988. return `<div class="form-group nf">` +
  989. `<div class="row">` +
  990. `<div class="col-md-3">` +
  991. `<label>Sub products</label>` +
  992. `</div>` +
  993. `<div class="col-md-9">` +
  994. `<div class="list-wrapper">` +
  995. sub_products +
  996. `<div><a href="javascript:fave.ShopAttachProduct(` + utils.IntToStr(data.A_id) + `);"><b>Attach product</b></a></div>` +
  997. `</div>` +
  998. `</div>` +
  999. `</div>` +
  1000. `</div>`
  1001. }
  1002. return ""
  1003. },
  1004. },
  1005. {
  1006. Kind: builder.DFKText,
  1007. CallBack: func(field *builder.DataFormField) string {
  1008. if data.A_id >= 1 && data.A_parent_id() != 0 {
  1009. return `<div class="form-group nf">` +
  1010. `<div class="row">` +
  1011. `<div class="col-md-3">` +
  1012. `<label>Parent</label>` +
  1013. `</div>` +
  1014. `<div class="col-md-9">` +
  1015. `<div class="list-wrapper">` +
  1016. this.shop_GetParentProduct(wrap, data.A_parent_id()) +
  1017. `</div>` +
  1018. `</div>` +
  1019. `</div>` +
  1020. `</div>`
  1021. }
  1022. return ""
  1023. },
  1024. },
  1025. {
  1026. Kind: builder.DFKText,
  1027. CallBack: func(field *builder.DataFormField) string {
  1028. if data.A_id >= 1 && data.A_parent_id() <= 0 && sub_products != "" {
  1029. return `<div class="form-group nf">` +
  1030. `<div class="row">` +
  1031. `<div class="col-md-3">` +
  1032. `<label for="lbl_gname">Group name</label>` +
  1033. `</div>` +
  1034. `<div class="col-md-9">` +
  1035. `<div><input class="form-control" type="text" id="lbl_gname" name="gname" value="` + html.EscapeString(data.A_gname) + `" maxlength="255" autocomplete="off"></div>` +
  1036. `</div>` +
  1037. `</div>` +
  1038. `</div>`
  1039. }
  1040. return ""
  1041. },
  1042. },
  1043. {
  1044. Kind: builder.DFKText,
  1045. Caption: "Product name",
  1046. Name: "name",
  1047. Value: data.A_name,
  1048. Required: true,
  1049. Min: "1",
  1050. Max: "255",
  1051. },
  1052. {
  1053. Kind: builder.DFKText,
  1054. Caption: "Product price",
  1055. Name: "price",
  1056. Value: "0",
  1057. CallBack: func(field *builder.DataFormField) string {
  1058. return `<div class="form-group n6">` +
  1059. `<div class="row">` +
  1060. `<div class="col-md-3">` +
  1061. `<label for="lbl_price">Product price</label>` +
  1062. `</div>` +
  1063. `<div class="col-md-9">` +
  1064. `<div>` +
  1065. `<div class="row">` +
  1066. `<div class="col-md-8">` +
  1067. `<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>` +
  1068. `<div class="d-md-none mb-3"></div>` +
  1069. `</div>` +
  1070. `<div class="col-md-4">` +
  1071. `<select class="selectpicker form-control" id="lbl_currency" name="currency" data-live-search="true">` +
  1072. this.shop_GetCurrencySelectOptions(wrap, data.A_currency) +
  1073. `</select>` +
  1074. `</div>` +
  1075. `</div>` +
  1076. `</div>` +
  1077. `</div>` +
  1078. `</div>` +
  1079. `</div>`
  1080. },
  1081. },
  1082. {
  1083. Kind: builder.DFKText,
  1084. Caption: "Product alias",
  1085. Name: "alias",
  1086. Value: data.A_alias,
  1087. Hint: "Example: mobile-phone",
  1088. Max: "255",
  1089. },
  1090. {
  1091. Kind: builder.DFKText,
  1092. Caption: "Vendor/Count",
  1093. Name: "vendor",
  1094. Value: "0",
  1095. CallBack: func(field *builder.DataFormField) string {
  1096. return `<div class="form-group n8">` +
  1097. `<div class="row">` +
  1098. `<div class="col-md-3">` +
  1099. `<label for="lbl_vendor">Vendor/Count</label>` +
  1100. `</div>` +
  1101. `<div class="col-md-9">` +
  1102. `<div>` +
  1103. `<div class="row">` +
  1104. `<div class="col-md-8">` +
  1105. `<div><input class="form-control" type="text" id="lbl_vendor" name="vendor" value="` + html.EscapeString(data.A_vendor) + `" placeholder="" autocomplete="off"></div>` +
  1106. `<div class="d-md-none mb-3"></div>` +
  1107. `</div>` +
  1108. `<div class="col-md-4">` +
  1109. `<input class="form-control" type="number" step="1" id="lbl_quantity" name="quantity" value="` + utils.IntToStr(data.A_quantity) + `" placeholder="" autocomplete="off">` +
  1110. `</div>` +
  1111. `</div>` +
  1112. `</div>` +
  1113. `</div>` +
  1114. `</div>` +
  1115. `</div>`
  1116. },
  1117. },
  1118. {
  1119. Kind: builder.DFKText,
  1120. Caption: "Category",
  1121. Name: "category",
  1122. Value: "0",
  1123. CallBack: func(field *builder.DataFormField) string {
  1124. return `<div class="form-group n9">` +
  1125. `<div class="row">` +
  1126. `<div class="col-md-3">` +
  1127. `<label for="lbl_category">Category</label>` +
  1128. `</div>` +
  1129. `<div class="col-md-9">` +
  1130. `<div>` +
  1131. `<select class="selectpicker form-control" id="lbl_category" name="category" data-live-search="true">` +
  1132. `<option title="Nothing selected" value="0">&mdash;</option>` +
  1133. this.shop_GetCategorySelectOptions(wrap, 0, data.A_category, []int{}) +
  1134. `</select>` +
  1135. `</div>` +
  1136. `</div>` +
  1137. `</div>` +
  1138. `</div>`
  1139. },
  1140. },
  1141. {
  1142. Kind: builder.DFKText,
  1143. Caption: "Categories",
  1144. Name: "cats",
  1145. Value: "0",
  1146. CallBack: func(field *builder.DataFormField) string {
  1147. return `<div class="form-group n10">` +
  1148. `<div class="row">` +
  1149. `<div class="col-md-3">` +
  1150. `<label for="lbl_cats">Categories</label>` +
  1151. `</div>` +
  1152. `<div class="col-md-9">` +
  1153. `<div>` +
  1154. `<select class="selectpicker form-control" id="lbl_cats" name="cats[]" data-live-search="true" multiple>` +
  1155. this.shop_GetCategorySelectOptions(wrap, 0, 0, selids) +
  1156. `</select>` +
  1157. `</div>` +
  1158. `</div>` +
  1159. `</div>` +
  1160. `</div>`
  1161. },
  1162. },
  1163. {
  1164. Kind: builder.DFKText,
  1165. Caption: "Attributes",
  1166. Name: "",
  1167. Value: "",
  1168. CallBack: func(field *builder.DataFormField) string {
  1169. return `<div class="form-group n11">` +
  1170. `<div class="row">` +
  1171. `<div class="col-md-3">` +
  1172. `<label>Attributes</label>` +
  1173. `</div>` +
  1174. `<div class="col-md-9">` +
  1175. `<div class="list-wrapper">` +
  1176. `<div id="list">` +
  1177. this.shop_GetProductValuesInputs(wrap, data.A_id) +
  1178. `</div>` +
  1179. `<div class="list-button position-relative">` +
  1180. `<select class="selectpicker form-control" id="lbl_attributes" data-live-search="true" onchange="fave.ShopProductsAdd();">` +
  1181. this.shop_GetAllAttributesSelectOptions(wrap) +
  1182. `</select>` +
  1183. `</div>` +
  1184. `</div>` +
  1185. `</div>` +
  1186. `</div>` +
  1187. `</div>`
  1188. },
  1189. },
  1190. {
  1191. Kind: builder.DFKTextArea,
  1192. Caption: "Briefly",
  1193. Name: "briefly",
  1194. Value: data.A_briefly,
  1195. Classes: "briefly wysiwyg",
  1196. },
  1197. {
  1198. Kind: builder.DFKTextArea,
  1199. Caption: "Product content",
  1200. Name: "content",
  1201. Value: data.A_content,
  1202. Classes: "wysiwyg",
  1203. },
  1204. {
  1205. Kind: builder.DFKText,
  1206. Caption: "Product images",
  1207. Name: "",
  1208. Value: "",
  1209. CallBack: func(field *builder.DataFormField) string {
  1210. if data.A_id == 0 {
  1211. return ``
  1212. }
  1213. return `<div class="form-group n14">` +
  1214. `<div class="row">` +
  1215. `<div class="col-md-3">` +
  1216. `<label>Product images</label>` +
  1217. `</div>` +
  1218. `<div class="col-md-9">` +
  1219. `<div class="list-wrapper">` +
  1220. `<div id="list-images">` +
  1221. this.shop_GetAllProductImages(wrap, data.A_id) +
  1222. `</div>` +
  1223. `<div id="img-upload-block" class="list-button position-relative">` +
  1224. `<div id="upload-msg">Uploading...</div>` +
  1225. `<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 />` +
  1226. `</div>` +
  1227. `</div>` +
  1228. `</div>` +
  1229. `</div>` +
  1230. `</div>` +
  1231. `<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>`
  1232. },
  1233. },
  1234. {
  1235. Kind: builder.DFKCheckBox,
  1236. Caption: "Active",
  1237. Name: "active",
  1238. Value: utils.IntToStr(data.A_active),
  1239. },
  1240. {
  1241. Kind: builder.DFKSubmit,
  1242. Value: btn_caption,
  1243. Target: "add-edit-button",
  1244. },
  1245. })
  1246. if wrap.CurrSubModule == "add" {
  1247. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1248. } else {
  1249. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1250. }
  1251. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  1252. if wrap.CurrSubModule == "categories-add" {
  1253. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1254. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1255. {Name: "Add new category"},
  1256. })
  1257. } else {
  1258. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1259. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1260. {Name: "Modify category"},
  1261. })
  1262. }
  1263. data := utils.MySql_shop_category{
  1264. A_id: 0,
  1265. A_user: 0,
  1266. A_name: "",
  1267. A_alias: "",
  1268. A_lft: 0,
  1269. A_rgt: 0,
  1270. }
  1271. if wrap.CurrSubModule == "categories-modify" {
  1272. if len(wrap.UrlArgs) != 3 {
  1273. return "", "", ""
  1274. }
  1275. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1276. return "", "", ""
  1277. }
  1278. err := wrap.DB.QueryRow(`
  1279. SELECT
  1280. id,
  1281. user,
  1282. name,
  1283. alias,
  1284. lft,
  1285. rgt
  1286. FROM
  1287. shop_cats
  1288. WHERE
  1289. id = ?
  1290. LIMIT 1;`,
  1291. utils.StrToInt(wrap.UrlArgs[2]),
  1292. ).Scan(
  1293. &data.A_id,
  1294. &data.A_user,
  1295. &data.A_name,
  1296. &data.A_alias,
  1297. &data.A_lft,
  1298. &data.A_rgt,
  1299. )
  1300. if *wrap.LogCpError(&err) != nil {
  1301. return "", "", ""
  1302. }
  1303. }
  1304. btn_caption := "Add"
  1305. if wrap.CurrSubModule == "categories-modify" {
  1306. btn_caption = "Save"
  1307. }
  1308. parentId := 0
  1309. if wrap.CurrSubModule == "categories-modify" {
  1310. parentId = this.shop_GetCategoryParentId(wrap, data.A_id)
  1311. }
  1312. content += builder.DataForm(wrap, []builder.DataFormField{
  1313. {
  1314. Kind: builder.DFKHidden,
  1315. Name: "action",
  1316. Value: "shop-categories-modify",
  1317. },
  1318. {
  1319. Kind: builder.DFKHidden,
  1320. Name: "id",
  1321. Value: utils.IntToStr(data.A_id),
  1322. },
  1323. {
  1324. Kind: builder.DFKText,
  1325. Caption: "Parent",
  1326. Name: "parent",
  1327. Value: "0",
  1328. CallBack: func(field *builder.DataFormField) string {
  1329. return `<div class="form-group n2">` +
  1330. `<div class="row">` +
  1331. `<div class="col-md-3">` +
  1332. `<label for="lbl_parent">Parent</label>` +
  1333. `</div>` +
  1334. `<div class="col-md-9">` +
  1335. `<div>` +
  1336. `<select class="selectpicker form-control" id="lbl_parent" name="parent" data-live-search="true">` +
  1337. `<option title="Nothing selected" value="0">&mdash;</option>` +
  1338. this.shop_GetCategorySelectOptions(wrap, data.A_id, parentId, []int{}) +
  1339. `</select>` +
  1340. `</div>` +
  1341. `</div>` +
  1342. `</div>` +
  1343. `</div>`
  1344. },
  1345. },
  1346. {
  1347. Kind: builder.DFKText,
  1348. Caption: "Name",
  1349. Name: "name",
  1350. Value: data.A_name,
  1351. Required: true,
  1352. Min: "1",
  1353. Max: "255",
  1354. },
  1355. {
  1356. Kind: builder.DFKText,
  1357. Caption: "Alias",
  1358. Name: "alias",
  1359. Value: data.A_alias,
  1360. Hint: "Example: popular-products",
  1361. Max: "255",
  1362. },
  1363. {
  1364. Kind: builder.DFKSubmit,
  1365. Value: btn_caption,
  1366. Target: "add-edit-button",
  1367. },
  1368. })
  1369. if wrap.CurrSubModule == "categories-add" {
  1370. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1371. } else {
  1372. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1373. }
  1374. } else if wrap.CurrSubModule == "attributes-add" || wrap.CurrSubModule == "attributes-modify" {
  1375. if wrap.CurrSubModule == "attributes-add" {
  1376. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1377. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1378. {Name: "Add new attribute"},
  1379. })
  1380. } else {
  1381. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1382. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1383. {Name: "Modify attribute"},
  1384. })
  1385. }
  1386. data := utils.MySql_shop_filter{
  1387. A_id: 0,
  1388. A_name: "",
  1389. A_filter: "",
  1390. }
  1391. if wrap.CurrSubModule == "attributes-modify" {
  1392. if len(wrap.UrlArgs) != 3 {
  1393. return "", "", ""
  1394. }
  1395. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1396. return "", "", ""
  1397. }
  1398. err := wrap.DB.QueryRow(`
  1399. SELECT
  1400. id,
  1401. name,
  1402. filter
  1403. FROM
  1404. shop_filters
  1405. WHERE
  1406. id = ?
  1407. LIMIT 1;`,
  1408. utils.StrToInt(wrap.UrlArgs[2]),
  1409. ).Scan(
  1410. &data.A_id,
  1411. &data.A_name,
  1412. &data.A_filter,
  1413. )
  1414. if *wrap.LogCpError(&err) != nil {
  1415. return "", "", ""
  1416. }
  1417. }
  1418. btn_caption := "Add"
  1419. if wrap.CurrSubModule == "attributes-modify" {
  1420. btn_caption = "Save"
  1421. }
  1422. content += builder.DataForm(wrap, []builder.DataFormField{
  1423. {
  1424. Kind: builder.DFKHidden,
  1425. Name: "action",
  1426. Value: "shop-attributes-modify",
  1427. },
  1428. {
  1429. Kind: builder.DFKHidden,
  1430. Name: "id",
  1431. Value: utils.IntToStr(data.A_id),
  1432. },
  1433. {
  1434. Kind: builder.DFKText,
  1435. Caption: "Attribute name",
  1436. Name: "name",
  1437. Value: data.A_name,
  1438. Required: true,
  1439. Min: "1",
  1440. Max: "255",
  1441. },
  1442. {
  1443. Kind: builder.DFKText,
  1444. Caption: "Attribute in filter",
  1445. Name: "filter",
  1446. Value: data.A_filter,
  1447. Required: true,
  1448. Min: "1",
  1449. Max: "255",
  1450. },
  1451. {
  1452. Kind: builder.DFKText,
  1453. Caption: "Attribute values",
  1454. Name: "",
  1455. Value: "",
  1456. CallBack: func(field *builder.DataFormField) string {
  1457. return `<div class="form-group n4">` +
  1458. `<div class="row">` +
  1459. `<div class="col-md-3">` +
  1460. `<label>Attribute values</label>` +
  1461. `</div>` +
  1462. `<div class="col-md-9">` +
  1463. `<div class="list-wrapper">` +
  1464. `<div id="list">` +
  1465. this.shop_GetFilterValuesInputs(wrap, data.A_id) +
  1466. `</div>` +
  1467. `<div class="list-button"><button type="button" class="btn btn-success" onclick="fave.ShopAttributesAdd();">Add attribute value</button></div>` +
  1468. `</div>` +
  1469. `</div>` +
  1470. `</div>` +
  1471. `</div>`
  1472. },
  1473. },
  1474. {
  1475. Kind: builder.DFKSubmit,
  1476. Value: btn_caption,
  1477. Target: "add-edit-button",
  1478. },
  1479. })
  1480. if wrap.CurrSubModule == "attributes-add" {
  1481. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1482. } else {
  1483. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1484. }
  1485. } else if wrap.CurrSubModule == "currencies-add" || wrap.CurrSubModule == "currencies-modify" {
  1486. if wrap.CurrSubModule == "currencies-add" {
  1487. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1488. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1489. {Name: "Add new currency"},
  1490. })
  1491. } else {
  1492. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1493. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1494. {Name: "Modify currency"},
  1495. })
  1496. }
  1497. data := utils.MySql_shop_currency{
  1498. A_id: 0,
  1499. A_name: "",
  1500. A_coefficient: 0,
  1501. A_code: "",
  1502. A_symbol: "",
  1503. }
  1504. if wrap.CurrSubModule == "currencies-modify" {
  1505. if len(wrap.UrlArgs) != 3 {
  1506. return "", "", ""
  1507. }
  1508. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1509. return "", "", ""
  1510. }
  1511. err := wrap.DB.QueryRow(`
  1512. SELECT
  1513. id,
  1514. name,
  1515. coefficient,
  1516. code,
  1517. symbol
  1518. FROM
  1519. shop_currencies
  1520. WHERE
  1521. id = ?
  1522. LIMIT 1;`,
  1523. utils.StrToInt(wrap.UrlArgs[2]),
  1524. ).Scan(
  1525. &data.A_id,
  1526. &data.A_name,
  1527. &data.A_coefficient,
  1528. &data.A_code,
  1529. &data.A_symbol,
  1530. )
  1531. if *wrap.LogCpError(&err) != nil {
  1532. return "", "", ""
  1533. }
  1534. }
  1535. btn_caption := "Add"
  1536. if wrap.CurrSubModule == "currencies-modify" {
  1537. btn_caption = "Save"
  1538. }
  1539. content += builder.DataForm(wrap, []builder.DataFormField{
  1540. {
  1541. Kind: builder.DFKHidden,
  1542. Name: "action",
  1543. Value: "shop-currencies-modify",
  1544. },
  1545. {
  1546. Kind: builder.DFKHidden,
  1547. Name: "id",
  1548. Value: utils.IntToStr(data.A_id),
  1549. },
  1550. {
  1551. Kind: builder.DFKText,
  1552. Caption: "Currency name",
  1553. Name: "name",
  1554. Value: data.A_name,
  1555. Required: true,
  1556. Min: "1",
  1557. Max: "255",
  1558. },
  1559. {
  1560. Kind: builder.DFKText,
  1561. Caption: "Currency coefficient",
  1562. Name: "coefficient",
  1563. Value: "0",
  1564. CallBack: func(field *builder.DataFormField) string {
  1565. return `<div class="form-group n3">` +
  1566. `<div class="row">` +
  1567. `<div class="col-md-3">` +
  1568. `<label for="lbl_coefficient">Currency coefficient</label>` +
  1569. `</div>` +
  1570. `<div class="col-md-9">` +
  1571. `<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>` +
  1572. `</div>` +
  1573. `</div>` +
  1574. `</div>`
  1575. },
  1576. },
  1577. {
  1578. Kind: builder.DFKText,
  1579. Caption: "Currency code",
  1580. Name: "code",
  1581. Value: data.A_code,
  1582. Required: true,
  1583. Min: "1",
  1584. Max: "10",
  1585. },
  1586. {
  1587. Kind: builder.DFKText,
  1588. Caption: "Currency symbol",
  1589. Name: "symbol",
  1590. Value: data.A_symbol,
  1591. Required: true,
  1592. Min: "1",
  1593. Max: "5",
  1594. },
  1595. {
  1596. Kind: builder.DFKSubmit,
  1597. Value: btn_caption,
  1598. Target: "add-edit-button",
  1599. },
  1600. })
  1601. if wrap.CurrSubModule == "currencies-add" {
  1602. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1603. } else {
  1604. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1605. }
  1606. }
  1607. return this.getSidebarModules(wrap), content, sidebar
  1608. })
  1609. }