示例#1
1
文件: nodetypes.go 项目: yanap/monsti
func renderContactForm(c *reqContext, context template.Context,
	formValues url.Values, h *nodeHandler) error {
	G, _, _, _ := gettext.DefaultLocales.Use("",
		c.SiteSettings.Fields["core.Locale"].Value().(string))
	m := c.Serv.Monsti()
	data := contactFormData{}
	form := htmlwidgets.NewForm(&data)
	form.AddWidget(&htmlwidgets.TextWidget{MinLength: 1,
		ValidationError: G("Required.")}, "Name", G("Name"), "")
	form.AddWidget(&htmlwidgets.TextWidget{MinLength: 1,
		ValidationError: G("Required.")}, "Email", G("Email"), "")
	form.AddWidget(&htmlwidgets.TextWidget{MinLength: 1,
		ValidationError: G("Required.")}, "Subject", G("Subject"), "")
	form.AddWidget(&htmlwidgets.TextAreaWidget{MinLength: 1,
		ValidationError: G("Required.")}, "Message", G("Message"), "")

	switch c.Req.Method {
	case "GET":
		if _, submitted := formValues["submitted"]; submitted {
			context["Submitted"] = 1
		}
	case "POST":
		if form.Fill(formValues) {
			mail := gomail.NewMessage()
			mail.SetAddressHeader("From",
				c.SiteSettings.StringValue("core.EmailAddress"),
				c.SiteSettings.StringValue("core.EmailName"))
			mail.SetAddressHeader("To",
				c.SiteSettings.StringValue("core.OwnerEmail"),
				c.SiteSettings.StringValue("core.OwnerName"))
			mail.SetAddressHeader("Reply-To", data.Email, data.Name)
			mail.SetHeader("Subject", data.Subject)
			body := fmt.Sprintf("%v\n%v\n\n%v",
				fmt.Sprintf(G("Received from contact form at %v"),
					c.SiteSettings.StringValue("core.Title")),
				fmt.Sprintf(G("Name: %v | Email: %v"), data.Name, data.Email),
				data.Message)
			mail.SetBody("text/plain", body)
			mailer := gomail.NewCustomMailer("", nil, gomail.SetSendMail(
				m.SendMailFunc()))
			err := mailer.Send(mail)
			if err != nil {
				return fmt.Errorf("Could not send mail: %v", err)
			}
			http.Redirect(c.Res, c.Req, path.Dir(c.Node.Path)+"/?submitted", http.StatusSeeOther)
			return nil
		}
	default:
		return fmt.Errorf("Request method not supported: %v", c.Req.Method)
	}
	context["Form"] = form.RenderData()
	return nil
}
示例#2
0
文件: session.go 项目: yanap/monsti
// RequestPasswordToken sends the user a token to be able to change
// the login password.
func (h *nodeHandler) RequestPasswordToken(c *reqContext) error {
	G, _, _, _ := gettext.DefaultLocales.Use("", c.UserSession.Locale)
	data := requestPasswordTokenFormData{}
	form := htmlwidgets.NewForm(&data)
	form.AddWidget(new(htmlwidgets.TextWidget), "User", G("Login"), "")

	sent := false
	switch c.Req.Method {
	case "GET":
		if _, ok := c.Req.Form["sent"]; ok {
			sent = true
		}
	case "POST":
		if form.Fill(c.Req.Form) {
			user, err := getUser(data.User,
				h.Settings.Monsti.GetSiteDataPath(c.Site))
			if err != nil {
				return fmt.Errorf("Could not get user: %v", err)
			}
			if user != nil {
				link := getRequestPasswordToken(c.Site, data.User,
					c.SiteSettings.StringValue("core.PasswordTokenKey"))

				// Send email to user
				mail := gomail.NewMessage()
				mail.SetAddressHeader("From",
					c.SiteSettings.StringValue("core.EmailAddress"),
					c.SiteSettings.StringValue("core.EmailName"))
				mail.SetAddressHeader("To", user.Email, user.Login)
				mail.SetHeader("Subject", G("Password request"))

				body, err := h.Renderer.Render("mails/change_password",
					template.Context{
						"SiteSettings": c.SiteSettings,
						"Account":      data.User,
						"ChangeLink": c.SiteSettings.StringValue("core.BaseURL") +
							"/@@change-password?token=" + link,
					}, c.UserSession.Locale, h.Settings.Monsti.GetSiteTemplatesPath(c.Site))
				if err != nil {
					return fmt.Errorf("Can't render password change mail: %v", err)
				}
				mail.SetBody("text/plain", string(body))
				mailer := gomail.NewCustomMailer("", nil, gomail.SetSendMail(
					c.Serv.Monsti().SendMailFunc()))
				err = mailer.Send(mail)
				if err != nil {
					return fmt.Errorf("Could not send mail: %v", err)
				}

				http.Redirect(c.Res, c.Req, "@@request-password-token?sent",
					http.StatusSeeOther)
				return nil
			} else {
				form.AddError("User", G("Unkown user."))
			}
		}
	default:
		return fmt.Errorf("Request method not supported: %v", c.Req.Method)
	}

	body, err := h.Renderer.Render("actions/request_password_token_form",
		template.Context{
			"Sent": sent,
			"Form": form.RenderData()}, c.UserSession.Locale,
		h.Settings.Monsti.GetSiteTemplatesPath(c.Site))
	if err != nil {
		return fmt.Errorf("Can't render login form: %v", err)
	}
	env := masterTmplEnv{
		Node:    c.Node,
		Session: c.UserSession,
		Title:   G("Request new password"),
		Flags:   EDIT_VIEW}
	rendered, _ := renderInMaster(h.Renderer, []byte(body), env, h.Settings,
		c.Site, c.SiteSettings, c.UserSession.Locale, c.Serv)
	c.Res.Write(rendered)
	return nil
}
示例#3
0
func renderContactForm(req *service.Request, session *service.Session) (
	*service.RenderNodeRet, error) {

	m := session.Monsti()
	siteSettings, err := m.LoadSiteSettings(req.Site)
	if err != nil {
		return nil, fmt.Errorf("Could not get site settings: %v", err)
	}
	G, _, _, _ := gettext.DefaultLocales.Use("",
		siteSettings.Fields["core.Locale"].Value().(string))

	node, err := m.GetNode(req.Site, req.NodePath)
	if err != nil {
		return nil, fmt.Errorf("Could not get contact form node: %v", err)
	}

	data := make(service.NestedMap)
	var dataFields []dataField
	form := htmlwidgets.NewForm(data)

	formFields := node.Fields["core.ContactFormFields"].(*service.ListField)

	for i, field := range formFields.Fields {
		combinedField := field.(*service.CombinedField)
		name := combinedField.Fields["Name"].Value().(string)
		required := combinedField.Fields["Required"].Value().(bool)
		fieldId := fmt.Sprintf("field_%d", i)
		dataFields = append(dataFields, dataField{fieldId, name})
		data[fieldId] = ""
		innerFieldType := combinedField.Fields["Field"].(*service.DynamicTypeField).DynamicType
		var widget htmlwidgets.Widget
		switch innerFieldType {
		case "text":
			textWidget := &htmlwidgets.TextWidget{ValidationError: G("Required.")}
			if required {
				textWidget.MinLength = 1
			}
			widget = textWidget
		case "textarea":
			areaWidget := &htmlwidgets.TextAreaWidget{ValidationError: G("Required.")}
			if required {
				areaWidget.MinLength = 1
			}
			widget = areaWidget
		default:
			panic(fmt.Sprintf("Unknow inner field type <%v>", innerFieldType))
		}
		form.AddWidget(widget, fieldId, name, "")
	}

	if len(formFields.Fields) == 0 {
		// Add default fields for backward compatibility
		data["Name"] = ""
		data["Email"] = ""
		data["Subject"] = ""
		data["Message"] = ""
		dataFields = []dataField{
			{"Name", G("Name")},
			{"Email", G("Email")},
			{"Subject", G("Subject")},
			{"Message", G("Message")},
		}
		form.AddWidget(&htmlwidgets.TextWidget{MinLength: 1,
			ValidationError: G("Required.")}, "Name", G("Name"), "")
		form.AddWidget(&htmlwidgets.TextWidget{MinLength: 1,
			ValidationError: G("Required.")}, "Email", G("Email"), "")
		form.AddWidget(&htmlwidgets.TextWidget{MinLength: 1,
			ValidationError: G("Required.")}, "Subject", G("Subject"), "")
		form.AddWidget(&htmlwidgets.TextAreaWidget{MinLength: 1,
			ValidationError: G("Required.")}, "Message", G("Message"), "")
	}

	context := make(map[string]interface{})
	switch req.Method {
	case "GET":
		if _, submitted := req.Form["submitted"]; submitted {
			context["Submitted"] = 1
		}
	case "POST":
		if form.Fill(req.PostForm) {
			mail := gomail.NewMessage()
			mail.SetAddressHeader("From",
				siteSettings.StringValue("core.EmailAddress"),
				siteSettings.StringValue("core.EmailName"))
			mail.SetAddressHeader("To",
				siteSettings.StringValue("core.OwnerEmail"),
				siteSettings.StringValue("core.OwnerName"))
			// mail.SetAddressHeader("Reply-To", data.Email, data.Name)
			mail.SetHeader("Subject", "Contact form submission")
			var fieldValues string
			for _, v := range dataFields {
				fieldValues += fmt.Sprintf("%v: %v\n", v.Name, data[v.Id])
			}
			body := fmt.Sprintf("%v\n\n%v",
				fmt.Sprintf(G("Received from contact form at %v"),
					siteSettings.StringValue("core.Title")), fieldValues)
			mail.SetBody("text/plain", body)
			mailer := gomail.NewCustomMailer("", nil, gomail.SetSendMail(
				m.SendMailFunc()))
			err := mailer.Send(mail)
			if err != nil {
				return nil, fmt.Errorf("Could not send mail: %v", err)
			}
			return &service.RenderNodeRet{
				Redirect: &service.Redirect{
					path.Dir(node.Path) + "/?submitted", http.StatusSeeOther}}, nil
		}
	default:
		return nil, fmt.Errorf("Request method not supported: %v", req.Method)
	}
	context["Form"] = form.RenderData()
	return &service.RenderNodeRet{Context: context}, err
}