//=== Init
        public static RangeElement Init(int? id = null, string label = null, string description = null, bool? required = null, bool? inlineMode = false, EType? rangeType = null)
        {
            RangeElement obj = new RangeElement();
            obj.Id = id ?? 0;
            obj.Type = FormElementType.Range;
            obj.Status = (int)Base.Enums.Status.Active;
            obj.Visible = true;
            obj.Disabled = false;
            obj.Index = 0;
            obj.RangeType = rangeType.GetValueOrDefault();

            obj.Label = label;
            obj.Description = description;

            obj.InlineMode = inlineMode ?? false;

            obj.Items = new List<RangeElementItem>();
            obj.Required = required ?? false;

            return obj;
        }
Exemple #2
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
        }