module_shop.go 41 KB

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