예제 #1
0
        /// <summary> Adds the form element to the block and put it into the proper column. </summary>
        public void AddFormElement(FormElement element, int?index = null)
        {
            // Set column
            if (element.Column == 0)
            {
                element.Column = 1;
            }


            // Get column
            Column col = null;

            col = this.Columns.FirstOrDefault(x => x.Number == element.Column);
            if (col == null)
            {
                col = new Column {
                    Number = element.Column
                };
                this.Columns.Add(col);
            }


            // Set index
            if (element.Index == 0)
            {
                element.Index = (index != null) ? index.Value : col.Elements.Count + 1;
            }


            col.Elements.Add(element);
        }
예제 #2
0
        /// <summary> Deletes a field (hard). </summary>
        public void DeleteField(FormElement field)
        {
            Column col = this.FindParentCol(x => x.Id == field.Id);

            if (col != null)
            {
                col.Elements.Remove(field);
            }
        }
예제 #3
0
        /// <summary> Injects posted form values into the form element. </summary>
        public static void InjectFormData(NameValueCollection data, FormElement element)
        {
            // Check
            if (element == null)
            {
                return;
            }

            IDictionary <string, string> formValues = data.AllKeys.ToDictionary(k => k, v => data[v]);

            Form.InjectFormData(formValues, element);
        }
예제 #4
0
        /// <summary> Adds a field. </summary>
        public void AddField(FormElement formElement, int?blockId = null, int?col = null, int?index = null)
        {
            // Check
            if (formElement == null)
            {
                throw new CoreException("Parameter 'formElement' can not be null.");
            }
            if (formElement.Type == Enums.FormElementType.List)
            {
                throw new CoreException("A list field in another list field is not allowed.");
            }

            // Set id
            if (formElement.Id == 0)
            {
                formElement.Id = this.CreateFieldId();
            }

            // Set column
            if (formElement.Column == 0)
            {
                formElement.Column = (col != null) ? col.Value : 1;
            }

            // Add to Block
            if (this.Blocks == null)
            {
                this.Blocks = new List <Block>();
            }

            //- Find block
            Block block = this.Blocks.FirstOrDefault(x => x.Id == blockId);

            if (block == null)
            {
                //-- Get default block
                block = this.Blocks.FirstOrDefault(x => x.Id == 0);

                //-- Create default block
                if (block == null)
                {
                    block = new Block();
                    if (blockId != null)
                    {
                        block.Id = blockId.Value;
                    }
                    this.Blocks.Add(block);
                }
            }

            block.AddFormElement(formElement, index: index);
        }
예제 #5
0
        /// <summary> Injects posted form values into the form element. </summary>
        public static void InjectFormData(string data, FormElement element)
        {
            // Check
            if (element == null)
            {
                return;
            }

            NameValueCollection nvc = new NameValueCollection();

            nvc.Add(element.Name, data);

            InjectFormData(nvc, element);
        }
예제 #6
0
        /// <summary> Moves the field. </summary>
        public bool MoveField(int fieldId, int?prevItemId = null)
        {
            // Get parent col
            Column col = this.FindParentCol(x => x.Id == fieldId);

            if (col == null)
            {
                return(false);
            }

            // Get previous field
            if (prevItemId == null)
            {
                prevItemId = 0;
            }
            FormElement prevItem = prevItemId > 0 ? col.Elements.FirstOrDefault(x => x.Id == prevItemId) : null;

            // Get field
            FormElement field = col.Elements.FirstOrDefault(x => x.Id == fieldId);

            if (field == null)
            {
                return(false);
            }

            // Update index
            col.Elements.Remove(field);
            if (prevItem == null)
            {
                // Insert at beginning
                col.Elements.Insert(0, field);
            }
            else
            {
                // Insert after previous item
                int prevItemIndex = col.Elements.IndexOf(prevItem);
                col.Elements.Insert(prevItemIndex + 1, field);
            }

            // Reset all indexes
            int index = -1;

            foreach (FormElement fe in col.Elements)
            {
                index++;
                fe.Index = index;
            }

            return(true);
        }
