コード例 #1
0
 protected static bool ValidateWebAddressRequired(DataItemBase item)
 {
     if (!ValidateRequired(item))
     {
         return(false);
     }
     item.DataControl.SetValue(
         Validation.StripWebProtocol(item.DataControl.GetValue()));
     return(true);
 }
コード例 #2
0
        protected static bool ValidateEmail(DataItemBase item)
        {
            item.StripRedundantWhiteSpace();
            var value = item.DataControl.GetValue();

            if (value.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase))
            {
                value = value.Substring(7);
            }
            if (!IsNullOrWhiteSpace(value) &&
                !Validation.IsValidEmailAddress(value))
            {
                item.Feedback.PostValidationError(item.DataControl,
                                                  "We do not recognize the Email as valid");
                return(false);
            }
            item.DataControl.SetValue(Validation.StripWebProtocol(value));
            return(true);
        }
コード例 #3
0
        protected static bool ValidateYouTubeVideoAddressOptional(DataItemBase item)
        {
            string message = null;
            var    value   = item.DataControl.GetValue();

            if (!IsNullOrWhiteSpace(value))
            {
                if (value.IsValidYouTubeVideoUrl())
                {
                    var id   = value.GetYouTubeVideoId();
                    var info = YouTubeVideoUtility.GetVideoInfo(id, true, 1);
                    if (!info.IsValid)
                    {
                        message = YouTubeVideoInfo.VideoIdNotFoundMessage;
                    }
                    else if (!info.IsPublic)
                    {
                        message = YouTubeVideoInfo.VideoNotPublicMessage;
                    }
                }
                else
                {
                    message = "The URL is not a valid YouTube video URL";
                }
            }

            if (IsNullOrWhiteSpace(message))
            {
                item.DataControl.SetValue(
                    Validation.StripWebProtocol(value));
            }
            else
            {
                item.Feedback.PostValidationError(item.DataControl, message);
            }

            return(IsNullOrWhiteSpace(message));
        }
