module_shop.go 42 KB

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