예제 #7
0
        /// <summary> Injects posted form values into the form element. </summary>
        public static void InjectFormData(Dictionary <string, string> data, FormElement element)
        {
            // Check
            if (element == null)
            {
                return;
            }

            NameValueCollection collection = new NameValueCollection();

            foreach (string key in data.Keys)
            {
                collection.Add(key, data[key]);
            }

            InjectFormData(collection, element);
        }
예제 #8
0
        /// <summary> Inject posted form values into the form. (field id, value) </summary>
        public void InjectFormData(Dictionary <int, object> data)
        {
            Form form = this;
            NameValueCollection collection = new NameValueCollection();

            foreach (KeyValuePair <int, object> pair in data)
            {
                FormElement element = form.GetFormElement(pair.Key);
                if (element == null)
                {
                    continue;
                }

                collection.Add(element.Key, pair.Value.ToString());
            }

            this.InjectFormData(collection);
        }
예제 #9
0
        /// <summary> Add Textarea. </summary>
        public Textarea AddTextarea(int id, int column)
        {
            FormElement sibling = this.LastElement();

            // Default
            Textarea element = new Textarea
            {
                Id       = id,
                Index    = (sibling != null ? sibling.Index + 1 : 0),
                Visible  = true,
                Column   = column,
                Required = false
            };

            this.AddFormElement(element);

            return(element);
        }
예제 #10
0
        /// <summary> Add Input. </summary>
        public Input AddInput(int id, int column)
        {
            FormElement sibling = this.LastElement();

            // Default
            Input element = new Input
            {
                Id       = id,
                Index    = (sibling != null ? sibling.Index + 1 : 0),
                Visible  = true,
                Column   = column,
                Required = false
            };

            this.AddFormElement(element);

            return(element);
        }
예제 #11
0
        /// <summary> Add Radiobutton. </summary>
        public Radiobutton AddRadiobutton(int id, int column)
        {
            FormElement sibling = this.LastElement();

            // Default
            Radiobutton element = new Radiobutton
            {
                Id       = id,
                Index    = (sibling != null ? sibling.Index + 1 : 0),
                Visible  = true,
                Column   = column,
                Required = false,
                Disabled = false
            };

            this.AddFormElement(element);

            return(element);
        }
예제 #12
0
        /// <summary> Add Dropdown. </summary>
        public Dropdown AddDropDown(int id, int column)
        {
            FormElement sibling = this.LastElement();

            // Default
            Dropdown element = new Dropdown
            {
                Id       = id,
                Index    = (sibling != null ? sibling.Index + 1 : 0),
                Visible  = true,
                Column   = column,
                Required = false,
                Disabled = false
            };

            this.AddFormElement(element);

            return(element);
        }