コード例 #4
0
        private SecurePage.UpdateStatus DoAnswerUpdate(IList <IGrouping <string, AnswersViewRow> > table,
                                                       string questionKey, bool reportUnchanged, ref int updateCount)
        {
            var page         = VotePage.GetPage <SecurePoliticianPage>();
            var updateStatus = SecurePage.UpdateStatus.Failure; // default
            var description  = string.Empty;

            var feedback =
                page.Master.FindMainContentControl("Feedback" + questionKey) as
                FeedbackContainerControl;
            var updatePanel =
                page.Master.FindMainContentControl("UpdatePanel" + questionKey) as UpdatePanel;
            var textAnswerBox =
                page.Master.FindMainContentControl("TextBox" + questionKey) as TextBox;
            var textSourceBox =
                page.Master.FindMainContentControl("Source" + questionKey) as TextBox;
            var textDateBox   = page.Master.FindMainContentControl("Date" + questionKey) as TextBox;
            var youTubeUrlBox =
                page.Master.FindMainContentControl("YouTubeUrl" + questionKey) as TextBox;
            var youTubeSourceBox      = page.Master.FindMainContentControl("YouTubeSource" + questionKey) as TextBox;
            var youTubeSourceUrlBox   = page.Master.FindMainContentControl("YouTubeSourceUrl" + questionKey) as TextBox;
            var youTubeDateBox        = page.Master.FindMainContentControl("YouTubeDate" + questionKey) as TextBox;
            var youTubeDescriptionBox =
                page.Master.FindMainContentControl("YouTubeDescription" + questionKey) as TextBox;
            var youTubeRunningTimeBox =
                page.Master.FindMainContentControl("YouTubeRunningTime" + questionKey) as TextBox;
            var youTubeFromCandidate =
                page.Master.FindMainContentControl("YouTubeFromCandidate" + questionKey) as HtmlInputCheckBox;
            var youTubeFromVoteUsa =
                page.Master.FindMainContentControl("YouTubeFromVoteUSA" + questionKey) as HtmlInputCheckBox;
            var sequenceHidden = page.Master.FindMainContentControl("Sequence" + questionKey) as HtmlInputHidden;
            var hasValue       =
                page.Master.FindMainContentControl("HasValue" + questionKey) as HtmlInputHidden;

            var textIsFromCandidate = SecurePage.IsPoliticianUser;

            var youTubeIsFromCandidate = SecurePage.IsPoliticianUser || youTubeFromCandidate.Checked ||
                                         youTubeSourceBox.Text.Trim() == YouTubeInfo.VideoUploadedByCandidateMessage;

            try
            {
                textAnswerBox.AddCssClasses("badupdate");

                FeedbackContainerControl.ClearValidationErrors(textAnswerBox, textSourceBox, textDateBox,
                                                               youTubeUrlBox, youTubeDateBox);

                var sequence = sequenceHidden.Value == "?"
          ? Answers.GetNextSequence(page.PoliticianKey, questionKey)
          : int.Parse(sequenceHidden.Value);

                var      newTextAnswer = textAnswerBox.GetValue();
                string   newTextSource;
                DateTime newTextDate;
                var      newYouTubeSource    = string.Empty;
                var      newYouTubeSourceUrl = string.Empty;
                var      newYouTubeDate      = VotePage.DefaultDbDate;
                bool     success;
                var      textDateWasEmpty    = true;
                var      youTubeDateWasEmpty = true;
                youTubeDescriptionBox.Text = string.Empty;
                youTubeRunningTimeBox.Text = string.Empty;

                newTextAnswer = feedback.StripHtml(newTextAnswer);
                newTextAnswer = newTextAnswer.StripRedundantSpaces();
                var oldResponses = table.Where(g => g.Key.IsEqIgnoreCase(questionKey))
                                   .SelectMany(g => g);
                var question = oldResponses.First();
                var oldRow   = table.Where(g => g.Key.IsEqIgnoreCase(questionKey))
                               .SelectMany(g => g)
                               .FirstOrDefault(r => r.Sequence == sequence);
                description = '"' + question.Question + '"';

                var isAnswerChanged = oldRow == null || /*newTextAnswer*/ textAnswerBox.GetValue().Trim() != oldRow.Answer.Trim();

                if (string.IsNullOrWhiteSpace(newTextAnswer))
                {
                    newTextSource = string.Empty;
                    newTextDate   = VotePage.DefaultDbDate;
                }
                else if (textIsFromCandidate)
                {
                    newTextSource = page.PageCache.Politicians.GetLastName(page.PoliticianKey);
                    newTextDate   = DateTime.UtcNow.Date;
                }
                else
                {
                    newTextSource    = textSourceBox.Text;
                    newTextSource    = feedback.StripHtml(newTextSource);
                    newTextSource    = newTextSource.StripRedundantSpaces();
                    textDateWasEmpty = string.IsNullOrWhiteSpace(textDateBox.Text);
                    newTextDate      =
                        feedback.ValidateDateOptional(textDateBox, out success, "Text Date",
                                                      isAnswerChanged ? DateTime.UtcNow.Date : VotePage.DefaultDbDate)
                        .Date;
                }

                var newYouTubeUrl = youTubeUrlBox.GetValue();
                newYouTubeUrl = feedback.StripHtml(newYouTubeUrl);
                newYouTubeUrl = newYouTubeUrl.StripRedundantSpaces();

                YouTubeInfo youTubeInfo = null;
                if (!string.IsNullOrWhiteSpace(newYouTubeUrl))
                {
                    var youTubeId = newYouTubeUrl.GetYouTubeVideoId();
                    if (youTubeFromCandidate != null && !youTubeFromCandidate.Checked &&
                        youTubeFromVoteUsa != null && !youTubeFromVoteUsa.Checked)
                    {
                        feedback.PostValidationError(new[] { youTubeFromCandidate, youTubeFromVoteUsa }, "Please select a type of video");
                    }
                    if (string.IsNullOrWhiteSpace(youTubeId))
                    {
                        feedback.PostValidationError(youTubeUrlBox, YouTubeInfo.InvalidVideoUrlMessage);
                    }
                    else
                    {
                        youTubeInfo = YouTubeUtility.GetVideoInfo(youTubeId, true, 1);
                        if (!youTubeInfo.IsValid)
                        {
                            feedback.PostValidationError(youTubeUrlBox, YouTubeInfo.VideoIdNotFoundMessage);
                        }
                        else if (!youTubeInfo.IsPublic)
                        {
                            feedback.PostValidationError(youTubeUrlBox, YouTubeInfo.VideoNotPublicMessage);
                        }
                        else
                        {
                            youTubeDateWasEmpty = string.IsNullOrWhiteSpace(youTubeDateBox?.Text);
                            if (youTubeIsFromCandidate)
                            {
                                newYouTubeSource    = YouTubeInfo.VideoUploadedByCandidateMessage;
                                newYouTubeSourceUrl = string.Empty;
                                newYouTubeDate      = youTubeInfo.PublishedAt;
                            }
                            else
                            {
                                newYouTubeSource    = youTubeSourceBox.Text;
                                newYouTubeSource    = feedback.StripHtml(newYouTubeSource);
                                newYouTubeSource    = newYouTubeSource.StripRedundantSpaces();
                                newYouTubeSourceUrl = youTubeSourceUrlBox == null
                  ? string.Empty
                  : youTubeSourceUrlBox.Text;
                                newYouTubeSourceUrl = feedback.StripHtml(newYouTubeSourceUrl);
                                newYouTubeSourceUrl = Validation.StripWebProtocol(newYouTubeSourceUrl);
                                newYouTubeDate      = youTubeDateWasEmpty
                  ? youTubeInfo.PublishedAt
                  : feedback.ValidateDate(youTubeDateBox, out success, "YouTube Date",
                                          new DateTime(2004, 1, 1), DateTime.UtcNow).Date;
                            }
                        }
                    }
                }

                if (feedback.ValidationErrorCount == 0)
                {
                    var oldTextAnswer       = string.Empty;
                    var oldTextSource       = string.Empty;
                    var oldTextDate         = VotePage.DefaultDbDate;
                    var oldYouTubeUrl       = string.Empty;
                    var oldYouTubeSource    = string.Empty;
                    var oldYouTubeSourceUrl = string.Empty;
                    var oldYouTubeDate      = VotePage.DefaultDbDate;

                    if (oldRow != null)
                    {
                        oldTextAnswer       = oldRow.Answer.SafeString();
                        oldTextSource       = oldRow.Source.SafeString();
                        oldTextDate         = oldRow.DateStamp.SafeDbDate();
                        oldYouTubeUrl       = oldRow.YouTubeUrl.SafeString();
                        oldYouTubeSource    = oldRow.YouTubeSource();
                        oldYouTubeSourceUrl = oldRow.YouTubeSourceUrl().SafeString();
                        oldYouTubeDate      = oldRow.YouTubeDate.SafeDbDate()
                                              .Date;
                    }

                    var unchanged = oldTextAnswer == newTextAnswer &&
                                    oldYouTubeUrl == newYouTubeUrl &&
                                    oldYouTubeSource == newYouTubeSource &&
                                    oldYouTubeSourceUrl == newYouTubeSourceUrl &&
                                    (oldYouTubeDate == newYouTubeDate || youTubeDateWasEmpty);
                    if (unchanged && !textIsFromCandidate)
                    {
                        unchanged = oldTextSource == newTextSource &&
                                    (oldTextDate == newTextDate || textDateWasEmpty);
                    }

                    if (unchanged)
                    {
                        if (reportUnchanged)
                        {
                            feedback.AddInfo("Your " + description + " entry was unchanged.");
                            updatePanel.Update();
                        }
                        updateStatus = SecurePage.UpdateStatus.Unchanged;
                    }
                    else
                    {
                        if (!textIsFromCandidate && !question.IsTextSourceOptional.GetValueOrDefault() &&
                            !string.IsNullOrWhiteSpace(newTextAnswer) &&
                            (isAnswerChanged || !string.IsNullOrWhiteSpace(oldTextSource)))
                        {
                            feedback.ValidateLength(textSourceBox, "Text Source", 1, 255, out success);
                        }
                        if (!string.IsNullOrWhiteSpace(newYouTubeUrl))
                        {
                            if (!youTubeIsFromCandidate)
                            {
                                feedback.ValidateRequired(youTubeSourceBox, "YouTube Source", out success);
                            }
                        }
                        if (feedback.ValidationErrorCount == 0)
                        {
                            string videoDescription = null;
                            var    videoRunningTime = default(TimeSpan);
                            if (youTubeInfo != null)
                            {
                                videoDescription           = youTubeInfo.ShortDescription;
                                videoRunningTime           = youTubeInfo.Duration;
                                youTubeDescriptionBox.Text = videoDescription;
                                youTubeRunningTimeBox.Text = videoRunningTime.FormatRunningTime();
                            }

                            if (youTubeSourceBox != null)
                            {
                                youTubeSourceBox.Text = newYouTubeSource;
                            }
                            if (youTubeSourceUrlBox != null)
                            {
                                youTubeSourceUrlBox.Text = newYouTubeSourceUrl;
                            }
                            if (youTubeDateBox != null)
                            {
                                youTubeDateBox.Text = newYouTubeDate.DbDateToShortDate();
                            }
                            if (youTubeFromCandidate != null)
                            {
                                youTubeFromCandidate.Checked = youTubeIsFromCandidate;
                            }
                            if (youTubeFromVoteUsa != null)
                            {
                                youTubeFromVoteUsa.Checked = !youTubeIsFromCandidate;
                            }

                            hasValue.Value = string.IsNullOrWhiteSpace(newTextAnswer) &&
                                             string.IsNullOrWhiteSpace(newYouTubeUrl)
                ? string.Empty
                : "Y";
                            page.LogPoliticianAnswerChange(questionKey, sequence, oldTextAnswer, newTextAnswer,
                                                           newTextSource);
                            page.UpdatePoliticianAnswer(questionKey, sequence, question.IssueKey, newTextAnswer,
                                                        newTextSource, newTextDate, newYouTubeUrl, videoDescription, videoRunningTime,
                                                        newYouTubeSource, newYouTubeSourceUrl, newYouTubeDate);
                            UpdateQuestion(questionKey, sequence);
                            feedback.AddInfo("Your " + description + " entry was updated.");
                            updateStatus = SecurePage.UpdateStatus.Success;
                            updateCount++;
                        }
                        updatePanel.Update();
                    }
                }

                if (updateStatus != SecurePage.UpdateStatus.Failure)
                {
                    if (newTextAnswer != textAnswerBox.Text)
                    {
                        updatePanel.Update();
                    }
                    textAnswerBox.SetValue(newTextAnswer);
                    if (!textIsFromCandidate)
                    {
                        if (string.IsNullOrWhiteSpace(newTextAnswer))
                        {
                            newTextSource = string.Empty;
                            newTextDate   = VotePage.DefaultDbDate;
                        }
                        var newDateText = newTextDate.DbDateToShortDate();
                        Debug.Assert(textSourceBox != null, "sourceBox != null");
                        Debug.Assert(textDateBox != null, "dateBox != null");
                        if (newTextSource != textSourceBox.Text || newDateText != textDateBox.Text)
                        {
                            updatePanel.Update();
                        }
                        textSourceBox.SetValue(newTextSource);
                        textDateBox.SetValue(newDateText);
                    }
                    if (!youTubeIsFromCandidate)
                    {
                        var newYouTubeDateText = newYouTubeDate == VotePage.DefaultDbDate
              ? string.Empty
              : newYouTubeDate.DbDateToShortDate();
                        if (newYouTubeDateText != youTubeDateBox.Text)
                        {
                            updatePanel.Update();
                        }
                        youTubeDateBox.SetValue(newYouTubeDateText);
                    }
                }

                textAnswerBox.RemoveCssClass("badupdate");
            }
            catch (Exception ex)
            {
                if (description == string.Empty)
                {
                    description = "your response";
                }
                feedback.AddError("There was an unexpected error updating " + description);
                feedback.HandleException(ex);
                updatePanel.Update();
            }

            return(updateStatus);
        }
