Esempio n. 1
0
        public static Node GetDescendantViaDocumentTypePath(this Node node, string path)
        {
            Node returnNode = null;

            if (string.IsNullOrEmpty(path))
            {
                // If no path is provided, just return self
                return(node);
            }

            XPathNavigator navigator = node.ToXPathNavigator();

            XPathNodeIterator nodeIterator = navigator.Select(path);

            if (nodeIterator.MoveNext())
            {
                XPathNavigator current     = nodeIterator.Current;
                XPathNavigator idNavigator = current.SelectSingleNode("@id");
                if (idNavigator != null)
                {
                    int nodeId = idNavigator.ValueAsInt;
                    returnNode = UmbracoUtil.GetNode(nodeId);
                }
            }
            return(returnNode);
        }
Esempio n. 2
0
        public Node GetAncestorAtLevel(int level)
        {
            if (level > Levels.Length)
            {
                throw new ArgumentOutOfRangeException("level", "level is to high");
            }
            int  nodeId      = Levels[level];
            Node nodeAtLevel = UmbracoUtil.GetNode(nodeId);

            return(nodeAtLevel);
        }
Esempio n. 3
0
        public static void SetDefaultValuesOnNew(Document newDocument, umbraco.cms.businesslogic.NewEventArgs e)
        {
            log.Info("SetDefaultValuesOnNew Startet");
            log.InfoFormat("Document id: {1} - Document Type Alias: {0}", newDocument.ContentType.Alias, newDocument.Id);
            // We wrap it in a try since this part should not break for the user in umbraco
            try
            {
                string path = newDocument.Path;
                if (string.IsNullOrEmpty(path))
                {
                    StopWithError("Path on new document not found!");
                    return;
                }

                string[] strings      = StringUtil.Split(path, ",", true);
                string   strHomeDocId = strings[1];

                // Now we have the doc home id, lets see if the default value document based on document type alias exists
                string cacheKeyDefaultValueDocumentTypeAlias = string.Format("SetDefaultValuesOnNew#homeDoc:{0}#DefaultValueDocumentTypeAlias:{1}", strHomeDocId, newDocument.ContentType.Alias);
                int    defaultValueDocId = -1;
                if (CacheUtil.Exist(cacheKeyDefaultValueDocumentTypeAlias))
                {
                    defaultValueDocId = CacheUtil.Get <int>(cacheKeyDefaultValueDocumentTypeAlias);
                }
                else
                {
                    // Does not exist in the cache, lets try to look it up
                    int defaultValuesFolderDocId = GetDefaultValuesFolderId(strHomeDocId);
                    if (defaultValuesFolderDocId == -1)
                    {
                        StopWithError("Could not load default values folder id!");
                        return;
                    }

                    // For speed we use the published node factory
                    Node node = UmbracoUtil.GetNode(defaultValuesFolderDocId);
                    if (node == null)
                    {
                        StopWithError(string.Format("Could not load published node for default values folder id: {0}!", defaultValuesFolderDocId));
                        return;
                    }

                    Node defaultValueNode = node.GetDescendantViaDocumentTypePath(newDocument.ContentType.Alias);
                    if (defaultValueNode != null)
                    {
                        defaultValueDocId = defaultValueNode.Id;
                        CacheUtil.Insert(cacheKeyDefaultValueDocumentTypeAlias, defaultValueDocId, 10, false);
                    }
                    else
                    {
                        log.DebugFormat("No default node for document type alias: {0}", newDocument.ContentType.Alias);
                    }
                }

                if (defaultValueDocId > -1)
                {
                    Document defaultValueDocument = new Document(true, defaultValueDocId);

                    log.DebugFormat("ContentType {0} match. Let's start copying", newDocument.ContentType.Alias);

                    umbraco.cms.businesslogic.property.Properties newDocumentProperties  = newDocument.GenericProperties;
                    umbraco.cms.businesslogic.property.Properties defaultValueProperties = defaultValueDocument.GenericProperties;

                    for (int i = 0; i < newDocumentProperties.Count; i++)
                    {
                        umbraco.cms.businesslogic.property.Property newDocumentProperty     = newDocumentProperties[i];
                        umbraco.cms.businesslogic.property.Property defaultDocumentProperty = defaultValueProperties[i];

                        if (newDocumentProperty.Value != defaultDocumentProperty.Value)
                        {
                            log.DebugFormat("Set value on property alias {0}", newDocumentProperty.PropertyType.Alias);
                            newDocumentProperty.Value = defaultDocumentProperty.Value;
                        }
                    }
                }
                log.Info("SetDefaultValuesOnNew Ended");
            }
            catch (Exception ex)
            {
                log.Error("Could not complete SetDefaultValuesOnNew - stopped with exception", ex);
            }
        }
Esempio n. 4
0
        public static string GetNodeUrl(Node node, string alternateTemplate, bool includeDomain)
        {
            // If node is null, just return an empty.String
            if (node == null)
            {
                return(string.Empty);
            }

            // Check to see if the node is a redirect node.
            if (node.NodeTypeAlias == Configuration.ConfigurationManager.DocumentTypeAliases.Redirect.Value)
            {
                // Is it an internel link, then change node.
                int umbracoRedirectNodeId = node.GetPropertyValue("umbracoRedirect", -1);
                if (umbracoRedirectNodeId > -1)
                {
                    node = UmbracoUtil.GetNode(umbracoRedirectNodeId);
                    if (node == null)
                    {
                        return(string.Empty);
                    }
                }
                else // check to see if we should do an external link
                {
                    string externalUrl = node.GetPropertyValue("externalLink", string.Empty);
                    if (externalUrl != string.Empty)
                    {
                        return(externalUrl);
                    }
                }
            }

            bool   umbracoUseDirectoryUrls = System.Configuration.ConfigurationManager.AppSettings["umbracoUseDirectoryUrls"] == "true";
            string url = string.Empty;

            string umbracoUrlAlias = node.GetPropertyValue("umbracoUrlAlias");

            if (umbracoUrlAlias != string.Empty && alternateTemplate == string.Empty)
            {
                if (umbracoUseDirectoryUrls == false && umbracoUrlAlias.EndsWith(".aspx") == false) // Running the site with .aspx extension
                {
                    url = "/" + umbracoUrlAlias + ".aspx";
                }
                else // Running the site with directory urls
                {
                    url = "/" + umbracoUrlAlias;
                }
            }
            else
            {
                int nodeLevel = node.Level();
                if (nodeLevel <= 2) // If we are on the frontpage of the site. (or below)
                {
                    url = "/";
                }
                else
                {
                    url = umbraco.library.NiceUrl(node.Id);
                    if (alternateTemplate != string.Empty)
                    {
                        if (umbracoUseDirectoryUrls) // Running the site with-out .aspx extension
                        {
                            url += "/" + alternateTemplate;
                        }
                        else // Running the site with directory urls
                        {
                            url = url.Replace(".aspx", "/" + alternateTemplate + ".aspx");
                        }
                    }
                }
            }

            if (includeDomain)
            {
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Request.ServerVariables["SERVER_NAME"] != null)
                    {
                        string domain   = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
                        string protocol = "http://";
                        if (HttpContext.Current.Request.ServerVariables["HTTPS"] != null && HttpContext.Current.Request.ServerVariables["HTTPS"] == "ON")
                        {
                            protocol = "https://";
                        }
                        url = string.Concat(protocol, domain, url);
                    }
                }
            }
            return(url);
        }