module_shop.go 48 KB

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