public override ActionResult Index(RenderModel model)
        {
            using (UnitOfWorkManager.NewUnitOfWork())
            {
                // Get the category
                var category = CategoryMapper.MapCategory(model.Content, true);

                // Set the page index
                var pageIndex = AppHelpers.ReturnCurrentPagingNo();

                // check the user has permission to this category
                var permissions = PermissionService.GetPermissions(category, _usersRole, MemberService, CategoryPermissionService);

                if (!permissions[AppConstants.PermissionDenyAccess].IsTicked)
                {
                    var topics = TopicService.GetPagedTopicsByCategory(pageIndex,
                                                                       Settings.TopicsPerPage,
                                                                       int.MaxValue, category.Id);

                    var isSubscribed = UserIsAuthenticated && (CategoryNotificationService.GetByUserAndCategory(CurrentMember, category).Any());

                    // Create the main view model for the category
                    var viewModel = new ViewCategoryViewModel(model.Content)
                    {
                        Permissions  = permissions,
                        Topics       = topics,
                        Category     = category,
                        PageIndex    = pageIndex,
                        TotalCount   = topics.TotalCount,
                        User         = CurrentMember,
                        IsSubscribed = isSubscribed
                    };

                    // If there are subcategories then add then with their permissions
                    if (category.SubCategories.Any())
                    {
                        var subCatViewModel = new CategoryListViewModel
                        {
                            AllPermissionSets = new Dictionary <Category, PermissionSet>()
                        };
                        foreach (var subCategory in category.SubCategories)
                        {
                            var permissionSet = PermissionService.GetPermissions(subCategory, _usersRole, MemberService, CategoryPermissionService);
                            subCatViewModel.AllPermissionSets.Add(subCategory, permissionSet);
                        }
                        viewModel.SubCategories = subCatViewModel;
                    }

                    return(View(PathHelper.GetThemeViewPath("Category"), viewModel));
                }

                return(ErrorToHomePage(Lang("Errors.NoPermission")));
            }
        }
        private static void MemberServiceOnDeleting(IMemberService sender, DeleteEventArgs <IMember> deleteEventArgs)
        {
            var memberService     = new Services.MemberService();
            var unitOfWorkManager = new UnitOfWorkManager(ContextPerRequest.Db);

            var uploadedFileService         = new UploadedFileService();
            var postService                 = new PostService();
            var memberPointsService         = new MemberPointsService();
            var pollService                 = new PollService();
            var topicService                = new TopicService();
            var topicNotificationService    = new TopicNotificationService();
            var activityService             = new ActivityService();
            var privateMessageService       = new PrivateMessageService();
            var badgeService                = new BadgeService();
            var voteService                 = new VoteService();
            var categoryNotificationService = new CategoryNotificationService();

            using (var unitOfWork = unitOfWorkManager.NewUnitOfWork())
            {
                try
                {
                    foreach (var member in deleteEventArgs.DeletedEntities)
                    {
                        var canDelete = memberService.DeleteAllAssociatedMemberInfo(member.Id, unitOfWork, uploadedFileService, postService, memberPointsService, pollService, topicService,
                                                                                    topicNotificationService, activityService, privateMessageService, badgeService, voteService, categoryNotificationService);
                        if (!canDelete)
                        {
                            deleteEventArgs.Cancel = true;
                            //TODO - THIS DOESN'T WORK - JUST LOG IT
                            //var clientTool = new ClientTools((Page)HttpContext.Current.CurrentHandler);
                            //clientTool.ShowSpeechBubble(SpeechBubbleIcon.Error, "Error", "Unable to delete member. Check logfile for further information");
                            AppHelpers.LogError($"There was an error attemping to delete member {member.Name} and all of their associated data (Posts, Topics etc...)");

                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    AppHelpers.LogError("Error attempting to delete members", ex);
                }
            }
        }
        private void NotifyNewTopics(Category cat)
        {
            // *CHANGE THIS TO BE CALLED LIKE THE BADGES VIA AN AJAX Method*
            // TODO: This really needs to be an async call so it doesn't hang when a user creates
            //  a topic if there are 1000's of users

            // Get all notifications for this category
            var notifications = CategoryNotificationService.GetByCategory(cat).Select(x => x.MemberId).ToList();

            if (notifications.Any())
            {
                // remove the current user from the notification, don't want to notify yourself that you
                // have just made a topic!
                notifications.Remove(CurrentMember.Id);

                if (notifications.Count > 0)
                {
                    // Now get all the users that need notifying
                    var usersToNotify = MemberService.GetUsersById(notifications);

                    // Create the email
                    var sb = new StringBuilder();
                    sb.AppendFormat("<p>{0}</p>", string.Format(Lang("Topic.Notification.NewTopics"), cat.Name));
                    sb.AppendFormat("<p>{0}</p>", string.Concat(Settings.ForumRootUrlWithDomain, cat.Url));

                    // create the emails and only send them to people who have not had notifications disabled
                    var emails = usersToNotify.Where(x => x.DisableEmailNotifications != true).Select(user => new Email
                    {
                        Body      = EmailService.EmailTemplate(user.UserName, sb.ToString()),
                        EmailFrom = Settings.NotificationReplyEmailAddress,
                        EmailTo   = user.Email,
                        NameTo    = user.UserName,
                        Subject   = string.Concat(Lang("Topic.Notification.Subject"), Settings.ForumName)
                    }).ToList();

                    // and now pass the emails in to be sent
                    EmailService.SendMail(emails);
                }
            }
        }
        public void UnSubscribe(UnSubscribeEmailViewModel subscription)
        {
            if (Request.IsAjaxRequest())
            {
                using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        // Add logic to add subscr
                        var isCategory = subscription.SubscriptionType.Contains("category");
                        var id         = subscription.Id;

                        if (isCategory)
                        {
                            // get the category
                            var cat = CategoryService.Get(Convert.ToInt32(id));

                            if (cat != null)
                            {
                                // get the notifications by user
                                var notifications = CategoryNotificationService.GetByUserAndCategory(CurrentMember, cat);

                                if (notifications.Any())
                                {
                                    foreach (var categoryNotification in notifications)
                                    {
                                        // Delete
                                        CategoryNotificationService.Delete(categoryNotification);
                                    }
                                }
                            }
                        }
                        else
                        {
                            // get the topic
                            var topic = TopicService.Get(new Guid(id));

                            if (topic != null)
                            {
                                // get the notifications by user
                                var notifications = TopicNotificationService.GetByUserAndTopic(CurrentMember, topic);

                                if (notifications.Any())
                                {
                                    foreach (var topicNotification in notifications)
                                    {
                                        // Delete
                                        TopicNotificationService.Delete(topicNotification);
                                    }
                                }
                            }
                        }

                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LogError(ex);
                        throw new Exception(Lang("Errors.GenericMessage"));
                    }
                }
            }
            else
            {
                throw new Exception(Lang("Errors.GenericMessage"));
            }
        }
        public void Subscribe(SubscribeEmailViewModel subscription)
        {
            if (Request.IsAjaxRequest())
            {
                using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        // Add logic to add subscr
                        var isCategory = subscription.SubscriptionType.Contains("category");
                        var id         = subscription.Id;

                        if (isCategory)
                        {
                            // get the category
                            var cat = CategoryService.Get(Convert.ToInt32(id));

                            if (cat != null)
                            {
                                // Create the notification
                                var categoryNotification = new CategoryNotification
                                {
                                    Category   = cat,
                                    CategoryId = cat.Id,
                                    Member     = CurrentMember,
                                    MemberId   = CurrentMember.Id
                                };
                                //save

                                CategoryNotificationService.Add(categoryNotification);
                            }
                        }
                        else
                        {
                            // get the category
                            var topic = TopicService.Get(new Guid(id));

                            // check its not null
                            if (topic != null)
                            {
                                // Create the notification
                                var topicNotification = new TopicNotification
                                {
                                    Topic    = topic,
                                    Member   = CurrentMember,
                                    MemberId = CurrentMember.Id
                                };
                                TopicNotificationService.Add(topicNotification);
                            }
                        }

                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LogError(ex);
                        throw new Exception(Lang("Errors.GenericMessage"));
                    }
                }
            }
            else
            {
                throw new Exception(Lang("Errors.GenericMessage"));
            }
        }