예제 #13
0
        /// <summary> Injects form values into the form element. </summary>
        private static void InjectFormData(IDictionary <string, string> formValues, FormElement formElement)
        {
            // Check
            if (formElement == null)
            {
                return;
            }

            #region Checkbox
            if (formElement.Type == FormElementType.Checkbox)
            {
                // possible multiple selection
                Checkbox      checkbox = (Checkbox)formElement;
                List <string> selectedCheckboxValues = new List <string>();
                foreach (var item in checkbox.Items)
                {
                    string key = string.Format("{0}_{1}", checkbox.Name, item.Id);
                    item.Selected = formValues.ContainsKey(key);

                    if (item.Selected)
                    {
                        selectedCheckboxValues.Add(item.Value);
                    }
                }
                checkbox.Value = selectedCheckboxValues.ToArray();
            }
            #endregion
            //#region FileUpload
            //else if (formElement.Type == FormElementType.FileUpload)
            //{
            //    List<string> keys = new List<string>();

            //    foreach (string key in formValues.Keys)
            //    {
            //        if (key.Equals(formElement.Name) || key.StartsWith(formElement.Name + "["))
            //        {
            //            keys.Add(key);
            //        }
            //    }

            //    foreach (string key in keys)
            //    {
            //        string value = formValues[key];

            //        if (!string.IsNullOrWhiteSpace(value))
            //        {
            //            FileUpload fileUpload = (FileUpload)formElement;

            //            // Parse values
            //            List<string> parsedValues = new List<string>();

            //            if (value.StartsWith("["))
            //            {
            //                parsedValues = JArray.Parse(value).Values<string>().ToList();
            //            }
            //            else
            //            {
            //                parsedValues.Add(value);
            //            }

            //            foreach (string parsedValue in parsedValues)
            //            {
            //                bool isParsed = false;

            //                // Try int parsing
            //                if (!isParsed)
            //                {
            //                    int intValue = 0;
            //                    bool isInt = Int32.TryParse(parsedValue, out intValue);

            //                    if (isInt)
            //                    {
            //                        fileUpload.AddValue(new ContentReference { ReferenceId = intValue });
            //                        isParsed = true;
            //                    }
            //                }

            //                // Try dtp request
            //                if (!isParsed)
            //                {
            //                    string[] info = parsedValue.Split(new char[] { '|' });

            //                    if (info.Length == 2)
            //                    {
            //                        string tag = info[0]; // 'dtp'
            //                        string id = info[1];

            //                        fileUpload.AddValue(new DtpReference { DtpRequestId = Convert.ToInt32(id) });
            //                        isParsed = true;
            //                    }
            //                }

            //                // Try temp upload
            //                if (!isParsed)
            //                {
            //                    object parsed = FileUpload.ParseTag(parsedValue);

            //                    if (parsed is FileReference)
            //                    {
            //                        fileUpload.AddValue((FileReference)parsed);
            //                        isParsed = true;
            //                    }
            //                    else if (parsed is ContentReference)
            //                    {
            //                        fileUpload.AddValue((ContentReference)parsed);
            //                        isParsed = true;
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
            //#endregion
            #region Dropdown
            else if (formElement.Type == FormElementType.Dropdown)
            {
                Dropdown dropdown = (Dropdown)formElement;
                string   value    = formValues.ContainsKey(formElement.Name) ? formValues[formElement.Name] : null;

                string[] multipleValues = null;

                if (dropdown.Multiple)
                {
                    bool parsed = false;

                    // Check Array
                    if (!parsed)
                    {
                        try
                        {
                            JArray ja = JArray.Parse(value);
                            multipleValues = ja.Values <string>().ToArray();
                            parsed         = true;
                        }
                        catch { }
                    }

                    // Check CSV
                    if (!parsed)
                    {
                        try
                        {
                            if (string.IsNullOrEmpty(value))
                            {
                                multipleValues = new string[0];
                            }
                            else
                            {
                                multipleValues = value.Split(',');
                            }
                            parsed = true;
                        }
                        catch { }
                    }
                }

                // Assign
                dropdown.Value = multipleValues ?? new string[] { value };

                // Mark Selection
                dropdown.Items.ForEach(x => x.Selected = dropdown.Value.Contains(x.Value));
            }
            #endregion
            #region Tagging
            else if (formElement.Type == FormElementType.Tagging)
            {
                Tagging tagging = (Tagging)formElement;
                string  value   = formValues.ContainsKey(formElement.Name) ? formValues[formElement.Name] : null;

                if (string.IsNullOrEmpty(value))
                {
                    tagging.Value = new List <TaggingItem>();
                }
                else
                {
                    if ((value.StartsWith("{") && value.EndsWith("}")) || //For object
                        (value.StartsWith("[") && value.EndsWith("]")))   //For array
                    {
                        JToken jValue = JToken.Parse(value);

                        if (jValue is JObject)
                        {
                            tagging.Value = new List <TaggingItem> {
                                JsonConvert.DeserializeObject <TaggingItem>(value)
                            };
                        }
                        else if (jValue is JArray)
                        {
                            tagging.Value = JsonConvert.DeserializeObject <List <TaggingItem> >(value);
                        }
                        else
                        {
                            tagging.Value = new List <TaggingItem>();
                        }
                    }
                    else
                    {
                        tagging.Value = new List <TaggingItem>();
                    }
                }
            }
            #endregion
            #region Link
            else if (formElement.Type == FormElementType.Link)
            {
                Link obj = formElement as Link;

                if (formValues.ContainsKey(formElement.Name))
                {
                    obj.Url = formValues[formElement.Name];
                }
                if (formValues.ContainsKey(formElement.Name + "_display"))
                {
                    obj.DisplayText = formValues[formElement.Name + "_display"];
                }
                if (formValues.ContainsKey(formElement.Name + "_title"))
                {
                    obj.Title = formValues[formElement.Name + "_title"];
                }
                if (formValues.ContainsKey(formElement.Name + "_target"))
                {
                    int targetId = Convert.ToInt32(formValues[formElement.Name + "_target"]);
                    obj.Target = (Link.LinkTarget)targetId;
                }
            }
            #endregion
            #region Radiobutton
            else if (formElement.Type == FormElementType.Radiobutton)
            {
                if (formValues.ContainsKey(formElement.Name))
                {
                    string value = formValues[formElement.Name];

                    ((Radiobutton)formElement).Value = value;
                    ((Radiobutton)formElement).Items.ForEach(x => x.Selected = x.Value == value);
                }
            }
            #endregion
            #region Html
            else if (formElement.Type == FormElementType.Html)
            {
                HtmlElement htmlElement = (HtmlElement)formElement;

                if (htmlElement.Editable)
                {
                    if (formValues.ContainsKey(formElement.Name))
                    {
                        string value = formValues[formElement.Name];
                        htmlElement.Value = value;
                    }
                }
            }
            #endregion
            #region Calendar
            else if (formElement.Type == FormElementType.Calendar)
            {
                Calendar element = (Calendar)formElement;

                // Simple Value
                element.Value = formValues.ContainsKey(formElement.Name) ? formValues[formElement.Name] : null;

                // Advanced Value
                string json = formValues.ContainsKey(formElement.Name + "_data") ? formValues[formElement.Name + "_data"] : null;

                element.ObjValue = Calendar.AdvancedValue.ParseJson(json);
            }
            #endregion
            #region List
            else if (formElement.Type == FormElementType.List)
            {
                ListElement obj = formElement as ListElement;

                string value = formValues.ContainsKey(formElement.Name) ? formValues[formElement.Name] : null;

                obj.RawValue = value;

                if (string.IsNullOrEmpty(value))
                {
                    obj.Value = null;
                }
                else
                {
                    JsonSerializerSettings jser = new JsonSerializerSettings();
                    jser.ContractResolver = new CamelCasePropertyNamesContractResolver();
                    obj.Value             = JsonConvert.DeserializeObject <ListElement.ValueItem[]>(value, jser);
                }
            }
            #endregion
            #region Ranges
            else if (formElement.Type == FormElementType.Range)
            {
                RangeElement range = formElement as RangeElement;

                if (formValues.ContainsKey(formElement.Name))
                {
                    string value = formValues[formElement.Name];

                    // Only change something when it has a value
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        // Overwrite current items
                        range.Items = new List <RangeElement.RangeElementItem>();
                        JArray array = JArray.Parse(value);

                        foreach (JObject jo in array)
                        {
                            RangeElement.RangeElementItem rangeItem = new RangeElement.RangeElementItem();
                            rangeItem.Id           = Convert.ToInt32(jo["Id"].ToString());
                            rangeItem.ColorCode    = jo["ColorCode"].ToString();
                            rangeItem.Label        = jo["Label"].ToString();
                            rangeItem.MinimumValue = 0;
                            rangeItem.MaximumValue = 0;

                            string minVal = jo["MinimumValue"].ToString();
                            string maxVal = jo["MaximumValue"].ToString();
                            if (minVal.IsInteger())
                            {
                                rangeItem.MinimumValue = Convert.ToInt32(minVal);
                            }
                            if (maxVal.IsInteger())
                            {
                                rangeItem.MaximumValue = Convert.ToInt32(maxVal);
                            }
                            range.Items.Add(rangeItem);
                        }
                    }
                }
            }
            #endregion
            #region Other: Input / Textarea / Calendar / Hidden / Lookup / Library Input
            else
            {
                string value = formValues.ContainsKey(formElement.Name) ? formValues[formElement.Name] : null;

                switch (formElement.Type)
                {
                case FormElementType.Input:
                    ((Input)formElement).Value = value;
                    break;

                case FormElementType.Textarea:
                    ((Textarea)formElement).Value = value;
                    break;

                case FormElementType.Hidden:
                    ((Hidden)formElement).Value = value;
                    break;
                }
            }
            #endregion
        }
