module_index.go 9.0 KB

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