Пример #1
0
        public GetSiteMenuActiveResponse GetSiteMenuActive(GetSiteMenuActiveRequest request)
        {
            var requestUrl = request.Url.Split('/').Where(x => x != "").ToArray();
            string requestController = string.Empty;
            string requestAction = string.Empty;
            string requestId = string.Empty;
            if (requestUrl.Length > 0)
            {
                string ctrl = string.Empty;
                if (requestUrl[0].Contains("?"))
                {
                    var ctrls = requestUrl[0].Split('?');
                    if (ctrls.Length > 0)
                    {
                        ctrl = ctrls[0];
                    }
                }

                requestController = string.IsNullOrEmpty(ctrl) ? requestUrl[0] : ctrl;
            }

            if (requestUrl.Length > 1)
            {
                requestAction = requestUrl[1];
            }

            if (requestUrl.Length > 2)
            {
                requestId = requestUrl[2];
            }

            IDictionary<int, int> dictionary = new Dictionary<int, int>();

            var menus =
                DataContext.Menus
                .Include(x => x.Parent)
                .Where(x => x.Url.Contains(requestController)).ToList();

            foreach (var menu in menus)
            {
                var urlMenu = menu.Url.Split('/').Where(x => x != "").ToArray();
                string controller = string.Empty;
                string action = string.Empty;
                string id = string.Empty;
                int priority = 0;
                if (urlMenu.Length > 0)
                {
                    controller = urlMenu[0];
                }

                if (urlMenu.Length > 1)
                {
                    action = urlMenu[1];
                }

                if (urlMenu.Length > 2)
                {
                    id = urlMenu[2];
                }

                if (urlMenu.Length == requestUrl.Length)
                {
                    priority += 1;
                }

                if (controller == requestController)
                {
                    priority += 1;
                }

                if (action == requestAction)
                {
                    priority += 1;
                }

                if (id == requestId)
                {
                    priority += 1;
                }

                dictionary.Add(menu.Id, priority);
            }

            var newMenu = new Data.Entities.Menu();
            var sortedDictionary = from x in dictionary orderby x.Value descending select x;
            if (sortedDictionary.Any())
            {
                newMenu = menus.Single(x => x.Id == sortedDictionary.ElementAt(0).Key);
            }

            var rootMenu = GetRootMenu2(newMenu);
            var response = rootMenu.MapTo<GetSiteMenuActiveResponse>();
            response.SelectedMenu = newMenu.MapTo<Data.Entities.Menu>();
            var history = new List<int>();
            history.Add(newMenu.Id);
            response.HistoryMenu = GetHistoryMenu(newMenu, history);
            return response;
        }
Пример #2
0
        public GetSiteMenuActiveResponse GetSiteMenuActive(GetSiteMenuActiveRequest request)
        {
            var response = new GetSiteMenuActiveResponse();
            //get the menu from url request
            var url_request = new StringBuilder("/").Append(request.Controller).ToString();
            if (!request.Action.ToLower().Equals("index"))
            {
                url_request = new StringBuilder(url_request).Append("/").Append(request.Action).ToString();
            }
            var url_controller = new StringBuilder("/").Append(request.Controller).ToString();

            try
            {
                //var menu = DataContext.Menus.Where(x => x.Url == request.Url).First();
                //var menu = DataContext.Menus.Where(x => x.Url.ToLower() == url_request).First();
                var menus = DataContext.Menus.Where(x => x.Url == url_request || x.Url.Contains(url_controller) || x.Url.Contains(url_request)).OrderBy(y => y.Id).ToList();
                url_request = this._CleanUpMenuUrl(url_request);
                if (menus.Count == 1)
                {
                    var menu = menus[0];
                    var RootMenu = this._GetActiveMenu(menu);
                    response = RootMenu.MapTo<GetSiteMenuActiveResponse>();
                    response.SelectedMenu = menu.MapTo<Data.Entities.Menu>();
                    response.IsSuccess = true;
                }
                else if (menus.Count > 1)
                {
                    int i = 0;
                    foreach (var menu in menus)
                    {
                        // skip inactive menu
                        //if (menu.IsActive == false) break;
                        string edited_menu_url = this._CleanUpMenuUrl(menu.Url.ToString());
                        if (menu.Url.ToLower() == request.Url.ToLower() || edited_menu_url.Equals(request.Url.ToLower()))
                        {
                            var root = this._GetActiveMenu(menu);
                            if (root.IsRoot)
                            {
                                response = root.MapTo<GetSiteMenuActiveResponse>();
                                response.SelectedMenu = menu.MapTo<Data.Entities.Menu>();
                                response.IsSuccess = true;
                            }
                            break;
                        }
                        else
                        {
                            i++;
                            if (edited_menu_url.Equals(url_request.ToLower()))
                            {
                                var root = this._GetActiveMenu(menu);
                                if (root.IsRoot)
                                {
                                    response = root.MapTo<GetSiteMenuActiveResponse>();
                                    response.SelectedMenu = menu.MapTo<Data.Entities.Menu>();
                                    response.IsSuccess = true;
                                }
                                break;
                            }
                            else
                            {
                                if (i == menus.Count)
                                {
                                    Data.Entities.Menu parent = null;
                                    Data.Entities.Menu root = null;
                                    if (menu.IsActive == false)
                                    {
                                        parent = menu.Parent;
                                        root = this._GetActiveMenu(parent);
                                        response = root.MapTo<GetSiteMenuActiveResponse>();
                                        response.SelectedMenu = parent.MapTo<Data.Entities.Menu>();
                                    }
                                    else {
                                        root = this._GetActiveMenu(menu);
                                        response = root.MapTo<GetSiteMenuActiveResponse>();
                                        response.SelectedMenu = menu.MapTo<Data.Entities.Menu>();
                                    }

                                    response.IsSuccess = true;
                                    break;

                                }
                            }
                        }

                    }
                }
                else
                {
                    var menu = DataContext.Menus.First(m => m.Id == 1);
                    menu = this._GetActiveMenu(menu);
                    response = menu.MapTo<GetSiteMenuActiveResponse>();
                    response.SelectedMenu = menu.MapTo<Data.Entities.Menu>();
                    response.IsSuccess = false;
                }
                //var menu = DataContext.Menus.Where(x => x.Url.ToLower() == url_request || x.Url.ToLower().Contains(url_request)).OrderBy(y=>y.Id).First();
                //menu = this._GetActiveMenu(menu);
                //response = menu.MapTo<GetSiteMenuActiveResponse>();

                return response;
            }
            catch (System.InvalidOperationException x)
            {
                var menu = DataContext.Menus.First(m => m.Id == 1);
                response = menu.MapTo<GetSiteMenuActiveResponse>();

                response.Message = x.Message;
                return response;
            }
        }