예제 #14
0
        /// <summary> Get Form Value. </summary>
        /// <param name="formLayoutId"> The form layout id. </param>
        public object GetValue(int formLayoutId)
        {
            IEnumerable <FormElement> elements = GetAllElements();

            FormElement element = elements.Where(x => x.Id == formLayoutId).FirstOrDefault();

            if (element == null)
            {
                return(null);
            }

            if (element.Type == FormElementType.Block)
            {
            }
            else if (element.Type == FormElementType.Html)
            {
                HtmlElement html = element as HtmlElement;

                return(html.Value);
            }
            else if (element.Type == FormElementType.Input)
            {
                Input input = element as Input;

                return(input.Value);
            }
            else if (element.Type == FormElementType.Textarea)
            {
                Textarea textarea = element as Textarea;

                return(textarea.Value);
            }
            else if (element.Type == FormElementType.Dropdown)
            {
                Dropdown dropdown = element as Dropdown;

                return(dropdown.Value);
            }
            else if (element.Type == FormElementType.Radiobutton)
            {
                Radiobutton radio = element as Radiobutton;

                return(radio.Value);
            }
            else if (element.Type == FormElementType.Checkbox)
            {
                Checkbox checkbox = element as Checkbox;

                return(checkbox.Value);
            }
            else if (element.Type == FormElementType.Calendar)
            {
                Calendar calendar = element as Calendar;

                return(calendar.Value);
            }
            else if (element.Type == FormElementType.Hidden)
            {
                Hidden hidden = element as Hidden;

                return(hidden.Value);
            }
            else if (element.Type == FormElementType.Link)
            {
                return((element as Link).GetElementValue());
            }

            return(null);
        }
