Ejemplo n.º 1
0
        private void PopulateEntryFields(FormEntry entry)
        {
            if (Form != null)
            {
                // get all the fields
                foreach (Field field in Form.Fields)
                {
                    HtmlTableRow row = new HtmlTableRow();
                    HtmlTableCell namecell = new HtmlTableCell();
                    namecell.Style.Add("width","80px");
                    namecell.Style.Add("vertical-align", "top");
                    namecell.InnerText = field.Name;
                    HtmlTableCell entrycell = new HtmlTableCell();
                    entrycell.InnerHtml = GetFieldHtml(field, entry);
                    row.Cells.Add(namecell);
                    row.Cells.Add(entrycell);

                    this.entrybody.Controls.Add(row);
                }
            }
        }
Ejemplo n.º 2
0
        public void Add(FormEntry response)
        {
            #region argument checking

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (!response.IsValid())
            {
                throw new InvalidOperationException("response is invalid");
            }

            #endregion

            //get the corresponding form
            Form form = FormsPlugin.Instance.Read(response.Form);

            if (form == null)
            {
                // response without form cannot be saved.
                throw new MessageException(FormsErrorCodes.NoFormFoundWithTheName, ResourceManager.GetMessage(FormMessages.FormNotFound));
            }

            // check to see that the input supplied adhere the rules of the form.
            foreach (Field field in form.Fields)
            {
                Data data = response.GetFieldData(field);

                if (field.IsRequired)
                {
                    // if the field value is required it must not be null
                    // and incase of string, must not be empty.
                    if ((data == null||data.Value == null) ||
                        (field.FieldType == FieldType.String && string.IsNullOrEmpty(data.Value.ToString())))
                    {
                        throw new MessageException(FormsErrorCodes.NotAllRequiredFieldsSupplied,
                            ResourceManager.GetMessage(FormMessages.NotAllFieldsSupplied));
                    }
                }

                bool fieldTypeMatch = true;

                if (data != null && data.Value != null)
                {
                    fieldTypeMatch = IsFieldTypeMatch(field, data.Value);
                }

                if (!fieldTypeMatch)
                {
                    //field's value does not match with the defined type.
                    throw new MessageException(FormsErrorCodes.FieldValueDoesnotMatchWithFieldType,
                        ResourceManager.GetMessage(FormMessages.FieldValueDoesnotMatchWithFieldType));
                }
            }

            // save the data
            try
            {
                // add the form
                PluginStore.Instance.Save(this, response);
            }
            catch (MessageException me)
            {
                if (me.ErrorNumber == PluginErrorCodes.IdAlreadyInUse)
                {
                    // throw with a new message
                    throw new MessageException(me.ErrorNumber, ResourceManager.GetMessage(FormMessages.ResponseIdInUse));
                }

                throw;
            }
        }
Ejemplo n.º 3
0
        private string GetFieldHtml(Field field, FormEntry entry)
        {
            StringBuilder builder = new StringBuilder();
            string fieldValue = string.Empty;
            if (entry != null)
            {
                Data data = entry.GetFieldData(field);
                if (data != null)
                {
                    fieldValue = entry.GetFieldData(field).Value as string;
                }
            }

            if (field.FieldType == FieldType.MultiLine)
            {
                builder.Append(@"<textarea style=""width:600px;""");
            }
            else if (field.FieldType == FieldType.Html)
            {
                builder.Append(@"<textarea style=""width:600px;"" rows='8'");
            }
            else
            {
                builder.Append("<input type=\"text\"");

                if (field.FieldType == FieldType.String || field.FieldType == FieldType.Url)
                {
                    builder.Append(@" style=""width:600px;""");
                }
                else if (field.FieldType == FieldType.DateTime)
                {
                    builder.Append(@" style=""width:100px;""");
                }
                else
                {
                    builder.Append(@" style=""width:300px;""");
                }

            }

            builder.AppendFormat(@" id=""{0}{1}"" ", IdPrefix, field.Name);

            if (field.FieldType == FieldType.MultiLine ||
                field.FieldType == FieldType.Html)
            {
                builder.AppendFormat(">{0}</textarea>", fieldValue);
            }
            else
            {

                string validate = GetValidateString(field);
                if (!string.IsNullOrEmpty(validate))
                {
                    builder.Append(validate);
                }

                builder.AppendFormat(@"value=""{0}""", fieldValue);
                builder.Append("/>");
            }

            return builder.ToString();
        }
Ejemplo n.º 4
0
        internal static void AddFormEntry(string formname, Guid uid, string fields, ServiceOutput output)
        {
            if (!string.IsNullOrEmpty(formname) &&
                !string.IsNullOrEmpty(fields))
            {
                //get the form.
                Form form = null;
                if (FormsPlugin.Instance.TryRead(formname, out form))
                {
                    //form was found.
                    logger.Log(LogLevel.Info, "Attempting to save entry for form - {0}.", form.Name);

                    //check if there is already an entry with uid
                    FormEntry entry = null;

                    if (uid != Guid.Empty)
                    {
                        // get the entry
                        entry = PluginStore.Instance.Read<FormEntry>(FormEntryPlugin.Instance, uid);
                    }

                    if (entry == null)
                    {
                        //create a new entry
                        entry = new FormEntry();
                        entry.Form = formname;
                        entry.Id = Utility.GetUniqueString();
                    }

                    // split the fields
                    string[] tokens = fields.Split(',');
                    foreach (string field in tokens)
                    {
                        if (string.IsNullOrEmpty(field))
                        {
                            continue;
                        }

                        //generate the response data
                        Data data = new Data();
                        data.Name = field;
                        data.Value = HttpContext.Current.Request[field];

                        entry.Add(data);
                    }

                    try
                    {
                        // app execution before saving the entry
                        AppEngine.ExecuteApps(AppEvent.FormEntrySaving, entry);

                        // submit the response
                        FormEntryPlugin.Instance.Add(entry);

                        AppEngine.ExecuteApps(AppEvent.FormEntrySaved, entry);

                        //notify
                        Notifier.Notify(ResourceManager.GetLiteral("FormResponses.NewResponseSubject") /* subject */,
                                        string.Format(ResourceManager.GetLiteral("FormResponses.NewResponseBody"), form.DisplayName) /* body */);

                        output.Success = true;
                        output.AddOutput("uid", entry.UId);
                    }
                    catch (MessageException me)
                    {
                        string errorMessage = me.Message;
                        if (me.ErrorNumber == FormsErrorCodes.NotAllRequiredFieldsSupplied)
                        {
                            errorMessage = ResourceManager.GetLiteral("FormResponses.NotAllRequiredFieldsSupplied");
                        }
                        else if (me.ErrorNumber == FormsErrorCodes.FieldValueDoesnotMatchWithFieldType)
                        {
                            errorMessage = ResourceManager.GetLiteral("FormResponses.FieldValueDoesnotMatchWithFieldType");
                        }

                        output.AddOutput(Constants.Json.Error, errorMessage);
                    }

                }
                else
                {
                    //form not found error
                    output.AddOutput(
                            Constants.Json.Error,
                            string.Format(ResourceManager.GetLiteral("Admin.Forms.NotFound"), formname)
                        );
                }
            }
            else
            {
                //either the form name was null, or the fields were null.
                output.AddOutput(Constants.Json.Error, ResourceManager.GetLiteral("FormResponses.Invalid"));
            }
        }