module_index.go 9.2 KB

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