/// <summary> /// Shows the lava help. /// </summary> private void ShowLavaHelp() { var rockContext = new RockContext(); List <object> mergeObjectsList = GetMergeObjectList(rockContext, 1); var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson); MergeTemplate mergeTemplate = new MergeTemplateService(rockContext).Get(mtPicker.SelectedValue.AsInteger()); MergeTemplateType mergeTemplateType = null; if (mergeTemplate != null) { mergeTemplateType = this.GetMergeTemplateType(rockContext, mergeTemplate); } if (mergeTemplateType != null) { // have the mergeTemplateType generate the help text lShowMergeFields.Text = mergeTemplateType.GetLavaDebugInfo(mergeObjectsList, mergeFields); } else { lShowMergeFields.Text = MergeTemplateType.GetDefaultLavaDebugInfo(mergeObjectsList, mergeFields); } }
/// <summary> /// Handles the FileUploaded event of the fuTemplateBinaryFile control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void fuTemplateBinaryFile_FileUploaded(object sender, EventArgs e) { nbFileTypeWarning.Visible = false; var mergeTemplateEntityType = EntityTypeCache.Get(ddlMergeTemplateType.SelectedValue.AsInteger()); var binaryFile = new BinaryFileService(new RockContext()).Get(fuTemplateBinaryFile.BinaryFileId ?? 0); if (binaryFile != null) { string fileExtension = Path.GetExtension(binaryFile.FileName); fileExtension = fileExtension.TrimStart('.'); if (string.IsNullOrWhiteSpace(fileExtension)) { // nothing more to do return; } MergeTemplateType mergeTemplateType = null; if (mergeTemplateEntityType != null) { mergeTemplateType = MergeTemplateTypeContainer.GetComponent(mergeTemplateEntityType.Name); } else { // if a merge template type isn't selected, automatically pick the first matching one foreach (var item in MergeTemplateTypeContainer.Instance.Components.Values) { if (item.Value.IsActive) { var testMergeTemplateType = item.Value; if (testMergeTemplateType.SupportedFileExtensions != null && testMergeTemplateType.SupportedFileExtensions.Any()) { if (testMergeTemplateType.SupportedFileExtensions.Contains(fileExtension)) { mergeTemplateType = testMergeTemplateType; var entityType = EntityTypeCache.Get(mergeTemplateType.EntityType.Id); if (entityType != null) { ddlMergeTemplateType.SetValue(entityType.Id); } break; } } } } } if (mergeTemplateType == null) { // couldn't automatically pick one, so warn that they need to pick it nbFileTypeWarning.Text = "Warning: Please select a template type."; nbFileTypeWarning.Visible = true; nbFileTypeWarning.Dismissable = true; return; } if (mergeTemplateType.SupportedFileExtensions != null && mergeTemplateType.SupportedFileExtensions.Any()) { if (!mergeTemplateType.SupportedFileExtensions.Contains(fileExtension)) { nbFileTypeWarning.Text = string.Format( "Warning: The selected template type doesn't support '{0}' files. Please use a {1} file for this template type.", fileExtension, mergeTemplateType.SupportedFileExtensions.Select(a => a.Quoted()).ToList().AsDelimited(", ", " or ")); nbFileTypeWarning.Visible = true; nbFileTypeWarning.Dismissable = true; return; } } } if (binaryFile != null && string.IsNullOrWhiteSpace(tbName.Text)) { tbName.Text = Path.GetFileNameWithoutExtension(binaryFile.FileName).SplitCase().ReplaceWhileExists(" ", " "); } }
/// <summary> /// Handles the Click event of the btnMerge control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void btnMerge_Click(object sender, EventArgs e) { // NOTE: This is a full postback (not a partial like most other blocks) var rockContext = new RockContext(); int?databaseTimeoutSeconds = GetAttributeValue("DatabaseTimeout").AsIntegerOrNull(); if (databaseTimeoutSeconds != null && databaseTimeoutSeconds.Value > 0) { rockContext.Database.CommandTimeout = databaseTimeoutSeconds.Value; } List <object> mergeObjectsList = GetMergeObjectList(rockContext); MergeTemplate mergeTemplate = new MergeTemplateService(rockContext).Get(mtPicker.SelectedValue.AsInteger()); if (mergeTemplate == null) { nbWarningMessage.Text = "Unable to get merge template"; nbWarningMessage.NotificationBoxType = NotificationBoxType.Danger; nbWarningMessage.Visible = true; return; } MergeTemplateType mergeTemplateType = this.GetMergeTemplateType(rockContext, mergeTemplate); if (mergeTemplateType == null) { nbWarningMessage.Text = "Unable to get merge template type"; nbWarningMessage.NotificationBoxType = NotificationBoxType.Danger; nbWarningMessage.Visible = true; return; } var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson); BinaryFile outputBinaryFileDoc = null; try { outputBinaryFileDoc = mergeTemplateType.CreateDocument(mergeTemplate, mergeObjectsList, mergeFields); if (mergeTemplateType.Exceptions != null && mergeTemplateType.Exceptions.Any()) { if (mergeTemplateType.Exceptions.Count == 1) { this.LogException(mergeTemplateType.Exceptions[0]); } else if (mergeTemplateType.Exceptions.Count > 50) { this.LogException(new AggregateException(string.Format("Exceptions merging template {0}. See InnerExceptions for top 50.", mergeTemplate.Name), mergeTemplateType.Exceptions.Take(50).ToList())); } else { this.LogException(new AggregateException(string.Format("Exceptions merging template {0}. See InnerExceptions", mergeTemplate.Name), mergeTemplateType.Exceptions.ToList())); } } var uri = new UriBuilder(outputBinaryFileDoc.Url); var qry = System.Web.HttpUtility.ParseQueryString(uri.Query); qry["attachment"] = true.ToTrueFalse(); uri.Query = qry.ToString(); Response.Redirect(uri.ToString(), false); Context.ApplicationInstance.CompleteRequest(); } catch (Exception ex) { this.LogException(ex); if (ex is System.FormatException) { nbMergeError.Text = "Error loading the merge template. Please verify that the merge template file is valid."; } else { nbMergeError.Text = "An error occurred while merging"; } nbMergeError.Details = ex.Message; nbMergeError.Visible = true; } }
/// <summary> /// Handles the Click event of the lbPrintAttendanceRoster control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void lbPrintAttendanceRoster_Click(object sender, EventArgs e) { // NOTE: lbPrintAttendanceRoster is a full postback since we are returning a download of the roster nbPrintRosterWarning.Visible = false; var rockContext = new RockContext(); Dictionary <int, object> mergeObjectsDictionary = new Dictionary <int, object>(); if (_attendees != null) { var personIdList = _attendees.Select(a => a.PersonId).ToList(); var personList = new PersonService(rockContext).GetByIds(personIdList); foreach (var person in personList.OrderBy(a => a.LastName).ThenBy(a => a.NickName)) { mergeObjectsDictionary.AddOrIgnore(person.Id, person); } } var mergeFields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, this.CurrentPerson); mergeFields.Add("Group", this._group); var mergeTemplate = new MergeTemplateService(rockContext).Get(this.GetAttributeValue("AttendanceRosterTemplate").AsGuid()); if (mergeTemplate == null) { this.LogException(new Exception("No Merge Template specified in block settings")); nbPrintRosterWarning.Visible = true; nbPrintRosterWarning.Text = "Unable to print Attendance Roster"; return; } MergeTemplateType mergeTemplateType = mergeTemplate.GetMergeTemplateType(); if (mergeTemplateType == null) { this.LogException(new Exception("Unable to determine Merge Template Type")); nbPrintRosterWarning.Visible = true; nbPrintRosterWarning.Text = "Error printing Attendance Roster"; return; } BinaryFile outputBinaryFileDoc = null; var mergeObjectList = mergeObjectsDictionary.Select(a => a.Value).ToList(); outputBinaryFileDoc = mergeTemplateType.CreateDocument(mergeTemplate, mergeObjectList, mergeFields); // set the name of the output doc outputBinaryFileDoc = new BinaryFileService(rockContext).Get(outputBinaryFileDoc.Id); outputBinaryFileDoc.FileName = _group.Name + " Attendance Roster" + Path.GetExtension(outputBinaryFileDoc.FileName ?? "") ?? ".docx"; rockContext.SaveChanges(); if (mergeTemplateType.Exceptions != null && mergeTemplateType.Exceptions.Any()) { if (mergeTemplateType.Exceptions.Count == 1) { this.LogException(mergeTemplateType.Exceptions[0]); } else if (mergeTemplateType.Exceptions.Count > 50) { this.LogException(new AggregateException(string.Format("Exceptions merging template {0}. See InnerExceptions for top 50.", mergeTemplate.Name), mergeTemplateType.Exceptions.Take(50).ToList())); } else { this.LogException(new AggregateException(string.Format("Exceptions merging template {0}. See InnerExceptions", mergeTemplate.Name), mergeTemplateType.Exceptions.ToList())); } } var uri = new UriBuilder(outputBinaryFileDoc.Url); var qry = System.Web.HttpUtility.ParseQueryString(uri.Query); qry["attachment"] = true.ToTrueFalse(); uri.Query = qry.ToString(); Response.Redirect(uri.ToString(), false); Context.ApplicationInstance.CompleteRequest(); }