module_shop.go 39 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
  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="/products/images/` + string(values[0]) + `/thumb-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. rou := &utils.MySql_user{}
  270. err := wrap.DB.QueryRow(`
  271. SELECT
  272. main.id,
  273. main.user,
  274. main.name,
  275. main.alias,
  276. main.lft,
  277. main.rgt,
  278. main.depth,
  279. parent.id AS parent_id,
  280. users.id,
  281. users.first_name,
  282. users.last_name,
  283. users.email,
  284. users.admin,
  285. users.active
  286. FROM
  287. (
  288. SELECT
  289. node.id,
  290. node.user,
  291. node.name,
  292. node.alias,
  293. node.lft,
  294. node.rgt,
  295. (COUNT(parent.id) - 1) AS depth
  296. FROM
  297. shop_cats AS node,
  298. shop_cats AS parent
  299. WHERE
  300. node.lft BETWEEN parent.lft AND parent.rgt
  301. GROUP BY
  302. node.id
  303. ORDER BY
  304. node.lft ASC
  305. ) AS main
  306. LEFT JOIN (
  307. SELECT
  308. node.id,
  309. node.user,
  310. node.name,
  311. node.alias,
  312. node.lft,
  313. node.rgt,
  314. (COUNT(parent.id) - 0) AS depth
  315. FROM
  316. shop_cats AS node,
  317. shop_cats AS parent
  318. WHERE
  319. node.lft BETWEEN parent.lft AND parent.rgt
  320. GROUP BY
  321. node.id
  322. ORDER BY
  323. node.lft ASC
  324. ) AS parent ON
  325. parent.depth = main.depth AND
  326. main.lft > parent.lft AND
  327. main.rgt < parent.rgt
  328. LEFT JOIN users ON users.id = main.user
  329. WHERE
  330. main.id > 1 AND
  331. main.alias = ?
  332. ORDER BY
  333. main.lft ASC
  334. ;`,
  335. wrap.UrlArgs[2],
  336. ).Scan(
  337. &row.A_id,
  338. &row.A_user,
  339. &row.A_name,
  340. &row.A_alias,
  341. &row.A_lft,
  342. &row.A_rgt,
  343. &row.A_depth,
  344. &row.A_parent,
  345. &rou.A_id,
  346. &rou.A_first_name,
  347. &rou.A_last_name,
  348. &rou.A_email,
  349. &rou.A_admin,
  350. &rou.A_active,
  351. )
  352. if err != nil && err != wrapper.ErrNoRows {
  353. // System error 500
  354. utils.SystemErrorPageEngine(wrap.W, err)
  355. return
  356. } else if err == wrapper.ErrNoRows {
  357. // User error 404 page
  358. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  359. return
  360. }
  361. // Fix url
  362. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  363. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  364. return
  365. }
  366. // Render template
  367. wrap.RenderFrontEnd("shop-category", fetdata.New(wrap, false, row, rou), http.StatusOK)
  368. return
  369. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {
  370. // Shop product
  371. row := &utils.MySql_shop_product{}
  372. rou := &utils.MySql_user{}
  373. err := wrap.DB.QueryRow(`
  374. SELECT
  375. shop_products.id,
  376. shop_products.user,
  377. shop_products.currency,
  378. shop_products.price,
  379. shop_products.name,
  380. shop_products.alias,
  381. shop_products.vendor,
  382. shop_products.quantity,
  383. shop_products.category,
  384. shop_products.briefly,
  385. shop_products.content,
  386. UNIX_TIMESTAMP(shop_products.datetime) as datetime,
  387. shop_products.active,
  388. users.id,
  389. users.first_name,
  390. users.last_name,
  391. users.email,
  392. users.admin,
  393. users.active
  394. FROM
  395. shop_products
  396. LEFT JOIN users ON users.id = shop_products.user
  397. WHERE
  398. shop_products.active = 1 and
  399. shop_products.alias = ?
  400. LIMIT 1;`,
  401. wrap.UrlArgs[1],
  402. ).Scan(
  403. &row.A_id,
  404. &row.A_user,
  405. &row.A_currency,
  406. &row.A_price,
  407. &row.A_name,
  408. &row.A_alias,
  409. &row.A_vendor,
  410. &row.A_quantity,
  411. &row.A_category,
  412. &row.A_briefly,
  413. &row.A_content,
  414. &row.A_datetime,
  415. &row.A_active,
  416. &rou.A_id,
  417. &rou.A_first_name,
  418. &rou.A_last_name,
  419. &rou.A_email,
  420. &rou.A_admin,
  421. &rou.A_active,
  422. )
  423. if err != nil && err != wrapper.ErrNoRows {
  424. // System error 500
  425. utils.SystemErrorPageEngine(wrap.W, err)
  426. return
  427. } else if err == wrapper.ErrNoRows {
  428. // User error 404 page
  429. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  430. return
  431. }
  432. // Fix url
  433. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  434. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  435. return
  436. }
  437. // Render template
  438. wrap.RenderFrontEnd("shop-product", fetdata.New(wrap, false, row, rou), http.StatusOK)
  439. return
  440. } else if len(wrap.UrlArgs) == 1 && wrap.UrlArgs[0] == "shop" {
  441. // Shop
  442. // Fix url
  443. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  444. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  445. return
  446. }
  447. // Render template
  448. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  449. return
  450. } else if (*wrap.Config).Engine.MainModule == 2 {
  451. // Render template
  452. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  453. return
  454. }
  455. // User error 404 page
  456. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  457. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  458. content := ""
  459. sidebar := ""
  460. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  461. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  462. {Name: "List of products"},
  463. })
  464. // Load currencies
  465. currencies := this.shop_GetAllCurrencies(wrap)
  466. content += builder.DataTable(
  467. wrap,
  468. "shop_products",
  469. "id",
  470. "DESC",
  471. &[]builder.DataTableRow{
  472. {
  473. DBField: "id",
  474. },
  475. {
  476. DBField: "name",
  477. NameInTable: "Product / URL",
  478. CallBack: func(values *[]string) string {
  479. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  480. alias := html.EscapeString((*values)[2])
  481. return `<div>` + name + `</div><div><small>/shop/` + alias + `/</small></div>`
  482. },
  483. },
  484. {
  485. DBField: "alias",
  486. },
  487. {
  488. DBField: "currency",
  489. },
  490. {
  491. DBField: "price",
  492. NameInTable: "Price",
  493. Classes: "d-none d-md-table-cell",
  494. CallBack: func(values *[]string) string {
  495. return `<div>` + utils.Float64ToStr(utils.StrToFloat64((*values)[4])) + `</div>` +
  496. `<div><small>` + currencies[utils.StrToInt((*values)[3])] + `</small></div>`
  497. },
  498. },
  499. {
  500. DBField: "datetime",
  501. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  502. NameInTable: "Date / Time",
  503. Classes: "d-none d-lg-table-cell",
  504. CallBack: func(values *[]string) string {
  505. t := int64(utils.StrToInt((*values)[5]))
  506. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  507. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  508. },
  509. },
  510. {
  511. DBField: "active",
  512. NameInTable: "Active",
  513. Classes: "d-none d-sm-table-cell",
  514. CallBack: func(values *[]string) string {
  515. return builder.CheckBox(utils.StrToInt((*values)[6]))
  516. },
  517. },
  518. },
  519. func(values *[]string) string {
  520. return builder.DataTableAction(&[]builder.DataTableActionRow{
  521. {
  522. Icon: assets.SysSvgIconView,
  523. Href: `/shop/` + (*values)[2] + `/`,
  524. Hint: "View",
  525. Target: "_blank",
  526. },
  527. {
  528. Icon: assets.SysSvgIconEdit,
  529. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  530. Hint: "Edit",
  531. },
  532. {
  533. Icon: assets.SysSvgIconRemove,
  534. Href: "javascript:fave.ActionDataTableDelete(this,'shop-delete','" +
  535. (*values)[0] + "','Are you sure want to delete product?');",
  536. Hint: "Delete",
  537. Classes: "delete",
  538. },
  539. })
  540. },
  541. "/cp/"+wrap.CurrModule+"/",
  542. nil,
  543. nil,
  544. true,
  545. )
  546. } else if wrap.CurrSubModule == "categories" {
  547. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  548. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  549. {Name: "List of categories"},
  550. })
  551. content += builder.DataTable(
  552. wrap,
  553. "shop_cats",
  554. "id",
  555. "ASC",
  556. &[]builder.DataTableRow{
  557. {
  558. DBField: "id",
  559. },
  560. {
  561. DBField: "user",
  562. },
  563. {
  564. DBField: "name",
  565. NameInTable: "Category",
  566. CallBack: func(values *[]string) string {
  567. depth := utils.StrToInt((*values)[4]) - 1
  568. if depth < 0 {
  569. depth = 0
  570. }
  571. sub := strings.Repeat("&mdash; ", depth)
  572. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  573. return `<div>` + name + `</div>`
  574. },
  575. },
  576. {
  577. DBField: "alias",
  578. },
  579. {
  580. DBField: "depth",
  581. },
  582. },
  583. func(values *[]string) string {
  584. return builder.DataTableAction(&[]builder.DataTableActionRow{
  585. {
  586. Icon: assets.SysSvgIconView,
  587. Href: `/shop/category/` + (*values)[3] + `/`,
  588. Hint: "View",
  589. Target: "_blank",
  590. },
  591. {
  592. Icon: assets.SysSvgIconEdit,
  593. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  594. Hint: "Edit",
  595. },
  596. {
  597. Icon: assets.SysSvgIconRemove,
  598. Href: "javascript:fave.ActionDataTableDelete(this,'shop-categories-delete','" +
  599. (*values)[0] + "','Are you sure want to delete category?');",
  600. Hint: "Delete",
  601. Classes: "delete",
  602. },
  603. })
  604. },
  605. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  606. nil,
  607. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  608. return wrap.DB.Query(
  609. `SELECT
  610. node.id,
  611. node.user,
  612. node.name,
  613. node.alias,
  614. (COUNT(parent.id) - 1) AS depth
  615. FROM
  616. shop_cats AS node,
  617. shop_cats AS parent
  618. WHERE
  619. node.lft BETWEEN parent.lft AND parent.rgt AND
  620. node.id > 1
  621. GROUP BY
  622. node.id
  623. ORDER BY
  624. node.lft ASC
  625. ;`,
  626. )
  627. },
  628. false,
  629. )
  630. } else if wrap.CurrSubModule == "attributes" {
  631. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  632. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  633. {Name: "List of attributes"},
  634. })
  635. content += builder.DataTable(
  636. wrap,
  637. "shop_filters",
  638. "id",
  639. "DESC",
  640. &[]builder.DataTableRow{
  641. {
  642. DBField: "id",
  643. },
  644. {
  645. DBField: "name",
  646. NameInTable: "Name",
  647. CallBack: func(values *[]string) string {
  648. name := `<a href="/cp/` + wrap.CurrModule + `/attributes-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  649. return `<div>` + name + `</div><div><small>` + html.EscapeString((*values)[2]) + `</small></div>`
  650. },
  651. },
  652. {
  653. DBField: "filter",
  654. },
  655. },
  656. func(values *[]string) string {
  657. return builder.DataTableAction(&[]builder.DataTableActionRow{
  658. {
  659. Icon: assets.SysSvgIconEdit,
  660. Href: "/cp/" + wrap.CurrModule + "/attributes-modify/" + (*values)[0] + "/",
  661. Hint: "Edit",
  662. },
  663. {
  664. Icon: assets.SysSvgIconRemove,
  665. Href: "javascript:fave.ActionDataTableDelete(this,'shop-attributes-delete','" +
  666. (*values)[0] + "','Are you sure want to delete attribute?');",
  667. Hint: "Delete",
  668. Classes: "delete",
  669. },
  670. })
  671. },
  672. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  673. nil,
  674. nil,
  675. true,
  676. )
  677. } else if wrap.CurrSubModule == "currencies" {
  678. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  679. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  680. {Name: "List of currencies"},
  681. })
  682. content += builder.DataTable(
  683. wrap,
  684. "shop_currencies",
  685. "id",
  686. "DESC",
  687. &[]builder.DataTableRow{
  688. {
  689. DBField: "id",
  690. },
  691. {
  692. DBField: "name",
  693. NameInTable: "Name",
  694. CallBack: func(values *[]string) string {
  695. name := `<a href="/cp/` + wrap.CurrModule + `/currencies-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + ` (` + (*values)[3] + `, ` + (*values)[4] + `)</a>`
  696. return `<div>` + name + `</div>`
  697. },
  698. },
  699. {
  700. DBField: "coefficient",
  701. NameInTable: "Coefficient",
  702. Classes: "d-none d-md-table-cell",
  703. CallBack: func(values *[]string) string {
  704. return utils.Float64ToStrF(utils.StrToFloat64((*values)[2]), "%.4f")
  705. },
  706. },
  707. {
  708. DBField: "code",
  709. },
  710. {
  711. DBField: "symbol",
  712. },
  713. },
  714. func(values *[]string) string {
  715. return builder.DataTableAction(&[]builder.DataTableActionRow{
  716. {
  717. Icon: assets.SysSvgIconEdit,
  718. Href: "/cp/" + wrap.CurrModule + "/currencies-modify/" + (*values)[0] + "/",
  719. Hint: "Edit",
  720. },
  721. {
  722. Icon: assets.SysSvgIconRemove,
  723. Href: "javascript:fave.ActionDataTableDelete(this,'shop-currencies-delete','" +
  724. (*values)[0] + "','Are you sure want to delete currency?');",
  725. Hint: "Delete",
  726. Classes: "delete",
  727. },
  728. })
  729. },
  730. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  731. nil,
  732. nil,
  733. true,
  734. )
  735. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  736. if wrap.CurrSubModule == "add" {
  737. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  738. {Name: "Add new product"},
  739. })
  740. } else {
  741. content += `<div class="product-copy"><a title="Duplicate product" href="#">` + assets.SysSvgIconCopy + `</a></div>`
  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. }