コード例 #1
0
        public void CreateComment(CivilCommentsSectionViewModel model)
        {
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                Database masterDb   = Sitecore.Configuration.Factory.GetDatabase("master");
                var      dataSource = model.DatasourceItem;
                dataSource = masterDb.GetItem(dataSource.ID);
                var commentsFolder = masterDb.GetItem("{A0660FA6-D500-453A-A807-4FFE7997F83C}");
                if (commentsFolder == null)
                {
                    return;
                }
                TemplateItem template   = masterDb.GetTemplate("{F0A8C4E9-FEE1-407E-99DB-A7F2D16024D1}");
                var          newComment =
                    commentsFolder.Add(String.Format("Comment {0}", DateTime.Now.ToString("g")).SanitizeToItemName(),
                                       template);

                var user     = Sitecore.Context.User;
                var username = user == null ? "anon" : user.Name;

                if (newComment == null)
                {
                    return;
                }
                newComment.Editing.BeginEdit();
                newComment.Fields["Comment"].Value  = model.Comment;
                newComment.Fields["Username"].Value = username;
                newComment.Editing.EndEdit();

                Sitecore.Publishing.PublishOptions publishOptions =
                    new Sitecore.Publishing.PublishOptions(newComment.Database,
                                                           Database.GetDatabase("web"),
                                                           Sitecore.Publishing.PublishMode.SingleItem,
                                                           newComment.Language,
                                                           System.DateTime.Now); // Create a publisher with the publishoptions
                Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions);

                publisher.Options.RootItem = newComment;
                publisher.Publish();
            }
        }
コード例 #2
0
        // GET: Default
        public ActionResult SubmitComment(CivilCommentsSectionViewModel model)
        {
            model.ClearTextbox = false;
            if (model.SubmitShittyComment == "true")
            {
                CreateComment(model);
                return(View("/Views/CivilCommentsSection.cshtml", new CivilCommentsSectionViewModel()
                {
                    ClearTextbox = true
                }));
            }

            model.PromptAreYouSure = false;

            var comment    = model.Comment;
            var dataSource = model.DatasourceItem;

            List <Word> words = new List <Word>();

            Sitecore.Data.Fields.MultilistField flaggedWords = dataSource.Fields["Flagged Words"];
            Sitecore.Data.Fields.MultilistField wordGroups   = dataSource.Fields["Flagged Word Groups"];

            var cooldownTime = dataSource.Fields["Cooldown"].Value;

            int cooldown = 5000;

            if (!String.IsNullOrEmpty(cooldownTime))
            {
                if (int.TryParse(cooldownTime, out cooldown))
                {
                    model.Cooldown = cooldown;
                }
            }

            foreach (var wordItem in flaggedWords.GetItems())
            {
                words.AddRange(GetWords(wordItem));
            }

            foreach (var wordGroup in wordGroups.GetItems())
            {
                var warning = wordGroup.Fields["Warning Text"].Value;
                Sitecore.Data.Fields.MultilistField wordsField = wordGroup.Fields["Words"];
                foreach (var wordItem in wordsField.GetItems())
                {
                    words.AddRange(GetWords(wordItem, warning));
                }
            }

            var warnings = 0;

            foreach (var word in words) // scan comment for the word
            {
                warnings += comment.CountMatches(word.Value);

                var regex = new Regex(word.Value, RegexOptions.IgnoreCase);

                string formattedWord = String.Format("<span class='warning-word' style='background-color:{2}'>{0}<span class='tool-tip'>{1}</span></span>", word.Value, word.Warning, word.Color);

                comment = regex.Replace(comment, formattedWord);
            }

            model.ReviewText = comment;

            if (model.Comment == model.PreviousComment)
            {
                model.PromptAreYouSure = true;
                model.DisplayWarnings  = true;
                model.PreviousComment  = model.Comment;
                return(View("/Views/CivilCommentsSection.cshtml", model));
            }

            if (warnings > 0)
            {
                model.DisplayWarnings = true;
                model.PreviousComment = model.Comment;
                model.TotalWarnings  += warnings;
                return(View("/Views/CivilCommentsSection.cshtml", model));
            }
            else
            {
                CreateComment(model);
                return(View("/Views/CivilCommentsSection.cshtml", new CivilCommentsSectionViewModel()
                {
                    ClearTextbox = true
                }));
            }
        }
コード例 #3
0
        public ActionResult Index()
        {
            var model = new CivilCommentsSectionViewModel();

            return(View("/Views/CivilCommentsSection.cshtml", model));
        }