protected void CreateMemberTypeAndMemberGroup(object sender, EventArgs e)
        {
            MemberGroup[] memberGroups     = MemberGroup.GetAll;
            bool          memberGroupExist = false;

            foreach (MemberGroup memberGroup in memberGroups)
            {
                if (memberGroup.Text == "Extranet")
                {
                    memberGroupExist = true;
                }
            }
            if (memberGroupExist == false)
            {
                MemberGroup memberType = MemberGroup.MakeNew("Extranet", umbraco.BasePages.UmbracoEnsuredPage.CurrentUser);
            }

            MemberType[] memberTypes     = MemberType.GetAll;
            bool         memberTypeExist = false;

            foreach (MemberType memberType in memberTypes)
            {
                if (memberType.Text == "Extranet")
                {
                    memberTypeExist = true;
                }
            }
            if (memberTypeExist == false)
            {
                MemberType memberType = MemberType.MakeNew(umbraco.BasePages.UmbracoEnsuredPage.CurrentUser, "Standard");
                memberType.Alias     = "Standard";
                memberType.Text      = "Standard";
                memberType.IconUrl   = "memberType.gif";
                memberType.Thumbnail = "member.png";

                int tabInfoId     = memberType.AddVirtualTab("Info");
                int tabPersonalId = memberType.AddVirtualTab("Personal");
                int tabServicesId = memberType.AddVirtualTab("Services");
                int tabDataId     = memberType.AddVirtualTab("Data");

                DataTypeDefinition textstring         = DataTypeDefinition.GetDataTypeDefinition(-88);
                DataTypeDefinition checkbox           = DataTypeDefinition.GetDataTypeDefinition(-49);
                DataTypeDefinition upload             = DataTypeDefinition.GetDataTypeDefinition(-90);
                DataTypeDefinition datePickerWithTime = DataTypeDefinition.GetDataTypeDefinition(-36);
                DataTypeDefinition numeric            = DataTypeDefinition.GetDataTypeDefinition(-51);
                DataTypeDefinition textboxMultiple    = DataTypeDefinition.GetDataTypeDefinition(-89);

                // Info
                memberType.AddPropertyType(textstring, "nickname", "Nickname");
                memberType.getPropertyType("nickname").TabId = tabInfoId;

                memberType.AddPropertyType(upload, "profilePicture", "Profile picture");
                memberType.getPropertyType("profilePicture").TabId = tabInfoId;

                // Personal
                memberType.AddPropertyType(datePickerWithTime, "birthdate", "Birthdate");
                memberType.getPropertyType("birthdate").TabId = tabPersonalId;

                memberType.AddPropertyType(textstring, "gender", "Gender");
                memberType.getPropertyType("gender").TabId = tabPersonalId;

                memberType.AddPropertyType(textstring, "firstname", "Firstname");
                memberType.getPropertyType("firstname").TabId = tabPersonalId;

                memberType.AddPropertyType(textstring, "surname", "Surname");
                memberType.getPropertyType("surname").TabId = tabPersonalId;

                memberType.AddPropertyType(textstring, "locationAddress", "Address");
                memberType.getPropertyType("locationAddress").TabId = tabPersonalId;

                memberType.AddPropertyType(textstring, "locationZip", "Zip code");
                memberType.getPropertyType("locationZip").TabId = tabPersonalId;

                memberType.AddPropertyType(textstring, "locationCity", "City");
                memberType.getPropertyType("locationCity").TabId = tabPersonalId;

                memberType.AddPropertyType(textstring, "locationCountry", "Country");
                memberType.getPropertyType("locationCountry").TabId = tabPersonalId;

                // Servives
                memberType.AddPropertyType(textstring, "twitterAlias", "Twitter alias");
                memberType.getPropertyType("twitterAlias").TabId = tabServicesId;

                memberType.AddPropertyType(textstring, "flickrAlias", "Flickr alias");
                memberType.getPropertyType("flickrAlias").TabId = tabServicesId;

                // Data
                memberType.AddPropertyType(checkbox, "approved", "Approved");
                memberType.getPropertyType("approved").TabId = tabDataId;

                memberType.AddPropertyType(checkbox, "lock", "Lock user");
                memberType.getPropertyType("lock").TabId = tabDataId;

                memberType.AddPropertyType(numeric, "failed_logins", "Failed Password Attempts");
                memberType.getPropertyType("failed_logins").TabId = tabDataId;

                memberType.AddPropertyType(textboxMultiple, "comments", "Comment");
                memberType.getPropertyType("comments").TabId = tabDataId;

                memberType.AddPropertyType(datePickerWithTime, "last_login", "LastLogin");
                memberType.getPropertyType("last_login").TabId = tabDataId;

                memberType.AddPropertyType(textstring, "SecretCode", "Hemmelig kode");
                PropertyType typeSecretCode = memberType.getPropertyType("SecretCode");
                typeSecretCode.TabId       = tabDataId;
                typeSecretCode.Description = "Bruges blandt andet til aktivering/sletning emails";
            }
        }
