public void AddActionToMenu(PluginPart <IAdditionalAction> action)
        {
            var item =
                new PluginMenuItemModel(
                    action.Item.GetActionTitle(),
                    PluginItemCommand,
                    action: action
                    );

            PluginMenuItems.Add(item);
        }
        private void AddToDatabase(PluginMenuItemModel pluginMenuItem, int?parentId, int currentPosition)
        {
            if (pluginMenuItem.Type == MenuItemTypeEnum.Link)
            {
                var newMenuItem = new MenuItem()
                {
                    E2EId          = pluginMenuItem.E2EId,
                    Name           = pluginMenuItem.Name,
                    Link           = pluginMenuItem.Link,
                    Type           = pluginMenuItem.Type,
                    Position       = currentPosition,
                    MenuTemplateId = _dbContext.MenuTemplates.First(x => x.E2EId == pluginMenuItem.E2EId).Id,
                    ParentId       = parentId,
                };

                _dbContext.MenuItems.Add(newMenuItem);
                _dbContext.SaveChanges();

                foreach (var translation in pluginMenuItem.Translations.Select(menuItemTranslation => new MenuItemTranslation
                {
                    Language = menuItemTranslation.Language,
                    LocaleName = menuItemTranslation.LocaleName,
                    Name = menuItemTranslation.Name,
                    MenuItemId = newMenuItem.Id,
                }))
                {
                    _dbContext.MenuItemTranslations.Add(translation);
                    _dbContext.SaveChanges();
                }
            }
            else
            {
                var newMenuItem = new MenuItem()
                {
                    E2EId          = pluginMenuItem.E2EId,
                    Name           = pluginMenuItem.Type == MenuItemTypeEnum.Dropdown ? "Dropdown" : pluginMenuItem.Name,
                    Link           = pluginMenuItem.Link,
                    Type           = pluginMenuItem.Type,
                    Position       = currentPosition,
                    MenuTemplateId = null,
                    ParentId       = parentId
                };

                _dbContext.MenuItems.Add(newMenuItem);
                _dbContext.SaveChanges();

                foreach (var translation in pluginMenuItem.Translations.Select(menuItemTranslation => new MenuItemTranslation
                {
                    Language = menuItemTranslation.Language,
                    LocaleName = menuItemTranslation.LocaleName,
                    Name = menuItemTranslation.Name,
                    MenuItemId = newMenuItem.Id,
                }))
                {
                    _dbContext.MenuItemTranslations.Add(translation);
                    _dbContext.SaveChanges();
                }

                if (pluginMenuItem.ChildItems.Any())
                {
                    var childPosition = 0;
                    foreach (var childMenuItem in pluginMenuItem.ChildItems)
                    {
                        AddToDatabase(childMenuItem, newMenuItem.Id, childPosition);
                        childPosition++;
                    }
                }
            }
        }
        public override void Load(PluginMenuItemModel MenuItem, string pluginId, int? parentId)
        {
            int currentPosition = 0;

            if(MenuItem.Type == MenuItemTypeEnum.Dropdown)
            {
                currentPosition = _dbContext.MenuItems
                                    .Where(x => x.ParentId == null)
                                    .Max(x => x.Position) + MenuItem.Position + 1;
            }
            else
            {
                currentPosition = parentId != null
                                        ? MenuItem.Position
                                        : _dbContext.MenuItems
                                            .Where(x => x.ParentId == null)
                                            .Max(x => x.Position) + MenuItem.Position + 1;
            }

            var newMenuItem = new MenuItem()
            {
                E2EId = MenuItem.E2EId,
                Name = MenuItem.Type == MenuItemTypeEnum.Dropdown ? "Dropdown" : MenuItem.Name,
                Link = MenuItem.Link,
                Type = MenuItem.Type,
                Position = currentPosition,
                MenuTemplateId = null,
                ParentId = parentId
            };

            _dbContext.MenuItems.Add(newMenuItem);
            _dbContext.SaveChanges();

            foreach (var menuItemTranslation in MenuItem.Translations)
            {
                var translation = new MenuItemTranslation
                {
                    Language = menuItemTranslation.Language,
                    LocaleName = menuItemTranslation.LocaleName,
                    Name = menuItemTranslation.Name,
                    MenuItemId = newMenuItem.Id,
                };

                _dbContext.MenuItemTranslations.Add(translation);
                _dbContext.SaveChanges();
            }

            if (MenuItem.ChildItems.Any())
            {
                foreach (var menuItemChild in MenuItem.ChildItems)
                {
                    var loaders = new List<AbstractLoader>()
                    {
                        new SimpleLinkLoader(_dbContext),
                        new IsNotSimpleLinkLoader(_dbContext)
                    };

                    foreach(var loader in loaders)
                    {
                        if(loader.IsExecute(menuItemChild))
                        {
                            loader.Load(menuItemChild, pluginId, newMenuItem.Id);
                        }
                    }
                }
            }
        }
 public override bool IsExecute(PluginMenuItemModel menuItem)
    => menuItem.Type != MenuItemTypeEnum.Link;
 public abstract void Load(PluginMenuItemModel menuItem, string pluginId, int?parentId);
 public abstract bool IsExecute(PluginMenuItemModel menuItem);
