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

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

            return(!e.Cancel);
        }
    void SimpilyForumsController_OnPostSaved(IContent sender, SimpilyForumsEventArgs e)
    {
        // we get passed IContent - but as we are going to go get loads of
        // other content, we will be quicker using IPublishedContent
        var umbHelper = new UmbracoHelper(UmbracoContext.Current);
        var mbrHelper = new MembershipHelper(UmbracoContext.Current);

        var post = umbHelper.TypedContent(sender.Id);
        if (post == null)
            return;

        // for us - the current user will have been the author
        var author = mbrHelper.GetCurrentMember();

        // work out the root of this post (top of the thread)
        var postRoot = post;
        if (post.Parent.DocumentTypeAlias == "Simpilypost")
        {
            // if we have a parent post, then this is a reply
            postRoot = post.Parent;
        }

        LogHelper.Info<ForumNotificationMgr>("Sending Notification for new post for {0}", () => postRoot.Name);

        List<string> receipients = GetRecipients(postRoot, mbrHelper);

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

        if (receipients.Any())
        {
            SendNotificationEmail(postRoot, post, author, receipients, e.NewPost);
        }
    }
        public ActionResult PostReply([Bind(Prefix = "Reply")] SimpilyForumsPostModel model)
        {
            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 _contentService = ApplicationContext.Services.ContentService;

            var postName =
                string.Format("post_{0}", DateTime.Now.ToString("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.CreateContent(postName, parent, "Simpilypost");
                    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 != "SimpilyForum")
                    {
                        // posts that are in a forum, are allowed replies
                        // thats how the threads work.
                        post.SetValue("allowReplies", true);
                    }

                    _contentService.SaveAndPublishWithStatus(post);

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

                    return(RedirectToCurrentUmbracoPage());
                }
            }
            ModelState.AddModelError("Reply", "Error creating the post");
            return(RedirectToCurrentUmbracoPage());
        }
        public ActionResult PostReply([Bind(Prefix = "Reply")] SimpilyForumsPostModel model)
        {
            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 _contentService = ApplicationContext.Services.ContentService;

            var postName = string.Format("post_{0}", DateTime.Now.ToString("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.CreateContent(postName, parent, DashboardArborController.POST_DOC_TYPE);
                    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);
                    post.SetValue("category", model.Category);

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

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

                    _contentService.SaveAndPublishWithStatus(post);

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

                    return RedirectToCurrentUmbracoPage();
                }
            }
            ModelState.AddModelError("Reply", "Error creating the post");
            return RedirectToCurrentUmbracoPage();
        }
 /// <summary>
 ///  events - fired before and after a post is created
 /// </summary>
 protected void PostSavedEvent(IContent item, SimpilyForumsEventArgs e)
 {
     if (OnPostSaved != null)
     {
         OnPostSaved(item, e);
     }
 }
        protected bool NewPostEvent(SimpilyForumsPostModel model)
        {
            var e = new SimpilyForumsEventArgs();

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

            return !e.Cancel;
        }