/// <summary> /// Saves form data (including XML representation) to the database /// </summary> /// <param name="form"></param> /// <param name="data"></param> private Guid SaveToDatabase(FormSubmission form, XmlDocument data) { using (FormProcessorDbContext db = new FormProcessorDbContext()) { FormSubmissionEntity formData = new FormSubmissionEntity { ID = Guid.NewGuid(), FormID = form.Meta.ID, Referrer = form.Meta.Referrer, Datetime = form.Meta.Datetime, ClientIP = form.Meta.ClientIP, Data = data.InnerXml }; db.Forms.Add(formData); db.SaveChanges(); _log.Debug(m => m("Form submission successfully saved to the database. (ID = {0})", formData.ID)); return formData.ID; } }
/// <summary> /// Creates a new <see cref="FormSubmission"/> object from the POSTed form /// </summary> /// <param name="formID"></param> /// <param name="request"></param> /// <param name="requiredFields"></param> /// <returns></returns> public static FormSubmission Create(Guid formID, HttpRequest request, string requiredFields) { IList<string> required = string.IsNullOrWhiteSpace(requiredFields) ? new List<string>() : new List<string>(requiredFields.Split(',')); _log.Trace(m => m("{0} required fields: {1}", required.Count, requiredFields)); FormSubmission form = new FormSubmission { Meta = { ID = formID, Referrer = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : "(empty)", // the following conversion forces the DateTime to be handled as a local time (rather // than UTC) in XML serialization. - 12/20/2011, [email protected] Datetime = XmlConvert.ToDateTime(DateTime.Now.ToString("s"), XmlDateTimeSerializationMode.RoundtripKind), ClientIP = request.UserHostAddress } }; _log.Trace(m => m("Meta data added:\n\nID = {0}\nReferrer = {1}\nDatetime = {2}\nClientIP = {3}", form.Meta.ID, form.Meta.Referrer, form.Meta.Datetime, form.Meta.ClientIP)); _log.Debug(m => m("Processing {0} POST variables...", request.Form.AllKeys.Length)); foreach (string name in request.Form.AllKeys) { // filter out form meta data and ASP.NET internal values if (!name.StartsWith("__")) { string fieldValue = request.Form[name]; _log.Trace(m => m("Creating form submission field: '{0}'", fieldValue)); if (name.StartsWith(Utility.META_FIELD_PREFIX)) { form.Meta.Fields.Add(FormField.Create(name.Substring(Utility.META_FIELD_PREFIX.Length), fieldValue ?? "(blank)" )); } else { form.Data.Fields.Add(FormField.Create(name, fieldValue, name.StartsWith(Utility.REQUIRED_FIELD_PREFIX) || required.Contains(name))); } } else { _log.Trace(m => m("Ignoring ASP.NET internal variable: {0}", name)); } } return form; }