Ejemplo n.º 7
0
        public override void Load(PluginMenuItemModel MenuItem, string pluginId, int?parentId)
        {
            var menuTemplate = new MenuTemplate()
            {
                Name          = MenuItem.MenuTemplate.Name,
                E2EId         = MenuItem.MenuTemplate.E2EId,
                DefaultLink   = MenuItem.MenuTemplate.DefaultLink,
                EformPluginId = _dbContext.EformPlugins.Single(x => x.PluginId == pluginId).Id
            };

            _dbContext.MenuTemplates.Add(menuTemplate);
            _dbContext.SaveChanges();

            foreach (var translation in MenuItem.MenuTemplate.Translations)
            {
                var menuTemplateTranslation = new MenuTemplateTranslation
                {
                    Language       = translation.Language,
                    LocaleName     = translation.LocaleName,
                    Name           = translation.Name,
                    MenuTemplateId = menuTemplate.Id,
                };

                _dbContext.MenuTemplateTranslations.Add(menuTemplateTranslation);
                _dbContext.SaveChanges();
            }

            if (MenuItem.MenuTemplate.Permissions.Any())
            {
                foreach (var itemPermission in MenuItem.MenuTemplate.Permissions)
                {
                    PermissionType newPermissionType = null;

                    var permissionType = _dbContext.PermissionTypes.FirstOrDefault(x => x.Name == itemPermission.PermissionTypeName);

                    if (permissionType == null)
                    {
                        newPermissionType = new PermissionType
                        {
                            Name = itemPermission.PermissionTypeName,
                        };

                        _dbContext.PermissionTypes.Add(newPermissionType);
                        _dbContext.SaveChanges();
                    }

                    var permission = new Permission
                    {
                        PermissionName   = itemPermission.PermissionName,
                        ClaimName        = itemPermission.ClaimName,
                        PermissionTypeId = newPermissionType == null
                            ? permissionType.Id
                            : newPermissionType.Id
                    };

                    _dbContext.Permissions.Add(permission);
                    _dbContext.SaveChanges();

                    var menuTemplatePermission = new MenuTemplatePermission
                    {
                        MenuTemplateId = menuTemplate.Id,
                        PermissionId   = permission.Id,
                    };

                    _dbContext.MenuTemplatePermissions.Add(menuTemplatePermission);
                    _dbContext.SaveChanges();
                }
            }

            var newMenuItem = new MenuItem()
            {
                E2EId    = MenuItem.E2EId,
                Name     = MenuItem.Name,
                Link     = MenuItem.Link,
                Type     = MenuItem.Type,
                Position = parentId != null
                    ? MenuItem.Position
                    : _dbContext.MenuItems.Where(x => x.ParentId == null).Max(x => x.Position) + MenuItem.Position + 1,
                MenuTemplateId = menuTemplate.Id,
                ParentId       = parentId,
            };

            _dbContext.MenuItems.Add(newMenuItem);
            _dbContext.SaveChanges();

            foreach (var menuItemTranslation in MenuItem.Translations)
            {
                var translation = new MenuItemTranslation
                {
                    Language   = menuItemTranslation.Language,
                    LocaleName = menuItemTranslation.LocaleName,
                    Name       = menuItemTranslation.Name,
                    MenuItemId = newMenuItem.Id,
                };

                _dbContext.MenuItemTranslations.Add(translation);
                _dbContext.SaveChanges();
            }
        }