module_index.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 (
  128. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  129. first_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name',
  130. last_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name',
  131. email VARCHAR(64) NOT NULL COMMENT 'User email',
  132. password VARCHAR(32) NOT NULL COMMENT 'User password (MD5)',
  133. PRIMARY KEY (id)
  134. ) ENGINE = InnoDB;`,
  135. pf_name))
  136. if err != nil {
  137. wrap.MsgError(err.Error())
  138. return
  139. }
  140. _, err = db.Query(fmt.Sprintf(
  141. `CREATE TABLE %s.pages (
  142. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  143. user int(11) NOT NULL COMMENT 'User id',
  144. name varchar(255) NOT NULL COMMENT 'Page name',
  145. slug varchar(255) NOT NULL COMMENT 'Page url part',
  146. content text NOT NULL COMMENT 'Page content',
  147. meta_title varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title',
  148. meta_keywords varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords',
  149. meta_description varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description',
  150. datetime datetime NOT NULL COMMENT 'Creation date/time',
  151. status enum('draft','public','trash') NOT NULL COMMENT 'Page status',
  152. PRIMARY KEY (id)
  153. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  154. pf_name))
  155. if err != nil {
  156. wrap.MsgError(err.Error())
  157. return
  158. }
  159. // Save mysql config file
  160. err = utils.MySqlConfigWrite(wrap.DConfig+string(os.PathSeparator)+"mysql.json", pf_host, pf_port, pf_name, pf_user, pf_password)
  161. if err != nil {
  162. wrap.MsgError(err.Error())
  163. return
  164. }
  165. // Reload current page
  166. wrap.Write(`window.location.reload(false);`)
  167. })
  168. }
  169. func (this *Modules) RegisterAction_CpFirstUser() *Action {
  170. return this.newAction(AInfo{
  171. WantDB: true,
  172. Mount: "first-user",
  173. }, func(wrap *wrapper.Wrapper) {
  174. pf_first_name := wrap.R.FormValue("first_name")
  175. pf_last_name := wrap.R.FormValue("last_name")
  176. pf_email := wrap.R.FormValue("email")
  177. pf_password := wrap.R.FormValue("password")
  178. if pf_email == "" {
  179. wrap.MsgError(`Please specify user email`)
  180. return
  181. }
  182. if !utils.IsValidEmail(pf_email) {
  183. wrap.MsgError(`Please specify correct user email`)
  184. return
  185. }
  186. if pf_password == "" {
  187. wrap.MsgError(`Please specify user password`)
  188. return
  189. }
  190. _, err := wrap.DB.Query(
  191. `INSERT INTO users SET
  192. first_name = ?,
  193. last_name = ?,
  194. email = ?,
  195. password = MD5(?)
  196. ;`,
  197. pf_first_name,
  198. pf_last_name,
  199. pf_email,
  200. pf_password,
  201. )
  202. if err != nil {
  203. wrap.MsgError(err.Error())
  204. return
  205. }
  206. // Reload current page
  207. wrap.Write(`window.location.reload(false);`)
  208. })
  209. }
  210. func (this *Modules) RegisterAction_CpUserLogin() *Action {
  211. return this.newAction(AInfo{
  212. WantDB: true,
  213. Mount: "signin",
  214. }, func(wrap *wrapper.Wrapper) {
  215. pf_email := wrap.R.FormValue("email")
  216. pf_password := wrap.R.FormValue("password")
  217. if pf_email == "" {
  218. wrap.MsgError(`Please specify user email`)
  219. return
  220. }
  221. if !utils.IsValidEmail(pf_email) {
  222. wrap.MsgError(`Please specify correct user email`)
  223. return
  224. }
  225. if pf_password == "" {
  226. wrap.MsgError(`Please specify user password`)
  227. return
  228. }
  229. if wrap.S.GetInt("UserId", 0) > 0 {
  230. wrap.MsgError(`You already logined`)
  231. return
  232. }
  233. var user_id int
  234. err := wrap.DB.QueryRow(
  235. `SELECT
  236. id
  237. FROM
  238. users
  239. WHERE
  240. email = ? and
  241. password = MD5(?)
  242. LIMIT 1;`,
  243. pf_email,
  244. pf_password,
  245. ).Scan(
  246. &user_id,
  247. )
  248. if err != nil && err != sql.ErrNoRows {
  249. wrap.MsgError(err.Error())
  250. return
  251. }
  252. if err == sql.ErrNoRows {
  253. wrap.MsgError(`Incorrect email or password`)
  254. return
  255. }
  256. // Save to current session
  257. wrap.S.SetInt("UserId", user_id)
  258. // Reload current page
  259. wrap.Write(`window.location.reload(false);`)
  260. })
  261. }
  262. func (this *Modules) RegisterAction_CpUserLogout() *Action {
  263. return this.newAction(AInfo{
  264. WantDB: true,
  265. Mount: "singout",
  266. WantUser: true,
  267. }, func(wrap *wrapper.Wrapper) {
  268. // Reset session var
  269. wrap.S.SetInt("UserId", 0)
  270. // Reload current page
  271. wrap.Write(`window.location.reload(false);`)
  272. })
  273. }
  274. func (this *Modules) RegisterAction_CpUserSettings() *Action {
  275. return this.newAction(AInfo{
  276. WantDB: true,
  277. Mount: "user-settings",
  278. WantUser: true,
  279. }, func(wrap *wrapper.Wrapper) {
  280. pf_first_name := wrap.R.FormValue("first_name")
  281. pf_last_name := wrap.R.FormValue("last_name")
  282. pf_email := wrap.R.FormValue("email")
  283. pf_password := wrap.R.FormValue("password")
  284. if pf_email == "" {
  285. wrap.MsgError(`Please specify user email`)
  286. return
  287. }
  288. if !utils.IsValidEmail(pf_email) {
  289. wrap.MsgError(`Please specify correct user email`)
  290. return
  291. }
  292. if pf_password != "" {
  293. // Update with password if set
  294. _, err := wrap.DB.Query(
  295. `UPDATE users SET
  296. first_name = ?,
  297. last_name = ?,
  298. email = ?,
  299. password = MD5(?)
  300. WHERE
  301. id = ?
  302. ;`,
  303. pf_first_name,
  304. pf_last_name,
  305. pf_email,
  306. pf_password,
  307. wrap.User.A_id,
  308. )
  309. if err != nil {
  310. wrap.MsgError(err.Error())
  311. return
  312. }
  313. } else {
  314. // Update without password if not set
  315. _, err := wrap.DB.Query(
  316. `UPDATE users SET
  317. first_name = ?,
  318. last_name = ?,
  319. email = ?
  320. WHERE
  321. id = ?
  322. ;`,
  323. pf_first_name,
  324. pf_last_name,
  325. pf_email,
  326. wrap.User.A_id,
  327. )
  328. if err != nil {
  329. wrap.MsgError(err.Error())
  330. return
  331. }
  332. }
  333. // Reload current page
  334. wrap.Write(`window.location.reload(false);`)
  335. })
  336. }