module_shop.go 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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) RegisterModule_Shop() *Module {
  242. return this.newModule(MInfo{
  243. WantDB: true,
  244. Mount: "shop",
  245. Name: "Shop",
  246. Order: 2,
  247. System: false,
  248. Icon: assets.SysSvgIconShop,
  249. Sub: &[]MISub{
  250. {Mount: "default", Name: "List of products", Show: true, Icon: assets.SysSvgIconList},
  251. {Mount: "add", Name: "Add new product", Show: true, Icon: assets.SysSvgIconPlus},
  252. {Mount: "modify", Name: "Modify product", Show: false},
  253. {Sep: true, Show: true},
  254. {Mount: "categories", Name: "List of categories", Show: true, Icon: assets.SysSvgIconList},
  255. {Mount: "categories-add", Name: "Add new category", Show: true, Icon: assets.SysSvgIconPlus},
  256. {Mount: "categories-modify", Name: "Modify category", Show: false},
  257. {Sep: true, Show: true},
  258. {Mount: "attributes", Name: "List of attributes", Show: true, Icon: assets.SysSvgIconList},
  259. {Mount: "attributes-add", Name: "Add new attribute", Show: true, Icon: assets.SysSvgIconPlus},
  260. {Mount: "attributes-modify", Name: "Modify attribute", Show: false},
  261. {Sep: true, Show: true},
  262. {Mount: "currencies", Name: "List of currencies", Show: true, Icon: assets.SysSvgIconList},
  263. {Mount: "currencies-add", Name: "Add new currency", Show: true, Icon: assets.SysSvgIconPlus},
  264. {Mount: "currencies-modify", Name: "Modify currency", Show: false},
  265. },
  266. }, func(wrap *wrapper.Wrapper) {
  267. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  268. // Shop category
  269. row := &utils.MySql_shop_category{}
  270. rou := &utils.MySql_user{}
  271. err := wrap.DB.QueryRow(`
  272. SELECT
  273. main.id,
  274. main.user,
  275. main.name,
  276. main.alias,
  277. main.lft,
  278. main.rgt,
  279. main.depth,
  280. parent.id AS parent_id,
  281. users.id,
  282. users.first_name,
  283. users.last_name,
  284. users.email,
  285. users.admin,
  286. users.active
  287. FROM
  288. (
  289. SELECT
  290. node.id,
  291. node.user,
  292. node.name,
  293. node.alias,
  294. node.lft,
  295. node.rgt,
  296. (COUNT(parent.id) - 1) AS depth
  297. FROM
  298. shop_cats AS node,
  299. shop_cats AS parent
  300. WHERE
  301. node.lft BETWEEN parent.lft AND parent.rgt
  302. GROUP BY
  303. node.id
  304. ORDER BY
  305. node.lft ASC
  306. ) AS main
  307. LEFT JOIN (
  308. SELECT
  309. node.id,
  310. node.user,
  311. node.name,
  312. node.alias,
  313. node.lft,
  314. node.rgt,
  315. (COUNT(parent.id) - 0) AS depth
  316. FROM
  317. shop_cats AS node,
  318. shop_cats AS parent
  319. WHERE
  320. node.lft BETWEEN parent.lft AND parent.rgt
  321. GROUP BY
  322. node.id
  323. ORDER BY
  324. node.lft ASC
  325. ) AS parent ON
  326. parent.depth = main.depth AND
  327. main.lft > parent.lft AND
  328. main.rgt < parent.rgt
  329. LEFT JOIN users ON users.id = main.user
  330. WHERE
  331. main.id > 1 AND
  332. main.alias = ?
  333. ORDER BY
  334. main.lft ASC
  335. ;`,
  336. wrap.UrlArgs[2],
  337. ).Scan(
  338. &row.A_id,
  339. &row.A_user,
  340. &row.A_name,
  341. &row.A_alias,
  342. &row.A_lft,
  343. &row.A_rgt,
  344. &row.A_depth,
  345. &row.A_parent,
  346. &rou.A_id,
  347. &rou.A_first_name,
  348. &rou.A_last_name,
  349. &rou.A_email,
  350. &rou.A_admin,
  351. &rou.A_active,
  352. )
  353. if err != nil && err != wrapper.ErrNoRows {
  354. // System error 500
  355. wrap.LogCpError(&err)
  356. utils.SystemErrorPageEngine(wrap.W, err)
  357. return
  358. } else if err == wrapper.ErrNoRows {
  359. // User error 404 page
  360. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  361. return
  362. }
  363. // Fix url
  364. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  365. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  366. return
  367. }
  368. // Render template
  369. wrap.RenderFrontEnd("shop-category", fetdata.New(wrap, false, row, rou), http.StatusOK)
  370. return
  371. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {
  372. // Shop product
  373. row := &utils.MySql_shop_product{}
  374. rou := &utils.MySql_user{}
  375. err := wrap.DB.QueryRow(`
  376. SELECT
  377. shop_products.id,
  378. shop_products.user,
  379. shop_products.currency,
  380. shop_products.price,
  381. shop_products.name,
  382. shop_products.alias,
  383. shop_products.vendor,
  384. shop_products.quantity,
  385. shop_products.category,
  386. shop_products.briefly,
  387. shop_products.content,
  388. UNIX_TIMESTAMP(shop_products.datetime) as datetime,
  389. shop_products.active,
  390. users.id,
  391. users.first_name,
  392. users.last_name,
  393. users.email,
  394. users.admin,
  395. users.active
  396. FROM
  397. shop_products
  398. LEFT JOIN users ON users.id = shop_products.user
  399. WHERE
  400. shop_products.active = 1 and
  401. shop_products.alias = ?
  402. LIMIT 1;`,
  403. wrap.UrlArgs[1],
  404. ).Scan(
  405. &row.A_id,
  406. &row.A_user,
  407. &row.A_currency,
  408. &row.A_price,
  409. &row.A_name,
  410. &row.A_alias,
  411. &row.A_vendor,
  412. &row.A_quantity,
  413. &row.A_category,
  414. &row.A_briefly,
  415. &row.A_content,
  416. &row.A_datetime,
  417. &row.A_active,
  418. &rou.A_id,
  419. &rou.A_first_name,
  420. &rou.A_last_name,
  421. &rou.A_email,
  422. &rou.A_admin,
  423. &rou.A_active,
  424. )
  425. if err != nil && err != wrapper.ErrNoRows {
  426. // System error 500
  427. wrap.LogCpError(&err)
  428. utils.SystemErrorPageEngine(wrap.W, err)
  429. return
  430. } else if err == wrapper.ErrNoRows {
  431. // User error 404 page
  432. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  433. return
  434. }
  435. // Fix url
  436. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  437. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  438. return
  439. }
  440. // Render template
  441. wrap.RenderFrontEnd("shop-product", fetdata.New(wrap, false, row, rou), http.StatusOK)
  442. return
  443. } else if len(wrap.UrlArgs) == 1 && wrap.UrlArgs[0] == "shop" {
  444. // Shop
  445. // Fix url
  446. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  447. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  448. return
  449. }
  450. // Render template
  451. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  452. return
  453. } else if (*wrap.Config).Engine.MainModule == 2 {
  454. // Render template
  455. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  456. return
  457. }
  458. // User error 404 page
  459. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  460. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  461. content := ""
  462. sidebar := ""
  463. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  464. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  465. {Name: "List of products"},
  466. })
  467. // Load currencies
  468. currencies := this.shop_GetAllCurrencies(wrap)
  469. content += builder.DataTable(
  470. wrap,
  471. "shop_products",
  472. "id",
  473. "DESC",
  474. &[]builder.DataTableRow{
  475. {
  476. DBField: "id",
  477. },
  478. {
  479. DBField: "name",
  480. NameInTable: "Product / URL",
  481. CallBack: func(values *[]string) string {
  482. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  483. alias := html.EscapeString((*values)[2])
  484. parent := ``
  485. if (*values)[7] != "" {
  486. parent = `<div class="parent">&uarr;<small><a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[7] + `/">` + html.EscapeString((*values)[8]) + `</a></small></div>`
  487. }
  488. return `<div>` + name + `</div><div><small>/shop/` + alias + `/</small></div>` + parent
  489. },
  490. },
  491. {
  492. DBField: "alias",
  493. },
  494. {
  495. DBField: "currency",
  496. },
  497. {
  498. DBField: "price",
  499. NameInTable: "Price",
  500. Classes: "d-none d-md-table-cell",
  501. CallBack: func(values *[]string) string {
  502. return `<div>` + utils.Float64ToStr(utils.StrToFloat64((*values)[4])) + `</div>` +
  503. `<div><small>` + currencies[utils.StrToInt((*values)[3])] + `</small></div>`
  504. },
  505. },
  506. {
  507. DBField: "datetime",
  508. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  509. NameInTable: "Date / Time",
  510. Classes: "d-none d-lg-table-cell",
  511. CallBack: func(values *[]string) string {
  512. t := int64(utils.StrToInt((*values)[5]))
  513. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  514. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  515. },
  516. },
  517. {
  518. DBField: "active",
  519. NameInTable: "Active",
  520. Classes: "d-none d-sm-table-cell",
  521. CallBack: func(values *[]string) string {
  522. return builder.CheckBox(utils.StrToInt((*values)[6]))
  523. },
  524. },
  525. {
  526. DBField: "parent_id",
  527. },
  528. {
  529. DBField: "pname",
  530. DBExp: "spp.name",
  531. },
  532. },
  533. func(values *[]string) string {
  534. return builder.DataTableAction(&[]builder.DataTableActionRow{
  535. {
  536. Icon: assets.SysSvgIconView,
  537. Href: `/shop/` + (*values)[2] + `/`,
  538. Hint: "View",
  539. Target: "_blank",
  540. },
  541. {
  542. Icon: assets.SysSvgIconEdit,
  543. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  544. Hint: "Edit",
  545. },
  546. {
  547. Icon: assets.SysSvgIconRemove,
  548. Href: "javascript:fave.ActionDataTableDelete(this,'shop-delete','" +
  549. (*values)[0] + "','Are you sure want to delete product?');",
  550. Hint: "Delete",
  551. Classes: "delete",
  552. },
  553. })
  554. },
  555. "/cp/"+wrap.CurrModule+"/",
  556. func() (int, error) {
  557. var count int
  558. return count, wrap.DB.QueryRow(
  559. "SELECT COUNT(*) FROM `shop_products`;",
  560. ).Scan(&count)
  561. },
  562. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  563. return wrap.DB.Query(
  564. `SELECT
  565. shop_products.id,
  566. shop_products.name,
  567. shop_products.alias,
  568. shop_products.currency,
  569. shop_products.price,
  570. UNIX_TIMESTAMP(`+"`shop_products`.`datetime`"+`) AS datetime,
  571. shop_products.active,
  572. shop_products.parent_id,
  573. spp.name AS pname
  574. FROM
  575. shop_products
  576. LEFT JOIN shop_products AS spp ON spp.id = shop_products.parent_id
  577. ORDER BY
  578. shop_products.id DESC
  579. LIMIT ?, ?;`,
  580. limit_offset,
  581. pear_page,
  582. )
  583. },
  584. true,
  585. )
  586. } else if wrap.CurrSubModule == "categories" {
  587. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  588. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  589. {Name: "List of categories"},
  590. })
  591. content += builder.DataTable(
  592. wrap,
  593. "shop_cats",
  594. "id",
  595. "ASC",
  596. &[]builder.DataTableRow{
  597. {
  598. DBField: "id",
  599. },
  600. {
  601. DBField: "user",
  602. },
  603. {
  604. DBField: "name",
  605. NameInTable: "Category",
  606. CallBack: func(values *[]string) string {
  607. depth := utils.StrToInt((*values)[4]) - 1
  608. if depth < 0 {
  609. depth = 0
  610. }
  611. sub := strings.Repeat("&mdash; ", depth)
  612. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  613. return `<div>` + name + `</div>`
  614. },
  615. },
  616. {
  617. DBField: "alias",
  618. },
  619. {
  620. DBField: "depth",
  621. },
  622. },
  623. func(values *[]string) string {
  624. return builder.DataTableAction(&[]builder.DataTableActionRow{
  625. {
  626. Icon: assets.SysSvgIconView,
  627. Href: `/shop/category/` + (*values)[3] + `/`,
  628. Hint: "View",
  629. Target: "_blank",
  630. },
  631. {
  632. Icon: assets.SysSvgIconEdit,
  633. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  634. Hint: "Edit",
  635. },
  636. {
  637. Icon: assets.SysSvgIconRemove,
  638. Href: "javascript:fave.ActionDataTableDelete(this,'shop-categories-delete','" +
  639. (*values)[0] + "','Are you sure want to delete category?');",
  640. Hint: "Delete",
  641. Classes: "delete",
  642. },
  643. })
  644. },
  645. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  646. nil,
  647. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  648. return wrap.DB.Query(
  649. `SELECT
  650. node.id,
  651. node.user,
  652. node.name,
  653. node.alias,
  654. (COUNT(parent.id) - 1) AS depth
  655. FROM
  656. shop_cats AS node,
  657. shop_cats AS parent
  658. WHERE
  659. node.lft BETWEEN parent.lft AND parent.rgt AND
  660. node.id > 1
  661. GROUP BY
  662. node.id
  663. ORDER BY
  664. node.lft ASC
  665. ;`,
  666. )
  667. },
  668. false,
  669. )
  670. } else if wrap.CurrSubModule == "attributes" {
  671. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  672. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  673. {Name: "List of attributes"},
  674. })
  675. content += builder.DataTable(
  676. wrap,
  677. "shop_filters",
  678. "id",
  679. "DESC",
  680. &[]builder.DataTableRow{
  681. {
  682. DBField: "id",
  683. },
  684. {
  685. DBField: "name",
  686. NameInTable: "Name",
  687. CallBack: func(values *[]string) string {
  688. name := `<a href="/cp/` + wrap.CurrModule + `/attributes-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  689. return `<div>` + name + `</div><div><small>` + html.EscapeString((*values)[2]) + `</small></div>`
  690. },
  691. },
  692. {
  693. DBField: "filter",
  694. },
  695. },
  696. func(values *[]string) string {
  697. return builder.DataTableAction(&[]builder.DataTableActionRow{
  698. {
  699. Icon: assets.SysSvgIconEdit,
  700. Href: "/cp/" + wrap.CurrModule + "/attributes-modify/" + (*values)[0] + "/",
  701. Hint: "Edit",
  702. },
  703. {
  704. Icon: assets.SysSvgIconRemove,
  705. Href: "javascript:fave.ActionDataTableDelete(this,'shop-attributes-delete','" +
  706. (*values)[0] + "','Are you sure want to delete attribute?');",
  707. Hint: "Delete",
  708. Classes: "delete",
  709. },
  710. })
  711. },
  712. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  713. nil,
  714. nil,
  715. true,
  716. )
  717. } else if wrap.CurrSubModule == "currencies" {
  718. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  719. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  720. {Name: "List of currencies"},
  721. })
  722. content += builder.DataTable(
  723. wrap,
  724. "shop_currencies",
  725. "id",
  726. "DESC",
  727. &[]builder.DataTableRow{
  728. {
  729. DBField: "id",
  730. },
  731. {
  732. DBField: "name",
  733. NameInTable: "Name",
  734. CallBack: func(values *[]string) string {
  735. name := `<a href="/cp/` + wrap.CurrModule + `/currencies-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + ` (` + (*values)[3] + `, ` + (*values)[4] + `)</a>`
  736. return `<div>` + name + `</div>`
  737. },
  738. },
  739. {
  740. DBField: "coefficient",
  741. NameInTable: "Coefficient",
  742. Classes: "d-none d-md-table-cell",
  743. CallBack: func(values *[]string) string {
  744. return utils.Float64ToStrF(utils.StrToFloat64((*values)[2]), "%.4f")
  745. },
  746. },
  747. {
  748. DBField: "code",
  749. },
  750. {
  751. DBField: "symbol",
  752. },
  753. },
  754. func(values *[]string) string {
  755. return builder.DataTableAction(&[]builder.DataTableActionRow{
  756. {
  757. Icon: assets.SysSvgIconEdit,
  758. Href: "/cp/" + wrap.CurrModule + "/currencies-modify/" + (*values)[0] + "/",
  759. Hint: "Edit",
  760. },
  761. {
  762. Icon: assets.SysSvgIconRemove,
  763. Href: "javascript:fave.ActionDataTableDelete(this,'shop-currencies-delete','" +
  764. (*values)[0] + "','Are you sure want to delete currency?');",
  765. Hint: "Delete",
  766. Classes: "delete",
  767. },
  768. })
  769. },
  770. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  771. nil,
  772. nil,
  773. true,
  774. )
  775. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  776. if wrap.CurrSubModule == "add" {
  777. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  778. {Name: "Add new product"},
  779. })
  780. } else {
  781. if len(wrap.UrlArgs) >= 3 && utils.IsNumeric(wrap.UrlArgs[2]) {
  782. content += `<div class="product-copy"><a title="Duplicate product" href="javascript:fave.ShopProductsDuplicate(this, ` + wrap.UrlArgs[2] + `);">` + assets.SysSvgIconCopy + `</a></div>`
  783. content += `<div class="product-another"><a title="The same with another attributes" href="javascript:fave.ShopProductsAnother(this, ` + wrap.UrlArgs[2] + `);">` + assets.SysSvgIconPlus + `</a></div>`
  784. }
  785. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  786. {Name: "Modify product"},
  787. })
  788. }
  789. data := utils.MySql_shop_product{
  790. A_id: 0,
  791. A_user: 0,
  792. A_currency: 0,
  793. A_price: 0,
  794. A_name: "",
  795. A_alias: "",
  796. A_vendor: "",
  797. A_quantity: 0,
  798. A_category: 0,
  799. A_briefly: "",
  800. A_content: "",
  801. A_datetime: 0,
  802. A_active: 0,
  803. }
  804. if wrap.CurrSubModule == "modify" {
  805. if len(wrap.UrlArgs) != 3 {
  806. return "", "", ""
  807. }
  808. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  809. return "", "", ""
  810. }
  811. err := wrap.DB.QueryRow(`
  812. SELECT
  813. id,
  814. parent_id,
  815. user,
  816. currency,
  817. price,
  818. name,
  819. alias,
  820. vendor,
  821. quantity,
  822. category,
  823. briefly,
  824. content,
  825. active
  826. FROM
  827. shop_products
  828. WHERE
  829. id = ?
  830. LIMIT 1;`,
  831. utils.StrToInt(wrap.UrlArgs[2]),
  832. ).Scan(
  833. &data.A_id,
  834. &data.A_parent,
  835. &data.A_user,
  836. &data.A_currency,
  837. &data.A_price,
  838. &data.A_name,
  839. &data.A_alias,
  840. &data.A_vendor,
  841. &data.A_quantity,
  842. &data.A_category,
  843. &data.A_briefly,
  844. &data.A_content,
  845. &data.A_active,
  846. )
  847. if *wrap.LogCpError(&err) != nil {
  848. return "", "", ""
  849. }
  850. }
  851. // All product current categories
  852. var selids []int
  853. if data.A_id > 0 {
  854. rows, err := wrap.DB.Query("SELECT category_id FROM shop_cat_product_rel WHERE product_id = ?;", data.A_id)
  855. if err == nil {
  856. defer rows.Close()
  857. values := make([]int, 1)
  858. scan := make([]interface{}, len(values))
  859. for i := range values {
  860. scan[i] = &values[i]
  861. }
  862. for rows.Next() {
  863. err = rows.Scan(scan...)
  864. if *wrap.LogCpError(&err) == nil {
  865. selids = append(selids, int(values[0]))
  866. }
  867. }
  868. }
  869. }
  870. btn_caption := "Add"
  871. if wrap.CurrSubModule == "modify" {
  872. btn_caption = "Save"
  873. }
  874. content += builder.DataForm(wrap, []builder.DataFormField{
  875. {
  876. Kind: builder.DFKHidden,
  877. Name: "action",
  878. Value: "shop-modify",
  879. },
  880. {
  881. Kind: builder.DFKHidden,
  882. Name: "id",
  883. Value: utils.IntToStr(data.A_id),
  884. },
  885. {
  886. Kind: builder.DFKText,
  887. Caption: "Product name",
  888. Name: "name",
  889. Value: data.A_name,
  890. Required: true,
  891. Min: "1",
  892. Max: "255",
  893. },
  894. {
  895. Kind: builder.DFKText,
  896. Caption: "Product price",
  897. Name: "price",
  898. Value: "0",
  899. CallBack: func(field *builder.DataFormField) string {
  900. return `<div class="form-group n3">` +
  901. `<div class="row">` +
  902. `<div class="col-md-3">` +
  903. `<label for="lbl_price">Product price</label>` +
  904. `</div>` +
  905. `<div class="col-md-9">` +
  906. `<div>` +
  907. `<div class="row">` +
  908. `<div class="col-md-8">` +
  909. `<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>` +
  910. `<div class="d-md-none mb-3"></div>` +
  911. `</div>` +
  912. `<div class="col-md-4">` +
  913. `<select class="selectpicker form-control" id="lbl_currency" name="currency" data-live-search="true">` +
  914. this.shop_GetCurrencySelectOptions(wrap, data.A_currency) +
  915. `</select>` +
  916. `</div>` +
  917. `</div>` +
  918. `</div>` +
  919. `</div>` +
  920. `</div>` +
  921. `</div>`
  922. },
  923. },
  924. {
  925. Kind: builder.DFKText,
  926. Caption: "Product alias",
  927. Name: "alias",
  928. Value: data.A_alias,
  929. Hint: "Example: mobile-phone",
  930. Max: "255",
  931. },
  932. {
  933. Kind: builder.DFKText,
  934. Caption: "Vendor/Count",
  935. Name: "vendor",
  936. Value: "0",
  937. CallBack: func(field *builder.DataFormField) string {
  938. return `<div class="form-group n5">` +
  939. `<div class="row">` +
  940. `<div class="col-md-3">` +
  941. `<label for="lbl_vendor">Vendor/Count</label>` +
  942. `</div>` +
  943. `<div class="col-md-9">` +
  944. `<div>` +
  945. `<div class="row">` +
  946. `<div class="col-md-8">` +
  947. `<div><input class="form-control" type="text" id="lbl_vendor" name="vendor" value="` + html.EscapeString(data.A_vendor) + `" placeholder="" autocomplete="off"></div>` +
  948. `<div class="d-md-none mb-3"></div>` +
  949. `</div>` +
  950. `<div class="col-md-4">` +
  951. `<input class="form-control" type="number" step="1" id="lbl_quantity" name="quantity" value="` + utils.IntToStr(data.A_quantity) + `" placeholder="" autocomplete="off">` +
  952. `</div>` +
  953. `</div>` +
  954. `</div>` +
  955. `</div>` +
  956. `</div>` +
  957. `</div>`
  958. },
  959. },
  960. {
  961. Kind: builder.DFKText,
  962. Caption: "Category",
  963. Name: "category",
  964. Value: "0",
  965. CallBack: func(field *builder.DataFormField) string {
  966. return `<div class="form-group n6">` +
  967. `<div class="row">` +
  968. `<div class="col-md-3">` +
  969. `<label for="lbl_category">Category</label>` +
  970. `</div>` +
  971. `<div class="col-md-9">` +
  972. `<div>` +
  973. `<select class="selectpicker form-control" id="lbl_category" name="category" data-live-search="true">` +
  974. `<option title="Nothing selected" value="0">&mdash;</option>` +
  975. this.shop_GetCategorySelectOptions(wrap, 0, data.A_category, []int{}) +
  976. `</select>` +
  977. `</div>` +
  978. `</div>` +
  979. `</div>` +
  980. `</div>`
  981. },
  982. },
  983. {
  984. Kind: builder.DFKText,
  985. Caption: "Categories",
  986. Name: "cats",
  987. Value: "0",
  988. CallBack: func(field *builder.DataFormField) string {
  989. return `<div class="form-group n7">` +
  990. `<div class="row">` +
  991. `<div class="col-md-3">` +
  992. `<label for="lbl_cats">Categories</label>` +
  993. `</div>` +
  994. `<div class="col-md-9">` +
  995. `<div>` +
  996. `<select class="selectpicker form-control" id="lbl_cats" name="cats[]" data-live-search="true" multiple>` +
  997. this.shop_GetCategorySelectOptions(wrap, 0, 0, selids) +
  998. `</select>` +
  999. `</div>` +
  1000. `</div>` +
  1001. `</div>` +
  1002. `</div>`
  1003. },
  1004. },
  1005. {
  1006. Kind: builder.DFKText,
  1007. Caption: "Attributes",
  1008. Name: "",
  1009. Value: "",
  1010. CallBack: func(field *builder.DataFormField) string {
  1011. return `<div class="form-group n8">` +
  1012. `<div class="row">` +
  1013. `<div class="col-md-3">` +
  1014. `<label>Attributes</label>` +
  1015. `</div>` +
  1016. `<div class="col-md-9">` +
  1017. `<div class="list-wrapper">` +
  1018. `<div id="list">` +
  1019. this.shop_GetProductValuesInputs(wrap, data.A_id) +
  1020. `</div>` +
  1021. `<div class="list-button position-relative">` +
  1022. `<select class="selectpicker form-control" id="lbl_attributes" data-live-search="true" onchange="fave.ShopProductsAdd();">` +
  1023. this.shop_GetAllAttributesSelectOptions(wrap) +
  1024. `</select>` +
  1025. `</div>` +
  1026. `</div>` +
  1027. `</div>` +
  1028. `</div>` +
  1029. `</div>`
  1030. },
  1031. },
  1032. {
  1033. Kind: builder.DFKTextArea,
  1034. Caption: "Briefly",
  1035. Name: "briefly",
  1036. Value: data.A_briefly,
  1037. Classes: "briefly wysiwyg",
  1038. },
  1039. {
  1040. Kind: builder.DFKTextArea,
  1041. Caption: "Product content",
  1042. Name: "content",
  1043. Value: data.A_content,
  1044. Classes: "wysiwyg",
  1045. },
  1046. {
  1047. Kind: builder.DFKText,
  1048. Caption: "Product images",
  1049. Name: "",
  1050. Value: "",
  1051. CallBack: func(field *builder.DataFormField) string {
  1052. if data.A_id == 0 {
  1053. return ``
  1054. }
  1055. return `<div class="form-group n11">` +
  1056. `<div class="row">` +
  1057. `<div class="col-md-3">` +
  1058. `<label>Product images</label>` +
  1059. `</div>` +
  1060. `<div class="col-md-9">` +
  1061. `<div class="list-wrapper">` +
  1062. `<div id="list-images">` +
  1063. this.shop_GetAllProductImages(wrap, data.A_id) +
  1064. `</div>` +
  1065. `<div id="img-upload-block" class="list-button position-relative">` +
  1066. `<div id="upload-msg">Uploading...</div>` +
  1067. `<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:13px;" multiple />` +
  1068. `</div>` +
  1069. `</div>` +
  1070. `</div>` +
  1071. `</div>` +
  1072. `</div>` +
  1073. `<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>`
  1074. },
  1075. },
  1076. {
  1077. Kind: builder.DFKCheckBox,
  1078. Caption: "Active",
  1079. Name: "active",
  1080. Value: utils.IntToStr(data.A_active),
  1081. },
  1082. {
  1083. Kind: builder.DFKSubmit,
  1084. Value: btn_caption,
  1085. Target: "add-edit-button",
  1086. },
  1087. })
  1088. if wrap.CurrSubModule == "add" {
  1089. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1090. } else {
  1091. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1092. }
  1093. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  1094. if wrap.CurrSubModule == "categories-add" {
  1095. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1096. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1097. {Name: "Add new category"},
  1098. })
  1099. } else {
  1100. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1101. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1102. {Name: "Modify category"},
  1103. })
  1104. }
  1105. data := utils.MySql_shop_category{
  1106. A_id: 0,
  1107. A_user: 0,
  1108. A_name: "",
  1109. A_alias: "",
  1110. A_lft: 0,
  1111. A_rgt: 0,
  1112. }
  1113. if wrap.CurrSubModule == "categories-modify" {
  1114. if len(wrap.UrlArgs) != 3 {
  1115. return "", "", ""
  1116. }
  1117. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1118. return "", "", ""
  1119. }
  1120. err := wrap.DB.QueryRow(`
  1121. SELECT
  1122. id,
  1123. user,
  1124. name,
  1125. alias,
  1126. lft,
  1127. rgt
  1128. FROM
  1129. shop_cats
  1130. WHERE
  1131. id = ?
  1132. LIMIT 1;`,
  1133. utils.StrToInt(wrap.UrlArgs[2]),
  1134. ).Scan(
  1135. &data.A_id,
  1136. &data.A_user,
  1137. &data.A_name,
  1138. &data.A_alias,
  1139. &data.A_lft,
  1140. &data.A_rgt,
  1141. )
  1142. if *wrap.LogCpError(&err) != nil {
  1143. return "", "", ""
  1144. }
  1145. }
  1146. btn_caption := "Add"
  1147. if wrap.CurrSubModule == "categories-modify" {
  1148. btn_caption = "Save"
  1149. }
  1150. parentId := 0
  1151. if wrap.CurrSubModule == "categories-modify" {
  1152. parentId = this.shop_GetCategoryParentId(wrap, data.A_id)
  1153. }
  1154. content += builder.DataForm(wrap, []builder.DataFormField{
  1155. {
  1156. Kind: builder.DFKHidden,
  1157. Name: "action",
  1158. Value: "shop-categories-modify",
  1159. },
  1160. {
  1161. Kind: builder.DFKHidden,
  1162. Name: "id",
  1163. Value: utils.IntToStr(data.A_id),
  1164. },
  1165. {
  1166. Kind: builder.DFKText,
  1167. Caption: "Parent",
  1168. Name: "parent",
  1169. Value: "0",
  1170. CallBack: func(field *builder.DataFormField) string {
  1171. return `<div class="form-group n2">` +
  1172. `<div class="row">` +
  1173. `<div class="col-md-3">` +
  1174. `<label for="lbl_parent">Parent</label>` +
  1175. `</div>` +
  1176. `<div class="col-md-9">` +
  1177. `<div>` +
  1178. `<select class="selectpicker form-control" id="lbl_parent" name="parent" data-live-search="true">` +
  1179. `<option title="Nothing selected" value="0">&mdash;</option>` +
  1180. this.shop_GetCategorySelectOptions(wrap, data.A_id, parentId, []int{}) +
  1181. `</select>` +
  1182. `</div>` +
  1183. `</div>` +
  1184. `</div>` +
  1185. `</div>`
  1186. },
  1187. },
  1188. {
  1189. Kind: builder.DFKText,
  1190. Caption: "Name",
  1191. Name: "name",
  1192. Value: data.A_name,
  1193. Required: true,
  1194. Min: "1",
  1195. Max: "255",
  1196. },
  1197. {
  1198. Kind: builder.DFKText,
  1199. Caption: "Alias",
  1200. Name: "alias",
  1201. Value: data.A_alias,
  1202. Hint: "Example: popular-products",
  1203. Max: "255",
  1204. },
  1205. {
  1206. Kind: builder.DFKSubmit,
  1207. Value: btn_caption,
  1208. Target: "add-edit-button",
  1209. },
  1210. })
  1211. if wrap.CurrSubModule == "categories-add" {
  1212. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1213. } else {
  1214. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1215. }
  1216. } else if wrap.CurrSubModule == "attributes-add" || wrap.CurrSubModule == "attributes-modify" {
  1217. if wrap.CurrSubModule == "attributes-add" {
  1218. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1219. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1220. {Name: "Add new attribute"},
  1221. })
  1222. } else {
  1223. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1224. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1225. {Name: "Modify attribute"},
  1226. })
  1227. }
  1228. data := utils.MySql_shop_filter{
  1229. A_id: 0,
  1230. A_name: "",
  1231. A_filter: "",
  1232. }
  1233. if wrap.CurrSubModule == "attributes-modify" {
  1234. if len(wrap.UrlArgs) != 3 {
  1235. return "", "", ""
  1236. }
  1237. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1238. return "", "", ""
  1239. }
  1240. err := wrap.DB.QueryRow(`
  1241. SELECT
  1242. id,
  1243. name,
  1244. filter
  1245. FROM
  1246. shop_filters
  1247. WHERE
  1248. id = ?
  1249. LIMIT 1;`,
  1250. utils.StrToInt(wrap.UrlArgs[2]),
  1251. ).Scan(
  1252. &data.A_id,
  1253. &data.A_name,
  1254. &data.A_filter,
  1255. )
  1256. if *wrap.LogCpError(&err) != nil {
  1257. return "", "", ""
  1258. }
  1259. }
  1260. btn_caption := "Add"
  1261. if wrap.CurrSubModule == "attributes-modify" {
  1262. btn_caption = "Save"
  1263. }
  1264. content += builder.DataForm(wrap, []builder.DataFormField{
  1265. {
  1266. Kind: builder.DFKHidden,
  1267. Name: "action",
  1268. Value: "shop-attributes-modify",
  1269. },
  1270. {
  1271. Kind: builder.DFKHidden,
  1272. Name: "id",
  1273. Value: utils.IntToStr(data.A_id),
  1274. },
  1275. {
  1276. Kind: builder.DFKText,
  1277. Caption: "Attribute name",
  1278. Name: "name",
  1279. Value: data.A_name,
  1280. Required: true,
  1281. Min: "1",
  1282. Max: "255",
  1283. },
  1284. {
  1285. Kind: builder.DFKText,
  1286. Caption: "Attribute in filter",
  1287. Name: "filter",
  1288. Value: data.A_filter,
  1289. Required: true,
  1290. Min: "1",
  1291. Max: "255",
  1292. },
  1293. {
  1294. Kind: builder.DFKText,
  1295. Caption: "Attribute values",
  1296. Name: "",
  1297. Value: "",
  1298. CallBack: func(field *builder.DataFormField) string {
  1299. return `<div class="form-group n4">` +
  1300. `<div class="row">` +
  1301. `<div class="col-md-3">` +
  1302. `<label>Attribute values</label>` +
  1303. `</div>` +
  1304. `<div class="col-md-9">` +
  1305. `<div class="list-wrapper">` +
  1306. `<div id="list">` +
  1307. this.shop_GetFilterValuesInputs(wrap, data.A_id) +
  1308. `</div>` +
  1309. `<div class="list-button"><button type="button" class="btn btn-success" onclick="fave.ShopAttributesAdd();">Add attribute value</button></div>` +
  1310. `</div>` +
  1311. `</div>` +
  1312. `</div>` +
  1313. `</div>`
  1314. },
  1315. },
  1316. {
  1317. Kind: builder.DFKSubmit,
  1318. Value: btn_caption,
  1319. Target: "add-edit-button",
  1320. },
  1321. })
  1322. if wrap.CurrSubModule == "attributes-add" {
  1323. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1324. } else {
  1325. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1326. }
  1327. } else if wrap.CurrSubModule == "currencies-add" || wrap.CurrSubModule == "currencies-modify" {
  1328. if wrap.CurrSubModule == "currencies-add" {
  1329. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1330. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1331. {Name: "Add new currency"},
  1332. })
  1333. } else {
  1334. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1335. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1336. {Name: "Modify currency"},
  1337. })
  1338. }
  1339. data := utils.MySql_shop_currency{
  1340. A_id: 0,
  1341. A_name: "",
  1342. A_coefficient: 0,
  1343. A_code: "",
  1344. A_symbol: "",
  1345. }
  1346. if wrap.CurrSubModule == "currencies-modify" {
  1347. if len(wrap.UrlArgs) != 3 {
  1348. return "", "", ""
  1349. }
  1350. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1351. return "", "", ""
  1352. }
  1353. err := wrap.DB.QueryRow(`
  1354. SELECT
  1355. id,
  1356. name,
  1357. coefficient,
  1358. code,
  1359. symbol
  1360. FROM
  1361. shop_currencies
  1362. WHERE
  1363. id = ?
  1364. LIMIT 1;`,
  1365. utils.StrToInt(wrap.UrlArgs[2]),
  1366. ).Scan(
  1367. &data.A_id,
  1368. &data.A_name,
  1369. &data.A_coefficient,
  1370. &data.A_code,
  1371. &data.A_symbol,
  1372. )
  1373. if *wrap.LogCpError(&err) != nil {
  1374. return "", "", ""
  1375. }
  1376. }
  1377. btn_caption := "Add"
  1378. if wrap.CurrSubModule == "currencies-modify" {
  1379. btn_caption = "Save"
  1380. }
  1381. content += builder.DataForm(wrap, []builder.DataFormField{
  1382. {
  1383. Kind: builder.DFKHidden,
  1384. Name: "action",
  1385. Value: "shop-currencies-modify",
  1386. },
  1387. {
  1388. Kind: builder.DFKHidden,
  1389. Name: "id",
  1390. Value: utils.IntToStr(data.A_id),
  1391. },
  1392. {
  1393. Kind: builder.DFKText,
  1394. Caption: "Currency name",
  1395. Name: "name",
  1396. Value: data.A_name,
  1397. Required: true,
  1398. Min: "1",
  1399. Max: "255",
  1400. },
  1401. {
  1402. Kind: builder.DFKText,
  1403. Caption: "Currency coefficient",
  1404. Name: "coefficient",
  1405. Value: "0",
  1406. CallBack: func(field *builder.DataFormField) string {
  1407. return `<div class="form-group n3">` +
  1408. `<div class="row">` +
  1409. `<div class="col-md-3">` +
  1410. `<label for="lbl_coefficient">Currency coefficient</label>` +
  1411. `</div>` +
  1412. `<div class="col-md-9">` +
  1413. `<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>` +
  1414. `</div>` +
  1415. `</div>` +
  1416. `</div>`
  1417. },
  1418. },
  1419. {
  1420. Kind: builder.DFKText,
  1421. Caption: "Currency code",
  1422. Name: "code",
  1423. Value: data.A_code,
  1424. Required: true,
  1425. Min: "1",
  1426. Max: "10",
  1427. },
  1428. {
  1429. Kind: builder.DFKText,
  1430. Caption: "Currency symbol",
  1431. Name: "symbol",
  1432. Value: data.A_symbol,
  1433. Required: true,
  1434. Min: "1",
  1435. Max: "5",
  1436. },
  1437. {
  1438. Kind: builder.DFKSubmit,
  1439. Value: btn_caption,
  1440. Target: "add-edit-button",
  1441. },
  1442. })
  1443. if wrap.CurrSubModule == "currencies-add" {
  1444. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1445. } else {
  1446. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1447. }
  1448. }
  1449. return this.getSidebarModules(wrap), content, sidebar
  1450. })
  1451. }