module_shop.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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="The same with another attributes" href="javascript:fave.ShopProductsAnother(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_parent_id: 0,
  792. A_user: 0,
  793. A_currency: 0,
  794. A_price: 0,
  795. A_name: "",
  796. A_alias: "",
  797. A_vendor: "",
  798. A_quantity: 0,
  799. A_category: 0,
  800. A_briefly: "",
  801. A_content: "",
  802. A_datetime: 0,
  803. A_active: 0,
  804. }
  805. if wrap.CurrSubModule == "modify" {
  806. if len(wrap.UrlArgs) != 3 {
  807. return "", "", ""
  808. }
  809. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  810. return "", "", ""
  811. }
  812. err := wrap.DB.QueryRow(`
  813. SELECT
  814. id,
  815. parent_id,
  816. user,
  817. currency,
  818. price,
  819. name,
  820. alias,
  821. vendor,
  822. quantity,
  823. category,
  824. briefly,
  825. content,
  826. active
  827. FROM
  828. shop_products
  829. WHERE
  830. id = ?
  831. LIMIT 1;`,
  832. utils.StrToInt(wrap.UrlArgs[2]),
  833. ).Scan(
  834. &data.A_id,
  835. &data.A_parent_id,
  836. &data.A_user,
  837. &data.A_currency,
  838. &data.A_price,
  839. &data.A_name,
  840. &data.A_alias,
  841. &data.A_vendor,
  842. &data.A_quantity,
  843. &data.A_category,
  844. &data.A_briefly,
  845. &data.A_content,
  846. &data.A_active,
  847. )
  848. if wrap.LogCpError(err) != nil {
  849. return "", "", ""
  850. }
  851. }
  852. // All product current categories
  853. var selids []int
  854. if data.A_id > 0 {
  855. rows, err := wrap.DB.Query("SELECT category_id FROM shop_cat_product_rel WHERE product_id = ?;", data.A_id)
  856. if err == nil {
  857. defer rows.Close()
  858. values := make([]int, 1)
  859. scan := make([]interface{}, len(values))
  860. for i := range values {
  861. scan[i] = &values[i]
  862. }
  863. for rows.Next() {
  864. err = rows.Scan(scan...)
  865. if wrap.LogCpError(err) == nil {
  866. selids = append(selids, int(values[0]))
  867. }
  868. }
  869. }
  870. }
  871. btn_caption := "Add"
  872. if wrap.CurrSubModule == "modify" {
  873. btn_caption = "Save"
  874. }
  875. content += builder.DataForm(wrap, []builder.DataFormField{
  876. {
  877. Kind: builder.DFKHidden,
  878. Name: "action",
  879. Value: "shop-modify",
  880. },
  881. {
  882. Kind: builder.DFKHidden,
  883. Name: "id",
  884. Value: utils.IntToStr(data.A_id),
  885. },
  886. {
  887. Kind: builder.DFKText,
  888. Caption: "Product name",
  889. Name: "name",
  890. Value: data.A_name,
  891. Required: true,
  892. Min: "1",
  893. Max: "255",
  894. },
  895. {
  896. Kind: builder.DFKText,
  897. Caption: "Product price",
  898. Name: "price",
  899. Value: "0",
  900. CallBack: func(field *builder.DataFormField) string {
  901. return `<div class="form-group n3">` +
  902. `<div class="row">` +
  903. `<div class="col-md-3">` +
  904. `<label for="lbl_price">Product price</label>` +
  905. `</div>` +
  906. `<div class="col-md-9">` +
  907. `<div>` +
  908. `<div class="row">` +
  909. `<div class="col-md-8">` +
  910. `<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>` +
  911. `<div class="d-md-none mb-3"></div>` +
  912. `</div>` +
  913. `<div class="col-md-4">` +
  914. `<select class="selectpicker form-control" id="lbl_currency" name="currency" data-live-search="true">` +
  915. this.shop_GetCurrencySelectOptions(wrap, data.A_currency) +
  916. `</select>` +
  917. `</div>` +
  918. `</div>` +
  919. `</div>` +
  920. `</div>` +
  921. `</div>` +
  922. `</div>`
  923. },
  924. },
  925. {
  926. Kind: builder.DFKText,
  927. Caption: "Product alias",
  928. Name: "alias",
  929. Value: data.A_alias,
  930. Hint: "Example: mobile-phone",
  931. Max: "255",
  932. },
  933. {
  934. Kind: builder.DFKText,
  935. Caption: "Vendor/Count",
  936. Name: "vendor",
  937. Value: "0",
  938. CallBack: func(field *builder.DataFormField) string {
  939. return `<div class="form-group n5">` +
  940. `<div class="row">` +
  941. `<div class="col-md-3">` +
  942. `<label for="lbl_vendor">Vendor/Count</label>` +
  943. `</div>` +
  944. `<div class="col-md-9">` +
  945. `<div>` +
  946. `<div class="row">` +
  947. `<div class="col-md-8">` +
  948. `<div><input class="form-control" type="text" id="lbl_vendor" name="vendor" value="` + html.EscapeString(data.A_vendor) + `" placeholder="" autocomplete="off"></div>` +
  949. `<div class="d-md-none mb-3"></div>` +
  950. `</div>` +
  951. `<div class="col-md-4">` +
  952. `<input class="form-control" type="number" step="1" id="lbl_quantity" name="quantity" value="` + utils.IntToStr(data.A_quantity) + `" placeholder="" autocomplete="off">` +
  953. `</div>` +
  954. `</div>` +
  955. `</div>` +
  956. `</div>` +
  957. `</div>` +
  958. `</div>`
  959. },
  960. },
  961. {
  962. Kind: builder.DFKText,
  963. Caption: "Category",
  964. Name: "category",
  965. Value: "0",
  966. CallBack: func(field *builder.DataFormField) string {
  967. return `<div class="form-group n6">` +
  968. `<div class="row">` +
  969. `<div class="col-md-3">` +
  970. `<label for="lbl_category">Category</label>` +
  971. `</div>` +
  972. `<div class="col-md-9">` +
  973. `<div>` +
  974. `<select class="selectpicker form-control" id="lbl_category" name="category" data-live-search="true">` +
  975. `<option title="Nothing selected" value="0">&mdash;</option>` +
  976. this.shop_GetCategorySelectOptions(wrap, 0, data.A_category, []int{}) +
  977. `</select>` +
  978. `</div>` +
  979. `</div>` +
  980. `</div>` +
  981. `</div>`
  982. },
  983. },
  984. {
  985. Kind: builder.DFKText,
  986. Caption: "Categories",
  987. Name: "cats",
  988. Value: "0",
  989. CallBack: func(field *builder.DataFormField) string {
  990. return `<div class="form-group n7">` +
  991. `<div class="row">` +
  992. `<div class="col-md-3">` +
  993. `<label for="lbl_cats">Categories</label>` +
  994. `</div>` +
  995. `<div class="col-md-9">` +
  996. `<div>` +
  997. `<select class="selectpicker form-control" id="lbl_cats" name="cats[]" data-live-search="true" multiple>` +
  998. this.shop_GetCategorySelectOptions(wrap, 0, 0, selids) +
  999. `</select>` +
  1000. `</div>` +
  1001. `</div>` +
  1002. `</div>` +
  1003. `</div>`
  1004. },
  1005. },
  1006. {
  1007. Kind: builder.DFKText,
  1008. Caption: "Attributes",
  1009. Name: "",
  1010. Value: "",
  1011. CallBack: func(field *builder.DataFormField) string {
  1012. return `<div class="form-group n8">` +
  1013. `<div class="row">` +
  1014. `<div class="col-md-3">` +
  1015. `<label>Attributes</label>` +
  1016. `</div>` +
  1017. `<div class="col-md-9">` +
  1018. `<div class="list-wrapper">` +
  1019. `<div id="list">` +
  1020. this.shop_GetProductValuesInputs(wrap, data.A_id) +
  1021. `</div>` +
  1022. `<div class="list-button position-relative">` +
  1023. `<select class="selectpicker form-control" id="lbl_attributes" data-live-search="true" onchange="fave.ShopProductsAdd();">` +
  1024. this.shop_GetAllAttributesSelectOptions(wrap) +
  1025. `</select>` +
  1026. `</div>` +
  1027. `</div>` +
  1028. `</div>` +
  1029. `</div>` +
  1030. `</div>`
  1031. },
  1032. },
  1033. {
  1034. Kind: builder.DFKTextArea,
  1035. Caption: "Briefly",
  1036. Name: "briefly",
  1037. Value: data.A_briefly,
  1038. Classes: "briefly wysiwyg",
  1039. },
  1040. {
  1041. Kind: builder.DFKTextArea,
  1042. Caption: "Product content",
  1043. Name: "content",
  1044. Value: data.A_content,
  1045. Classes: "wysiwyg",
  1046. },
  1047. {
  1048. Kind: builder.DFKText,
  1049. Caption: "Product images",
  1050. Name: "",
  1051. Value: "",
  1052. CallBack: func(field *builder.DataFormField) string {
  1053. if data.A_id == 0 {
  1054. return ``
  1055. }
  1056. return `<div class="form-group n11">` +
  1057. `<div class="row">` +
  1058. `<div class="col-md-3">` +
  1059. `<label>Product images</label>` +
  1060. `</div>` +
  1061. `<div class="col-md-9">` +
  1062. `<div class="list-wrapper">` +
  1063. `<div id="list-images">` +
  1064. this.shop_GetAllProductImages(wrap, data.A_id) +
  1065. `</div>` +
  1066. `<div id="img-upload-block" class="list-button position-relative">` +
  1067. `<div id="upload-msg">Uploading...</div>` +
  1068. `<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 />` +
  1069. `</div>` +
  1070. `</div>` +
  1071. `</div>` +
  1072. `</div>` +
  1073. `</div>` +
  1074. `<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>`
  1075. },
  1076. },
  1077. {
  1078. Kind: builder.DFKCheckBox,
  1079. Caption: "Active",
  1080. Name: "active",
  1081. Value: utils.IntToStr(data.A_active),
  1082. },
  1083. {
  1084. Kind: builder.DFKSubmit,
  1085. Value: btn_caption,
  1086. Target: "add-edit-button",
  1087. },
  1088. })
  1089. if wrap.CurrSubModule == "add" {
  1090. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1091. } else {
  1092. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1093. }
  1094. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  1095. if wrap.CurrSubModule == "categories-add" {
  1096. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1097. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1098. {Name: "Add new category"},
  1099. })
  1100. } else {
  1101. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1102. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1103. {Name: "Modify category"},
  1104. })
  1105. }
  1106. data := utils.MySql_shop_category{
  1107. A_id: 0,
  1108. A_user: 0,
  1109. A_name: "",
  1110. A_alias: "",
  1111. A_lft: 0,
  1112. A_rgt: 0,
  1113. }
  1114. if wrap.CurrSubModule == "categories-modify" {
  1115. if len(wrap.UrlArgs) != 3 {
  1116. return "", "", ""
  1117. }
  1118. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1119. return "", "", ""
  1120. }
  1121. err := wrap.DB.QueryRow(`
  1122. SELECT
  1123. id,
  1124. user,
  1125. name,
  1126. alias,
  1127. lft,
  1128. rgt
  1129. FROM
  1130. shop_cats
  1131. WHERE
  1132. id = ?
  1133. LIMIT 1;`,
  1134. utils.StrToInt(wrap.UrlArgs[2]),
  1135. ).Scan(
  1136. &data.A_id,
  1137. &data.A_user,
  1138. &data.A_name,
  1139. &data.A_alias,
  1140. &data.A_lft,
  1141. &data.A_rgt,
  1142. )
  1143. if wrap.LogCpError(err) != nil {
  1144. return "", "", ""
  1145. }
  1146. }
  1147. btn_caption := "Add"
  1148. if wrap.CurrSubModule == "categories-modify" {
  1149. btn_caption = "Save"
  1150. }
  1151. parentId := 0
  1152. if wrap.CurrSubModule == "categories-modify" {
  1153. parentId = this.shop_GetCategoryParentId(wrap, data.A_id)
  1154. }
  1155. content += builder.DataForm(wrap, []builder.DataFormField{
  1156. {
  1157. Kind: builder.DFKHidden,
  1158. Name: "action",
  1159. Value: "shop-categories-modify",
  1160. },
  1161. {
  1162. Kind: builder.DFKHidden,
  1163. Name: "id",
  1164. Value: utils.IntToStr(data.A_id),
  1165. },
  1166. {
  1167. Kind: builder.DFKText,
  1168. Caption: "Parent",
  1169. Name: "parent",
  1170. Value: "0",
  1171. CallBack: func(field *builder.DataFormField) string {
  1172. return `<div class="form-group n2">` +
  1173. `<div class="row">` +
  1174. `<div class="col-md-3">` +
  1175. `<label for="lbl_parent">Parent</label>` +
  1176. `</div>` +
  1177. `<div class="col-md-9">` +
  1178. `<div>` +
  1179. `<select class="selectpicker form-control" id="lbl_parent" name="parent" data-live-search="true">` +
  1180. `<option title="Nothing selected" value="0">&mdash;</option>` +
  1181. this.shop_GetCategorySelectOptions(wrap, data.A_id, parentId, []int{}) +
  1182. `</select>` +
  1183. `</div>` +
  1184. `</div>` +
  1185. `</div>` +
  1186. `</div>`
  1187. },
  1188. },
  1189. {
  1190. Kind: builder.DFKText,
  1191. Caption: "Name",
  1192. Name: "name",
  1193. Value: data.A_name,
  1194. Required: true,
  1195. Min: "1",
  1196. Max: "255",
  1197. },
  1198. {
  1199. Kind: builder.DFKText,
  1200. Caption: "Alias",
  1201. Name: "alias",
  1202. Value: data.A_alias,
  1203. Hint: "Example: popular-products",
  1204. Max: "255",
  1205. },
  1206. {
  1207. Kind: builder.DFKSubmit,
  1208. Value: btn_caption,
  1209. Target: "add-edit-button",
  1210. },
  1211. })
  1212. if wrap.CurrSubModule == "categories-add" {
  1213. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1214. } else {
  1215. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1216. }
  1217. } else if wrap.CurrSubModule == "attributes-add" || wrap.CurrSubModule == "attributes-modify" {
  1218. if wrap.CurrSubModule == "attributes-add" {
  1219. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1220. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1221. {Name: "Add new attribute"},
  1222. })
  1223. } else {
  1224. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1225. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1226. {Name: "Modify attribute"},
  1227. })
  1228. }
  1229. data := utils.MySql_shop_filter{
  1230. A_id: 0,
  1231. A_name: "",
  1232. A_filter: "",
  1233. }
  1234. if wrap.CurrSubModule == "attributes-modify" {
  1235. if len(wrap.UrlArgs) != 3 {
  1236. return "", "", ""
  1237. }
  1238. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1239. return "", "", ""
  1240. }
  1241. err := wrap.DB.QueryRow(`
  1242. SELECT
  1243. id,
  1244. name,
  1245. filter
  1246. FROM
  1247. shop_filters
  1248. WHERE
  1249. id = ?
  1250. LIMIT 1;`,
  1251. utils.StrToInt(wrap.UrlArgs[2]),
  1252. ).Scan(
  1253. &data.A_id,
  1254. &data.A_name,
  1255. &data.A_filter,
  1256. )
  1257. if wrap.LogCpError(err) != nil {
  1258. return "", "", ""
  1259. }
  1260. }
  1261. btn_caption := "Add"
  1262. if wrap.CurrSubModule == "attributes-modify" {
  1263. btn_caption = "Save"
  1264. }
  1265. content += builder.DataForm(wrap, []builder.DataFormField{
  1266. {
  1267. Kind: builder.DFKHidden,
  1268. Name: "action",
  1269. Value: "shop-attributes-modify",
  1270. },
  1271. {
  1272. Kind: builder.DFKHidden,
  1273. Name: "id",
  1274. Value: utils.IntToStr(data.A_id),
  1275. },
  1276. {
  1277. Kind: builder.DFKText,
  1278. Caption: "Attribute name",
  1279. Name: "name",
  1280. Value: data.A_name,
  1281. Required: true,
  1282. Min: "1",
  1283. Max: "255",
  1284. },
  1285. {
  1286. Kind: builder.DFKText,
  1287. Caption: "Attribute in filter",
  1288. Name: "filter",
  1289. Value: data.A_filter,
  1290. Required: true,
  1291. Min: "1",
  1292. Max: "255",
  1293. },
  1294. {
  1295. Kind: builder.DFKText,
  1296. Caption: "Attribute values",
  1297. Name: "",
  1298. Value: "",
  1299. CallBack: func(field *builder.DataFormField) string {
  1300. return `<div class="form-group n4">` +
  1301. `<div class="row">` +
  1302. `<div class="col-md-3">` +
  1303. `<label>Attribute values</label>` +
  1304. `</div>` +
  1305. `<div class="col-md-9">` +
  1306. `<div class="list-wrapper">` +
  1307. `<div id="list">` +
  1308. this.shop_GetFilterValuesInputs(wrap, data.A_id) +
  1309. `</div>` +
  1310. `<div class="list-button"><button type="button" class="btn btn-success" onclick="fave.ShopAttributesAdd();">Add attribute value</button></div>` +
  1311. `</div>` +
  1312. `</div>` +
  1313. `</div>` +
  1314. `</div>`
  1315. },
  1316. },
  1317. {
  1318. Kind: builder.DFKSubmit,
  1319. Value: btn_caption,
  1320. Target: "add-edit-button",
  1321. },
  1322. })
  1323. if wrap.CurrSubModule == "attributes-add" {
  1324. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1325. } else {
  1326. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1327. }
  1328. } else if wrap.CurrSubModule == "currencies-add" || wrap.CurrSubModule == "currencies-modify" {
  1329. if wrap.CurrSubModule == "currencies-add" {
  1330. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1331. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1332. {Name: "Add new currency"},
  1333. })
  1334. } else {
  1335. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1336. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1337. {Name: "Modify currency"},
  1338. })
  1339. }
  1340. data := utils.MySql_shop_currency{
  1341. A_id: 0,
  1342. A_name: "",
  1343. A_coefficient: 0,
  1344. A_code: "",
  1345. A_symbol: "",
  1346. }
  1347. if wrap.CurrSubModule == "currencies-modify" {
  1348. if len(wrap.UrlArgs) != 3 {
  1349. return "", "", ""
  1350. }
  1351. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1352. return "", "", ""
  1353. }
  1354. err := wrap.DB.QueryRow(`
  1355. SELECT
  1356. id,
  1357. name,
  1358. coefficient,
  1359. code,
  1360. symbol
  1361. FROM
  1362. shop_currencies
  1363. WHERE
  1364. id = ?
  1365. LIMIT 1;`,
  1366. utils.StrToInt(wrap.UrlArgs[2]),
  1367. ).Scan(
  1368. &data.A_id,
  1369. &data.A_name,
  1370. &data.A_coefficient,
  1371. &data.A_code,
  1372. &data.A_symbol,
  1373. )
  1374. if wrap.LogCpError(err) != nil {
  1375. return "", "", ""
  1376. }
  1377. }
  1378. btn_caption := "Add"
  1379. if wrap.CurrSubModule == "currencies-modify" {
  1380. btn_caption = "Save"
  1381. }
  1382. content += builder.DataForm(wrap, []builder.DataFormField{
  1383. {
  1384. Kind: builder.DFKHidden,
  1385. Name: "action",
  1386. Value: "shop-currencies-modify",
  1387. },
  1388. {
  1389. Kind: builder.DFKHidden,
  1390. Name: "id",
  1391. Value: utils.IntToStr(data.A_id),
  1392. },
  1393. {
  1394. Kind: builder.DFKText,
  1395. Caption: "Currency name",
  1396. Name: "name",
  1397. Value: data.A_name,
  1398. Required: true,
  1399. Min: "1",
  1400. Max: "255",
  1401. },
  1402. {
  1403. Kind: builder.DFKText,
  1404. Caption: "Currency coefficient",
  1405. Name: "coefficient",
  1406. Value: "0",
  1407. CallBack: func(field *builder.DataFormField) string {
  1408. return `<div class="form-group n3">` +
  1409. `<div class="row">` +
  1410. `<div class="col-md-3">` +
  1411. `<label for="lbl_coefficient">Currency coefficient</label>` +
  1412. `</div>` +
  1413. `<div class="col-md-9">` +
  1414. `<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>` +
  1415. `</div>` +
  1416. `</div>` +
  1417. `</div>`
  1418. },
  1419. },
  1420. {
  1421. Kind: builder.DFKText,
  1422. Caption: "Currency code",
  1423. Name: "code",
  1424. Value: data.A_code,
  1425. Required: true,
  1426. Min: "1",
  1427. Max: "10",
  1428. },
  1429. {
  1430. Kind: builder.DFKText,
  1431. Caption: "Currency symbol",
  1432. Name: "symbol",
  1433. Value: data.A_symbol,
  1434. Required: true,
  1435. Min: "1",
  1436. Max: "5",
  1437. },
  1438. {
  1439. Kind: builder.DFKSubmit,
  1440. Value: btn_caption,
  1441. Target: "add-edit-button",
  1442. },
  1443. })
  1444. if wrap.CurrSubModule == "currencies-add" {
  1445. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1446. } else {
  1447. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1448. }
  1449. }
  1450. return this.getSidebarModules(wrap), content, sidebar
  1451. })
  1452. }