module_index.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "fmt"
  6. "html"
  7. "os"
  8. "strconv"
  9. "golang-fave/assets"
  10. "golang-fave/consts"
  11. "golang-fave/engine/builder"
  12. "golang-fave/engine/wrapper"
  13. "golang-fave/utils"
  14. )
  15. func (this *Modules) RegisterModule_Index() *Module {
  16. return this.newModule(MInfo{
  17. WantDB: true,
  18. Mount: "index",
  19. Name: "Pages",
  20. Order: 0,
  21. Icon: assets.SysSvgIconPage,
  22. Sub: &[]MISub{
  23. {Mount: "default", Name: "List of Pages", Icon: assets.SysSvgIconList},
  24. {Mount: "add", Name: "Add New Page", Icon: assets.SysSvgIconPlus},
  25. },
  26. }, func(wrap *wrapper.Wrapper) {
  27. // Front-end
  28. wrap.RenderFrontEnd("index", consts.TmplDataModIndex{
  29. MetaTitle: "Meta Title",
  30. MetaKeywords: "Meta Keywords",
  31. MetaDescription: "Meta Description",
  32. MainMenuItems: []consts.TmplDataMainMenuItem{
  33. {Name: "Home", Link: "/", Active: true},
  34. {Name: "Item 1", Link: "/#1", Active: false},
  35. {Name: "Item 2", Link: "/#2", Active: false},
  36. {Name: "Item 3", Link: "/#3", Active: false},
  37. },
  38. })
  39. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  40. content := ""
  41. sidebar := ""
  42. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  43. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  44. {Name: "List of Pages"},
  45. })
  46. content += builder.DataTable(wrap, "pages", "id", "DESC", []builder.DataTableRow{
  47. {
  48. DBField: "id",
  49. },
  50. {
  51. DBField: "name",
  52. NameInTable: "Page",
  53. CallBack: func(values *[]string) string {
  54. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  55. slug := html.EscapeString((*values)[2])
  56. return `<div>` + name + `</div><div><small>` + slug + `</small></div>`
  57. },
  58. },
  59. {
  60. DBField: "slug",
  61. },
  62. {
  63. DBField: "datetime",
  64. NameInTable: "Date/Time",
  65. },
  66. {
  67. DBField: "status",
  68. NameInTable: "Active",
  69. },
  70. }, func(values *[]string) string {
  71. return `<a class="ico" href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` +
  72. assets.SysSvgIconEdit + `</a>` +
  73. `<a class="ico" href="#">` + assets.SysSvgIconRemove + `</a>`
  74. }, "/cp/"+wrap.CurrModule+"/")
  75. } else if wrap.CurrSubModule == "add" {
  76. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  77. {Name: "Add New Page"},
  78. })
  79. }
  80. return this.getSidebarModules(wrap), content, sidebar
  81. })
  82. }
  83. func (this *Modules) RegisterAction_MysqlSetup() *Action {
  84. return this.newAction(AInfo{
  85. WantDB: false,
  86. Mount: "mysql",
  87. }, func(wrap *wrapper.Wrapper) {
  88. pf_host := wrap.R.FormValue("host")
  89. pf_port := wrap.R.FormValue("port")
  90. pf_name := wrap.R.FormValue("name")
  91. pf_user := wrap.R.FormValue("user")
  92. pf_password := wrap.R.FormValue("password")
  93. if pf_host == "" {
  94. wrap.MsgError(`Please specify host for MySQL connection`)
  95. return
  96. }
  97. if pf_port == "" {
  98. wrap.MsgError(`Please specify host port for MySQL connection`)
  99. return
  100. }
  101. if _, err := strconv.Atoi(pf_port); err != nil {
  102. wrap.MsgError(`MySQL host port must be integer number`)
  103. return
  104. }
  105. if pf_name == "" {
  106. wrap.MsgError(`Please specify MySQL database name`)
  107. return
  108. }
  109. if pf_user == "" {
  110. wrap.MsgError(`Please specify MySQL user`)
  111. return
  112. }
  113. // Try connect to mysql
  114. db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
  115. if err != nil {
  116. wrap.MsgError(err.Error())
  117. return
  118. }
  119. defer db.Close()
  120. err = db.Ping()
  121. if err != nil {
  122. wrap.MsgError(err.Error())
  123. return
  124. }
  125. // Try to install all tables
  126. _, err = db.Query(fmt.Sprintf(
  127. "CREATE TABLE `%s`.`users` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI', `first_name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name', `last_name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name', `email` VARCHAR(64) NOT NULL COMMENT 'User email', `password` VARCHAR(32) NOT NULL COMMENT 'User password (MD5)', PRIMARY KEY (`id`)) ENGINE = InnoDB;",
  128. pf_name))
  129. if err != nil {
  130. wrap.MsgError(err.Error())
  131. return
  132. }
  133. _, err = db.Query(fmt.Sprintf(
  134. "CREATE TABLE `%s`.`pages` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI', `user` int(11) NOT NULL COMMENT 'User id', `name` varchar(255) NOT NULL COMMENT 'Page name', `slug` varchar(255) NOT NULL COMMENT 'Page url part', `content` text NOT NULL COMMENT 'Page content', `meta_title` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title', `meta_keywords` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords', `meta_description` varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description', `datetime` datetime NOT NULL COMMENT 'Creation date/time', `status` enum('draft','public','trash') NOT NULL COMMENT 'Page status', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
  135. pf_name))
  136. if err != nil {
  137. wrap.MsgError(err.Error())
  138. return
  139. }
  140. // Save mysql config file
  141. err = utils.MySqlConfigWrite(wrap.DConfig+string(os.PathSeparator)+"mysql.json", pf_host, pf_port, pf_name, pf_user, pf_password)
  142. if err != nil {
  143. wrap.MsgError(err.Error())
  144. return
  145. }
  146. // Reload current page
  147. wrap.Write(`window.location.reload(false);`)
  148. })
  149. }
  150. func (this *Modules) RegisterAction_CpFirstUser() *Action {
  151. return this.newAction(AInfo{
  152. WantDB: true,
  153. Mount: "first-user",
  154. }, func(wrap *wrapper.Wrapper) {
  155. pf_first_name := wrap.R.FormValue("first_name")
  156. pf_last_name := wrap.R.FormValue("last_name")
  157. pf_email := wrap.R.FormValue("email")
  158. pf_password := wrap.R.FormValue("password")
  159. if pf_email == "" {
  160. wrap.MsgError(`Please specify user email`)
  161. return
  162. }
  163. if !utils.IsValidEmail(pf_email) {
  164. wrap.MsgError(`Please specify correct user email`)
  165. return
  166. }
  167. if pf_password == "" {
  168. wrap.MsgError(`Please specify user password`)
  169. return
  170. }
  171. _, err := wrap.DB.Query(
  172. "INSERT INTO `users` SET `first_name` = ?, `last_name` = ?, `email` = ?, `password` = MD5(?);",
  173. pf_first_name, pf_last_name, pf_email, pf_password)
  174. if err != nil {
  175. wrap.MsgError(err.Error())
  176. return
  177. }
  178. // Reload current page
  179. wrap.Write(`window.location.reload(false);`)
  180. })
  181. }
  182. func (this *Modules) RegisterAction_CpUserLogin() *Action {
  183. return this.newAction(AInfo{
  184. WantDB: true,
  185. Mount: "signin",
  186. }, func(wrap *wrapper.Wrapper) {
  187. pf_email := wrap.R.FormValue("email")
  188. pf_password := wrap.R.FormValue("password")
  189. if pf_email == "" {
  190. wrap.MsgError(`Please specify user email`)
  191. return
  192. }
  193. if !utils.IsValidEmail(pf_email) {
  194. wrap.MsgError(`Please specify correct user email`)
  195. return
  196. }
  197. if pf_password == "" {
  198. wrap.MsgError(`Please specify user password`)
  199. return
  200. }
  201. if wrap.S.GetInt("UserId", 0) > 0 {
  202. wrap.MsgError(`You already logined`)
  203. return
  204. }
  205. var user_id int
  206. err := wrap.DB.QueryRow(
  207. "SELECT `id` FROM `users` WHERE `email` = ? and `password` = MD5(?) LIMIT 1;",
  208. pf_email, pf_password).Scan(&user_id)
  209. if err != nil && err != sql.ErrNoRows {
  210. wrap.MsgError(err.Error())
  211. return
  212. }
  213. if err == sql.ErrNoRows {
  214. wrap.MsgError(`Incorrect email or password`)
  215. return
  216. }
  217. // Save to current session
  218. wrap.S.SetInt("UserId", user_id)
  219. // Reload current page
  220. wrap.Write(`window.location.reload(false);`)
  221. })
  222. }
  223. func (this *Modules) RegisterAction_CpUserLogout() *Action {
  224. return this.newAction(AInfo{
  225. WantDB: true,
  226. Mount: "singout",
  227. WantUser: true,
  228. }, func(wrap *wrapper.Wrapper) {
  229. // Reset session var
  230. wrap.S.SetInt("UserId", 0)
  231. // Reload current page
  232. wrap.Write(`window.location.reload(false);`)
  233. })
  234. }
  235. func (this *Modules) RegisterAction_CpUserSettings() *Action {
  236. return this.newAction(AInfo{
  237. WantDB: true,
  238. Mount: "user-settings",
  239. WantUser: true,
  240. }, func(wrap *wrapper.Wrapper) {
  241. pf_first_name := wrap.R.FormValue("first_name")
  242. pf_last_name := wrap.R.FormValue("last_name")
  243. pf_email := wrap.R.FormValue("email")
  244. pf_password := wrap.R.FormValue("password")
  245. if pf_email == "" {
  246. wrap.MsgError(`Please specify user email`)
  247. return
  248. }
  249. if !utils.IsValidEmail(pf_email) {
  250. wrap.MsgError(`Please specify correct user email`)
  251. return
  252. }
  253. if pf_password != "" {
  254. // Update with password if set
  255. _, err := wrap.DB.Query(
  256. "UPDATE `users` SET `first_name` = ?, `last_name` = ?, `email` = ?, `password` = MD5(?) WHERE `id` = ?;",
  257. pf_first_name, pf_last_name, pf_email, pf_password, wrap.User.A_id)
  258. if err != nil {
  259. wrap.MsgError(err.Error())
  260. return
  261. }
  262. } else {
  263. // Update without password if not set
  264. _, err := wrap.DB.Query(
  265. "UPDATE `users` SET `first_name` = ?, `last_name` = ?, `email` = ? WHERE `id` = ?;",
  266. pf_first_name, pf_last_name, pf_email, wrap.User.A_id)
  267. if err != nil {
  268. wrap.MsgError(err.Error())
  269. return
  270. }
  271. }
  272. // Reload current page
  273. wrap.Write(`window.location.reload(false);`)
  274. })
  275. }