Beispiel #1
0
        internal static string GetValue(string name, SNC.Content parentContent, bool storedData = false, CultureInfo cultureInfo = null, bool formattedValue = true)
        {
            string[] parts = name.Split(new char[] { '.' });
            if (parts.Length == 0)
            {
                return("");
            }

            string currPart = "";

            #region special values

            if (parts[0].ToLower() == "current" && parts.Length > 1)
            {
                switch (parts[1].ToLower())
                {
                case "url":
                    if (parts.Length > 2)
                    {
                        switch (parts[2].ToLower())
                        {
                        case "hostname":
                            foreach (string url in SNP.Page.Current.Site.UrlList.Keys)
                            {
                                if (Portal.Virtualization.PortalContext.Current.OriginalUri.ToString().IndexOf(string.Concat(Portal.Virtualization.PortalContext.Current.OriginalUri.Scheme, "://", url, "/")) == 0)
                                {
                                    return(url);
                                }
                            }
                            return("");

                        case "host":
                            foreach (string url in SNP.Page.Current.Site.UrlList.Keys)
                            {
                                if (Portal.Virtualization.PortalContext.Current.OriginalUri.ToString().IndexOf(string.Concat(Portal.Virtualization.PortalContext.Current.OriginalUri.Scheme, "://", url, "/")) == 0)
                                {
                                    return(string.Concat(Portal.Virtualization.PortalContext.Current.OriginalUri.Scheme, "://", url));
                                }
                            }
                            return("");

                        case "name":
                            return(Portal.Virtualization.PortalContext.Current.OriginalUri.OriginalString);

                        default:
                            return("");
                        }
                    }
                    else
                    {
                        return("");
                    }

                case "user":
                    if (User.Current != null && parts.Length > 1)
                    {
                        switch (parts[2].ToLower())
                        {
                        case "isauthenticated":
                            return(User.Current.IsAuthenticated ? "1" : "0");

                        default:
                            return(((User)User.Current).GetProperty(parts[2]).ToString());
                        }
                    }
                    else
                    {
                        return("");
                    }

                case "site":
                    if (PortalContext.Current.Site != null)
                    {
                        return(PortalContext.Current.Site.GetProperty(parts[2]).ToString());
                    }
                    else
                    {
                        return("");
                    }

                case "page":
                    if (PortalContext.Current.Page != null)
                    {
                        return(PortalContext.Current.Page.GetProperty(parts[2]).ToString());
                    }
                    else
                    {
                        return("");
                    }
                }
            }

            #endregion

            object obj             = null;
            object previousListObj = null; // Needed because of IList fields with no indexing
            foreach (string _currPart in parts)
            {
                currPart = _currPart;
                int index = 0;
                //bool isIndexed = false;

                #region Custom properties

                // Check if current part is indexed and if it is get the index and take it off the current part
                if (IsIndexedField(currPart))
                {
                    index = GetIndexedFieldValue(currPart);
                    //isIndexed = true;
                    currPart = StripIndex(currPart);
                }

                if (currPart == "Count")
                {
                    if (previousListObj is ICollection)
                    {
                        return(((ICollection)previousListObj).Count.ToString());
                    }
                    else if (obj is ICollection)
                    {
                        return(((ICollection)obj).Count.ToString());
                    }
                }

                if (currPart == "AsList()" || Regex.IsMatch(currPart, "AsList[(](['\"](.*)['\"])[)]"))
                {
                    if (previousListObj is IList)
                    {
                        Match  firstMatch      = Regex.Match(currPart, "AsList[(](['\"](.*)['\"])[)]");
                        string separatorString = firstMatch.Groups[1].Value;
                        if (separatorString == String.Empty)
                        {
                            separatorString = ", "; // default
                        }
                        List <string> elements = new List <string>();
                        foreach (object currObj in previousListObj as IList)
                        {
                            elements.Add(currObj.ToString());
                        }
                        return(string.Join(separatorString, elements.ToArray()));
                    }
                }

                if (_currPart == "Load()" && obj is string)
                {
                    obj = Node.Load <Node>(obj as string);
                    if (!(obj is Node))
                    {
                        return("");
                    }
                    else
                    {
                        parentContent = SNC.Content.Load(((Node)obj).Id);
                    }
                    continue;
                }

                if (currPart == "SiteRelativePath")
                {
                    if (SNP.Page.Current.Site != null && ((Node)parentContent.ContentHandler).Path.StartsWith(SNP.Page.Current.Site.Path))
                    {
                        return(((Node)parentContent.ContentHandler).Path.Substring(SNP.Page.Current.Site.Path.Length));
                    }
                    else
                    {
                        currPart = "Path";
                    }
                }

                if (currPart == "PageRelativePath")
                {
                    if (SNP.Page.Current != null && ((Node)parentContent.ContentHandler).Path.StartsWith(SNP.Page.Current.Path))
                    {
                        return(((Node)parentContent.ContentHandler).Path.Substring(String.Concat(SNP.Page.Current.Path, "/").Length));
                    }
                    else
                    {
                        currPart = "Path";
                    }
                }

                if (currPart == "Children" && obj is IFolder)
                {
                    IFolder f = obj as IFolder;
                    previousListObj = f.Children;
                    obj             = f.Children;
                    if (index < f.ChildCount)
                    {
                        obj           = f.Children.ToArray <Node>()[index];
                        parentContent = SNC.Content.Load(((Node)obj).Id);
                    }
                    continue;
                }

                // Check for 'string' parts
                if (Regex.IsMatch(_currPart, @"'(.+)'"))
                {
                    if (obj == null || obj is string)
                    {
                        Match firstMatch = Regex.Match(_currPart, @"'(.+)'");
                        obj += firstMatch.Groups[1].Value;
                        continue;
                    }
                    else
                    {
                        return("");
                    }
                }

                #region Custom Field property mappings

                if (obj is ContentRepository.Fields.HyperLinkField.HyperlinkData)
                {
                    var hyperlinkObj = (ContentRepository.Fields.HyperLinkField.HyperlinkData)obj;
                    if (currPart == "Href")
                    {
                        obj = hyperlinkObj.Href;
                    }
                    if (currPart == "Text")
                    {
                        obj = hyperlinkObj.Text;
                    }
                    if (currPart == "Title")
                    {
                        obj = hyperlinkObj.Title;
                    }
                    if (currPart == "Target")
                    {
                        obj = hyperlinkObj.Target;
                    }
                    continue;
                }

                #endregion

                #endregion

                // If parent content is empty next part can not be resolved. Later on however something could be returned if 'string' or Load() part is present
                if (parentContent == null)
                {
                    obj = null;
                    continue;
                }

                // Try to get the property of the current part
                try
                {
                    if (storedData)
                    {
                        obj = parentContent.GetStoredValue(_currPart);
                    }
                    else if (cultureInfo == null)
                    {
                        Field field;

                        //try to get the formatted value in case of number/currency and datetime fields
                        if (formattedValue && parentContent.Fields.TryGetValue(_currPart, out field) &&
                            (field is NumberField || field is DateTimeField))
                        {
                            obj = field.GetFormattedValue();
                        }
                        else
                        {
                            obj = parentContent[_currPart];
                        }
                    }
                    else
                    {
                        obj = parentContent.GetLocalizedValue(_currPart, cultureInfo);
                    }
                }
                catch (Exception e) //logged
                {
                    Logger.WriteException(e);
                    obj = ((GenericContent)parentContent.ContentHandler).GetProperty(currPart);
                }

                #region Check the type of the obj and deal with it and set previousListObj, parentContent and obj accordingly
                if (obj is IList)
                {
                    IList list = obj as IList;
                    obj             = list;
                    previousListObj = list;
                    if (index < list.Count)
                    {
                        obj = list[index];
                        Node node = obj as Node;
                        if (node != null)
                        {
                            parentContent = SNC.Content.Load(node.Id);
                        }
                        else
                        {
                            parentContent = null;
                        }
                    }
                    else
                    {
                        parentContent = null;
                    }
                    continue;
                }
                else if (obj is ContentRepository.Fields.HyperLinkField.HyperlinkData)
                {
                    continue;
                }
                else if (obj is Node)//Not link and not HyperlinkData
                {
                    parentContent = SNC.Content.Load(((Node)obj).Id);
                    continue;
                }
                else // If the object was not of the above then carry it on to the next iteration
                {
                    continue;
                }
                #endregion
            }
            if (obj != null) // No more parts left
            {
                return(obj.ToString());
            }
            else
            {
                return("");
                //throw new Exception(String.Format("{0} could not be resolved at {1}.", name, currPart));
            }
        }