/// <summary>
 ///  events - fired before and after a post is created
 /// </summary>
 protected void PostSavedEvent(IContent item, ForumsEventArgs e)
 {
     if (OnPostSaved != null)
     {
         OnPostSaved(item, e);
     }
 }
        protected bool NewPostEvent(ForumsPostModel model)
        {
            var e = new ForumsEventArgs();

            if (OnNewPost != null)
            {
                OnNewPost(model, e);
            }

            return(!e.Cancel);
        }
Example #3
0
        private void ForumsController_OnPostSaved(IContent sender, ForumsEventArgs e)
        {
            // we get passed IContent - but as we are going to go get loads of
            // other content, we will be quicker using IPublishedContent
            using (var cref = _context.EnsureUmbracoContext())
            {
                var cache = cref.UmbracoContext.Content;

                var post = cache.GetById(sender.Id);
                if (post == null)
                {
                    return;
                }

                // for us - the current user will have been the author
                var author = post.Value("PostAuthor");

                // work out the root of this post (top of the thread)
                var postRoot = post;
                var parent   = _ContentService.GetParent(sender);
                if (parent.ContentType.Alias == "forumPost")
                {
                    // if we have a parent post, then this is a reply
                    postRoot = post.Parent;
                }
                _Logger.Info <ForumNotificationMgr>("Sending Notification for new post for {0}", postRoot.Name);

                List <string> receipients = GetRecipients(postRoot);

                // remove the author from the list.
                var postAuthor = GetAuthorEmail(post);
                if (receipients.Contains(postAuthor))
                {
                    receipients.Remove(postAuthor);
                }

                if (receipients.Any())
                {
                    SendNotificationEmail(postRoot, post, author, receipients, e.NewPost);
                }
            }
        }
        public ActionResult PostReply([Bind(Prefix = "Post")] ForumsPostModel model)
        {
            IEnumerable <ILanguage> languages = Services.LocalizationService.GetAllLanguages();

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("Reply", "Error posting (invalid model)");
                return(CurrentUmbracoPage());
            }

            if (!CanPost(model))
            {
                ModelState.AddModelError("Reply", "You do not have permissions to post here");
                return(CurrentUmbracoPage());
            }

            // fire the pre save event.
            // here you could put in things like spam protection.
            // new PostEvent returns false if one ofthe delegated events sets cancel = true;
            if (!NewPostEvent(model))
            {
                ModelState.AddModelError("Reply", "Error posting (blocked)");
                return(CurrentUmbracoPage());
            }

            var posttype = model.IsTopic ? "topic" : "reply";

            var _contentService = Services.ContentService;

            var postName =
                $"reply_{DateTime.UtcNow:yyyyMMddhhmmss}";

            if (!string.IsNullOrWhiteSpace(model.Title))
            {
                postName = model.Title;
            }


            var  parent  = _contentService.GetById(model.ParentId);
            bool newPost = false;

            if (parent != null)
            {
                IContent post = null;
                if (model.Id > 0)
                {
                    post = _contentService.GetById(model.Id);
                }

                if (post == null)
                {
                    post = _contentService.Create(postName, parent, "forumPost");

                    if (post.AvailableCultures.Any())
                    {
                        foreach (var language in languages)
                        {
                            post.SetCultureName(postName, language.IsoCode);
                        }
                    }

                    newPost = true;
                }

                // unlikely but possible we still don't have a node.
                if (post != null)
                {
                    post.SetValue("postTitle", model.Title);
                    post.SetValue("postBody", model.Body);

                    var author = Members.GetById(model.AuthorId);
                    if (author != null)
                    {
                        post.SetValue("postCreator", author.Name);
                        post.SetValue("postAuthor", author.Id);
                    }

                    if (parent.ContentType.Alias != "Forum")
                    {
                        // posts that are in a forum, are allowed replies
                        // thats how the threads work.
                        post.SetValue("allowReplies", true);
                    }

                    post.SetValue("postType", model.IsTopic);
                    if (model.IsTopic)
                    {
                        post.SetValue("intPageSize", parent.GetValue <int>("intPageSize"));
                    }

                    if (!newPost)
                    {
                        post.SetValue("editDate", DateTime.UtcNow);
                    }
                    _contentService.SaveAndPublish(post);

                    // notifications - handled by events
                    // you can write your own handler here,
                    // to be notified when any posts are made
                    ForumsEventArgs e = new ForumsEventArgs {
                        NewPost = newPost
                    };
                    PostSavedEvent(post, e);

                    return(RedirectToCurrentUmbracoPage());
                }
            }
            ModelState.AddModelError("Post", "Error creating the post");
            return(RedirectToCurrentUmbracoPage());
        }