Browse Source

Pages add/edit form, utils funcs

Vova Tkach 6 years ago
parent
commit
fdc4066cb9
2 changed files with 144 additions and 5 deletions
  1. 99 0
      modules/module_index.go
  2. 45 5
      utils/utils.go

+ 99 - 0
modules/module_index.go

@@ -216,6 +216,105 @@ func (this *Modules) RegisterModule_Index() *Module {
 	})
 }
 
+func (this *Modules) RegisterAction_IndexModify() *Action {
+	return this.newAction(AInfo{
+		WantDB:    true,
+		Mount:     "index-modify",
+		WantAdmin: true,
+	}, func(wrap *wrapper.Wrapper) {
+		pf_id := wrap.R.FormValue("id")
+		pf_name := wrap.R.FormValue("name")
+		pf_alias := wrap.R.FormValue("alias")
+		pf_content := wrap.R.FormValue("content")
+		pf_meta_title := wrap.R.FormValue("meta_title")
+		pf_meta_keywords := wrap.R.FormValue("meta_keywords")
+		pf_meta_description := wrap.R.FormValue("meta_description")
+		pf_active := wrap.R.FormValue("active")
+
+		if pf_active == "" {
+			pf_active = "0"
+		}
+
+		if !utils.IsNumeric(pf_id) {
+			wrap.MsgError(`Inner system error`)
+			return
+		}
+
+		if pf_name == "" {
+			wrap.MsgError(`Please specify page name`)
+			return
+		}
+
+		if pf_alias == "" {
+			pf_alias = utils.GenerateAlias(pf_name)
+		}
+
+		if !utils.IsValidAlias(pf_alias) {
+			wrap.MsgError(`Please specify correct page alias`)
+			return
+		}
+
+		if pf_id == "0" {
+			// Add new page
+			_, err := wrap.DB.Query(
+				`INSERT INTO pages SET
+					user = ?,
+					name = ?,
+					alias = ?,
+					content = ?,
+					meta_title = ?,
+					meta_keywords = ?,
+					meta_description = ?,
+					datetime = ?,
+					active = ?
+				;`,
+				wrap.User.A_id,
+				pf_name,
+				pf_alias,
+				pf_content,
+				pf_meta_title,
+				pf_meta_keywords,
+				pf_meta_description,
+				utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
+				pf_active,
+			)
+			if err != nil {
+				wrap.MsgError(err.Error())
+				return
+			}
+			wrap.Write(`window.location='/cp/';`)
+		} else {
+			// Update page
+			_, err := wrap.DB.Query(
+				`UPDATE pages SET
+					name = ?,
+					alias = ?,
+					content = ?,
+					meta_title = ?,
+					meta_keywords = ?,
+					meta_description = ?,
+					active = ?
+				WHERE
+					id = ?
+				;`,
+				pf_name,
+				pf_alias,
+				pf_content,
+				pf_meta_title,
+				pf_meta_keywords,
+				pf_meta_description,
+				pf_active,
+				utils.StrToInt(pf_id),
+			)
+			if err != nil {
+				wrap.MsgError(err.Error())
+				return
+			}
+			wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
+		}
+	})
+}
+
 func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
 	return this.newAction(AInfo{
 		WantDB: false,

+ 45 - 5
utils/utils.go

@@ -10,6 +10,7 @@ import (
 	"regexp"
 	"strconv"
 	"strings"
+	"time"
 
 	"golang-fave/assets"
 	"golang-fave/consts"
@@ -42,11 +43,6 @@ func IsDirExists(path string) bool {
 	return false
 }
 
-func IsValidEmail(email string) bool {
-	regexpe := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
-	return regexpe.MatchString(email)
-}
-
 func IsNumeric(str string) bool {
 	if _, err := strconv.Atoi(str); err == nil {
 		return true
@@ -54,6 +50,16 @@ func IsNumeric(str string) bool {
 	return false
 }
 
+func IsValidEmail(email string) bool {
+	regexpe := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
+	return regexpe.MatchString(email)
+}
+
+func IsValidAlias(alias string) bool {
+	regexpe := regexp.MustCompile(`^\/([a-zA-Z0-9\/\-_\.]+)\/?$`)
+	return regexpe.MatchString(alias)
+}
+
 func FixPath(path string) string {
 	newPath := strings.TrimSpace(path)
 	if len(newPath) <= 0 {
@@ -109,6 +115,10 @@ func GetMd5(str string) string {
 	return hex.EncodeToString(hasher.Sum(nil))
 }
 
+func GetCurrentUnixTimestamp() int64 {
+	return int64(time.Now().Unix())
+}
+
 func SystemRenderTemplate(w http.ResponseWriter, c []byte, d interface{}) {
 	tmpl, err := template.New("template").Parse(string(c))
 	if err != nil {
@@ -182,3 +192,33 @@ func StrToInt(str string) int {
 	}
 	return 0
 }
+
+func GenerateAlias(str string) string {
+	if str == "" {
+		return ""
+	}
+
+	lat := []string{"EH", "I", "i", "#", "eh", "A", "B", "V", "G", "D", "E", "JO", "ZH", "Z", "I", "JJ", "K", "L", "M", "N", "O", "P", "R", "S", "T", "U", "F", "KH", "C", "CH", "SH", "SHH", "'", "Y", "", "EH", "YU", "YA", "a", "b", "v", "g", "d", "e", "jo", "zh", "z", "i", "jj", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u", "f", "kh", "c", "ch", "sh", "shh", "", "y", "", "eh", "yu", "ya", "", "", "-", "-", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "-", "-", ":", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M"}
+	cyr := []string{"Є", "І", "і", "№", "є", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "Ь", "Э", "Ю", "Я", "а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", "ь", "э", "ю", "я", "«", "»", "—", " ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "", "", "a", "s", "d", "f", "g", "h", "j", "k", "l", "", "", "z", "x", "c", "v", "b", "n", "m", "", "", "", "(", ")", "", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M"}
+
+	var alias string = ""
+	for i := 0; i < len(str); i++ {
+		for j := 0; j < len(cyr); j++ {
+			if string(str[i]) == cyr[j] {
+				alias += lat[j]
+			}
+		}
+	}
+	alias = strings.ToLower(alias)
+
+	// Cut repeated chars
+	if reg, err := regexp.Compile("[\\-]+"); err == nil {
+		alias = strings.Trim(reg.ReplaceAllString(alias, "-"), "-")
+	}
+
+	return "/" + strings.Trim(alias, " ") + "/"
+}
+
+func UnixTimestampToMySqlDateTime(value int64) string {
+	return time.Unix(value, 0).Format("2006-01-02 15:04:05")
+}