Example #1
0
        public ActionResult CreateSubCategory(ForumSubCategory model)
        {
            if (_mu == null) return new EmptyResult();

            using (var context = new DasKlubDbContext())
            {
                if (model == null) return new EmptyResult();

                ForumCategory forum = context.ForumCategory.First(x => x.Key == model.Key);

                ViewBag.Forum = forum;

                if (!ModelState.IsValid)
                {
                    return View(model);
                }

                if (forum.ThreadsRequirePermission && ((_ua == null) || !_ua.IsAdmin))
                {
                    return new EmptyResult();
                }

                model.LastActiveDateTimeUtc = DateTime.UtcNow; // nulls are allowed, change when modifying
                model.LastActiveUserId = _ua.UserAccountID;
                model.ForumCategoryID = forum.ForumCategoryID;
                model.CreatedByUserID = _ua.UserAccountID;
                model.Key = FromString.UrlKey(model.Title);
                model.Title = model.Title.Trim();
                model.Description = model.Description.Trim();
                model.PostCount += 1;

                var existingThreadInForum = context.ForumSubCategory.FirstOrDefault(
                    forumSubCategory =>
                        forumSubCategory.ForumCategoryID == model.ForumCategoryID &&
                        forumSubCategory.Key             == model.Key);

                if (existingThreadInForum != null)
                {
                    // TODO: LOCALIZE
                    existingThreadInForum.ForumCategory = forum;

                    ModelState.AddModelError("Title",
                        string.Format(@"The title '{0}' is already taken, change your title.",
                        model.Title));

                    return View(model);
                }

                context.ForumSubCategory.Add(model);

                //update the forum category
                forum.PostCount += 1;
                forum.ThreadCount += 1;

                context.Entry(forum).State = EntityState.Modified;

                var notification = new ForumPostNotification
                {
                    CreatedByUserID = Convert.ToInt32(_mu.ProviderUserKey),
                    IsRead = true,
                    UserAccountID = Convert.ToInt32(_mu.ProviderUserKey),
                    ForumSubCategoryID = model.ForumSubCategoryID
                };

                context.ForumPostNotification.Add(notification);

                context.SaveChanges();

                // update to the last active
                forum.LastActiveSubCategoryId = model.ForumSubCategoryID;
                context.Entry(forum).State = EntityState.Modified;
                model.LastActiveDateTimeUtc = model.CreateDate;
                context.Entry(model);

                context.SaveChanges();

                return RedirectToAction("ForumPost", "Forum", new {subkey = model.Key, key = forum.Key});
            }
        }
Example #2
0
        public ActionResult CreateForumPost(ForumPost model, int forumSubCategoryID)
        {
            using (var context = new DasKlubDbContext())
            {
                string currentLang = Utilities.GetCurrentLanguageCode();

                ForumSubCategory subForum = context.ForumSubCategory
                    .First(x => x.ForumSubCategoryID == forumSubCategoryID);

                if (!ModelState.IsValid)
                {
                    ViewBag.SubForum = subForum;

                    return View(model);
                }

                var ua = new UserAccount(Convert.ToInt32(_mu.ProviderUserKey));

                model.ForumSubCategoryID = forumSubCategoryID;
                model.CreatedByUserID = ua.UserAccountID;
                context.ForumPost.Add(model);

                ForumPostNotification currentUserNotification = context.ForumPostNotification
                    .FirstOrDefault(
                        x => x.ForumSubCategoryID == forumSubCategoryID && x.UserAccountID == ua.UserAccountID);

                if (currentUserNotification == null || currentUserNotification.ForumPostNotificationID == 0)
                {
                    var notification = new ForumPostNotification
                    {
                        CreatedByUserID = Convert.ToInt32(_mu.ProviderUserKey),
                        IsRead = true,
                        UserAccountID = Convert.ToInt32(_mu.ProviderUserKey),
                        ForumSubCategoryID = forumSubCategoryID
                    };

                    context.ForumPostNotification.Add(notification);
                }

                List<ForumPostNotification> allUserNotifications =
                    context.ForumPostNotification.Where(x => x.ForumSubCategoryID == forumSubCategoryID).ToList();

                subForum.ForumCategory = context.ForumCategory.First(x => x.ForumCategoryID == subForum.ForumCategoryID);

                if (context.ForumPost.FirstOrDefault(
                    x =>
                        x.ForumSubCategoryID == forumSubCategoryID && x.Detail == model.Detail &&
                        x.CreatedByUserID == ua.UserAccountID) != null) return new EmptyResult();

                Thread.CurrentThread.CurrentUICulture =
                    CultureInfo.CreateSpecificCulture(SiteEnums.SiteLanguages.EN.ToString());

                Thread.CurrentThread.CurrentCulture =
                    CultureInfo.CreateSpecificCulture(SiteEnums.SiteLanguages.EN.ToString());

                foreach (var forumPostNotification in
                        allUserNotifications.Where(
                            forumPostNotification => forumPostNotification.UserAccountID != ua.UserAccountID))
                {
                    forumPostNotification.IsRead = false;
                    forumPostNotification.UpdatedByUserID = Convert.ToInt32(_mu.ProviderUserKey);
                    context.Entry(forumPostNotification).State = EntityState.Modified;

                    var notifiedUser = new UserAccount(forumPostNotification.UserAccountID);
                    var notifiedUserDetails = new UserAccountDetail();
                    notifiedUserDetails.GetUserAccountDeailForUser(forumPostNotification.UserAccountID);

                    if (!notifiedUserDetails.EmailMessages) continue;

                    string title = ua.UserName + " => " + subForum.Title;
                    var body = new StringBuilder(100);
                    body.Append(Messages.New);
                    body.Append(": ");
                    body.Append(subForum.SubForumURL);
                    body.AppendLine();
                    body.AppendLine();
                    body.Append(model.Detail);
                    body.AppendLine();
                    body.AppendLine();
                    body.Append(Messages.Reply);
                    body.Append(": ");
                    body.AppendFormat("{0}/create", subForum.SubForumURL);

                    try
                    {
                        _mail.SendMail(AmazonCloudConfigs.SendFromEmail, notifiedUser.EMail, title, body.ToString());
                    }
                    catch (Exception ex)
                    {
                        Utilities.LogError(ex);
                    }
                }

                var forum = context.ForumCategory.First(x => x.ForumCategoryID == subForum.ForumCategoryID);

                forum.PostCount += 1;
                forum.LastActiveSubCategoryId = forumSubCategoryID;
                context.Entry(forum).State = EntityState.Modified;

                try
                {
                    context.SaveChanges();

                    subForum.PostCount += 1;
                    subForum.LastActiveDateTimeUtc = model.CreateDate;
                    subForum.LastActiveForumPostId = model.ForumPostID;
                    subForum.LastActiveUserId = model.CreatedByUserID;
                    context.Entry(subForum).State = EntityState.Modified;

                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Utilities.LogError(ex);
                }

                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(currentLang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(currentLang);

                Response.Redirect(subForum.SubForumURL.ToString());

                return new EmptyResult();
            }
        }