module_shop.go 40 KB

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