module_shop.go 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. package modules
  2. import (
  3. "html"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "golang-fave/assets"
  8. "golang-fave/consts"
  9. "golang-fave/engine/basket"
  10. "golang-fave/engine/builder"
  11. "golang-fave/engine/fetdata"
  12. "golang-fave/engine/sqlw"
  13. "golang-fave/engine/wrapper"
  14. "golang-fave/utils"
  15. )
  16. func (this *Modules) shop_GetCurrencySelectOptions(wrap *wrapper.Wrapper, id int) string {
  17. result := ``
  18. rows, err := wrap.DB.Query(
  19. `SELECT
  20. id,
  21. code
  22. FROM
  23. shop_currencies
  24. ORDER BY
  25. id ASC
  26. ;`,
  27. )
  28. if err == nil {
  29. defer rows.Close()
  30. values := make([]string, 2)
  31. scan := make([]interface{}, len(values))
  32. for i := range values {
  33. scan[i] = &values[i]
  34. }
  35. idStr := utils.IntToStr(id)
  36. for rows.Next() {
  37. err = rows.Scan(scan...)
  38. if *wrap.LogCpError(&err) == nil {
  39. selected := ""
  40. if string(values[0]) == idStr {
  41. selected = " selected"
  42. }
  43. result += `<option title="` + html.EscapeString(string(values[1])) + `" value="` + html.EscapeString(string(values[0])) + `"` + selected + `>` + html.EscapeString(string(values[1])) + `</option>`
  44. }
  45. }
  46. }
  47. return result
  48. }
  49. func (this *Modules) shop_GetProductValuesInputs(wrap *wrapper.Wrapper, product_id int) string {
  50. result := ``
  51. rows, err := wrap.DB.Query(
  52. `SELECT
  53. shop_filters.id,
  54. shop_filters.name,
  55. shop_filters_values.id,
  56. shop_filters_values.name,
  57. IF(shop_filter_product_values.filter_value_id > 0, 1, 0) as selected
  58. FROM
  59. shop_filters_values
  60. LEFT JOIN shop_filters ON shop_filters.id = shop_filters_values.filter_id
  61. LEFT JOIN shop_filter_product_values ON
  62. shop_filter_product_values.filter_value_id = shop_filters_values.id AND
  63. shop_filter_product_values.product_id = ` + utils.IntToStr(product_id) + `
  64. LEFT JOIN (
  65. SELECT
  66. shop_filters_values.filter_id,
  67. shop_filter_product_values.product_id
  68. FROM
  69. shop_filter_product_values
  70. LEFT JOIN shop_filters_values ON shop_filters_values.id = shop_filter_product_values.filter_value_id
  71. WHERE
  72. shop_filter_product_values.product_id = ` + utils.IntToStr(product_id) + `
  73. GROUP BY
  74. shop_filters_values.filter_id
  75. ) as filter_used ON filter_used.filter_id = shop_filters.id
  76. WHERE
  77. filter_used.filter_id IS NOT NULL
  78. ORDER BY
  79. shop_filters.name ASC,
  80. shop_filters_values.name ASC
  81. ;`,
  82. )
  83. filter_ids := []int{}
  84. filter_names := map[int]string{}
  85. filter_values := map[int][]string{}
  86. if err == nil {
  87. defer rows.Close()
  88. values := make([]string, 5)
  89. scan := make([]interface{}, len(values))
  90. for i := range values {
  91. scan[i] = &values[i]
  92. }
  93. for rows.Next() {
  94. err = rows.Scan(scan...)
  95. if *wrap.LogCpError(&err) == nil {
  96. filter_id := utils.StrToInt(string(values[0]))
  97. if !utils.InArrayInt(filter_ids, filter_id) {
  98. filter_ids = append(filter_ids, filter_id)
  99. }
  100. filter_names[filter_id] = html.EscapeString(string(values[1]))
  101. selected := ``
  102. if utils.StrToInt(string(values[4])) == 1 {
  103. selected = ` selected`
  104. }
  105. filter_values[filter_id] = append(filter_values[filter_id], `<option value="`+html.EscapeString(string(values[2]))+`"`+selected+`>`+html.EscapeString(string(values[3]))+`</option>`)
  106. }
  107. }
  108. }
  109. for _, filter_id := range filter_ids {
  110. result += `<div class="form-group" id="prod_attr_` + utils.IntToStr(filter_id) + `">` +
  111. `<div><b>` + filter_names[filter_id] + `</b></div>` +
  112. `<div class="position-relative">` +
  113. `<select class="selectpicker form-control" name="value.` + utils.IntToStr(filter_id) + `" autocomplete="off" required multiple>` +
  114. strings.Join(filter_values[filter_id], "") +
  115. `</select>` +
  116. `<button type="button" class="btn btn-danger btn-dynamic-remove" onclick="fave.ShopProductsRemove(this);">&times;</button>` +
  117. `</div>` +
  118. `</div>`
  119. }
  120. return result
  121. }
  122. func (this *Modules) shop_GetFilterValuesInputs(wrap *wrapper.Wrapper, filter_id int) string {
  123. result := ``
  124. rows, err := wrap.DB.Query(
  125. `SELECT
  126. id,
  127. name
  128. FROM
  129. shop_filters_values
  130. WHERE
  131. filter_id = ?
  132. ORDER BY
  133. name ASC
  134. ;`,
  135. filter_id,
  136. )
  137. if err == nil {
  138. defer rows.Close()
  139. values := make([]string, 2)
  140. scan := make([]interface{}, len(values))
  141. for i := range values {
  142. scan[i] = &values[i]
  143. }
  144. for rows.Next() {
  145. err = rows.Scan(scan...)
  146. if *wrap.LogCpError(&err) == nil {
  147. result += `<div class="form-group position-relative"><input class="form-control" type="text" name="value.` + html.EscapeString(string(values[0])) + `" value="` + html.EscapeString(string(values[1])) + `" placeholder="" autocomplete="off" required><button type="button" class="btn btn-danger btn-dynamic-remove" onclick="fave.ShopAttributesRemove(this);">&times;</button></div>`
  148. }
  149. }
  150. }
  151. return result
  152. }
  153. func (this *Modules) shop_GetAllAttributesSelectOptions(wrap *wrapper.Wrapper) string {
  154. result := ``
  155. rows, err := wrap.DB.Query(
  156. `SELECT
  157. id,
  158. name,
  159. filter
  160. FROM
  161. shop_filters
  162. ORDER BY
  163. name ASC
  164. ;`,
  165. )
  166. result += `<option title="&mdash;" value="0">&mdash;</option>`
  167. if err == nil {
  168. defer rows.Close()
  169. values := make([]string, 3)
  170. scan := make([]interface{}, len(values))
  171. for i := range values {
  172. scan[i] = &values[i]
  173. }
  174. for rows.Next() {
  175. err = rows.Scan(scan...)
  176. if *wrap.LogCpError(&err) == nil {
  177. result += `<option title="` + html.EscapeString(string(values[1])) + `" value="` + html.EscapeString(string(values[0])) + `">` + html.EscapeString(string(values[1])) + `</option>`
  178. }
  179. }
  180. }
  181. return result
  182. }
  183. func (this *Modules) shop_GetAllCurrencies(wrap *wrapper.Wrapper) map[int]string {
  184. result := map[int]string{}
  185. rows, err := wrap.DB.Query(
  186. `SELECT
  187. id,
  188. code
  189. FROM
  190. shop_currencies
  191. ORDER BY
  192. id ASC
  193. ;`,
  194. )
  195. if err == nil {
  196. defer rows.Close()
  197. values := make([]string, 2)
  198. scan := make([]interface{}, len(values))
  199. for i := range values {
  200. scan[i] = &values[i]
  201. }
  202. for rows.Next() {
  203. err = rows.Scan(scan...)
  204. if *wrap.LogCpError(&err) == nil {
  205. result[utils.StrToInt(string(values[0]))] = html.EscapeString(string(values[1]))
  206. }
  207. }
  208. }
  209. return result
  210. }
  211. func (this *Modules) shop_GetAllProductImages(wrap *wrapper.Wrapper, product_id int) string {
  212. result := ``
  213. rows, err := wrap.DB.Query(
  214. `SELECT
  215. id,
  216. product_id,
  217. filename
  218. FROM
  219. shop_product_images
  220. WHERE
  221. product_id = ?
  222. ORDER BY
  223. ord ASC
  224. ;`,
  225. product_id,
  226. )
  227. if err == nil {
  228. defer rows.Close()
  229. values := make([]string, 3)
  230. scan := make([]interface{}, len(values))
  231. for i := range values {
  232. scan[i] = &values[i]
  233. }
  234. for rows.Next() {
  235. err = rows.Scan(scan...)
  236. if *wrap.LogCpError(&err) == nil {
  237. 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>`
  238. }
  239. }
  240. }
  241. return result
  242. }
  243. func (this *Modules) shop_GetSubProducts(wrap *wrapper.Wrapper, id int) string {
  244. result := ``
  245. rows, err := wrap.DB.Query(
  246. `SELECT
  247. id,
  248. name
  249. FROM
  250. shop_products
  251. WHERE
  252. parent_id = ?
  253. ORDER BY
  254. id DESC
  255. ;`,
  256. id,
  257. )
  258. if err == nil {
  259. defer rows.Close()
  260. values := make([]string, 2)
  261. scan := make([]interface{}, len(values))
  262. for i := range values {
  263. scan[i] = &values[i]
  264. }
  265. for rows.Next() {
  266. err = rows.Scan(scan...)
  267. if *wrap.LogCpError(&err) == nil {
  268. result += `<div><a href="/cp/` + wrap.CurrModule + `/modify/` + html.EscapeString(string(values[0])) + `/">` + html.EscapeString(string(values[1])) + ` ` + html.EscapeString(string(values[0])) + `</a> <a class="ico delete" title="Delete" href="javascript:fave.ActionDataTableDelete(this,'shop-detach','` + html.EscapeString(string(values[0])) + `','Are you sure want to detach product?');"><svg viewBox="0 0 16 16" width="16" height="16" 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>`
  269. }
  270. }
  271. }
  272. return result
  273. }
  274. func (this *Modules) shop_GetParentProduct(wrap *wrapper.Wrapper, id int) string {
  275. result := ``
  276. rows, err := wrap.DB.Query(
  277. `SELECT
  278. id,
  279. name
  280. FROM
  281. shop_products
  282. WHERE
  283. id = ?
  284. ;`,
  285. id,
  286. )
  287. if err == nil {
  288. defer rows.Close()
  289. values := make([]string, 2)
  290. scan := make([]interface{}, len(values))
  291. for i := range values {
  292. scan[i] = &values[i]
  293. }
  294. for rows.Next() {
  295. err = rows.Scan(scan...)
  296. if *wrap.LogCpError(&err) == nil {
  297. result += `<div><a href="/cp/` + wrap.CurrModule + `/modify/` + html.EscapeString(string(values[0])) + `/">` + html.EscapeString(string(values[1])) + ` ` + html.EscapeString(string(values[0])) + `</a></div>`
  298. }
  299. }
  300. }
  301. return result
  302. }
  303. func (this *Modules) RegisterModule_Shop() *Module {
  304. return this.newModule(MInfo{
  305. WantDB: true,
  306. Mount: "shop",
  307. Name: "Shop",
  308. Order: 2,
  309. System: false,
  310. Icon: assets.SysSvgIconShop,
  311. Sub: &[]MISub{
  312. {Mount: "default", Name: "List of products", Show: true, Icon: assets.SysSvgIconList},
  313. {Mount: "add", Name: "Add new product", Show: true, Icon: assets.SysSvgIconPlus},
  314. {Mount: "modify", Name: "Modify product", Show: false},
  315. {Sep: true, Show: true},
  316. {Mount: "categories", Name: "List of categories", Show: true, Icon: assets.SysSvgIconList},
  317. {Mount: "categories-add", Name: "Add new category", Show: true, Icon: assets.SysSvgIconPlus},
  318. {Mount: "categories-modify", Name: "Modify category", Show: false},
  319. {Sep: true, Show: true},
  320. {Mount: "attributes", Name: "List of attributes", Show: true, Icon: assets.SysSvgIconList},
  321. {Mount: "attributes-add", Name: "Add new attribute", Show: true, Icon: assets.SysSvgIconPlus},
  322. {Mount: "attributes-modify", Name: "Modify attribute", Show: false},
  323. {Sep: true, Show: true},
  324. {Mount: "currencies", Name: "List of currencies", Show: true, Icon: assets.SysSvgIconList},
  325. {Mount: "currencies-add", Name: "Add new currency", Show: true, Icon: assets.SysSvgIconPlus},
  326. {Mount: "currencies-modify", Name: "Modify currency", Show: false},
  327. },
  328. }, func(wrap *wrapper.Wrapper) {
  329. if len(wrap.UrlArgs) == 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "category" && wrap.UrlArgs[2] != "" {
  330. // Shop category
  331. row := &utils.MySql_shop_category{}
  332. rou := &utils.MySql_user{}
  333. err := wrap.DB.QueryRow(`
  334. SELECT
  335. main.id,
  336. main.user,
  337. main.name,
  338. main.alias,
  339. main.lft,
  340. main.rgt,
  341. main.depth,
  342. parent.id AS parent_id,
  343. users.id,
  344. users.first_name,
  345. users.last_name,
  346. users.email,
  347. users.admin,
  348. users.active
  349. FROM
  350. (
  351. SELECT
  352. node.id,
  353. node.user,
  354. node.name,
  355. node.alias,
  356. node.lft,
  357. node.rgt,
  358. (COUNT(parent.id) - 1) AS depth
  359. FROM
  360. shop_cats AS node,
  361. shop_cats AS parent
  362. WHERE
  363. node.lft BETWEEN parent.lft AND parent.rgt
  364. GROUP BY
  365. node.id
  366. ORDER BY
  367. node.lft ASC
  368. ) AS main
  369. LEFT JOIN (
  370. SELECT
  371. node.id,
  372. node.user,
  373. node.name,
  374. node.alias,
  375. node.lft,
  376. node.rgt,
  377. (COUNT(parent.id) - 0) AS depth
  378. FROM
  379. shop_cats AS node,
  380. shop_cats AS parent
  381. WHERE
  382. node.lft BETWEEN parent.lft AND parent.rgt
  383. GROUP BY
  384. node.id
  385. ORDER BY
  386. node.lft ASC
  387. ) AS parent ON
  388. parent.depth = main.depth AND
  389. main.lft > parent.lft AND
  390. main.rgt < parent.rgt
  391. LEFT JOIN users ON users.id = main.user
  392. WHERE
  393. main.id > 1 AND
  394. main.alias = ?
  395. ORDER BY
  396. main.lft ASC
  397. ;`,
  398. wrap.UrlArgs[2],
  399. ).Scan(
  400. &row.A_id,
  401. &row.A_user,
  402. &row.A_name,
  403. &row.A_alias,
  404. &row.A_lft,
  405. &row.A_rgt,
  406. &row.A_depth,
  407. &row.A_parent,
  408. &rou.A_id,
  409. &rou.A_first_name,
  410. &rou.A_last_name,
  411. &rou.A_email,
  412. &rou.A_admin,
  413. &rou.A_active,
  414. )
  415. if err != nil && err != wrapper.ErrNoRows {
  416. // System error 500
  417. wrap.LogCpError(&err)
  418. utils.SystemErrorPageEngine(wrap.W, err)
  419. return
  420. } else if err == wrapper.ErrNoRows {
  421. // User error 404 page
  422. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  423. return
  424. }
  425. // Fix url
  426. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  427. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  428. return
  429. }
  430. // Render template
  431. wrap.RenderFrontEnd("shop-category", fetdata.New(wrap, false, row, rou), http.StatusOK)
  432. return
  433. } else if len(wrap.UrlArgs) >= 3 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] == "basket" && (wrap.UrlArgs[2] == "info" || wrap.UrlArgs[2] == "plus" || wrap.UrlArgs[2] == "minus" || wrap.UrlArgs[2] == "remove" || wrap.UrlArgs[2] == "currency") {
  434. SBParam := basket.SBParam{
  435. R: wrap.R,
  436. DB: wrap.DB,
  437. Host: wrap.CurrHost,
  438. Config: wrap.Config,
  439. SessionId: wrap.GetSessionId(),
  440. }
  441. if wrap.UrlArgs[2] == "info" {
  442. wrap.W.WriteHeader(http.StatusOK)
  443. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  444. wrap.W.Header().Set("Content-Type", "application/json; charset=utf-8")
  445. wrap.W.Write([]byte(wrap.ShopBasket.Info(&SBParam)))
  446. wrap.S.SetString("LastBasketAction", wrap.UrlArgs[2])
  447. return
  448. } else if wrap.UrlArgs[2] == "plus" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  449. wrap.W.WriteHeader(http.StatusOK)
  450. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  451. wrap.W.Header().Set("Content-Type", "application/json; charset=utf-8")
  452. wrap.W.Write([]byte(wrap.ShopBasket.Plus(&SBParam, utils.StrToInt(wrap.UrlArgs[3]))))
  453. wrap.S.SetString("LastBasketAction", wrap.UrlArgs[2])
  454. return
  455. } else if wrap.UrlArgs[2] == "minus" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  456. wrap.W.WriteHeader(http.StatusOK)
  457. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  458. wrap.W.Header().Set("Content-Type", "application/json; charset=utf-8")
  459. wrap.W.Write([]byte(wrap.ShopBasket.Minus(&SBParam, utils.StrToInt(wrap.UrlArgs[3]))))
  460. wrap.S.SetString("LastBasketAction", wrap.UrlArgs[2])
  461. return
  462. } else if wrap.UrlArgs[2] == "remove" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  463. wrap.W.WriteHeader(http.StatusOK)
  464. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  465. wrap.W.Header().Set("Content-Type", "application/json; charset=utf-8")
  466. wrap.W.Write([]byte(wrap.ShopBasket.Remove(&SBParam, utils.StrToInt(wrap.UrlArgs[3]))))
  467. wrap.S.SetString("LastBasketAction", wrap.UrlArgs[2])
  468. return
  469. } else if wrap.UrlArgs[2] == "currency" && len(wrap.UrlArgs) == 4 && utils.IsNumeric(wrap.UrlArgs[3]) {
  470. http.SetCookie(wrap.W, &http.Cookie{
  471. Name: "currency",
  472. Value: wrap.UrlArgs[3],
  473. Path: "/",
  474. Expires: time.Now().Add(7 * 24 * time.Hour),
  475. HttpOnly: true,
  476. })
  477. redirectUrl := wrap.R.Referer()
  478. if redirectUrl == "" {
  479. redirectUrl = "/"
  480. }
  481. http.Redirect(wrap.W, wrap.R, redirectUrl, 302)
  482. wrap.S.SetString("LastBasketAction", wrap.UrlArgs[2])
  483. return
  484. }
  485. } else if len(wrap.UrlArgs) == 2 && wrap.UrlArgs[0] == "shop" && wrap.UrlArgs[1] != "" {
  486. // Shop product
  487. row := &utils.MySql_shop_product{}
  488. rou := &utils.MySql_user{}
  489. err := wrap.DB.QueryRow(`
  490. SELECT
  491. shop_products.id,
  492. shop_products.parent_id,
  493. shop_products.user,
  494. shop_products.currency,
  495. shop_products.price,
  496. shop_products.name,
  497. shop_products.alias,
  498. shop_products.vendor,
  499. shop_products.quantity,
  500. shop_products.category,
  501. shop_products.briefly,
  502. shop_products.content,
  503. UNIX_TIMESTAMP(shop_products.datetime) as datetime,
  504. shop_products.active,
  505. users.id,
  506. users.first_name,
  507. users.last_name,
  508. users.email,
  509. users.admin,
  510. users.active
  511. FROM
  512. shop_products
  513. LEFT JOIN users ON users.id = shop_products.user
  514. WHERE
  515. shop_products.active = 1 and
  516. shop_products.alias = ?
  517. LIMIT 1;`,
  518. wrap.UrlArgs[1],
  519. ).Scan(
  520. &row.A_id,
  521. &row.A_parent,
  522. &row.A_user,
  523. &row.A_currency,
  524. &row.A_price,
  525. &row.A_name,
  526. &row.A_alias,
  527. &row.A_vendor,
  528. &row.A_quantity,
  529. &row.A_category,
  530. &row.A_briefly,
  531. &row.A_content,
  532. &row.A_datetime,
  533. &row.A_active,
  534. &rou.A_id,
  535. &rou.A_first_name,
  536. &rou.A_last_name,
  537. &rou.A_email,
  538. &rou.A_admin,
  539. &rou.A_active,
  540. )
  541. if err != nil && err != wrapper.ErrNoRows {
  542. // System error 500
  543. wrap.LogCpError(&err)
  544. utils.SystemErrorPageEngine(wrap.W, err)
  545. return
  546. } else if err == wrapper.ErrNoRows {
  547. // User error 404 page
  548. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  549. return
  550. }
  551. // Fix url
  552. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  553. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  554. return
  555. }
  556. // Render template
  557. wrap.RenderFrontEnd("shop-product", fetdata.New(wrap, false, row, rou), http.StatusOK)
  558. return
  559. } else if len(wrap.UrlArgs) == 1 && wrap.UrlArgs[0] == "shop" {
  560. // Shop
  561. // Fix url
  562. if wrap.R.URL.Path[len(wrap.R.URL.Path)-1] != '/' {
  563. http.Redirect(wrap.W, wrap.R, wrap.R.URL.Path+"/"+utils.ExtractGetParams(wrap.R.RequestURI), 301)
  564. return
  565. }
  566. // Render template
  567. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  568. return
  569. } else if (*wrap.Config).Engine.MainModule == 2 {
  570. // Render template
  571. wrap.RenderFrontEnd("shop", fetdata.New(wrap, false, nil, nil), http.StatusOK)
  572. return
  573. }
  574. // User error 404 page
  575. wrap.RenderFrontEnd("404", fetdata.New(wrap, true, nil, nil), http.StatusNotFound)
  576. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  577. content := ""
  578. sidebar := ""
  579. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  580. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  581. {Name: "List of products"},
  582. })
  583. // Load currencies
  584. currencies := this.shop_GetAllCurrencies(wrap)
  585. content += builder.DataTable(
  586. wrap,
  587. "shop_products",
  588. "id",
  589. "DESC",
  590. &[]builder.DataTableRow{
  591. {
  592. DBField: "id",
  593. },
  594. {
  595. DBField: "name",
  596. NameInTable: "Product / URL",
  597. CallBack: func(values *[]string) string {
  598. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + ` ` + html.EscapeString((*values)[0]) + `</a>`
  599. alias := html.EscapeString((*values)[2])
  600. parent := ``
  601. if (*values)[7] != "" {
  602. parent = `<div class="parent">&uarr;<small><a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[7] + `/">` + html.EscapeString((*values)[8]) + ` ` + (*values)[7] + `</a></small></div>`
  603. }
  604. return `<div>` + name + `</div><div><small>/shop/` + alias + `/</small></div>` + parent
  605. },
  606. },
  607. {
  608. DBField: "alias",
  609. },
  610. {
  611. DBField: "currency",
  612. },
  613. {
  614. DBField: "price",
  615. NameInTable: "Price",
  616. Classes: "d-none d-md-table-cell",
  617. CallBack: func(values *[]string) string {
  618. return `<div>` + utils.Float64ToStr(utils.StrToFloat64((*values)[4])) + `</div>` +
  619. `<div><small>` + currencies[utils.StrToInt((*values)[3])] + `</small></div>`
  620. },
  621. },
  622. {
  623. DBField: "datetime",
  624. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  625. NameInTable: "Date / Time",
  626. Classes: "d-none d-lg-table-cell",
  627. CallBack: func(values *[]string) string {
  628. t := int64(utils.StrToInt((*values)[5]))
  629. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  630. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  631. },
  632. },
  633. {
  634. DBField: "active",
  635. NameInTable: "Active",
  636. Classes: "d-none d-sm-table-cell",
  637. CallBack: func(values *[]string) string {
  638. return builder.CheckBox(utils.StrToInt((*values)[6]))
  639. },
  640. },
  641. {
  642. DBField: "parent_id",
  643. },
  644. {
  645. DBField: "pname",
  646. DBExp: "spp.name",
  647. },
  648. },
  649. func(values *[]string) string {
  650. return builder.DataTableAction(&[]builder.DataTableActionRow{
  651. {
  652. Icon: assets.SysSvgIconView,
  653. Href: `/shop/` + (*values)[2] + `/`,
  654. Hint: "View",
  655. Target: "_blank",
  656. },
  657. {
  658. Icon: assets.SysSvgIconEdit,
  659. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  660. Hint: "Edit",
  661. },
  662. {
  663. Icon: assets.SysSvgIconRemove,
  664. Href: "javascript:fave.ActionDataTableDelete(this,'shop-delete','" +
  665. (*values)[0] + "','Are you sure want to delete product?');",
  666. Hint: "Delete",
  667. Classes: "delete",
  668. },
  669. })
  670. },
  671. "/cp/"+wrap.CurrModule+"/",
  672. func() (int, error) {
  673. var count int
  674. return count, wrap.DB.QueryRow(
  675. "SELECT COUNT(*) FROM `shop_products`;",
  676. ).Scan(&count)
  677. },
  678. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  679. return wrap.DB.Query(
  680. `SELECT
  681. shop_products.id,
  682. shop_products.name,
  683. shop_products.alias,
  684. shop_products.currency,
  685. shop_products.price,
  686. UNIX_TIMESTAMP(`+"`shop_products`.`datetime`"+`) AS datetime,
  687. shop_products.active,
  688. shop_products.parent_id,
  689. spp.name AS pname
  690. FROM
  691. shop_products
  692. LEFT JOIN shop_products AS spp ON spp.id = shop_products.parent_id
  693. ORDER BY
  694. shop_products.id DESC
  695. LIMIT ?, ?;`,
  696. limit_offset,
  697. pear_page,
  698. )
  699. },
  700. true,
  701. )
  702. } else if wrap.CurrSubModule == "categories" {
  703. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  704. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  705. {Name: "List of categories"},
  706. })
  707. content += builder.DataTable(
  708. wrap,
  709. "shop_cats",
  710. "id",
  711. "ASC",
  712. &[]builder.DataTableRow{
  713. {
  714. DBField: "id",
  715. },
  716. {
  717. DBField: "user",
  718. },
  719. {
  720. DBField: "name",
  721. NameInTable: "Category",
  722. CallBack: func(values *[]string) string {
  723. depth := utils.StrToInt((*values)[4]) - 1
  724. if depth < 0 {
  725. depth = 0
  726. }
  727. sub := strings.Repeat("&mdash; ", depth)
  728. name := `<a href="/cp/` + wrap.CurrModule + `/categories-modify/` + (*values)[0] + `/">` + sub + html.EscapeString((*values)[2]) + `</a>`
  729. return `<div>` + name + `</div>`
  730. },
  731. },
  732. {
  733. DBField: "alias",
  734. },
  735. {
  736. DBField: "depth",
  737. },
  738. },
  739. func(values *[]string) string {
  740. return builder.DataTableAction(&[]builder.DataTableActionRow{
  741. {
  742. Icon: assets.SysSvgIconView,
  743. Href: `/shop/category/` + (*values)[3] + `/`,
  744. Hint: "View",
  745. Target: "_blank",
  746. },
  747. {
  748. Icon: assets.SysSvgIconEdit,
  749. Href: "/cp/" + wrap.CurrModule + "/categories-modify/" + (*values)[0] + "/",
  750. Hint: "Edit",
  751. },
  752. {
  753. Icon: assets.SysSvgIconRemove,
  754. Href: "javascript:fave.ActionDataTableDelete(this,'shop-categories-delete','" +
  755. (*values)[0] + "','Are you sure want to delete category?');",
  756. Hint: "Delete",
  757. Classes: "delete",
  758. },
  759. })
  760. },
  761. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  762. nil,
  763. func(limit_offset int, pear_page int) (*sqlw.Rows, error) {
  764. return wrap.DB.Query(
  765. `SELECT
  766. node.id,
  767. node.user,
  768. node.name,
  769. node.alias,
  770. (COUNT(parent.id) - 1) AS depth
  771. FROM
  772. shop_cats AS node,
  773. shop_cats AS parent
  774. WHERE
  775. node.lft BETWEEN parent.lft AND parent.rgt AND
  776. node.id > 1
  777. GROUP BY
  778. node.id
  779. ORDER BY
  780. node.lft ASC
  781. ;`,
  782. )
  783. },
  784. false,
  785. )
  786. } else if wrap.CurrSubModule == "attributes" {
  787. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  788. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  789. {Name: "List of attributes"},
  790. })
  791. content += builder.DataTable(
  792. wrap,
  793. "shop_filters",
  794. "id",
  795. "DESC",
  796. &[]builder.DataTableRow{
  797. {
  798. DBField: "id",
  799. },
  800. {
  801. DBField: "name",
  802. NameInTable: "Name",
  803. CallBack: func(values *[]string) string {
  804. name := `<a href="/cp/` + wrap.CurrModule + `/attributes-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  805. return `<div>` + name + `</div><div><small>` + html.EscapeString((*values)[2]) + `</small></div>`
  806. },
  807. },
  808. {
  809. DBField: "filter",
  810. },
  811. },
  812. func(values *[]string) string {
  813. return builder.DataTableAction(&[]builder.DataTableActionRow{
  814. {
  815. Icon: assets.SysSvgIconEdit,
  816. Href: "/cp/" + wrap.CurrModule + "/attributes-modify/" + (*values)[0] + "/",
  817. Hint: "Edit",
  818. },
  819. {
  820. Icon: assets.SysSvgIconRemove,
  821. Href: "javascript:fave.ActionDataTableDelete(this,'shop-attributes-delete','" +
  822. (*values)[0] + "','Are you sure want to delete attribute?');",
  823. Hint: "Delete",
  824. Classes: "delete",
  825. },
  826. })
  827. },
  828. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  829. nil,
  830. nil,
  831. true,
  832. )
  833. } else if wrap.CurrSubModule == "currencies" {
  834. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  835. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/" + wrap.CurrSubModule + "/"},
  836. {Name: "List of currencies"},
  837. })
  838. content += builder.DataTable(
  839. wrap,
  840. "shop_currencies",
  841. "id",
  842. "DESC",
  843. &[]builder.DataTableRow{
  844. {
  845. DBField: "id",
  846. },
  847. {
  848. DBField: "name",
  849. NameInTable: "Name",
  850. CallBack: func(values *[]string) string {
  851. name := `<a href="/cp/` + wrap.CurrModule + `/currencies-modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + ` (` + (*values)[3] + `, ` + (*values)[4] + `)</a>`
  852. return `<div>` + name + `</div>`
  853. },
  854. },
  855. {
  856. DBField: "coefficient",
  857. NameInTable: "Coefficient",
  858. Classes: "d-none d-md-table-cell",
  859. CallBack: func(values *[]string) string {
  860. return utils.Float64ToStrF(utils.StrToFloat64((*values)[2]), "%.4f")
  861. },
  862. },
  863. {
  864. DBField: "code",
  865. },
  866. {
  867. DBField: "symbol",
  868. },
  869. },
  870. func(values *[]string) string {
  871. return builder.DataTableAction(&[]builder.DataTableActionRow{
  872. {
  873. Icon: assets.SysSvgIconEdit,
  874. Href: "/cp/" + wrap.CurrModule + "/currencies-modify/" + (*values)[0] + "/",
  875. Hint: "Edit",
  876. },
  877. {
  878. Icon: assets.SysSvgIconRemove,
  879. Href: "javascript:fave.ActionDataTableDelete(this,'shop-currencies-delete','" +
  880. (*values)[0] + "','Are you sure want to delete currency?');",
  881. Hint: "Delete",
  882. Classes: "delete",
  883. },
  884. })
  885. },
  886. "/cp/"+wrap.CurrModule+"/"+wrap.CurrSubModule+"/",
  887. nil,
  888. nil,
  889. true,
  890. )
  891. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  892. if wrap.CurrSubModule == "add" {
  893. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  894. {Name: "Add new product"},
  895. })
  896. } else {
  897. if len(wrap.UrlArgs) >= 3 && utils.IsNumeric(wrap.UrlArgs[2]) {
  898. content += `<div class="product-copy"><a title="Duplicate product" href="javascript:fave.ShopProductsDuplicate(this, ` + wrap.UrlArgs[2] + `);">` + assets.SysSvgIconCopy + `</a></div>`
  899. content += `<div class="product-another"><a title="Duplicate product and attach" href="javascript:fave.ShopProductsDuplicateWithAttach(this, ` + wrap.UrlArgs[2] + `);">` + assets.SysSvgIconPlus + `</a></div>`
  900. }
  901. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  902. {Name: "Modify product"},
  903. })
  904. }
  905. data := utils.MySql_shop_product{
  906. A_id: 0,
  907. A_user: 0,
  908. A_currency: 0,
  909. A_price: 0,
  910. A_gname: "",
  911. A_name: "",
  912. A_alias: "",
  913. A_vendor: "",
  914. A_quantity: 0,
  915. A_category: 0,
  916. A_briefly: "",
  917. A_content: "",
  918. A_datetime: 0,
  919. A_active: 0,
  920. }
  921. if wrap.CurrSubModule == "modify" {
  922. if len(wrap.UrlArgs) != 3 {
  923. return "", "", ""
  924. }
  925. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  926. return "", "", ""
  927. }
  928. err := wrap.DB.QueryRow(`
  929. SELECT
  930. id,
  931. parent_id,
  932. user,
  933. currency,
  934. price,
  935. gname,
  936. name,
  937. alias,
  938. vendor,
  939. quantity,
  940. category,
  941. briefly,
  942. content,
  943. active
  944. FROM
  945. shop_products
  946. WHERE
  947. id = ?
  948. LIMIT 1;`,
  949. utils.StrToInt(wrap.UrlArgs[2]),
  950. ).Scan(
  951. &data.A_id,
  952. &data.A_parent,
  953. &data.A_user,
  954. &data.A_currency,
  955. &data.A_price,
  956. &data.A_gname,
  957. &data.A_name,
  958. &data.A_alias,
  959. &data.A_vendor,
  960. &data.A_quantity,
  961. &data.A_category,
  962. &data.A_briefly,
  963. &data.A_content,
  964. &data.A_active,
  965. )
  966. if *wrap.LogCpError(&err) != nil {
  967. return "", "", ""
  968. }
  969. }
  970. if data.A_parent_id() > 0 {
  971. content += `<style>.product-another{display:none}</style>`
  972. }
  973. // All product current categories
  974. var selids []int
  975. if data.A_id > 0 {
  976. rows, err := wrap.DB.Query("SELECT category_id FROM shop_cat_product_rel WHERE product_id = ?;", data.A_id)
  977. if err == nil {
  978. defer rows.Close()
  979. values := make([]int, 1)
  980. scan := make([]interface{}, len(values))
  981. for i := range values {
  982. scan[i] = &values[i]
  983. }
  984. for rows.Next() {
  985. err = rows.Scan(scan...)
  986. if *wrap.LogCpError(&err) == nil {
  987. selids = append(selids, int(values[0]))
  988. }
  989. }
  990. }
  991. }
  992. // Sub products
  993. sub_products := ``
  994. if data.A_id >= 1 && data.A_parent_id() <= 0 {
  995. sub_products = this.shop_GetSubProducts(wrap, data.A_id)
  996. }
  997. btn_caption := "Add"
  998. if wrap.CurrSubModule == "modify" {
  999. btn_caption = "Save"
  1000. }
  1001. content += builder.DataForm(wrap, []builder.DataFormField{
  1002. {
  1003. Kind: builder.DFKHidden,
  1004. Name: "action",
  1005. Value: "shop-modify",
  1006. },
  1007. {
  1008. Kind: builder.DFKHidden,
  1009. Name: "id",
  1010. Value: utils.IntToStr(data.A_id),
  1011. },
  1012. {
  1013. Kind: builder.DFKText,
  1014. CallBack: func(field *builder.DataFormField) string {
  1015. if data.A_id >= 1 && data.A_parent_id() <= 0 {
  1016. return `<div class="form-group nf">` +
  1017. `<div class="row">` +
  1018. `<div class="col-md-3">` +
  1019. `<label>Sub products</label>` +
  1020. `</div>` +
  1021. `<div class="col-md-9">` +
  1022. `<div class="list-wrapper">` +
  1023. sub_products +
  1024. `<div><a href="javascript:fave.ShopAttachProduct(` + utils.IntToStr(data.A_id) + `);"><b>Attach product</b></a></div>` +
  1025. `</div>` +
  1026. `</div>` +
  1027. `</div>` +
  1028. `</div>`
  1029. }
  1030. return ""
  1031. },
  1032. },
  1033. {
  1034. Kind: builder.DFKText,
  1035. CallBack: func(field *builder.DataFormField) string {
  1036. if data.A_id >= 1 && data.A_parent_id() != 0 {
  1037. return `<div class="form-group nf">` +
  1038. `<div class="row">` +
  1039. `<div class="col-md-3">` +
  1040. `<label>Parent</label>` +
  1041. `</div>` +
  1042. `<div class="col-md-9">` +
  1043. `<div class="list-wrapper">` +
  1044. this.shop_GetParentProduct(wrap, data.A_parent_id()) +
  1045. `</div>` +
  1046. `</div>` +
  1047. `</div>` +
  1048. `</div>`
  1049. }
  1050. return ""
  1051. },
  1052. },
  1053. {
  1054. Kind: builder.DFKText,
  1055. CallBack: func(field *builder.DataFormField) string {
  1056. if data.A_id >= 1 && data.A_parent_id() <= 0 && sub_products != "" {
  1057. return `<div class="form-group nf">` +
  1058. `<div class="row">` +
  1059. `<div class="col-md-3">` +
  1060. `<label for="lbl_gname">Group name</label>` +
  1061. `</div>` +
  1062. `<div class="col-md-9">` +
  1063. `<div><input class="form-control" type="text" id="lbl_gname" name="gname" value="` + html.EscapeString(data.A_gname) + `" maxlength="255" autocomplete="off"></div>` +
  1064. `</div>` +
  1065. `</div>` +
  1066. `</div>`
  1067. }
  1068. return ""
  1069. },
  1070. },
  1071. {
  1072. Kind: builder.DFKText,
  1073. Caption: "Product name",
  1074. Name: "name",
  1075. Value: data.A_name,
  1076. Required: true,
  1077. Min: "1",
  1078. Max: "255",
  1079. },
  1080. {
  1081. Kind: builder.DFKText,
  1082. Caption: "Product price",
  1083. Name: "price",
  1084. Value: "0",
  1085. CallBack: func(field *builder.DataFormField) string {
  1086. return `<div class="form-group n6">` +
  1087. `<div class="row">` +
  1088. `<div class="col-md-3">` +
  1089. `<label for="lbl_price">Product price</label>` +
  1090. `</div>` +
  1091. `<div class="col-md-9">` +
  1092. `<div>` +
  1093. `<div class="row">` +
  1094. `<div class="col-md-8">` +
  1095. `<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>` +
  1096. `<div class="d-md-none mb-3"></div>` +
  1097. `</div>` +
  1098. `<div class="col-md-4">` +
  1099. `<select class="selectpicker form-control" id="lbl_currency" name="currency" data-live-search="true">` +
  1100. this.shop_GetCurrencySelectOptions(wrap, data.A_currency) +
  1101. `</select>` +
  1102. `</div>` +
  1103. `</div>` +
  1104. `</div>` +
  1105. `</div>` +
  1106. `</div>` +
  1107. `</div>`
  1108. },
  1109. },
  1110. {
  1111. Kind: builder.DFKText,
  1112. Caption: "Product alias",
  1113. Name: "alias",
  1114. Value: data.A_alias,
  1115. Hint: "Example: mobile-phone",
  1116. Max: "255",
  1117. },
  1118. {
  1119. Kind: builder.DFKText,
  1120. Caption: "Vendor/Count",
  1121. Name: "vendor",
  1122. Value: "0",
  1123. CallBack: func(field *builder.DataFormField) string {
  1124. return `<div class="form-group n8">` +
  1125. `<div class="row">` +
  1126. `<div class="col-md-3">` +
  1127. `<label for="lbl_vendor">Vendor/Count</label>` +
  1128. `</div>` +
  1129. `<div class="col-md-9">` +
  1130. `<div>` +
  1131. `<div class="row">` +
  1132. `<div class="col-md-8">` +
  1133. `<div><input class="form-control" type="text" id="lbl_vendor" name="vendor" value="` + html.EscapeString(data.A_vendor) + `" placeholder="" autocomplete="off"></div>` +
  1134. `<div class="d-md-none mb-3"></div>` +
  1135. `</div>` +
  1136. `<div class="col-md-4">` +
  1137. `<input class="form-control" type="number" step="1" id="lbl_quantity" name="quantity" value="` + utils.IntToStr(data.A_quantity) + `" placeholder="" autocomplete="off">` +
  1138. `</div>` +
  1139. `</div>` +
  1140. `</div>` +
  1141. `</div>` +
  1142. `</div>` +
  1143. `</div>`
  1144. },
  1145. },
  1146. {
  1147. Kind: builder.DFKText,
  1148. Caption: "Category",
  1149. Name: "category",
  1150. Value: "0",
  1151. CallBack: func(field *builder.DataFormField) string {
  1152. return `<div class="form-group n9">` +
  1153. `<div class="row">` +
  1154. `<div class="col-md-3">` +
  1155. `<label for="lbl_category">Category</label>` +
  1156. `</div>` +
  1157. `<div class="col-md-9">` +
  1158. `<div>` +
  1159. `<select class="selectpicker form-control" id="lbl_category" name="category" data-live-search="true">` +
  1160. `<option title="Nothing selected" value="0">&mdash;</option>` +
  1161. this.shop_GetCategorySelectOptions(wrap, 0, data.A_category, []int{}) +
  1162. `</select>` +
  1163. `</div>` +
  1164. `</div>` +
  1165. `</div>` +
  1166. `</div>`
  1167. },
  1168. },
  1169. {
  1170. Kind: builder.DFKText,
  1171. Caption: "Categories",
  1172. Name: "cats",
  1173. Value: "0",
  1174. CallBack: func(field *builder.DataFormField) string {
  1175. return `<div class="form-group n10">` +
  1176. `<div class="row">` +
  1177. `<div class="col-md-3">` +
  1178. `<label for="lbl_cats">Categories</label>` +
  1179. `</div>` +
  1180. `<div class="col-md-9">` +
  1181. `<div>` +
  1182. `<select class="selectpicker form-control" id="lbl_cats" name="cats[]" data-live-search="true" multiple>` +
  1183. this.shop_GetCategorySelectOptions(wrap, 0, 0, selids) +
  1184. `</select>` +
  1185. `</div>` +
  1186. `</div>` +
  1187. `</div>` +
  1188. `</div>`
  1189. },
  1190. },
  1191. {
  1192. Kind: builder.DFKText,
  1193. Caption: "Attributes",
  1194. Name: "",
  1195. Value: "",
  1196. CallBack: func(field *builder.DataFormField) string {
  1197. return `<div class="form-group n11">` +
  1198. `<div class="row">` +
  1199. `<div class="col-md-3">` +
  1200. `<label>Attributes</label>` +
  1201. `</div>` +
  1202. `<div class="col-md-9">` +
  1203. `<div class="list-wrapper">` +
  1204. `<div id="list">` +
  1205. this.shop_GetProductValuesInputs(wrap, data.A_id) +
  1206. `</div>` +
  1207. `<div class="list-button position-relative">` +
  1208. `<select class="selectpicker form-control" id="lbl_attributes" data-live-search="true" onchange="fave.ShopProductsAdd();">` +
  1209. this.shop_GetAllAttributesSelectOptions(wrap) +
  1210. `</select>` +
  1211. `</div>` +
  1212. `</div>` +
  1213. `</div>` +
  1214. `</div>` +
  1215. `</div>`
  1216. },
  1217. },
  1218. {
  1219. Kind: builder.DFKTextArea,
  1220. Caption: "Briefly",
  1221. Name: "briefly",
  1222. Value: data.A_briefly,
  1223. Classes: "briefly wysiwyg",
  1224. },
  1225. {
  1226. Kind: builder.DFKTextArea,
  1227. Caption: "Product content",
  1228. Name: "content",
  1229. Value: data.A_content,
  1230. Classes: "wysiwyg",
  1231. },
  1232. {
  1233. Kind: builder.DFKText,
  1234. Caption: "Product images",
  1235. Name: "",
  1236. Value: "",
  1237. CallBack: func(field *builder.DataFormField) string {
  1238. if data.A_id == 0 {
  1239. return ``
  1240. }
  1241. return `<div class="form-group n14">` +
  1242. `<div class="row">` +
  1243. `<div class="col-md-3">` +
  1244. `<label>Product images</label>` +
  1245. `</div>` +
  1246. `<div class="col-md-9">` +
  1247. `<div class="list-wrapper">` +
  1248. `<div id="list-images">` +
  1249. this.shop_GetAllProductImages(wrap, data.A_id) +
  1250. `</div>` +
  1251. `<div id="img-upload-block" class="list-button position-relative">` +
  1252. `<div id="upload-msg">Uploading...</div>` +
  1253. `<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:12px;" multiple />` +
  1254. `</div>` +
  1255. `</div>` +
  1256. `</div>` +
  1257. `</div>` +
  1258. `</div>` +
  1259. `<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>`
  1260. },
  1261. },
  1262. {
  1263. Kind: builder.DFKCheckBox,
  1264. Caption: "Active",
  1265. Name: "active",
  1266. Value: utils.IntToStr(data.A_active),
  1267. },
  1268. {
  1269. Kind: builder.DFKSubmit,
  1270. Value: btn_caption,
  1271. Target: "add-edit-button",
  1272. },
  1273. })
  1274. if wrap.CurrSubModule == "add" {
  1275. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1276. } else {
  1277. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1278. }
  1279. } else if wrap.CurrSubModule == "categories-add" || wrap.CurrSubModule == "categories-modify" {
  1280. if wrap.CurrSubModule == "categories-add" {
  1281. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1282. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1283. {Name: "Add new category"},
  1284. })
  1285. } else {
  1286. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1287. {Name: "Categories", Link: "/cp/" + wrap.CurrModule + "/categories/"},
  1288. {Name: "Modify category"},
  1289. })
  1290. }
  1291. data := utils.MySql_shop_category{
  1292. A_id: 0,
  1293. A_user: 0,
  1294. A_name: "",
  1295. A_alias: "",
  1296. A_lft: 0,
  1297. A_rgt: 0,
  1298. }
  1299. if wrap.CurrSubModule == "categories-modify" {
  1300. if len(wrap.UrlArgs) != 3 {
  1301. return "", "", ""
  1302. }
  1303. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1304. return "", "", ""
  1305. }
  1306. err := wrap.DB.QueryRow(`
  1307. SELECT
  1308. id,
  1309. user,
  1310. name,
  1311. alias,
  1312. lft,
  1313. rgt
  1314. FROM
  1315. shop_cats
  1316. WHERE
  1317. id = ?
  1318. LIMIT 1;`,
  1319. utils.StrToInt(wrap.UrlArgs[2]),
  1320. ).Scan(
  1321. &data.A_id,
  1322. &data.A_user,
  1323. &data.A_name,
  1324. &data.A_alias,
  1325. &data.A_lft,
  1326. &data.A_rgt,
  1327. )
  1328. if *wrap.LogCpError(&err) != nil {
  1329. return "", "", ""
  1330. }
  1331. }
  1332. btn_caption := "Add"
  1333. if wrap.CurrSubModule == "categories-modify" {
  1334. btn_caption = "Save"
  1335. }
  1336. parentId := 0
  1337. if wrap.CurrSubModule == "categories-modify" {
  1338. parentId = this.shop_GetCategoryParentId(wrap, data.A_id)
  1339. }
  1340. content += builder.DataForm(wrap, []builder.DataFormField{
  1341. {
  1342. Kind: builder.DFKHidden,
  1343. Name: "action",
  1344. Value: "shop-categories-modify",
  1345. },
  1346. {
  1347. Kind: builder.DFKHidden,
  1348. Name: "id",
  1349. Value: utils.IntToStr(data.A_id),
  1350. },
  1351. {
  1352. Kind: builder.DFKText,
  1353. Caption: "Parent",
  1354. Name: "parent",
  1355. Value: "0",
  1356. CallBack: func(field *builder.DataFormField) string {
  1357. return `<div class="form-group n2">` +
  1358. `<div class="row">` +
  1359. `<div class="col-md-3">` +
  1360. `<label for="lbl_parent">Parent</label>` +
  1361. `</div>` +
  1362. `<div class="col-md-9">` +
  1363. `<div>` +
  1364. `<select class="selectpicker form-control" id="lbl_parent" name="parent" data-live-search="true">` +
  1365. `<option title="Nothing selected" value="0">&mdash;</option>` +
  1366. this.shop_GetCategorySelectOptions(wrap, data.A_id, parentId, []int{}) +
  1367. `</select>` +
  1368. `</div>` +
  1369. `</div>` +
  1370. `</div>` +
  1371. `</div>`
  1372. },
  1373. },
  1374. {
  1375. Kind: builder.DFKText,
  1376. Caption: "Name",
  1377. Name: "name",
  1378. Value: data.A_name,
  1379. Required: true,
  1380. Min: "1",
  1381. Max: "255",
  1382. },
  1383. {
  1384. Kind: builder.DFKText,
  1385. Caption: "Alias",
  1386. Name: "alias",
  1387. Value: data.A_alias,
  1388. Hint: "Example: popular-products",
  1389. Max: "255",
  1390. },
  1391. {
  1392. Kind: builder.DFKSubmit,
  1393. Value: btn_caption,
  1394. Target: "add-edit-button",
  1395. },
  1396. })
  1397. if wrap.CurrSubModule == "categories-add" {
  1398. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1399. } else {
  1400. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1401. }
  1402. } else if wrap.CurrSubModule == "attributes-add" || wrap.CurrSubModule == "attributes-modify" {
  1403. if wrap.CurrSubModule == "attributes-add" {
  1404. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1405. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1406. {Name: "Add new attribute"},
  1407. })
  1408. } else {
  1409. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1410. {Name: "Attributes", Link: "/cp/" + wrap.CurrModule + "/attributes/"},
  1411. {Name: "Modify attribute"},
  1412. })
  1413. }
  1414. data := utils.MySql_shop_filter{
  1415. A_id: 0,
  1416. A_name: "",
  1417. A_filter: "",
  1418. }
  1419. if wrap.CurrSubModule == "attributes-modify" {
  1420. if len(wrap.UrlArgs) != 3 {
  1421. return "", "", ""
  1422. }
  1423. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1424. return "", "", ""
  1425. }
  1426. err := wrap.DB.QueryRow(`
  1427. SELECT
  1428. id,
  1429. name,
  1430. filter
  1431. FROM
  1432. shop_filters
  1433. WHERE
  1434. id = ?
  1435. LIMIT 1;`,
  1436. utils.StrToInt(wrap.UrlArgs[2]),
  1437. ).Scan(
  1438. &data.A_id,
  1439. &data.A_name,
  1440. &data.A_filter,
  1441. )
  1442. if *wrap.LogCpError(&err) != nil {
  1443. return "", "", ""
  1444. }
  1445. }
  1446. btn_caption := "Add"
  1447. if wrap.CurrSubModule == "attributes-modify" {
  1448. btn_caption = "Save"
  1449. }
  1450. content += builder.DataForm(wrap, []builder.DataFormField{
  1451. {
  1452. Kind: builder.DFKHidden,
  1453. Name: "action",
  1454. Value: "shop-attributes-modify",
  1455. },
  1456. {
  1457. Kind: builder.DFKHidden,
  1458. Name: "id",
  1459. Value: utils.IntToStr(data.A_id),
  1460. },
  1461. {
  1462. Kind: builder.DFKText,
  1463. Caption: "Attribute name",
  1464. Name: "name",
  1465. Value: data.A_name,
  1466. Required: true,
  1467. Min: "1",
  1468. Max: "255",
  1469. },
  1470. {
  1471. Kind: builder.DFKText,
  1472. Caption: "Attribute in filter",
  1473. Name: "filter",
  1474. Value: data.A_filter,
  1475. Required: true,
  1476. Min: "1",
  1477. Max: "255",
  1478. },
  1479. {
  1480. Kind: builder.DFKText,
  1481. Caption: "Attribute values",
  1482. Name: "",
  1483. Value: "",
  1484. CallBack: func(field *builder.DataFormField) string {
  1485. return `<div class="form-group n4">` +
  1486. `<div class="row">` +
  1487. `<div class="col-md-3">` +
  1488. `<label>Attribute values</label>` +
  1489. `</div>` +
  1490. `<div class="col-md-9">` +
  1491. `<div class="list-wrapper">` +
  1492. `<div id="list">` +
  1493. this.shop_GetFilterValuesInputs(wrap, data.A_id) +
  1494. `</div>` +
  1495. `<div class="list-button"><button type="button" class="btn btn-success" onclick="fave.ShopAttributesAdd();">Add attribute value</button></div>` +
  1496. `</div>` +
  1497. `</div>` +
  1498. `</div>` +
  1499. `</div>`
  1500. },
  1501. },
  1502. {
  1503. Kind: builder.DFKSubmit,
  1504. Value: btn_caption,
  1505. Target: "add-edit-button",
  1506. },
  1507. })
  1508. if wrap.CurrSubModule == "attributes-add" {
  1509. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1510. } else {
  1511. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1512. }
  1513. } else if wrap.CurrSubModule == "currencies-add" || wrap.CurrSubModule == "currencies-modify" {
  1514. if wrap.CurrSubModule == "currencies-add" {
  1515. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1516. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1517. {Name: "Add new currency"},
  1518. })
  1519. } else {
  1520. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  1521. {Name: "Currencies", Link: "/cp/" + wrap.CurrModule + "/currencies/"},
  1522. {Name: "Modify currency"},
  1523. })
  1524. }
  1525. data := utils.MySql_shop_currency{
  1526. A_id: 0,
  1527. A_name: "",
  1528. A_coefficient: 0,
  1529. A_code: "",
  1530. A_symbol: "",
  1531. }
  1532. if wrap.CurrSubModule == "currencies-modify" {
  1533. if len(wrap.UrlArgs) != 3 {
  1534. return "", "", ""
  1535. }
  1536. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  1537. return "", "", ""
  1538. }
  1539. err := wrap.DB.QueryRow(`
  1540. SELECT
  1541. id,
  1542. name,
  1543. coefficient,
  1544. code,
  1545. symbol
  1546. FROM
  1547. shop_currencies
  1548. WHERE
  1549. id = ?
  1550. LIMIT 1;`,
  1551. utils.StrToInt(wrap.UrlArgs[2]),
  1552. ).Scan(
  1553. &data.A_id,
  1554. &data.A_name,
  1555. &data.A_coefficient,
  1556. &data.A_code,
  1557. &data.A_symbol,
  1558. )
  1559. if *wrap.LogCpError(&err) != nil {
  1560. return "", "", ""
  1561. }
  1562. }
  1563. btn_caption := "Add"
  1564. if wrap.CurrSubModule == "currencies-modify" {
  1565. btn_caption = "Save"
  1566. }
  1567. content += builder.DataForm(wrap, []builder.DataFormField{
  1568. {
  1569. Kind: builder.DFKHidden,
  1570. Name: "action",
  1571. Value: "shop-currencies-modify",
  1572. },
  1573. {
  1574. Kind: builder.DFKHidden,
  1575. Name: "id",
  1576. Value: utils.IntToStr(data.A_id),
  1577. },
  1578. {
  1579. Kind: builder.DFKText,
  1580. Caption: "Currency name",
  1581. Name: "name",
  1582. Value: data.A_name,
  1583. Required: true,
  1584. Min: "1",
  1585. Max: "255",
  1586. },
  1587. {
  1588. Kind: builder.DFKText,
  1589. Caption: "Currency coefficient",
  1590. Name: "coefficient",
  1591. Value: "0",
  1592. CallBack: func(field *builder.DataFormField) string {
  1593. return `<div class="form-group n3">` +
  1594. `<div class="row">` +
  1595. `<div class="col-md-3">` +
  1596. `<label for="lbl_coefficient">Currency coefficient</label>` +
  1597. `</div>` +
  1598. `<div class="col-md-9">` +
  1599. `<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>` +
  1600. `</div>` +
  1601. `</div>` +
  1602. `</div>`
  1603. },
  1604. },
  1605. {
  1606. Kind: builder.DFKText,
  1607. Caption: "Currency code",
  1608. Name: "code",
  1609. Value: data.A_code,
  1610. Required: true,
  1611. Min: "1",
  1612. Max: "10",
  1613. },
  1614. {
  1615. Kind: builder.DFKText,
  1616. Caption: "Currency symbol",
  1617. Name: "symbol",
  1618. Value: data.A_symbol,
  1619. Required: true,
  1620. Min: "1",
  1621. Max: "5",
  1622. },
  1623. {
  1624. Kind: builder.DFKSubmit,
  1625. Value: btn_caption,
  1626. Target: "add-edit-button",
  1627. },
  1628. })
  1629. if wrap.CurrSubModule == "currencies-add" {
  1630. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  1631. } else {
  1632. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  1633. }
  1634. }
  1635. return this.getSidebarModules(wrap), content, sidebar
  1636. })
  1637. }