Exemplo n.º 2
0
        private void SynchronizeMemberTypeProperties(Type typeMemberType, MemberType memberType)
        {
            int propertySortOrder = 0;

            foreach (PropertyInfo propInfo in typeMemberType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                DocumentTypePropertyAttribute propAttr = Util.GetAttribute <DocumentTypePropertyAttribute>(propInfo);
                if (propAttr == null)
                {
                    continue; // skip this property - not part of a document type
                }

                // getting name and alias
                string propertyName;
                string propertyAlias;
                DocumentTypeManager.ReadPropertyNameAndAlias(propInfo, propAttr, out propertyName, out propertyAlias);

                if (HasObsoleteAttribute(propInfo))
                {
                    Util.DeletePropertyType(memberType, propertyAlias);
                    continue;
                }

                if (propAttr.DefaultValue != null)
                {
                    _hadDefaultValues = true; // at least one property has a default value
                }


                umbraco.cms.businesslogic.datatype.DataTypeDefinition dataTypeDefinition = GetDataTypeDefinition(typeMemberType, propAttr, propInfo);

                // getting property if already exists, or creating new if it not exists
                umbraco.cms.businesslogic.propertytype.PropertyType propertyType = memberType.PropertyTypes.FirstOrDefault(p => p.Alias == propertyAlias);
                if (propertyType == null) // if not exists, create it
                {
                    memberType.AddPropertyType(dataTypeDefinition, propertyAlias, propertyName);
                    propertyType = memberType.PropertyTypes.FirstOrDefault(p => p.Alias == propertyAlias);
                }

                //// Setting up the tab of this property. If tab doesn't exists, create it.
                if (!string.IsNullOrEmpty(propAttr.TabAsString) && propAttr.TabAsString.ToLower() != DocumentTypeDefaultValues.TabGenericProperties.ToLower())
                {
                    // try to find this tab
                    umbraco.cms.businesslogic.propertytype.PropertyTypeGroup pg = memberType.PropertyTypeGroups.FirstOrDefault(x => x.Name == propAttr.TabAsString);
                    if (pg == null) // if found
                    {
                        memberType.AddVirtualTab(propAttr.TabAsString);
                        pg = memberType.PropertyTypeGroups.FirstOrDefault(x => x.Name == propAttr.TabAsString);
                    }

                    if (propAttr.TabOrder.HasValue)
                    {
                        pg.SortOrder = propAttr.TabOrder.Value;
                    }

                    propertyType.PropertyTypeGroup = pg.Id;
                }

                propertyType.Name             = propertyName;
                propertyType.Mandatory        = propAttr.Mandatory;
                propertyType.ValidationRegExp = propAttr.ValidationRegExp;
                propertyType.Description      = propAttr.Description;
                propertyType.SortOrder        = propertySortOrder;

                propertySortOrder++;
            }
        }
        public static void Import(XmlNode n, bool ImportStructure)
        {
            if (n == null)
            {
                throw new ArgumentNullException("Node cannot be null");
            }

            //
            // using xmlHelper not XmlHelper because GetNodeValue has gone all
            // Internall on us, this function probibly does belong in the core
            // (umbraco.cms.buisnesslogic.packageInstaller) so that packages
            // can also do member types, but at the mo, it's uSync's until i read
            // about contributing to the core.
            //

            // using user 0 will come unstuck oneday
            User u = new User(0);

            // does this member type already exist ?
            string alias = xmlHelper.GetNodeValue(n.SelectSingleNode("Info/Alias"));

            if (String.IsNullOrEmpty(alias))
            {
                throw new Exception("no alias in sync file");
            }

            MemberType mt = null;

            try
            {
                mt = MemberType.GetByAlias(alias);
            }
            catch (Exception ex)
            {
                LogHelper.Debug <SyncMemberType>("Member type corrupt? {0}", () => ex.ToString());
            }

            if (mt == null)
            {
                // we are new
                mt       = MemberType.MakeNew(u, xmlHelper.GetNodeValue(n.SelectSingleNode("Info/Name")));
                mt.Alias = xmlHelper.GetNodeValue(n.SelectSingleNode("Info/Alias"));
            }
            else
            {
                mt.Text = xmlHelper.GetNodeValue(n.SelectSingleNode("Info/Name"));
            }

            // core
            mt.IconUrl     = xmlHelper.GetNodeValue(n.SelectSingleNode("Info/Icon"));
            mt.Thumbnail   = xmlHelper.GetNodeValue(n.SelectSingleNode("Info/Thumbnail"));
            mt.Description = xmlHelper.GetNodeValue(n.SelectSingleNode("Info/Description"));

            // v6 you can have allow at root.
            // Allow at root (check for node due to legacy)
            bool   allowAtRoot     = false;
            string allowAtRootNode = xmlHelper.GetNodeValue(n.SelectSingleNode("Info/AllowAtRoot"));

            if (!String.IsNullOrEmpty(allowAtRootNode))
            {
                bool.TryParse(allowAtRootNode, out allowAtRoot);
            }
            mt.AllowAtRoot = allowAtRoot;

            //Master content type
            string master = xmlHelper.GetNodeValue(n.SelectSingleNode("Info/Master"));


            if (!String.IsNullOrEmpty(master))
            {
                // throw new System.Exception(String.Format("Throwing in {0}, master {1}", mt.Text, master));
                try
                {
                    MemberType pmt = MemberType.GetByAlias(master);
                    if (pmt != null)
                    {
                        mt.MasterContentType = pmt.Id;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Debug <SyncMemberType>("Member type corrupt? {0}", () => ex.ToString());
                }
            }

            //tabs

            ContentType.TabI[] tabs = mt.getVirtualTabs;

            // load the current tabs
            string tabnames = ";";

            for (int t = 0; t < tabs.Length; t++)
            {
                tabnames += tabs[t].Caption + ";";
            }

            Hashtable ht = new Hashtable();

            foreach (XmlNode t in n.SelectNodes("Tabs/Tab"))
            {
                // is this a new tab?
                // if ( tabnames.IndexOf(";" + xmlHelper.GetNodeValue(t.SelectSingleNode("Caption")) + ";" == -1)
                if (!tabnames.Contains(";" + xmlHelper.GetNodeValue(t.SelectSingleNode("Caption")) + ";"))
                {
                    ht.Add(int.Parse(xmlHelper.GetNodeValue(t.SelectSingleNode("Id"))),
                           mt.AddVirtualTab(xmlHelper.GetNodeValue(t.SelectSingleNode("Caption"))));
                }
            }
            // clear cache
            mt.ClearVirtualTabs();

            // put tabs in a hashtable, so we can check they exist when we add properties.
            Hashtable tabList = new Hashtable();

            foreach (ContentType.TabI t in mt.getVirtualTabs.ToList())
            {
                if (!tabList.ContainsKey(t.Caption))
                {
                    tabList.Add(t.Caption, t.Id);
                }
            }

            // properties..
            global::umbraco.cms.businesslogic.datatype.controls.Factory f =
                new global::umbraco.cms.businesslogic.datatype.controls.Factory();

            foreach (XmlNode gp in n.SelectNodes("GenericProperties/GenericProperty"))
            {
                int  dfId = 0;
                Guid dtId = new Guid(xmlHelper.GetNodeValue(gp.SelectSingleNode("Type")));

                if (gp.SelectSingleNode("Definition") != null && !string.IsNullOrEmpty(xmlHelper.GetNodeValue(gp.SelectSingleNode("Definition"))))
                {
                    Guid dtdId = new Guid(xmlHelper.GetNodeValue(gp.SelectSingleNode("Definition")));
                    if (CMSNode.IsNode(dtdId))
                    {
                        dfId = new CMSNode(dtdId).Id;
                    }
                }

                if (dfId == 0)
                {
                    try
                    {
                        dfId = findDataTypeDefinitionFromType(ref dtId);
                    }
                    catch
                    {
                        throw new Exception(String.Format("Cound not find datatype with id {0}.", dtId));
                    }
                }

                //fix for ritch text editor
                if (dfId == 0 && dtId == new Guid("a3776494-0574-4d93-b7de-efdfdec6f2d1"))
                {
                    dtId = new Guid("83722133-f80c-4273-bdb6-1befaa04a612");
                    dfId = findDataTypeDefinitionFromType(ref dtId);
                }

                if (dfId != 0)
                {
                    PropertyType pt = mt.getPropertyType(xmlHelper.GetNodeValue(gp.SelectSingleNode("Alias")));
                    if (pt == null)
                    {
                        mt.AddPropertyType(
                            global::umbraco.cms.businesslogic.datatype.DataTypeDefinition.GetDataTypeDefinition(dfId),
                            xmlHelper.GetNodeValue(gp.SelectSingleNode("Alias")),
                            xmlHelper.GetNodeValue(gp.SelectSingleNode("Name"))
                            );
                        pt = mt.getPropertyType(xmlHelper.GetNodeValue(gp.SelectSingleNode("Alias")));
                    }
                    else
                    {
                        pt.DataTypeDefinition = global::umbraco.cms.businesslogic.datatype.DataTypeDefinition.GetDataTypeDefinition(dfId);
                        pt.Name = xmlHelper.GetNodeValue(gp.SelectSingleNode("Name"));
                    }

                    pt.Mandatory        = bool.Parse(xmlHelper.GetNodeValue(gp.SelectSingleNode("Mandatory")));
                    pt.ValidationRegExp = xmlHelper.GetNodeValue(gp.SelectSingleNode("Validation"));
                    pt.Description      = xmlHelper.GetNodeValue(gp.SelectSingleNode("Description"));

                    // tab
                    try
                    {
                        if (tabList.ContainsKey(xmlHelper.GetNodeValue(gp.SelectSingleNode("Tab"))))
                        {
                            pt.TabId = (int)tabList[xmlHelper.GetNodeValue(gp.SelectSingleNode("Tab"))];
                        }
                    }
                    catch (Exception ee)
                    {
                        LogHelper.Debug <SyncMemberType>("Packager: Error assigning property to tab: {0}", () => ee.ToString());
                    }
                    pt.Save();
                }
            }

            if (ImportStructure)
            {
                if (mt != null)
                {
                    ArrayList allowed = new ArrayList();
                    foreach (XmlNode structure in n.SelectNodes("Structure/MemberType"))
                    {
                        try
                        {
                            MemberType dtt = MemberType.GetByAlias(xmlHelper.GetNodeValue(structure));
                            if (dtt != null)
                            {
                                allowed.Add(dtt.Id);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Info <uSync>("Can't find structure MemberType - so skipping");
                        }
                    }

                    int[] adt = new int[allowed.Count];
                    for (int i = 0; i < allowed.Count; i++)
                    {
                        adt[i] = (int)allowed[i];
                    }
                    mt.AllowedChildContentTypeIDs = adt;
                }
            }

            mt.Save();

            /*
             * foreach (MediaType.TabI t in mt.getVirtualTabs.ToList())
             * {
             *  MediaType.FlushTabCache(t.Id, mt.Id);
             * }
             *
             * // need to do this more i think
             * MediaType.FlushFromCache(mt.Id);
             */
        }