예제 #15
0
        /// <summary> Set Form Value. </summary>
        /// <param name="elementId"> The form element id. </param>
        /// <param name="typeId"> The form element type id. </param>
        /// <param name="value"> The form value. </param>
        public void SetValue(int formLayoutId, object value)
        {
            IEnumerable <FormElement> elements = GetAllElements();

            FormElement element = elements.Where(x => x.Id == formLayoutId).FirstOrDefault();

            if (element == null)
            {
                return;
            }

            if (element.Type == FormElementType.Block)
            {
            }
            else if (element.Type == FormElementType.Html)
            {
                HtmlElement html = element as HtmlElement;

                html.Value = Convert.ToString(value);
            }
            else if (element.Type == FormElementType.Input)
            {
                Input input = element as Input;

                input.Value = Convert.ToString(value);
            }
            else if (element.Type == FormElementType.Textarea)
            {
                Textarea textarea = element as Textarea;

                textarea.Value = Convert.ToString(value);
            }
            else if (element.Type == FormElementType.Dropdown)
            {
                Dropdown dropdown = element as Dropdown;

                // Vast value to string array
                string[] valueArray = null;

                if (value != null)
                {
                    Type valueType         = value.GetType();
                    Type genType           = valueType.IsGenericType ? valueType.GetGenericTypeDefinition() : null;
                    bool valueIsEnumerable = valueType.IsArray || (genType != null && (genType == typeof(List <>) || genType == typeof(IEnumerable <>)));

                    if (valueIsEnumerable)
                    {
                        valueArray = ((IEnumerable)value).Cast <object>().Select(x => x.ToString()).ToArray();
                    }
                    else
                    {
                        string sValue = Convert.ToString(value);
                        if (sValue != null)
                        {
                            valueArray = new string[] { sValue };
                        }
                    }
                }

                // Dropdown with items, or ajax source
                if (dropdown.Items != null && dropdown.Items.Count > 0)
                {
                    // Deselect every item
                    foreach (DropdownItem itemX in dropdown.Items)
                    {
                        itemX.Selected = false;
                    }

                    // Values set?
                    if (valueArray != null)
                    {
                        // Valid item placeholder
                        List <string> validItems = new List <string>();

                        // Loop over every value in the value array
                        foreach (string val in valueArray)
                        {
                            // Select item
                            DropdownItem item = dropdown.Items.Where(x => x.Value != null && x.Value.Equals(val, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                            // Check if selected item exists
                            if (item != null)
                            {
                                item.Selected = true;
                                validItems.Add(item.Value);
                            }
                        }

                        // Set dropdown value
                        dropdown.Value = validItems.ToArray();
                    }
                }
                else
                {
                    // No items, maybe it's an ajax source
                    if (dropdown.AjaxSource != null)
                    {
                        dropdown.Value = valueArray;
                    }
                }
            }
            else if (element.Type == FormElementType.Radiobutton)
            {
                Radiobutton radio = element as Radiobutton;

                string sValue = Convert.ToString(value);

                RadiobuttonItem item = radio.Items.Where(x => x.Value.Equals(sValue, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                if (item != null)
                {
                    foreach (RadiobuttonItem itemX in radio.Items)
                    {
                        itemX.Selected = false;
                    }

                    item.Selected = true;

                    radio.Value = sValue;
                }
            }
            else if (element.Type == FormElementType.Checkbox)
            {
                Checkbox checkbox = element as Checkbox;

                // Vast value to string array
                string[] valueArray = null;
                Type     valueType  = value.GetType();
                if (valueType.IsArray)
                {
                    valueArray = ((IEnumerable)value).Cast <object>().Select(x => x.ToString()).ToArray();
                }
                else
                {
                    string sValue = Convert.ToString(value);
                    valueArray = new string[] { sValue };
                }

                // Deselect every item
                foreach (CheckboxItem itemX in checkbox.Items)
                {
                    itemX.Selected = false;
                }

                // Values set?
                if (valueArray != null)
                {
                    // Valid item placeholder
                    List <string> validItems = new List <string>();

                    // Loop over every value in the value array
                    foreach (string val in valueArray)
                    {
                        CheckboxItem item = checkbox.Items.Where(x => x.Value.Equals(val, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                        if (item != null)
                        {
                            item.Selected = true;
                            validItems.Add(item.Value);
                        }
                    }

                    // Set checkbox value
                    checkbox.Value = validItems.ToArray();
                }
            }
            else if (element.Type == FormElementType.Calendar)
            {
                Calendar calendar = element as Calendar;
                calendar.SetElementValue(value);
            }
            else if (element.Type == FormElementType.Hidden)
            {
                Hidden hidden = element as Hidden;

                hidden.Value = Convert.ToString(value);
            }
            else if (element.Type == FormElementType.Tagging)
            {
                Tagging tagging = element as Tagging;

                tagging.Value = (List <TaggingItem>)value;
            }
            else if (element.Type == FormElementType.Link)
            {
                Link obj = element as Link;

                obj.SetElementValue(value);
            }
            else if (element.Type == FormElementType.Label)
            {
                Label label = element as Label;
                label.SetElementValue(value);
            }
        }
예제 #16
0
        /// <summary> Gets a maximum existing element Index among all form elements. </summary>
        public int GetMaxExistingElementIndex()
        {
            FormElement last = this.GetAllElements().OrderBy(x => x.Id).LastOrDefault();

            return(last != null ? last.Index : 0);
        }