private void ButtonUpdate_OnClick(object sender, EventArgs e) { var page = VotePage.GetPage <SecurePage>(); var button = sender as Button; if (button == null || !button.ID.StartsWith("Button", StringComparison.Ordinal)) { return; } var questionKey = button.ID.Substring(6); var feedback = page.Master.FindMainContentControl("Feedback" + questionKey) as FeedbackContainerControl; try { DoAnswersUpdate(questionKey, true); page.InvalidatePageCache(); } catch (Exception ex) { feedback?.HandleException(ex); } }
private void UpdateQuestion(string questionKey, int sequence) { var page = VotePage.GetPage <SecurePoliticianPage>(); var responses = Answers.GetDataByPoliticianKeyQuestionKey(page.PoliticianKey, questionKey) .Rows.Cast <DataRow>().ToList(); var row = responses.FirstOrDefault(r => r.Sequence() == sequence); // refresh the sequence // if the sequence no longer exists, make it "?" var sequenceHidden = page.Master.FindMainContentControl("Sequence" + questionKey) as HtmlInputHidden; Debug.Assert(sequenceHidden != null, "sequenceHidden != null"); sequenceHidden.Value = row == null ? "?" : sequence.ToString(CultureInfo.InvariantCulture); SetResponseData(responses, row, sequenceHidden); // recreate the action menu (it might need changing) var actionContainer = page.Master.FindMainContentControl("Action" + questionKey) as HtmlContainerControl; CreateActionMenu(actionContainer, responses, row == null); }
protected static FeedbackContainerControl LoadFeedbackContainerControl() { var page = VotePage.GetPage <Page>(); return (page.LoadControl("/controls/feedbackcontainer.ascx") as FeedbackContainerControl); }
public IList <IGrouping <string, AnswersViewRow> > GetDataTable() { if (_DataTable == null) { AnswersViewTable table = null; if (_Usage == Usage.ForIssues) { var page = VotePage.GetPage <SecurePoliticianPage>(); var officeKey = page.PageCache.Politicians.GetOfficeKey(page.PoliticianKey); var officeClass = page.PageCache.Offices.GetOfficeClass(officeKey); if (officeClass.IsFederal()) { table = AnswersView.GetAllDataByIssueLevelStateCodePoliticianKey("B", "US", page.PoliticianKey); } else { table = AnswersView.GetAllDataByIssueLevelStateCodePoliticianKey("C", Politicians.GetStateCodeFromKey(page.PoliticianKey), page.PoliticianKey); } } else if (_Usage == Usage.ForIntroBio) { table = AnswersView.GetAllDataByIssueKeyPoliticianKey("ALLBio", VotePage.GetPage <SecurePoliticianPage>().PoliticianKey); } else if (_Usage == Usage.ForIntroReasons) { table = AnswersView.GetAllDataByIssueKeyPoliticianKey("ALLPersonal", VotePage.GetPage <SecurePoliticianPage>().PoliticianKey); } Debug.Assert(table != null, "table != null"); _DataTable = table .GroupBy(r => r.QuestionKey) //.Select(g => g.First()) .ToList(); } return(_DataTable); }
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); }