Пример #1
0
        public IActionResult Init(string appName  = "", string areaName = "", string nodeName   = "",
                                  string pageName = "", Guid?recordId   = null, Guid?relationId = null, Guid?parentRecordId = null)
        {
            //Stopwatch sw = new Stopwatch();
            //sw.Start();

            if (String.IsNullOrWhiteSpace(appName))
            {
                appName = AppName;
            }
            if (String.IsNullOrWhiteSpace(areaName))
            {
                areaName = AreaName;
            }
            if (String.IsNullOrWhiteSpace(nodeName))
            {
                nodeName = NodeName;
            }
            if (String.IsNullOrWhiteSpace(pageName))
            {
                pageName = PageName;
            }
            if (recordId == null)
            {
                recordId = RecordId;
            }
            if (relationId == null)
            {
                relationId = RelationId;
            }
            if (parentRecordId == null)
            {
                parentRecordId = ParentRecordId;
            }

            var urlInfo = new PageService().GetInfoFromPath(HttpContext.Request.Path);

            if (String.IsNullOrWhiteSpace(appName))
            {
                appName = urlInfo.AppName;
                if (AppName != appName)
                {
                    AppName = appName;                     //When dealing with non standard routing in pages
                }
            }
            if (String.IsNullOrWhiteSpace(areaName))
            {
                areaName = urlInfo.AreaName;
                if (AreaName != areaName)
                {
                    AreaName = areaName;                     //When dealing with non standard routing in pages
                }
            }
            if (String.IsNullOrWhiteSpace(nodeName))
            {
                nodeName = urlInfo.NodeName;
                if (NodeName != nodeName)
                {
                    NodeName = nodeName;                     //When dealing with non standard routing in pages
                }
            }
            if (String.IsNullOrWhiteSpace(pageName))
            {
                pageName = urlInfo.PageName;
                if (PageName != pageName)
                {
                    PageName = pageName;                     //When dealing with non standard routing in pages
                }
            }
            if (recordId == null)
            {
                recordId = urlInfo.RecordId;
                if (RecordId != recordId)
                {
                    RecordId = recordId;                     //When dealing with non standard routing in pages
                }
            }
            if (relationId == null)
            {
                relationId = urlInfo.RelationId;
                if (RelationId != relationId)
                {
                    RelationId = relationId;                     //When dealing with non standard routing in pages
                }
            }
            if (parentRecordId == null)
            {
                parentRecordId = urlInfo.ParentRecordId;
                if (ParentRecordId != parentRecordId)
                {
                    ParentRecordId = parentRecordId;                     //When dealing with non standard routing in pages
                }
            }


            ErpRequestContext.SetCurrentApp(appName, areaName, nodeName);
            ErpRequestContext.SetCurrentPage(PageContext, pageName, appName, areaName, nodeName, recordId, relationId, parentRecordId);

            ErpRequestContext.RecordId       = recordId;
            ErpRequestContext.RelationId     = relationId;
            ErpRequestContext.ParentRecordId = parentRecordId;
            ErpRequestContext.PageContext    = PageContext;

            if (PageContext.HttpContext.Request.Query.ContainsKey("returnUrl"))
            {
                ReturnUrl = HttpUtility.UrlDecode(PageContext.HttpContext.Request.Query["returnUrl"].ToString());
            }
            ErpAppContext = ErpAppContext.Current;
            CurrentUrl    = PageUtils.GetCurrentUrl(PageContext.HttpContext);

            #region << Init Navigation >>
            //Application navigation
            if (ErpRequestContext.App != null)
            {
                var sitemap  = ErpRequestContext.App.Sitemap;
                var appPages = new PageService().GetAppControlledPages(ErpRequestContext.App.Id);
                //Calculate node Urls
                foreach (var area in sitemap.Areas)
                {
                    foreach (var currentNode in area.Nodes)
                    {
                        switch (currentNode.Type)
                        {
                        case SitemapNodeType.ApplicationPage:
                            var nodePages = appPages.FindAll(x => x.NodeId == currentNode.Id).ToList();
                            //Case 1: Node has attached pages
                            if (nodePages.Count > 0)
                            {
                                nodePages       = nodePages.OrderBy(x => x.Weight).ToList();
                                currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/a/{nodePages[0].Name}";
                            }
                            else
                            {
                                var firstAppPage = appPages.FindAll(x => x.Type == PageType.Application).OrderBy(x => x.Weight).FirstOrDefault();
                                if (firstAppPage == null)
                                {
                                    currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/a/";
                                }
                                else
                                {
                                    currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/a/{firstAppPage.Name}";
                                }
                            }
                            break;

                        case SitemapNodeType.EntityList:
                            var firstListPage = appPages.FindAll(x => x.Type == PageType.RecordList && x.EntityId == currentNode.EntityId).OrderBy(x => x.Weight).FirstOrDefault();
                            if (firstListPage == null)
                            {
                                currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/l/";
                            }
                            else
                            {
                                currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/l/{firstListPage.Name}";
                            }
                            break;

                        case SitemapNodeType.Url:
                            //Do nothing
                            break;

                        default:
                            throw new Exception("Type not found");
                        }
                        continue;
                    }
                }
                //Convert to MenuItem
                foreach (var area in sitemap.Areas)
                {
                    if (area.Nodes.Count == 0)
                    {
                        continue;
                    }

                    var areaMenuItem = new MenuItem();
                    if (area.Nodes.Count > 1)
                    {
                        var areaLink = $"<a href=\"javascript: void(0)\" title=\"{area.Label}\" data-navclick-handler>";
                        areaLink    += $"<span class=\"menu-label\">{area.Label}</span>";
                        areaLink    += $"<span class=\"menu-nav-icon fa fa-angle-down nav-caret\"></span>";
                        areaLink    += $"</a>";
                        areaMenuItem = new MenuItem()
                        {
                            Content = areaLink
                        };

                        foreach (var node in area.Nodes)
                        {
                            var nodeLink = $"<a class=\"dropdown-item\" href=\"{node.Url}\" title=\"{node.Label}\"><span class=\"{node.IconClass} icon fa-fw\"></span> {node.Label}</a>";
                            areaMenuItem.Nodes.Add(new MenuItem()
                            {
                                Content = nodeLink
                            });
                        }
                    }
                    else if (area.Nodes.Count == 1)
                    {
                        var areaLink = $"<a href=\"{area.Nodes[0].Url}\" title=\"{area.Label}\">";
                        areaLink    += $"<span class=\"menu-label\">{area.Label}</span>";
                        areaLink    += $"</a>";
                        areaMenuItem = new MenuItem()
                        {
                            Content = areaLink
                        };
                    }

                    if (ErpRequestContext.SitemapArea == null && ErpRequestContext.Page != null && ErpRequestContext.Page.Type != PageType.Application)
                    {
                        return(new NotFoundResult());
                    }

                    if (ErpRequestContext.SitemapArea != null && area.Id == ErpRequestContext.SitemapArea.Id)
                    {
                        areaMenuItem.Class = "current";
                    }

                    //Process the an unusual case when the area has a node type URL which has a link to an app Page or a site page.
                    //Then there is no SitemapArea in the ErpRequest as the URL does not has the information about one but still it needs to be
                    //marked as current
                    if (ErpRequestContext.SitemapArea == null)
                    {
                        var urlNodes = area.Nodes.FindAll(x => x.Type == SitemapNodeType.Url);
                        var path     = HttpContext.Request.Path;
                        foreach (var urlNode in urlNodes)
                        {
                            if (path == urlNode.Url)
                            {
                                areaMenuItem.Class = "current";
                            }
                        }
                    }

                    ApplicationMenu.Add(areaMenuItem);
                }
            }

            //Site menu
            var pageSrv   = new PageService();
            var sitePages = pageSrv.GetSitePages();
            foreach (var sitePage in sitePages)
            {
                if (sitePage.Weight < 1000)
                {
                    SiteMenu.Add(new MenuItem()
                    {
                        Content = $"<a class=\"dropdown-item\" href=\"/s/{sitePage.Name}\">{sitePage.Label}</a>"
                    });
                }
            }


            #endregion


            DataModel = new PageDataModel(this);

            List <Guid> currentUserRoles = new List <Guid>();
            if (CurrentUser != null)
            {
                currentUserRoles.AddRange(CurrentUser.Roles.Select(x => x.Id));
            }

            if (ErpRequestContext.App != null)
            {
                if (ErpRequestContext.App.Access == null || ErpRequestContext.App.Access.Count == 0)
                {
                    new LocalRedirectResult("/error?401");
                }

                IEnumerable <Guid> rolesWithAccess = ErpRequestContext.App.Access.Intersect(currentUserRoles);
                if (!rolesWithAccess.Any())
                {
                    new LocalRedirectResult("/error?401");
                }
            }

            //Debug.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>> Base page init: " + sw.ElapsedMilliseconds);
            return(null);
        }
