예제 #1
0
        protected override TreeNode <MenuItem> Build()
        {
            var entities = _menuStorage.GetMenuItems(Name, Services.StoreContext.CurrentStore.Id);
            var tree     = entities.GetTree("DatabaseMenu", _menuItemProviders);

            return(tree);
        }
예제 #2
0
        public ActionResult MoveItem(int menuId, int sourceId, string direction)
        {
            if (menuId == 0 || sourceId == 0 || direction.IsEmpty())
            {
                return(new EmptyResult());
            }

            if (!Services.Permissions.Authorize(StandardPermissionProvider.ManageMenus))
            {
                NotifyAccessDenied();
                return(new EmptyResult());
            }

            using (var scope = new DbContextScope(ctx: Services.DbContext, autoCommit: false))
            {
                var allItems   = _menuStorage.GetMenuItems(menuId, 0, true).ToDictionary(x => x.Id, x => x);
                var sourceItem = allItems[sourceId];

                var siblings = allItems.Select(x => x.Value)
                               .Where(x => x.ParentItemId == sourceItem.ParentItemId)
                               .OrderBy(x => x.DisplayOrder)
                               .ToList();

                var index = siblings.IndexOf(sourceItem) + (direction == "up" ? -1 : 1);
                if (index >= 0 && index < siblings.Count)
                {
                    var targetItem = siblings[index];

                    // Ensure unique display order starting from 1.
                    var count = 0;
                    siblings.Each(x => x.DisplayOrder = ++count);

                    // Swap display order of source and target item.
                    var tmp = sourceItem.DisplayOrder;
                    sourceItem.DisplayOrder = targetItem.DisplayOrder;
                    targetItem.DisplayOrder = tmp;

                    scope.Commit();
                }
            }

            return(RedirectToAction("ItemList", new { id = menuId }));
        }