module_index.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. Classes: "d-none d-sm-table-cell",
  112. CallBack: func(values *[]string) string {
  113. t := int64(utils.StrToInt((*values)[3]))
  114. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  115. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  116. },
  117. },
  118. {
  119. DBField: "active",
  120. NameInTable: "Active",
  121. Classes: "d-none d-sm-table-cell",
  122. CallBack: func(values *[]string) string {
  123. return builder.CheckBox(utils.StrToInt((*values)[4]))
  124. },
  125. },
  126. }, func(values *[]string) string {
  127. return builder.DataTableAction(&[]builder.DataTableActionRow{
  128. {
  129. Icon: assets.SysSvgIconView,
  130. Href: (*values)[2],
  131. Hint: "View",
  132. Target: "_blank",
  133. },
  134. {
  135. Icon: assets.SysSvgIconEdit,
  136. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  137. Hint: "Edit",
  138. },
  139. {
  140. Icon: assets.SysSvgIconRemove,
  141. Href: "javascript:fave.ActionDataTableDelete(this,'index-delete','" +
  142. (*values)[0] + "','Are you sure want to delete page?');",
  143. Hint: "Delete",
  144. },
  145. })
  146. }, "/cp/"+wrap.CurrModule+"/")
  147. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  148. if wrap.CurrSubModule == "add" {
  149. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  150. {Name: "Add new page"},
  151. })
  152. } else {
  153. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  154. {Name: "Modify page"},
  155. })
  156. }
  157. data := utils.MySql_page{
  158. A_id: 0,
  159. A_user: 0,
  160. A_name: "",
  161. A_alias: "",
  162. A_content: "",
  163. A_meta_title: "",
  164. A_meta_keywords: "",
  165. A_meta_description: "",
  166. A_datetime: 0,
  167. A_active: 0,
  168. }
  169. if wrap.CurrSubModule == "modify" {
  170. if len(wrap.UrlArgs) != 3 {
  171. return "", "", ""
  172. }
  173. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  174. return "", "", ""
  175. }
  176. err := wrap.DB.QueryRow(`
  177. SELECT
  178. id,
  179. user,
  180. name,
  181. alias,
  182. content,
  183. meta_title,
  184. meta_keywords,
  185. meta_description,
  186. active
  187. FROM
  188. pages
  189. WHERE
  190. id = ?
  191. LIMIT 1;`,
  192. utils.StrToInt(wrap.UrlArgs[2]),
  193. ).Scan(
  194. &data.A_id,
  195. &data.A_user,
  196. &data.A_name,
  197. &data.A_alias,
  198. &data.A_content,
  199. &data.A_meta_title,
  200. &data.A_meta_keywords,
  201. &data.A_meta_description,
  202. &data.A_active,
  203. )
  204. if err != nil {
  205. return "", "", ""
  206. }
  207. }
  208. content += builder.DataForm(wrap, []builder.DataFormField{
  209. {
  210. Kind: builder.DFKHidden,
  211. Name: "action",
  212. Value: "index-modify",
  213. },
  214. {
  215. Kind: builder.DFKHidden,
  216. Name: "id",
  217. Value: utils.IntToStr(data.A_id),
  218. },
  219. {
  220. Kind: builder.DFKText,
  221. Caption: "Page name",
  222. Name: "name",
  223. Value: data.A_name,
  224. },
  225. {
  226. Kind: builder.DFKText,
  227. Caption: "Page alias",
  228. Name: "alias",
  229. Value: data.A_alias,
  230. Hint: "Example: /about-us/ or /about-us.html or /about/team.html",
  231. },
  232. {
  233. Kind: builder.DFKTextArea,
  234. Caption: "Page content",
  235. Name: "content",
  236. Value: data.A_content,
  237. },
  238. {
  239. Kind: builder.DFKText,
  240. Caption: "Meta title",
  241. Name: "meta_title",
  242. Value: data.A_meta_title,
  243. },
  244. {
  245. Kind: builder.DFKText,
  246. Caption: "Meta keywords",
  247. Name: "meta_keywords",
  248. Value: data.A_meta_keywords,
  249. },
  250. {
  251. Kind: builder.DFKTextArea,
  252. Caption: "Meta description",
  253. Name: "meta_description",
  254. Value: data.A_meta_description,
  255. },
  256. {
  257. Kind: builder.DFKCheckBox,
  258. Caption: "Active",
  259. Name: "active",
  260. Value: utils.IntToStr(data.A_active),
  261. },
  262. {
  263. Kind: builder.DFKMessage,
  264. },
  265. {
  266. Kind: builder.DFKSubmit,
  267. Value: "Add",
  268. Target: "add-edit-button",
  269. },
  270. })
  271. if wrap.CurrSubModule == "add" {
  272. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  273. } else {
  274. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  275. }
  276. }
  277. return this.getSidebarModules(wrap), content, sidebar
  278. })
  279. }
  280. func (this *Modules) RegisterAction_IndexModify() *Action {
  281. return this.newAction(AInfo{
  282. WantDB: true,
  283. Mount: "index-modify",
  284. WantAdmin: true,
  285. }, func(wrap *wrapper.Wrapper) {
  286. pf_id := wrap.R.FormValue("id")
  287. pf_name := wrap.R.FormValue("name")
  288. pf_alias := wrap.R.FormValue("alias")
  289. pf_content := wrap.R.FormValue("content")
  290. pf_meta_title := wrap.R.FormValue("meta_title")
  291. pf_meta_keywords := wrap.R.FormValue("meta_keywords")
  292. pf_meta_description := wrap.R.FormValue("meta_description")
  293. pf_active := wrap.R.FormValue("active")
  294. if pf_active == "" {
  295. pf_active = "0"
  296. }
  297. if !utils.IsNumeric(pf_id) {
  298. wrap.MsgError(`Inner system error`)
  299. return
  300. }
  301. if pf_name == "" {
  302. wrap.MsgError(`Please specify page name`)
  303. return
  304. }
  305. if pf_alias == "" {
  306. pf_alias = utils.GenerateAlias(pf_name)
  307. }
  308. if !utils.IsValidAlias(pf_alias) {
  309. wrap.MsgError(`Please specify correct page alias`)
  310. return
  311. }
  312. if pf_id == "0" {
  313. // Add new page
  314. _, err := wrap.DB.Query(
  315. `INSERT INTO pages SET
  316. user = ?,
  317. name = ?,
  318. alias = ?,
  319. content = ?,
  320. meta_title = ?,
  321. meta_keywords = ?,
  322. meta_description = ?,
  323. datetime = ?,
  324. active = ?
  325. ;`,
  326. wrap.User.A_id,
  327. pf_name,
  328. pf_alias,
  329. pf_content,
  330. pf_meta_title,
  331. pf_meta_keywords,
  332. pf_meta_description,
  333. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  334. pf_active,
  335. )
  336. if err != nil {
  337. wrap.MsgError(err.Error())
  338. return
  339. }
  340. wrap.Write(`window.location='/cp/';`)
  341. } else {
  342. // Update page
  343. _, err := wrap.DB.Query(
  344. `UPDATE pages SET
  345. name = ?,
  346. alias = ?,
  347. content = ?,
  348. meta_title = ?,
  349. meta_keywords = ?,
  350. meta_description = ?,
  351. active = ?
  352. WHERE
  353. id = ?
  354. ;`,
  355. pf_name,
  356. pf_alias,
  357. pf_content,
  358. pf_meta_title,
  359. pf_meta_keywords,
  360. pf_meta_description,
  361. pf_active,
  362. utils.StrToInt(pf_id),
  363. )
  364. if err != nil {
  365. wrap.MsgError(err.Error())
  366. return
  367. }
  368. wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
  369. }
  370. })
  371. }
  372. func (this *Modules) RegisterAction_IndexDelete() *Action {
  373. return this.newAction(AInfo{
  374. WantDB: true,
  375. Mount: "index-delete",
  376. WantAdmin: true,
  377. }, func(wrap *wrapper.Wrapper) {
  378. pf_id := wrap.R.FormValue("id")
  379. if !utils.IsNumeric(pf_id) {
  380. wrap.MsgError(`Inner system error`)
  381. return
  382. }
  383. // Delete page
  384. _, err := wrap.DB.Query(
  385. `DELETE FROM pages WHERE id = ?;`,
  386. utils.StrToInt(pf_id),
  387. )
  388. if err != nil {
  389. wrap.MsgError(err.Error())
  390. return
  391. }
  392. // Reload current page
  393. wrap.Write(`window.location.reload(false);`)
  394. })
  395. }
  396. func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
  397. return this.newAction(AInfo{
  398. WantDB: false,
  399. Mount: "index-mysql-setup",
  400. }, func(wrap *wrapper.Wrapper) {
  401. pf_host := wrap.R.FormValue("host")
  402. pf_port := wrap.R.FormValue("port")
  403. pf_name := wrap.R.FormValue("name")
  404. pf_user := wrap.R.FormValue("user")
  405. pf_password := wrap.R.FormValue("password")
  406. if pf_host == "" {
  407. wrap.MsgError(`Please specify host for MySQL connection`)
  408. return
  409. }
  410. if pf_port == "" {
  411. wrap.MsgError(`Please specify host port for MySQL connection`)
  412. return
  413. }
  414. if _, err := strconv.Atoi(pf_port); err != nil {
  415. wrap.MsgError(`MySQL host port must be integer number`)
  416. return
  417. }
  418. if pf_name == "" {
  419. wrap.MsgError(`Please specify MySQL database name`)
  420. return
  421. }
  422. if pf_user == "" {
  423. wrap.MsgError(`Please specify MySQL user`)
  424. return
  425. }
  426. // Try connect to mysql
  427. db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
  428. if err != nil {
  429. wrap.MsgError(err.Error())
  430. return
  431. }
  432. defer db.Close()
  433. err = db.Ping()
  434. if err != nil {
  435. wrap.MsgError(err.Error())
  436. return
  437. }
  438. // Try to install all tables
  439. _, err = db.Query(fmt.Sprintf(
  440. `CREATE TABLE %s.users (
  441. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  442. first_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name',
  443. last_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name',
  444. email VARCHAR(64) NOT NULL COMMENT 'User email',
  445. password VARCHAR(32) NOT NULL COMMENT 'User password (MD5)',
  446. admin int(1) NOT NULL COMMENT 'Is admin user or not',
  447. active int(1) NOT NULL COMMENT 'Is active user or not',
  448. PRIMARY KEY (id)
  449. ) ENGINE = InnoDB;`,
  450. pf_name))
  451. if err != nil {
  452. wrap.MsgError(err.Error())
  453. return
  454. }
  455. _, err = db.Query(fmt.Sprintf(
  456. `ALTER TABLE %s.users ADD UNIQUE KEY email (email);`,
  457. pf_name))
  458. if err != nil {
  459. wrap.MsgError(err.Error())
  460. return
  461. }
  462. _, err = db.Query(fmt.Sprintf(
  463. `CREATE TABLE %s.pages (
  464. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  465. user int(11) NOT NULL COMMENT 'User id',
  466. name varchar(255) NOT NULL COMMENT 'Page name',
  467. alias varchar(255) NOT NULL COMMENT 'Page url part',
  468. content text NOT NULL COMMENT 'Page content',
  469. meta_title varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title',
  470. meta_keywords varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords',
  471. meta_description varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description',
  472. datetime datetime NOT NULL COMMENT 'Creation date/time',
  473. active int(1) NOT NULL COMMENT 'Is active page or not',
  474. PRIMARY KEY (id)
  475. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  476. pf_name))
  477. if err != nil {
  478. wrap.MsgError(err.Error())
  479. return
  480. }
  481. _, err = db.Query(fmt.Sprintf(
  482. `ALTER TABLE %s.pages ADD UNIQUE KEY alias (alias);`,
  483. pf_name))
  484. if err != nil {
  485. wrap.MsgError(err.Error())
  486. return
  487. }
  488. // Save mysql config file
  489. err = utils.MySqlConfigWrite(wrap.DConfig+string(os.PathSeparator)+"mysql.json", pf_host, pf_port, pf_name, pf_user, pf_password)
  490. if err != nil {
  491. wrap.MsgError(err.Error())
  492. return
  493. }
  494. // Reload current page
  495. wrap.Write(`window.location.reload(false);`)
  496. })
  497. }
  498. func (this *Modules) RegisterAction_IndexFirstUser() *Action {
  499. return this.newAction(AInfo{
  500. WantDB: true,
  501. Mount: "index-first-user",
  502. }, func(wrap *wrapper.Wrapper) {
  503. pf_first_name := wrap.R.FormValue("first_name")
  504. pf_last_name := wrap.R.FormValue("last_name")
  505. pf_email := wrap.R.FormValue("email")
  506. pf_password := wrap.R.FormValue("password")
  507. if pf_email == "" {
  508. wrap.MsgError(`Please specify user email`)
  509. return
  510. }
  511. if !utils.IsValidEmail(pf_email) {
  512. wrap.MsgError(`Please specify correct user email`)
  513. return
  514. }
  515. if pf_password == "" {
  516. wrap.MsgError(`Please specify user password`)
  517. return
  518. }
  519. _, err := wrap.DB.Query(
  520. `INSERT INTO users SET
  521. first_name = ?,
  522. last_name = ?,
  523. email = ?,
  524. password = MD5(?),
  525. admin = 1,
  526. active = 1
  527. ;`,
  528. pf_first_name,
  529. pf_last_name,
  530. pf_email,
  531. pf_password,
  532. )
  533. if err != nil {
  534. wrap.MsgError(err.Error())
  535. return
  536. }
  537. // Add home page
  538. _, err = wrap.DB.Query(
  539. `INSERT INTO pages SET
  540. user = ?,
  541. name = ?,
  542. alias = ?,
  543. content = ?,
  544. datetime = ?,
  545. active = ?
  546. ;`,
  547. 1,
  548. "Home",
  549. "/",
  550. "<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>",
  551. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  552. 1,
  553. )
  554. if err != nil {
  555. wrap.MsgError(err.Error())
  556. return
  557. }
  558. // Reload current page
  559. wrap.Write(`window.location.reload(false);`)
  560. })
  561. }
  562. func (this *Modules) RegisterAction_IndexUserSignIn() *Action {
  563. return this.newAction(AInfo{
  564. WantDB: true,
  565. Mount: "index-user-sign-in",
  566. }, func(wrap *wrapper.Wrapper) {
  567. pf_email := wrap.R.FormValue("email")
  568. pf_password := wrap.R.FormValue("password")
  569. if pf_email == "" {
  570. wrap.MsgError(`Please specify user email`)
  571. return
  572. }
  573. if !utils.IsValidEmail(pf_email) {
  574. wrap.MsgError(`Please specify correct user email`)
  575. return
  576. }
  577. if pf_password == "" {
  578. wrap.MsgError(`Please specify user password`)
  579. return
  580. }
  581. if wrap.S.GetInt("UserId", 0) > 0 {
  582. wrap.MsgError(`You already logined`)
  583. return
  584. }
  585. var user_id int
  586. err := wrap.DB.QueryRow(
  587. `SELECT
  588. id
  589. FROM
  590. users
  591. WHERE
  592. email = ? and
  593. password = MD5(?) and
  594. admin = 1 and
  595. active = 1
  596. LIMIT 1;`,
  597. pf_email,
  598. pf_password,
  599. ).Scan(
  600. &user_id,
  601. )
  602. if err != nil && err != sql.ErrNoRows {
  603. wrap.MsgError(err.Error())
  604. return
  605. }
  606. if err == sql.ErrNoRows {
  607. wrap.MsgError(`Incorrect email or password`)
  608. return
  609. }
  610. // Save to current session
  611. wrap.S.SetInt("UserId", user_id)
  612. // Reload current page
  613. wrap.Write(`window.location.reload(false);`)
  614. })
  615. }
  616. func (this *Modules) RegisterAction_IndexUserLogout() *Action {
  617. return this.newAction(AInfo{
  618. WantDB: true,
  619. Mount: "index-user-logout",
  620. WantUser: true,
  621. }, func(wrap *wrapper.Wrapper) {
  622. // Reset session var
  623. wrap.S.SetInt("UserId", 0)
  624. // Reload current page
  625. wrap.Write(`window.location.reload(false);`)
  626. })
  627. }
  628. func (this *Modules) RegisterAction_IndexUserUpdateProfile() *Action {
  629. return this.newAction(AInfo{
  630. WantDB: true,
  631. Mount: "index-user-update-profile",
  632. WantUser: true,
  633. }, func(wrap *wrapper.Wrapper) {
  634. pf_first_name := wrap.R.FormValue("first_name")
  635. pf_last_name := wrap.R.FormValue("last_name")
  636. pf_email := wrap.R.FormValue("email")
  637. pf_password := wrap.R.FormValue("password")
  638. if pf_email == "" {
  639. wrap.MsgError(`Please specify user email`)
  640. return
  641. }
  642. if !utils.IsValidEmail(pf_email) {
  643. wrap.MsgError(`Please specify correct user email`)
  644. return
  645. }
  646. if pf_password != "" {
  647. // Update with password if set
  648. _, err := wrap.DB.Query(
  649. `UPDATE users SET
  650. first_name = ?,
  651. last_name = ?,
  652. email = ?,
  653. password = MD5(?)
  654. WHERE
  655. id = ?
  656. ;`,
  657. pf_first_name,
  658. pf_last_name,
  659. pf_email,
  660. pf_password,
  661. wrap.User.A_id,
  662. )
  663. if err != nil {
  664. wrap.MsgError(err.Error())
  665. return
  666. }
  667. } else {
  668. // Update without password if not set
  669. _, err := wrap.DB.Query(
  670. `UPDATE users SET
  671. first_name = ?,
  672. last_name = ?,
  673. email = ?
  674. WHERE
  675. id = ?
  676. ;`,
  677. pf_first_name,
  678. pf_last_name,
  679. pf_email,
  680. wrap.User.A_id,
  681. )
  682. if err != nil {
  683. wrap.MsgError(err.Error())
  684. return
  685. }
  686. }
  687. // Reload current page
  688. wrap.Write(`window.location.reload(false);`)
  689. })
  690. }