module_shop.go 46 KB

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