module_shop.go 38 KB

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