Carrotware.CMS.Core.ObjectProperty.GetWidgetProperties C# (CSharp) Метод

GetWidgetProperties() публичный статический Метод

public static GetWidgetProperties ( Carrotware.CMS.Core.Widget w, System.Guid guidContentID ) : List
w Carrotware.CMS.Core.Widget
guidContentID System.Guid
Результат List
        public static List<ObjectProperty> GetWidgetProperties(Widget w, Guid guidContentID)
        {
            Object widget = new Object();

            List<WidgetProps> lstProps = w.ParseDefaultControlProperties();

            if (w.ControlPath.Contains(":")) {
                if (w.ControlPath.ToUpperInvariant().StartsWith("CLASS:")) {
                    try {
                        string className = w.ControlPath.Replace("CLASS:", "");
                        Type t = ReflectionUtilities.GetTypeFromString(className);
                        widget = Activator.CreateInstance(t);
                    } catch (Exception ex) { }
                } else {
                    try {
                        string[] path = w.ControlPath.Split(':');
                        string objectPrefix = path[0];
                        string objectClass = path[1];

                        Type t = ReflectionUtilities.GetTypeFromString(objectClass);

                        Object obj = Activator.CreateInstance(t);
                        Object attrib = ReflectionUtilities.GetAttribute<WidgetActionSettingModelAttribute>(t, objectPrefix);

                        if (attrib != null && attrib is WidgetActionSettingModelAttribute) {
                            string attrClass = (attrib as WidgetActionSettingModelAttribute).ClassName;
                            Type s = ReflectionUtilities.GetTypeFromString(attrClass);

                            widget = Activator.CreateInstance(s);
                        }
                    } catch (Exception ex) { }
                }
            } else {
                if (w.ControlPath.Contains("|")) {
                    try {
                        string[] path = w.ControlPath.Split('|');
                        string viewPath = path[0];
                        string modelClass = String.Empty;
                        if (path.Length > 1) {
                            modelClass = path[1];
                            Type objType = ReflectionUtilities.GetTypeFromString(modelClass);

                            widget = Activator.CreateInstance(objType);
                        }
                    } catch (Exception ex) { }
                }
            }

            if (widget is IAdminModule) {
                var w1 = (IAdminModule)widget;
                w1.SiteID = SiteData.CurrentSiteID;
            }

            if (widget is IWidget) {
                var w1 = (IWidget)widget;
                w1.SiteID = SiteData.CurrentSiteID;
                w1.RootContentID = w.Root_ContentID;
                w1.PageWidgetID = w.Root_WidgetID;
                w1.IsDynamicInserted = true;
            }

            if (widget is IWidgetRawData) {
                var w1 = (IWidgetRawData)widget;
                w1.RawWidgetData = w.ControlProperties;
            }

            List<ObjectProperty> lstDefProps = ObjectProperty.GetObjectProperties(widget);

            //require that widget be attributed to be on the list
            List<string> limitedPropertyList = (from ww in widget.GetType().GetProperties()
                                                where Attribute.IsDefined(ww, typeof(WidgetAttribute))
                                                select ww.Name.ToLowerInvariant()).ToList();

            List<ObjectProperty> lstPropsToEdit = (from p in lstDefProps
                                                   join l in limitedPropertyList on p.Name.ToLowerInvariant() equals l.ToLowerInvariant()
                                                   where p.CanRead == true
                                                       && p.CanWrite == true
                                                   select p).ToList();

            foreach (var dp in lstPropsToEdit) {
                string sName = dp.Name.ToLowerInvariant();
                List<WidgetProps> lstItmVals = lstProps.Where(x => x.KeyName.ToLowerInvariant().StartsWith(sName + "|") || x.KeyName.ToLowerInvariant() == sName).ToList();

                ObjectProperty sourceProperty = new ObjectProperty();

                string sListSourcePropertyName = (from p in lstDefProps
                                                  where p.Name.ToLowerInvariant() == sName.ToLowerInvariant()
                                                        && !String.IsNullOrEmpty(p.CompanionSourceFieldName)
                                                  select p.CompanionSourceFieldName).FirstOrDefault();

                if (String.IsNullOrEmpty(sListSourcePropertyName)) {
                    sListSourcePropertyName = String.Empty;
                }

                sourceProperty = (from p in lstDefProps
                                  where p.CanRead == true
                                     && p.CanWrite == false
                                     && p.Name.ToLowerInvariant() == sListSourcePropertyName.ToLowerInvariant()
                                  select p).FirstOrDefault();

                if (dp.FieldMode != WidgetAttribute.FieldMode.CheckBoxList) {
                    string sDefTxt = String.Empty;

                    if (lstItmVals != null && lstItmVals.Any()) {
                        dp.TextValue = lstItmVals != null ? lstItmVals.FirstOrDefault().KeyValue : String.Empty;
                        dp.DefValue = dp.TextValue;
                    } else {
                        if (dp.DefValue != null) {
                            sDefTxt = dp.DefValue.ToString();

                            if (dp.PropertyType == typeof(Boolean)) {
                                bool vB = Convert.ToBoolean(dp.DefValue.ToString());
                                sDefTxt = vB.ToString();
                            }
                            if (dp.PropertyType == typeof(System.Drawing.Color)) {
                                System.Drawing.Color vC = (System.Drawing.Color)dp.DefValue;
                                sDefTxt = System.Drawing.ColorTranslator.ToHtml(vC);
                            }
                        }

                        dp.TextValue = sDefTxt;
                    }
                }

                Type pt = dp.PropertyType;

                if (sourceProperty != null) {
                    if (sourceProperty.DefValue is Dictionary<string, string>) {
                        dp.Options = OptionSelections.GetOptionsFromDictionary((Dictionary<string, string>)sourceProperty.DefValue);

                        // work with a checkbox list, allow more than one value
                        if (dp.FieldMode == WidgetAttribute.FieldMode.CheckBoxList) {
                            // since this is a multi selected capable field, look for anything that starts with the
                            // field name and has the delimiter trailing

                            if (lstItmVals.Any() && dp.Options.Any()) {
                                foreach (var v in dp.Options) {
                                    v.Selected = (from p in lstItmVals
                                                  where p.KeyValue == v.Key
                                                  select p.KeyValue).Any();
                                }
                            }
                        }
                    }
                }

                if (dp.FieldMode == WidgetAttribute.FieldMode.Unknown) {
                    if (pt == typeof(String) || pt == typeof(DateTime)
                        || pt == typeof(Int16) || pt == typeof(Int32) || pt == typeof(Int64)
                        || pt == typeof(float) || pt == typeof(Decimal)
                        || pt == typeof(Guid) || pt == typeof(System.Drawing.Color)) {
                        dp.FieldMode = WidgetAttribute.FieldMode.TextBox;
                    }
                }

                if ((pt == typeof(Boolean)) || dp.FieldMode == WidgetAttribute.FieldMode.CheckBox) {
                    dp.FieldMode = WidgetAttribute.FieldMode.CheckBox;
                    dp.CheckBoxState = Convert.ToBoolean(dp.TextValue);
                }
            }

            return lstPropsToEdit;
        }