Пример #2
0
        public void Init(string appName  = "", string areaName = "", string nodeName   = "",
                         string pageName = "", Guid?recordId   = null, Guid?relationId = null, Guid?parentRecordId = null)
        {
            //Stopwatch sw = new Stopwatch();
            //sw.Start();

            if (String.IsNullOrWhiteSpace(appName))
            {
                appName = AppName;
            }
            if (String.IsNullOrWhiteSpace(areaName))
            {
                areaName = AreaName;
            }
            if (String.IsNullOrWhiteSpace(nodeName))
            {
                nodeName = NodeName;
            }
            if (String.IsNullOrWhiteSpace(pageName))
            {
                pageName = PageName;
            }
            if (recordId == null)
            {
                recordId = RecordId;
            }
            if (relationId == null)
            {
                relationId = RelationId;
            }
            if (parentRecordId == null)
            {
                parentRecordId = ParentRecordId;
            }

            var urlInfo = new PageService().GetInfoFromPath(HttpContext.Request.Path);

            if (String.IsNullOrWhiteSpace(appName))
            {
                appName = urlInfo.AppName;
                if (AppName != appName)
                {
                    AppName = appName;                     //When dealing with non standard routing in pages
                }
            }
            if (String.IsNullOrWhiteSpace(areaName))
            {
                areaName = urlInfo.AreaName;
                if (AreaName != areaName)
                {
                    AreaName = areaName;                     //When dealing with non standard routing in pages
                }
            }
            if (String.IsNullOrWhiteSpace(nodeName))
            {
                nodeName = urlInfo.NodeName;
                if (NodeName != nodeName)
                {
                    NodeName = nodeName;                     //When dealing with non standard routing in pages
                }
            }
            if (String.IsNullOrWhiteSpace(pageName))
            {
                pageName = urlInfo.PageName;
                if (PageName != pageName)
                {
                    PageName = pageName;                     //When dealing with non standard routing in pages
                }
            }
            if (recordId == null)
            {
                recordId = urlInfo.RecordId;
                if (RecordId != recordId)
                {
                    RecordId = recordId;                     //When dealing with non standard routing in pages
                }
            }
            if (relationId == null)
            {
                relationId = urlInfo.RelationId;
                if (RelationId != relationId)
                {
                    RelationId = relationId;                     //When dealing with non standard routing in pages
                }
            }
            if (parentRecordId == null)
            {
                parentRecordId = urlInfo.ParentRecordId;
                if (ParentRecordId != parentRecordId)
                {
                    ParentRecordId = parentRecordId;                     //When dealing with non standard routing in pages
                }
            }


            ErpRequestContext.SetCurrentApp(appName, areaName, nodeName);
            ErpRequestContext.SetCurrentPage(PageContext, pageName, appName, areaName, nodeName, recordId, relationId, parentRecordId);

            ErpRequestContext.RecordId       = recordId;
            ErpRequestContext.RelationId     = relationId;
            ErpRequestContext.ParentRecordId = parentRecordId;
            ErpRequestContext.PageContext    = PageContext;

            if (PageContext.HttpContext.Request.Query.ContainsKey("returnUrl"))
            {
                ReturnUrl = HttpUtility.UrlDecode(PageContext.HttpContext.Request.Query["returnUrl"].ToString());
            }
            ErpAppContext = ErpAppContext.Current;
            CurrentUrl    = PageUtils.GetCurrentUrl(PageContext.HttpContext);

            #region << Init Navigation >>
            //Application navigation
            if (ErpRequestContext.App != null)
            {
                var sitemap  = ErpRequestContext.App.Sitemap;
                var appPages = new PageService().GetAppPages(ErpRequestContext.App.Id);
                //Calculate node Urls
                foreach (var area in sitemap.Areas)
                {
                    foreach (var currentNode in area.Nodes)
                    {
                        switch (currentNode.Type)
                        {
                        case SitemapNodeType.ApplicationPage:
                            var nodePages = appPages.FindAll(x => x.NodeId == currentNode.Id).ToList();
                            //Case 1: Node has attached pages
                            if (nodePages.Count > 0)
                            {
                                nodePages       = nodePages.OrderBy(x => x.Weight).ToList();
                                currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/a/{nodePages[0].Name}";
                            }
                            else
                            {
                                var firstAppPage = appPages.FindAll(x => x.Type == PageType.Application).OrderBy(x => x.Weight).FirstOrDefault();
                                if (firstAppPage == null)
                                {
                                    currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/a/";
                                }
                                else
                                {
                                    currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/a/{firstAppPage.Name}";
                                }
                            }
                            break;

                        case SitemapNodeType.EntityList:
                            var firstListPage = appPages.FindAll(x => x.Type == PageType.RecordList && x.EntityId == currentNode.EntityId).OrderBy(x => x.Weight).FirstOrDefault();
                            if (firstListPage == null)
                            {
                                currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/l/";
                            }
                            else
                            {
                                currentNode.Url = $"/{ErpRequestContext.App.Name}/{area.Name}/{currentNode.Name}/l/{firstListPage.Name}";
                            }
                            break;

                        case SitemapNodeType.Url:
                            //Do nothing
                            break;

                        default:
                            throw new Exception("Type not found");
                        }
                        continue;
                    }
                }
                //Convert to MenuItem
                foreach (var area in sitemap.Areas)
                {
                    if (area.Nodes.Count == 0)
                    {
                        continue;
                    }

                    var areaMenuItem = new MenuItem();
                    if (area.Nodes.Count > 1)
                    {
                        var areaLink = $"<a href=\"javascript: void(0)\" title=\"{area.Label}\" data-navclick-handler>";
                        areaLink    += $"<span class=\"menu-label\">{area.Label}</span>";
                        areaLink    += $"<span class=\"menu-nav-icon ti-angle-down nav-caret\"></span>";
                        areaLink    += $"</a>";
                        areaMenuItem = new MenuItem()
                        {
                            Content = areaLink
                        };

                        foreach (var node in area.Nodes)
                        {
                            var nodeLink = $"<a class=\"dropdown-item\" href=\"{node.Url}\" title=\"{node.Label}\"><span class=\"{node.IconClass} icon\"></span> {node.Label}</a>";
                            areaMenuItem.Nodes.Add(new MenuItem()
                            {
                                Content = nodeLink
                            });
                        }
                    }
                    else if (area.Nodes.Count == 1)
                    {
                        var areaLink = $"<a href=\"{area.Nodes[0].Url}\" title=\"{area.Label}\">";
                        areaLink    += $"<span class=\"menu-label\">{area.Label}</span>";
                        areaLink    += $"</a>";
                        areaMenuItem = new MenuItem()
                        {
                            Content = areaLink
                        };
                    }
                    ApplicationMenu.Add(areaMenuItem);
                }
            }

            //Site menu
            var pageSrv   = new PageService();
            var sitePages = pageSrv.GetSitePages();
            foreach (var sitePage in sitePages)
            {
                SiteMenu.Add(new MenuItem()
                {
                    Content = $"<a class=\"dropdown-item\" href=\"/s/{sitePage.Name}\">{sitePage.Label}</a>"
                });
            }


            #endregion


            DataModel = new PageDataModel(this);
            //Debug.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>> Base page init: " + sw.ElapsedMilliseconds);
        }