module_shop.go 41 KB

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