public static void UpdateDynamicPageIndex(AgilityPage page)
        {
            if (!string.IsNullOrEmpty(page.DynamicPageContentViewReferenceName) ||
                !string.IsNullOrEmpty(page.DynamicPageParentFieldName))
            {
                if (!string.IsNullOrEmpty(page.DynamicPageContentViewReferenceName.ToLowerInvariant()))
                {
                    BaseCache.UpdateDynamicPageIndex(page.ID, page.DynamicPageContentViewReferenceName.ToLowerInvariant());
                }

                //update the Dynamic Page Formula Index that use this page...
                BaseCache.ClearDynamicDynamicPageFormulaIndex(page);
            }
        }
        private static ResolvedPage ResolvePage(
            ref Dictionary <string, AgilityRouteCacheItem> routes,
            AgilityRouteCacheItem routeItem,
            string dynamicRoute,
            string languageCode,
            string websiteName,
            ref List <ResolvedPage> resolvedPages,
            string url)
        {
            AgilityPage page = BaseCache.GetPageFromID(routeItem.PageID, languageCode, websiteName, url);



            if (string.IsNullOrEmpty(dynamicRoute))
            {
                //make sure we have the page, if not, then kick out
                if (page == null)
                {
                    return(null);
                }

                if (!string.IsNullOrEmpty(page.DynamicPageContentViewReferenceName) ||
                    !string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    //we have NO dynamic route, but the page is dynamic, kick out
                    return(null);
                }

                //not a dynamic page (a static page nested under a dynamic one...)
                return(new ResolvedPage(page, null));
            }
            else
            {
                //resolve the Dynamic Route

                if (string.IsNullOrEmpty(routeItem.ChildDynamicPagePath))
                {
                    //no dynamic page attached to this static page.. kick out
                    return(null);
                }

                //get the route item for the child dynamic page
                if (!routes.TryGetValue(routeItem.ChildDynamicPagePath, out routeItem))
                {
                    return(null);
                }



                //get the dynamic page
                page = BaseCache.GetPageFromID(routeItem.PageID, languageCode, websiteName, url);

                if (page == null)
                {
                    Agility.Web.Tracing.WebTrace.WriteWarningLine(string.Format("Routing: could not load page with id: {0} in language {1} websitename {2} url {3}", routeItem.PageID, languageCode, websiteName, url));
                    return(null);
                }


                if (string.IsNullOrEmpty(page.DynamicPageContentViewReferenceName) &&
                    string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    //we have a dynamic route, but the page is not dynamic, kick out
                    return(null);
                }

                string contentReferenceName = page.DynamicPageContentViewReferenceName;
                int    parentContentID      = -1;
                if (string.IsNullOrEmpty(contentReferenceName))
                {
                    if (!string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                    {
                        for (int i = resolvedPages.Count - 1; i >= 0; i--)
                        {
                            DynamicPageFormulaItem dpParent = resolvedPages[i].DynamicPageItem;
                            if (dpParent == null)
                            {
                                continue;
                            }

                            //get the content reference name from the parent page...
                            object fieldValueObj = dpParent.GetContentItemValue(page.DynamicPageParentFieldName);
                            if (fieldValueObj != null)
                            {
                                contentReferenceName = fieldValueObj as string;
                                break;
                            }
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    //filter the dynamic page list by the parent field...
                    for (int i = resolvedPages.Count - 1; i >= 0; i--)
                    {
                        DynamicPageFormulaItem dpParent = resolvedPages[i].DynamicPageItem;
                        if (dpParent == null)
                        {
                            continue;
                        }
                        if (dpParent != null)
                        {
                            parentContentID = dpParent.ContentID;
                            break;
                        }
                    }
                    if (parentContentID < 1)
                    {
                        return(null);
                    }
                }

                //if we didn't resolve a content view, kick out...
                if (string.IsNullOrEmpty(contentReferenceName))
                {
                    return(null);
                }

                //create/update the dynamic page index with this page id and dynamic page list reference name
                BaseCache.UpdateDynamicPageIndex(page.ID, contentReferenceName);

                //get the content first if we are in development or staging mode...
                if (AgilityContext.CurrentMode == Enum.Mode.Staging || Agility.Web.Configuration.Current.Settings.DevelopmentMode)
                {
                    AgilityContentServer.AgilityContent content = BaseCache.GetContent(contentReferenceName, page.LanguageCode, AgilityContext.WebsiteName);
                }

                //get the dynamic page list dictionary for this page (this method also updates if neccessary)...
                DynamicPageFormulaItemIndex dpIndex = BaseCache.GetDynamicPageFormulaIndex(page.ID, contentReferenceName, page.LanguageCode, page, true);

                DynamicPageFormulaItem dpItem = null;
                if (dpIndex == null || !dpIndex.TryGetValue(dynamicRoute, out dpItem))
                {
                    //check if friendly-making the route would result in a match
                    string friendly = DynamicPageFormulaItem.MakePathFriendly(dynamicRoute);
                    if (friendly == dynamicRoute || !dpIndex.TryGetValue(friendly, out dpItem))
                    {
                        //we didn't find the item, and making the given path friendly didn't work either. Kick out.
                        return(null);
                    }

                    //there's an item, but only after fixing the path. redirect.
                    string baseUrl     = Data.GetSiteMapNode(page.ID).PagePath;
                    string redirectUrl = baseUrl.LastIndexOf('/') > 0
                                                                                   ? baseUrl.Substring(0, baseUrl.LastIndexOf('/')) + friendly
                                                                                   : friendly;
                    if (url.ToLower().EndsWith(".aspx"))
                    {
                        redirectUrl += ".aspx";
                    }

                    //add the current query string..
                    string query = AgilityContext.HttpContext.Request.QueryString.Value;
                    if (!string.IsNullOrEmpty(query))
                    {
                        redirectUrl += query;
                    }

                    //set the url to get this page, and the ActionResult or HttpModule will 301 redirect to it.
                    ResolvedPage resolvedPage = new ResolvedPage(page, dpItem);
                    resolvedPage.RedirectURL = redirectUrl;
                    return(resolvedPage);
                }

                //check parent content if neccessary
                if (parentContentID > 0 &&
                    !string.IsNullOrEmpty(page.DynamicPageParentFieldName))
                {
                    object testParentContentIDObj = dpItem.GetContentItemValue(page.DynamicPageParentFieldName);

                    if (testParentContentIDObj != null)
                    {
                        int    testParentContentID    = -1;
                        string testParentContentIDStr = string.Format("{0}", testParentContentIDObj);

                        if (int.TryParse(testParentContentIDStr, out testParentContentID))
                        {
                            //if the value is an int, test for equality...
                            if (parentContentID != testParentContentID)
                            {
                                return(null);
                            }
                        }
                        else
                        {
                            //value is NOT an int, test for "in" '[id],' or ',[id],' or ',[id]'
                            if (!testParentContentIDStr.StartsWith(string.Format("{0},", parentContentID)) &&
                                !testParentContentIDStr.EndsWith(string.Format(",{0}", parentContentID)) &&
                                !testParentContentIDStr.Contains(string.Format(",{0},", parentContentID)))
                            {
                                return(null);
                            }
                        }
                    }
                }

                return(new ResolvedPage(page, dpItem));
            }
        }
示例#3
0
        private List <AgilitySiteMapNode> GetDynamicChildNodes(AgilitySiteMapNode anode, AgilitySiteMapNode parentNode, List <AgilitySiteMapNode> collection)
        {
            string dynamicPageContentReferenceName = anode.DynamicPageContentReferenceName;
            string dynamicPageParentFieldName      = anode.DynamicPageParentFieldName;

            if (string.IsNullOrEmpty(dynamicPageContentReferenceName) && string.IsNullOrEmpty(dynamicPageParentFieldName))
            {
                return(collection);
            }


            //the child pages are dynamic pages...
            AgilityPage page            = anode.AgilityPage;
            int         parentContentID = 0;

            if (page != null)
            {
                string contentReferenceName = dynamicPageContentReferenceName;

                if (string.IsNullOrEmpty(contentReferenceName))
                {
                    AgilityDynamicSiteMapNode dpNode = anode.ParentNode as AgilityDynamicSiteMapNode;

                    if (dpNode == null)
                    {
                        AgilitySiteMapNode pnode = parentNode;

                        while (pnode != null)
                        {
                            dpNode = pnode.ParentNode as AgilityDynamicSiteMapNode;
                            if (dpNode != null)
                            {
                                break;
                            }
                            pnode = pnode.ParentNode;
                        }
                    }

                    if (!string.IsNullOrEmpty(dynamicPageParentFieldName) && dpNode != null && !string.IsNullOrEmpty(dpNode.ReferenceName))
                    {
                        //get the content reference name from the parent page...
                        AgilityContent parentContent = BaseCache.GetContent(dpNode.ReferenceName, AgilityContext.LanguageCode, AgilityContext.WebsiteName);
                        if (parentContent != null &&
                            parentContent.DataSet != null &&
                            parentContent.DataSet.Tables["ContentItems"] != null &&
                            parentContent.DataSet.Tables["ContentItems"].Columns.Contains(dynamicPageParentFieldName))
                        {
                            DataRow row = parentContent.GetItemByContentID(dpNode.ContentID);

                            if (row != null)
                            {
                                //the contentReferenceName is stored in the field value...
                                contentReferenceName = row[dynamicPageParentFieldName] as string;
                            }
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(dynamicPageParentFieldName))
                {
                    //filter the dynamic page list by the parent field...
                    AgilityDynamicSiteMapNode dpNode = anode.ParentNode as AgilityDynamicSiteMapNode;

                    if (dpNode == null)
                    {
                        AgilitySiteMapNode pnode = parentNode;

                        while (pnode != null)
                        {
                            dpNode = pnode.ParentNode as AgilityDynamicSiteMapNode;
                            if (dpNode != null)
                            {
                                break;
                            }
                            pnode = pnode.ParentNode;
                        }
                    }

                    parentContentID = -1;
                    if (!string.IsNullOrEmpty(dynamicPageParentFieldName) && dpNode != null && !string.IsNullOrEmpty(dpNode.ReferenceName))
                    {
                        parentContentID = dpNode.ContentID;
                    }
                }


                if (!string.IsNullOrEmpty(contentReferenceName))
                {
                    //add this page and reference name to the page/dynamic content index
                    BaseCache.UpdateDynamicPageIndex(page.ID, contentReferenceName);

                    //get the content first
                    DataView dv = Data.GetContentView(contentReferenceName);

                    //get the dynamic page formula index
                    Dictionary <string, DynamicPageFormulaItem> dpIndex = BaseCache.GetDynamicPageFormulaIndex(page.ID, contentReferenceName, AgilityContext.LanguageCode, page.ServerPage, true);

                    if (dpIndex != null && dv != null)
                    {
                        //make an ID based index
                        Dictionary <int, DynamicPageFormulaItem> idIndex = new Dictionary <int, DynamicPageFormulaItem>();


                        foreach (var item in dpIndex.Values)
                        {
                            idIndex[item.ContentID] = item;
                        }


                        //loop all of the dynamic pages....
                        foreach (DataRowView dvr in dv)
                        {
                            DynamicPageFormulaItem item = null;

                            int contentID = -1;
                            if (!int.TryParse($"{dvr["ContentID"]}", out contentID))
                            {
                                contentID = -1;
                            }

                            if (!idIndex.TryGetValue(contentID, out item))
                            {
                                continue;
                            }

                            if (parentContentID != 0)
                            {
                                //do a lookup to ensure the parent id condition is met if necessary

                                object testParentContentIDObj = item.GetContentItemValue(dynamicPageParentFieldName);
                                if (testParentContentIDObj != null)
                                {
                                    int    testParentContentID    = -1;
                                    string testParentContentIDStr = string.Format("{0}", testParentContentIDObj);

                                    if (int.TryParse(testParentContentIDStr, out testParentContentID))
                                    {
                                        //if the value is an int, test for equality...
                                        if (parentContentID != testParentContentID)
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        //value is NOT an int, test for "in" '[id],' or ',[id],' or ',[id]'
                                        if (!testParentContentIDStr.StartsWith(string.Format("{0},", parentContentID)) &&
                                            !testParentContentIDStr.EndsWith(string.Format(",{0}", parentContentID)) &&
                                            !testParentContentIDStr.Contains(string.Format(",{0},", parentContentID)))
                                        {
                                            continue;
                                        }
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }

                            AgilityDynamicSiteMapNode thisDNode = AgilityDynamicSiteMapNode.GetDynamicNode(anode, item, page);

                            collection.Add(thisDNode);
                        }
                    }
                }
            }

            return(collection);
        }