module_index.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "fmt"
  6. "html"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. "golang-fave/assets"
  11. "golang-fave/consts"
  12. "golang-fave/engine/builder"
  13. "golang-fave/engine/fetdata"
  14. "golang-fave/engine/wrapper"
  15. "golang-fave/utils"
  16. )
  17. func (this *Modules) RegisterModule_Index() *Module {
  18. return this.newModule(MInfo{
  19. WantDB: true,
  20. Mount: "index",
  21. Name: "Pages",
  22. Order: 0,
  23. Icon: assets.SysSvgIconPage,
  24. Sub: &[]MISub{
  25. {Mount: "default", Name: "List of pages", Show: true, Icon: assets.SysSvgIconList},
  26. {Mount: "add", Name: "Add new page", Show: true, Icon: assets.SysSvgIconPlus},
  27. {Mount: "modify", Name: "Modify page", Show: false},
  28. },
  29. }, func(wrap *wrapper.Wrapper) {
  30. // Front-end
  31. row := &utils.MySql_page{}
  32. err := wrap.DB.QueryRow(`
  33. SELECT
  34. id,
  35. user,
  36. name,
  37. alias,
  38. content,
  39. meta_title,
  40. meta_keywords,
  41. meta_description,
  42. UNIX_TIMESTAMP(datetime) as datetime,
  43. active
  44. FROM
  45. pages
  46. WHERE
  47. active = 1 and
  48. alias = ?
  49. LIMIT 1;`,
  50. wrap.R.URL.Path,
  51. ).Scan(
  52. &row.A_id,
  53. &row.A_user,
  54. &row.A_name,
  55. &row.A_alias,
  56. &row.A_content,
  57. &row.A_meta_title,
  58. &row.A_meta_keywords,
  59. &row.A_meta_description,
  60. &row.A_datetime,
  61. &row.A_active,
  62. )
  63. if err != nil && err != sql.ErrNoRows {
  64. // System error 500
  65. utils.SystemErrorPageEngine(wrap.W, err)
  66. return
  67. } else if err == sql.ErrNoRows {
  68. // User error 404 page
  69. wrap.W.WriteHeader(http.StatusNotFound)
  70. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true))
  71. return
  72. }
  73. // Replace title with page name
  74. if row.A_meta_title == "" {
  75. row.A_meta_title = row.A_name
  76. }
  77. // Which template
  78. tmpl_name := "index"
  79. if wrap.R.URL.Path != "/" {
  80. tmpl_name = "page"
  81. }
  82. // Render template
  83. wrap.RenderFrontEnd(tmpl_name, fetdata.New(wrap, row, false))
  84. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  85. content := ""
  86. sidebar := ""
  87. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  88. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  89. {Name: "List of pages"},
  90. })
  91. content += builder.DataTable(wrap, "pages", "id", "DESC", &[]builder.DataTableRow{
  92. {
  93. DBField: "id",
  94. },
  95. {
  96. DBField: "name",
  97. NameInTable: "Page / Alias",
  98. CallBack: func(values *[]string) string {
  99. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  100. alias := html.EscapeString((*values)[2])
  101. return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
  102. },
  103. },
  104. {
  105. DBField: "alias",
  106. },
  107. {
  108. DBField: "datetime",
  109. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  110. NameInTable: "Date / Time",
  111. CallBack: func(values *[]string) string {
  112. t := int64(utils.StrToInt((*values)[3]))
  113. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  114. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  115. },
  116. },
  117. {
  118. DBField: "active",
  119. NameInTable: "Active",
  120. CallBack: func(values *[]string) string {
  121. return builder.CheckBox(utils.StrToInt((*values)[4]))
  122. },
  123. },
  124. }, func(values *[]string) string {
  125. return builder.DataTableAction(&[]builder.DataTableActionRow{
  126. {
  127. Icon: assets.SysSvgIconView,
  128. Href: (*values)[2],
  129. Hint: "View",
  130. Target: "_blank",
  131. },
  132. {
  133. Icon: assets.SysSvgIconEdit,
  134. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  135. Hint: "Edit",
  136. },
  137. {
  138. Icon: assets.SysSvgIconRemove,
  139. Href: "javascript:fave.ActionDataTableDelete(this,'index-delete','" +
  140. (*values)[0] + "','Are you sure want to delete page?');",
  141. Hint: "Delete",
  142. },
  143. })
  144. }, "/cp/"+wrap.CurrModule+"/")
  145. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  146. if wrap.CurrSubModule == "add" {
  147. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  148. {Name: "Add new page"},
  149. })
  150. } else {
  151. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  152. {Name: "Modify page"},
  153. })
  154. }
  155. data := utils.MySql_page{
  156. A_id: 0,
  157. A_user: 0,
  158. A_name: "",
  159. A_alias: "",
  160. A_content: "",
  161. A_meta_title: "",
  162. A_meta_keywords: "",
  163. A_meta_description: "",
  164. A_datetime: 0,
  165. A_active: 0,
  166. }
  167. if wrap.CurrSubModule == "modify" {
  168. if len(wrap.UrlArgs) != 3 {
  169. return "", "", ""
  170. }
  171. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  172. return "", "", ""
  173. }
  174. err := wrap.DB.QueryRow(`
  175. SELECT
  176. id,
  177. user,
  178. name,
  179. alias,
  180. content,
  181. meta_title,
  182. meta_keywords,
  183. meta_description,
  184. active
  185. FROM
  186. pages
  187. WHERE
  188. id = ?
  189. LIMIT 1;`,
  190. utils.StrToInt(wrap.UrlArgs[2]),
  191. ).Scan(
  192. &data.A_id,
  193. &data.A_user,
  194. &data.A_name,
  195. &data.A_alias,
  196. &data.A_content,
  197. &data.A_meta_title,
  198. &data.A_meta_keywords,
  199. &data.A_meta_description,
  200. &data.A_active,
  201. )
  202. if err != nil {
  203. return "", "", ""
  204. }
  205. }
  206. content += builder.DataForm(wrap, []builder.DataFormField{
  207. {
  208. Kind: builder.DFKHidden,
  209. Name: "action",
  210. Value: "index-modify",
  211. },
  212. {
  213. Kind: builder.DFKHidden,
  214. Name: "id",
  215. Value: utils.IntToStr(data.A_id),
  216. },
  217. {
  218. Kind: builder.DFKText,
  219. Caption: "Page name",
  220. Name: "name",
  221. Value: data.A_name,
  222. },
  223. {
  224. Kind: builder.DFKText,
  225. Caption: "Page alias",
  226. Name: "alias",
  227. Value: data.A_alias,
  228. Hint: "Example: /about-us/ or /about-us.html or /about/team.html",
  229. },
  230. {
  231. Kind: builder.DFKTextArea,
  232. Caption: "Page content",
  233. Name: "content",
  234. Value: data.A_content,
  235. },
  236. {
  237. Kind: builder.DFKText,
  238. Caption: "Meta title",
  239. Name: "meta_title",
  240. Value: data.A_meta_title,
  241. },
  242. {
  243. Kind: builder.DFKText,
  244. Caption: "Meta keywords",
  245. Name: "meta_keywords",
  246. Value: data.A_meta_keywords,
  247. },
  248. {
  249. Kind: builder.DFKTextArea,
  250. Caption: "Meta description",
  251. Name: "meta_description",
  252. Value: data.A_meta_description,
  253. },
  254. {
  255. Kind: builder.DFKCheckBox,
  256. Caption: "Active",
  257. Name: "active",
  258. Value: utils.IntToStr(data.A_active),
  259. },
  260. {
  261. Kind: builder.DFKMessage,
  262. },
  263. {
  264. Kind: builder.DFKSubmit,
  265. Value: "Add",
  266. Target: "add-edit-button",
  267. },
  268. })
  269. if wrap.CurrSubModule == "add" {
  270. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  271. } else {
  272. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  273. }
  274. }
  275. return this.getSidebarModules(wrap), content, sidebar
  276. })
  277. }
  278. func (this *Modules) RegisterAction_IndexModify() *Action {
  279. return this.newAction(AInfo{
  280. WantDB: true,
  281. Mount: "index-modify",
  282. WantAdmin: true,
  283. }, func(wrap *wrapper.Wrapper) {
  284. pf_id := wrap.R.FormValue("id")
  285. pf_name := wrap.R.FormValue("name")
  286. pf_alias := wrap.R.FormValue("alias")
  287. pf_content := wrap.R.FormValue("content")
  288. pf_meta_title := wrap.R.FormValue("meta_title")
  289. pf_meta_keywords := wrap.R.FormValue("meta_keywords")
  290. pf_meta_description := wrap.R.FormValue("meta_description")
  291. pf_active := wrap.R.FormValue("active")
  292. if pf_active == "" {
  293. pf_active = "0"
  294. }
  295. if !utils.IsNumeric(pf_id) {
  296. wrap.MsgError(`Inner system error`)
  297. return
  298. }
  299. if pf_name == "" {
  300. wrap.MsgError(`Please specify page name`)
  301. return
  302. }
  303. if pf_alias == "" {
  304. pf_alias = utils.GenerateAlias(pf_name)
  305. }
  306. if !utils.IsValidAlias(pf_alias) {
  307. wrap.MsgError(`Please specify correct page alias`)
  308. return
  309. }
  310. if pf_id == "0" {
  311. // Add new page
  312. _, err := wrap.DB.Query(
  313. `INSERT INTO pages SET
  314. user = ?,
  315. name = ?,
  316. alias = ?,
  317. content = ?,
  318. meta_title = ?,
  319. meta_keywords = ?,
  320. meta_description = ?,
  321. datetime = ?,
  322. active = ?
  323. ;`,
  324. wrap.User.A_id,
  325. pf_name,
  326. pf_alias,
  327. pf_content,
  328. pf_meta_title,
  329. pf_meta_keywords,
  330. pf_meta_description,
  331. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  332. pf_active,
  333. )
  334. if err != nil {
  335. wrap.MsgError(err.Error())
  336. return
  337. }
  338. wrap.Write(`window.location='/cp/';`)
  339. } else {
  340. // Update page
  341. _, err := wrap.DB.Query(
  342. `UPDATE pages SET
  343. name = ?,
  344. alias = ?,
  345. content = ?,
  346. meta_title = ?,
  347. meta_keywords = ?,
  348. meta_description = ?,
  349. active = ?
  350. WHERE
  351. id = ?
  352. ;`,
  353. pf_name,
  354. pf_alias,
  355. pf_content,
  356. pf_meta_title,
  357. pf_meta_keywords,
  358. pf_meta_description,
  359. pf_active,
  360. utils.StrToInt(pf_id),
  361. )
  362. if err != nil {
  363. wrap.MsgError(err.Error())
  364. return
  365. }
  366. wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
  367. }
  368. })
  369. }
  370. func (this *Modules) RegisterAction_IndexDelete() *Action {
  371. return this.newAction(AInfo{
  372. WantDB: true,
  373. Mount: "index-delete",
  374. WantAdmin: true,
  375. }, func(wrap *wrapper.Wrapper) {
  376. pf_id := wrap.R.FormValue("id")
  377. if !utils.IsNumeric(pf_id) {
  378. wrap.MsgError(`Inner system error`)
  379. return
  380. }
  381. // Delete page
  382. _, err := wrap.DB.Query(
  383. `DELETE FROM pages WHERE id = ?;`,
  384. utils.StrToInt(pf_id),
  385. )
  386. if err != nil {
  387. wrap.MsgError(err.Error())
  388. return
  389. }
  390. // Reload current page
  391. wrap.Write(`window.location.reload(false);`)
  392. })
  393. }
  394. func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
  395. return this.newAction(AInfo{
  396. WantDB: false,
  397. Mount: "index-mysql-setup",
  398. }, func(wrap *wrapper.Wrapper) {
  399. pf_host := wrap.R.FormValue("host")
  400. pf_port := wrap.R.FormValue("port")
  401. pf_name := wrap.R.FormValue("name")
  402. pf_user := wrap.R.FormValue("user")
  403. pf_password := wrap.R.FormValue("password")
  404. if pf_host == "" {
  405. wrap.MsgError(`Please specify host for MySQL connection`)
  406. return
  407. }
  408. if pf_port == "" {
  409. wrap.MsgError(`Please specify host port for MySQL connection`)
  410. return
  411. }
  412. if _, err := strconv.Atoi(pf_port); err != nil {
  413. wrap.MsgError(`MySQL host port must be integer number`)
  414. return
  415. }
  416. if pf_name == "" {
  417. wrap.MsgError(`Please specify MySQL database name`)
  418. return
  419. }
  420. if pf_user == "" {
  421. wrap.MsgError(`Please specify MySQL user`)
  422. return
  423. }
  424. // Try connect to mysql
  425. db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
  426. if err != nil {
  427. wrap.MsgError(err.Error())
  428. return
  429. }
  430. defer db.Close()
  431. err = db.Ping()
  432. if err != nil {
  433. wrap.MsgError(err.Error())
  434. return
  435. }
  436. // Try to install all tables
  437. _, err = db.Query(fmt.Sprintf(
  438. `CREATE TABLE %s.users (
  439. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  440. first_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name',
  441. last_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name',
  442. email VARCHAR(64) NOT NULL COMMENT 'User email',
  443. password VARCHAR(32) NOT NULL COMMENT 'User password (MD5)',
  444. admin int(1) NOT NULL COMMENT 'Is admin user or not',
  445. active int(1) NOT NULL COMMENT 'Is active user or not',
  446. PRIMARY KEY (id)
  447. ) ENGINE = InnoDB;`,
  448. pf_name))
  449. if err != nil {
  450. wrap.MsgError(err.Error())
  451. return
  452. }
  453. _, err = db.Query(fmt.Sprintf(
  454. `ALTER TABLE %s.users ADD UNIQUE KEY email (email);`,
  455. pf_name))
  456. if err != nil {
  457. wrap.MsgError(err.Error())
  458. return
  459. }
  460. _, err = db.Query(fmt.Sprintf(
  461. `CREATE TABLE %s.pages (
  462. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  463. user int(11) NOT NULL COMMENT 'User id',
  464. name varchar(255) NOT NULL COMMENT 'Page name',
  465. alias varchar(255) NOT NULL COMMENT 'Page url part',
  466. content text NOT NULL COMMENT 'Page content',
  467. meta_title varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title',
  468. meta_keywords varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords',
  469. meta_description varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description',
  470. datetime datetime NOT NULL COMMENT 'Creation date/time',
  471. active int(1) NOT NULL COMMENT 'Is active page or not',
  472. PRIMARY KEY (id)
  473. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  474. pf_name))
  475. if err != nil {
  476. wrap.MsgError(err.Error())
  477. return
  478. }
  479. _, err = db.Query(fmt.Sprintf(
  480. `ALTER TABLE %s.pages ADD UNIQUE KEY alias (alias);`,
  481. pf_name))
  482. if err != nil {
  483. wrap.MsgError(err.Error())
  484. return
  485. }
  486. // Save mysql config file
  487. err = utils.MySqlConfigWrite(wrap.DConfig+string(os.PathSeparator)+"mysql.json", pf_host, pf_port, pf_name, pf_user, pf_password)
  488. if err != nil {
  489. wrap.MsgError(err.Error())
  490. return
  491. }
  492. // Reload current page
  493. wrap.Write(`window.location.reload(false);`)
  494. })
  495. }
  496. func (this *Modules) RegisterAction_IndexFirstUser() *Action {
  497. return this.newAction(AInfo{
  498. WantDB: true,
  499. Mount: "index-first-user",
  500. }, func(wrap *wrapper.Wrapper) {
  501. pf_first_name := wrap.R.FormValue("first_name")
  502. pf_last_name := wrap.R.FormValue("last_name")
  503. pf_email := wrap.R.FormValue("email")
  504. pf_password := wrap.R.FormValue("password")
  505. if pf_email == "" {
  506. wrap.MsgError(`Please specify user email`)
  507. return
  508. }
  509. if !utils.IsValidEmail(pf_email) {
  510. wrap.MsgError(`Please specify correct user email`)
  511. return
  512. }
  513. if pf_password == "" {
  514. wrap.MsgError(`Please specify user password`)
  515. return
  516. }
  517. _, err := wrap.DB.Query(
  518. `INSERT INTO users SET
  519. first_name = ?,
  520. last_name = ?,
  521. email = ?,
  522. password = MD5(?),
  523. admin = 1,
  524. active = 1
  525. ;`,
  526. pf_first_name,
  527. pf_last_name,
  528. pf_email,
  529. pf_password,
  530. )
  531. if err != nil {
  532. wrap.MsgError(err.Error())
  533. return
  534. }
  535. // Add home page
  536. _, err = wrap.DB.Query(
  537. `INSERT INTO pages SET
  538. user = ?,
  539. name = ?,
  540. alias = ?,
  541. content = ?,
  542. datetime = ?,
  543. active = ?
  544. ;`,
  545. 1,
  546. "Home",
  547. "/",
  548. "<p>Hello World from Fave CMS!</p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Feugiat in ante metus dictum at tempor commodo ullamcorper a. Et malesuada fames ac turpis egestas sed tempus urna et. Euismod elementum nisi quis eleifend. Nisi porta lorem mollis aliquam ut porttitor. Ac turpis egestas maecenas pharetra convallis posuere. Nunc non blandit massa enim nec dui. Commodo elit at imperdiet dui accumsan sit amet nulla. Viverra accumsan in nisl nisi scelerisque. Dui nunc mattis enim ut tellus. Molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Faucibus ornare suspendisse sed nisi lacus. Nulla facilisi morbi tempus iaculis. Ut eu sem integer vitae justo eget magna fermentum iaculis. Ullamcorper sit amet risus nullam eget felis eget nunc. Volutpat sed cras ornare arcu dui vivamus. Eget magna fermentum iaculis eu non diam.</p><p>Arcu ac tortor dignissim convallis aenean et tortor. Vitae auctor eu augue ut lectus arcu. Ac turpis egestas integer eget aliquet nibh praesent. Interdum velit euismod in pellentesque massa placerat duis. Vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt. Nisl rhoncus mattis rhoncus urna neque viverra justo. Odio ut enim blandit volutpat. Ac auctor augue mauris augue neque gravida. Ut lectus arcu bibendum at varius vel. Porttitor leo a diam sollicitudin tempor id eu nisl nunc. Dolor sit amet consectetur adipiscing elit duis tristique. Semper quis lectus nulla at volutpat diam ut. Sapien eget mi proin sed.</p><p>Ante metus dictum at tempor commodo ullamcorper a. Facilisis mauris sit amet massa vitae. Enim neque volutpat ac tincidunt vitae. Tempus quam pellentesque nec nam aliquam sem. Mollis aliquam ut porttitor leo a diam sollicitudin. Nunc pulvinar sapien et ligula ullamcorper. Dignissim suspendisse in est ante in nibh mauris. Eget egestas purus viverra accumsan in. Vitae tempus quam pellentesque nec nam aliquam sem et. Sodales ut etiam sit amet nisl. Aliquet risus feugiat in ante. Rhoncus urna neque viverra justo nec ultrices dui sapien. Sit amet aliquam id diam maecenas ultricies. Sed odio morbi quis commodo odio aenean sed adipiscing diam.</p><p>Integer enim neque volutpat ac. Euismod quis viverra nibh cras pulvinar mattis nunc sed blandit. Pellentesque habitant morbi tristique senectus. Vitae purus faucibus ornare suspendisse sed nisi lacus. Sem fringilla ut morbi tincidunt augue. Purus non enim praesent elementum facilisis leo vel fringilla est. Dictumst vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt. Magna eget est lorem ipsum. At tempor commodo ullamcorper a. Pulvinar pellentesque habitant morbi tristique senectus et.</p><p>Nisl purus in mollis nunc sed. Tincidunt vitae semper quis lectus nulla. Eget felis eget nunc lobortis mattis aliquam faucibus purus in. Integer enim neque volutpat ac tincidunt vitae semper. Urna nec tincidunt praesent semper feugiat. Dis parturient montes nascetur ridiculus mus mauris vitae ultricies. Non odio euismod lacinia at. Aenean sed adipiscing diam donec adipiscing tristique risus. Sem nulla pharetra diam sit amet nisl suscipit. Mauris nunc congue nisi vitae suscipit. Magna fermentum iaculis eu non diam phasellus vestibulum lorem sed. Donec massa sapien faucibus et. Purus non enim praesent elementum facilisis. Nisi vitae suscipit tellus mauris a diam. Donec ultrices tincidunt arcu non sodales neque. Praesent tristique magna sit amet purus gravida quis blandit turpis. Aliquet eget sit amet tellus cras. Senectus et netus et malesuada fames. Faucibus pulvinar elementum integer enim. Non nisi est sit amet facilisis magna etiam tempor orci.</p>",
  549. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  550. 1,
  551. )
  552. if err != nil {
  553. wrap.MsgError(err.Error())
  554. return
  555. }
  556. // Reload current page
  557. wrap.Write(`window.location.reload(false);`)
  558. })
  559. }
  560. func (this *Modules) RegisterAction_IndexUserSignIn() *Action {
  561. return this.newAction(AInfo{
  562. WantDB: true,
  563. Mount: "index-user-sign-in",
  564. }, func(wrap *wrapper.Wrapper) {
  565. pf_email := wrap.R.FormValue("email")
  566. pf_password := wrap.R.FormValue("password")
  567. if pf_email == "" {
  568. wrap.MsgError(`Please specify user email`)
  569. return
  570. }
  571. if !utils.IsValidEmail(pf_email) {
  572. wrap.MsgError(`Please specify correct user email`)
  573. return
  574. }
  575. if pf_password == "" {
  576. wrap.MsgError(`Please specify user password`)
  577. return
  578. }
  579. if wrap.S.GetInt("UserId", 0) > 0 {
  580. wrap.MsgError(`You already logined`)
  581. return
  582. }
  583. var user_id int
  584. err := wrap.DB.QueryRow(
  585. `SELECT
  586. id
  587. FROM
  588. users
  589. WHERE
  590. email = ? and
  591. password = MD5(?) and
  592. admin = 1 and
  593. active = 1
  594. LIMIT 1;`,
  595. pf_email,
  596. pf_password,
  597. ).Scan(
  598. &user_id,
  599. )
  600. if err != nil && err != sql.ErrNoRows {
  601. wrap.MsgError(err.Error())
  602. return
  603. }
  604. if err == sql.ErrNoRows {
  605. wrap.MsgError(`Incorrect email or password`)
  606. return
  607. }
  608. // Save to current session
  609. wrap.S.SetInt("UserId", user_id)
  610. // Reload current page
  611. wrap.Write(`window.location.reload(false);`)
  612. })
  613. }
  614. func (this *Modules) RegisterAction_IndexUserLogout() *Action {
  615. return this.newAction(AInfo{
  616. WantDB: true,
  617. Mount: "index-user-logout",
  618. WantUser: true,
  619. }, func(wrap *wrapper.Wrapper) {
  620. // Reset session var
  621. wrap.S.SetInt("UserId", 0)
  622. // Reload current page
  623. wrap.Write(`window.location.reload(false);`)
  624. })
  625. }
  626. func (this *Modules) RegisterAction_IndexUserUpdateProfile() *Action {
  627. return this.newAction(AInfo{
  628. WantDB: true,
  629. Mount: "index-user-update-profile",
  630. WantUser: true,
  631. }, func(wrap *wrapper.Wrapper) {
  632. pf_first_name := wrap.R.FormValue("first_name")
  633. pf_last_name := wrap.R.FormValue("last_name")
  634. pf_email := wrap.R.FormValue("email")
  635. pf_password := wrap.R.FormValue("password")
  636. if pf_email == "" {
  637. wrap.MsgError(`Please specify user email`)
  638. return
  639. }
  640. if !utils.IsValidEmail(pf_email) {
  641. wrap.MsgError(`Please specify correct user email`)
  642. return
  643. }
  644. if pf_password != "" {
  645. // Update with password if set
  646. _, err := wrap.DB.Query(
  647. `UPDATE users SET
  648. first_name = ?,
  649. last_name = ?,
  650. email = ?,
  651. password = MD5(?)
  652. WHERE
  653. id = ?
  654. ;`,
  655. pf_first_name,
  656. pf_last_name,
  657. pf_email,
  658. pf_password,
  659. wrap.User.A_id,
  660. )
  661. if err != nil {
  662. wrap.MsgError(err.Error())
  663. return
  664. }
  665. } else {
  666. // Update without password if not set
  667. _, err := wrap.DB.Query(
  668. `UPDATE users SET
  669. first_name = ?,
  670. last_name = ?,
  671. email = ?
  672. WHERE
  673. id = ?
  674. ;`,
  675. pf_first_name,
  676. pf_last_name,
  677. pf_email,
  678. wrap.User.A_id,
  679. )
  680. if err != nil {
  681. wrap.MsgError(err.Error())
  682. return
  683. }
  684. }
  685. // Reload current page
  686. wrap.Write(`window.location.reload(false);`)
  687. })
  688. }