module_shop.go 46 KB

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