コード例 #5
0
        protected static bool ValidateYouTubeAddressOptional(DataItemBase item)
        {
            string message = null;
            var    value   = item.DataControl.GetValue();

            if (!IsNullOrWhiteSpace(value))
            {
                if (value.IsValidYouTubeVideoUrl())
                {
                    var id   = value.GetYouTubeVideoId();
                    var info = YouTubeVideoUtility.GetVideoInfo(id, true, 1);
                    if (!info.IsValid)
                    {
                        message = YouTubeVideoInfo.VideoIdNotFoundMessage;
                    }
                    else if (!info.IsPublic)
                    {
                        message = YouTubeVideoInfo.VideoNotPublicMessage;
                    }
                }
                else if (value.IsValidYouTubePlaylistUrl())
                {
                    var id   = value.GetYouTubePlaylistId();
                    var info = YouTubeVideoUtility.GetPlaylistInfo(id, true, 1);
                    if (!info.IsValid)
                    {
                        message = YouTubeVideoInfo.PlaylistIdNotFoundMessage;
                    }
                    else if (!info.IsPublic)
                    {
                        message = YouTubeVideoInfo.PlaylistNotPublicMessage;
                    }
                }
                else if (value.IsValidYouTubeChannelUrl() || value.IsValidYouTubeCustomChannelUrl() ||
                         value.IsValidYouTubeUserChannelUrl())
                {
                    var id = YouTubeVideoUtility.LookupChannelId(value, 1);
                    if (IsNullOrWhiteSpace(id))
                    {
                        message = YouTubeVideoInfo.ChannelIdNotFoundMessage;
                    }
                    else
                    {
                        var info = YouTubeVideoUtility.GetChannelInfo(id, true, 1);
                        if (!info.IsValid)
                        {
                            message = YouTubeVideoInfo.ChannelIdNotFoundMessage;
                        }
                        else if (!info.IsPublic)
                        {
                            message = YouTubeVideoInfo.ChannelNotPublicMessage;
                        }
                    }
                }
                else
                {
                    message = "The URL is not a valid YouTube channel, playlist or video URL";
                }
            }

            if (IsNullOrWhiteSpace(message))
            {
                item.DataControl.SetValue(
                    Validation.StripWebProtocol(value));
            }
            else
            {
                item.Feedback.PostValidationError(item.DataControl, message);
            }

            return(IsNullOrWhiteSpace(message));
        }
コード例 #6
0
 protected static bool ValidateWebAddress(DataItemBase item)
 {
     item.DataControl.SetValue(
         Validation.StripWebProtocol(item.DataControl.GetValue()));
     return(true);
 }