Ejemplo n.º 1
0
        /// <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;
            }
        }
Ejemplo n.º 2
0
        /// <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();
        }