module_shop.go 44 KB

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