module_shop.go 48 KB

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