Exemplo n.º 1
0
        /// <summary>
        /// Set the note as Watched or Unwatched by the current person
        /// </summary>
        /// <param name="noteId">The note identifier.</param>
        /// <param name="watching">if set to <c>true</c> [watching].</param>
        private void WatchNote(int?noteId, bool watching)
        {
            var rockPage = this.Page as RockPage;

            if (rockPage != null)
            {
                var currentPerson = rockPage.CurrentPerson;

                var rockContext      = new RockContext();
                var noteService      = new NoteService(rockContext);
                var noteWatchService = new NoteWatchService(rockContext);

                if (noteId.HasValue)
                {
                    var note = noteService.Get(noteId.Value);
                    if (note != null && note.IsAuthorized(Authorization.VIEW, currentPerson))
                    {
                        var noteWatch = noteWatchService.Queryable().Where(a => a.NoteId == noteId.Value && a.WatcherPersonAlias.PersonId == currentPerson.Id).FirstOrDefault();
                        if (noteWatch == null)
                        {
                            noteWatch = new NoteWatch {
                                NoteId = noteId.Value, WatcherPersonAliasId = rockPage.CurrentPersonAliasId
                            };
                            noteWatchService.Add(noteWatch);
                        }

                        noteWatch.IsWatching = watching;
                        rockContext.SaveChanges();
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            RockContext      rockContext      = new RockContext();
            NoteWatchService noteWatchService = new NoteWatchService(rockContext);

            var qry = noteWatchService.Queryable().Include(a => a.WatcherPersonAlias.Person).Include(a => a.WatcherGroup);

            Guid?blockEntityTypeGuid = this.GetAttributeValue("EntityType").AsGuidOrNull();
            Guid?blockNoteTypeGuid   = this.GetAttributeValue("NoteType").AsGuidOrNull();

            if (blockNoteTypeGuid.HasValue)
            {
                // if a NoteType was specified in block settings, only list note watches for the specified note type
                int noteTypeId = EntityTypeCache.Get(blockNoteTypeGuid.Value).Id;
                qry = qry.Where(a => a.NoteTypeId.HasValue && a.NoteTypeId == noteTypeId);
            }
            else if (blockEntityTypeGuid.HasValue)
            {
                // if an EntityType was specific in block settings, only list note watches for the specified entity type (or for NoteTypes of the specified EntityType)
                int entityTypeId = EntityTypeCache.Get(blockEntityTypeGuid.Value).Id;
                qry = qry.Where(a =>
                                (a.EntityTypeId.HasValue && a.EntityTypeId.Value == entityTypeId) ||
                                (a.NoteTypeId.HasValue && a.NoteType.EntityTypeId == entityTypeId));
            }

            var contextPerson = ContextEntity <Person>();
            var contextGroup  = ContextEntity <Group>();

            if (contextPerson != null)
            {
                // if there is a Person context, only list note watches that where the watcher is the person context
                qry = qry.Where(a => a.WatcherPersonAliasId.HasValue && a.WatcherPersonAlias.PersonId == contextPerson.Id);
            }
            else if (contextGroup != null)
            {
                // if there is a Group context, only list note watches that where the watcher is the group context
                qry = qry.Where(a => a.WatcherGroupId.HasValue && a.WatcherGroupId == contextGroup.Id);
            }

            var sortProperty = gList.SortProperty;

            if (sortProperty != null)
            {
                qry = qry.Sort(sortProperty);
            }
            else
            {
                qry = qry.OrderBy(d => d.EntityType.Name).ThenBy(a => a.NoteType.Name);
            }

            gList.SetLinqDataSource(qry);
            gList.DataBind();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Handles the Delete event of the gList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RowEventArgs"/> instance containing the event data.</param>
        protected void gList_Delete(object sender, RowEventArgs e)
        {
            var rockContext = new RockContext();
            var service     = new NoteWatchService(rockContext);
            var noteWatch   = service.Get(e.RowKeyId);

            if (noteWatch != null)
            {
                string errorMessage;
                if (!service.CanDelete(noteWatch, out errorMessage))
                {
                    mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                    return;
                }

                service.Delete(noteWatch);
                rockContext.SaveChanges();
            }

            BindGrid();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sends the note watch notifications.
        /// </summary>
        /// <param name="context">The context.</param>
        private List <string> SendNoteWatchNotifications(IJobExecutionContext context)
        {
            var        errors = new List <string>();
            List <int> noteIdsToProcessNoteWatchesList = new List <int>();

            using (var rockContext = new RockContext())
            {
                var noteService      = new NoteService(rockContext);
                var noteWatchService = new NoteWatchService(rockContext);
                var noteWatchQuery   = noteWatchService.Queryable();

                if (!noteWatchQuery.Any())
                {
                    // there aren't any note watches, so there is nothing to do
                    return(errors);
                }

                // get all notes that haven't processed notifications yet
                var notesToNotifyQuery = noteService.Queryable().Where(a =>
                                                                       a.NotificationsSent == false &&
                                                                       a.NoteType.AllowsWatching == true &&
                                                                       a.EditedDateTime > _cutoffNoteEditDateTime);

                // limit to notes that don't require approval or are approved
                notesToNotifyQuery = notesToNotifyQuery.Where(a => a.NoteType.RequiresApprovals == false || a.ApprovalStatus == NoteApprovalStatus.Approved);

                if (!notesToNotifyQuery.Any())
                {
                    // there aren't any notes that haven't had notifications processed yet
                    return(errors);
                }

                noteIdsToProcessNoteWatchesList = notesToNotifyQuery.Select(a => a.Id).ToList();
            }

            // make a list of notifications to send to each personId
            Dictionary <int, NoteWatchPersonToNotifyList> personNotificationDigestList = new Dictionary <int, NoteWatchPersonToNotifyList>();

            using (var rockContext = new RockContext())
            {
                foreach (int noteId in noteIdsToProcessNoteWatchesList)
                {
                    this.UpdateNoteWatchNotificationDigest(personNotificationDigestList, rockContext, noteId);
                }

                // Send NoteWatch notifications
                if (personNotificationDigestList.Any())
                {
                    foreach (var personNotificationDigest in personNotificationDigestList)
                    {
                        var         recipients     = new List <RecipientData>();
                        Person      personToNotify = personNotificationDigest.Value.Person;
                        List <Note> noteList       = personNotificationDigest.Value.Select(a => a.Note).OrderBy(a => a.EditedDateTime).ToList();

                        // make sure a person doesn't get a notification on a note that they wrote
                        noteList = noteList.Where(a => a.EditedByPersonAlias?.PersonId != personToNotify.Id).ToList();

                        if (!string.IsNullOrEmpty(personToNotify.Email) && personToNotify.IsEmailActive && personToNotify.EmailPreference != EmailPreference.DoNotEmail && noteList.Any())
                        {
                            var mergeFields = new Dictionary <string, object>(_defaultMergeFields);
                            mergeFields.Add("Person", personToNotify);
                            mergeFields.Add("NoteList", noteList);
                            recipients.Add(new RecipientData(personToNotify.Email, mergeFields));
                        }

                        if (_noteWatchNotificationEmailGuid.HasValue)
                        {
                            var emailMessage = new RockEmailMessage(_noteWatchNotificationEmailGuid.Value);
                            emailMessage.SetRecipients(recipients);
                            emailMessage.Send(out errors);
                            _noteWatchNotificationsSent += recipients.Count();
                        }
                    }
                }
            }

            using (var rockUpdateContext = new RockContext())
            {
                var notesToMarkNotified = new NoteService(rockUpdateContext).Queryable().Where(a => noteIdsToProcessNoteWatchesList.Contains(a.Id));

                // use BulkUpdate to mark all the notes that we processed to NotificationsSent = true
                rockUpdateContext.BulkUpdate(notesToMarkNotified, n => new Note {
                    NotificationsSent = true
                });
            }
            return(errors);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="noteWatchId">The note watch identifier.</param>
        public void ShowDetail(int noteWatchId)
        {
            pnlView.Visible = true;
            var rockContext = new RockContext();

            // Load depending on Add(0) or Edit
            NoteWatch noteWatch = null;

            if (noteWatchId > 0)
            {
                noteWatch         = new NoteWatchService(rockContext).Get(noteWatchId);
                lActionTitle.Text = ActionTitle.Edit(NoteWatch.FriendlyTypeName).FormatAsHtmlTitle();
                pdAuditDetails.SetEntity(noteWatch, ResolveRockUrl("~"));
            }

            pdAuditDetails.Visible = noteWatch != null;

            var contextPerson = ContextEntity <Person>();
            var contextGroup  = ContextEntity <Group>();

            if (noteWatch == null)
            {
                noteWatch = new NoteWatch {
                    Id = 0
                };
                lActionTitle.Text = ActionTitle.Add(NoteWatch.FriendlyTypeName).FormatAsHtmlTitle();

                if (contextPerson != null)
                {
                    noteWatch.WatcherPersonAliasId = contextPerson.PrimaryAliasId;
                    noteWatch.WatcherPersonAlias   = contextPerson.PrimaryAlias;
                }
                else if (contextGroup != null)
                {
                    noteWatch.WatcherGroupId = contextGroup.Id;
                    noteWatch.WatcherGroup   = contextGroup;
                }
            }

            if (contextPerson != null)
            {
                ppWatcherPerson.Enabled = false;
                gpWatcherGroup.Visible  = false;

                // make sure we are seeing details for a NoteWatch that the current person is watching
                if (!noteWatch.WatcherPersonAliasId.HasValue || !contextPerson.Aliases.Any(a => a.Id == noteWatch.WatcherPersonAliasId.Value))
                {
                    // The NoteWatchId in the url isn't a NoteWatch for the PersonContext, so just hide the block
                    pnlView.Visible = false;
                }
            }
            else if (contextGroup != null)
            {
                ppWatcherPerson.Visible = false;
                gpWatcherGroup.Enabled  = false;

                // make sure we are seeing details for a NoteWatch that the current group context is watching
                if (!noteWatch.WatcherGroupId.HasValue || !(contextGroup.Id != noteWatch.WatcherGroupId))
                {
                    // The NoteWatchId in the url isn't a NoteWatch for the GroupContext, so just hide the block
                    pnlView.Visible = false;
                }
            }

            hfNoteWatchId.Value = noteWatchId.ToString();

            etpEntityType.SetValue(noteWatch.EntityTypeId);
            LoadNoteTypeDropDown(noteWatch.EntityTypeId);

            ddlNoteType.SetValue(noteWatch.NoteTypeId);

            if (noteWatch.WatcherPersonAlias != null)
            {
                ppWatcherPerson.SetValue(noteWatch.WatcherPersonAlias.Person);
            }
            else
            {
                ppWatcherPerson.SetValue(( Person )null);
            }

            gpWatcherGroup.SetValue(noteWatch.WatcherGroup);

            cbIsWatching.Checked = noteWatch.IsWatching;
            cbIsWatching_CheckedChanged(null, null);

            cbAllowOverride.Checked = noteWatch.AllowOverride;

            ShowEntityPicker(etpEntityType.SelectedEntityTypeId);

            etpEntityType.Enabled = true;
            if (noteWatch.EntityTypeId.HasValue && noteWatch.EntityId.HasValue)
            {
                var     watchedEntityTypeId = noteWatch.EntityTypeId;
                IEntity watchedEntity       = new EntityTypeService(rockContext).GetEntity(noteWatch.EntityTypeId.Value, noteWatch.EntityId.Value);

                if (watchedEntity != null)
                {
                    if (watchedEntity is Rock.Model.Person)
                    {
                        ppWatchedPerson.SetValue(watchedEntity as Rock.Model.Person);
                    }
                    else if (watchedEntity is Rock.Model.Group)
                    {
                        gpWatchedGroup.SetValue(watchedEntity as Rock.Model.Group);
                    }
                    else
                    {
                        lWatchedEntityName.Text = watchedEntity.ToString();
                        nbWatchedEntityId.Text  = watchedEntity.Id.ToString();
                    }

                    // Don't let the EntityType get changed if there is a specific Entity getting watched
                    etpEntityType.Enabled = false;
                }
            }

            lWatchedNote.Visible = false;
            if (noteWatch.Note != null)
            {
                var mergefields = Rock.Lava.LavaHelper.GetCommonMergeFields(this.RockPage, null, new Rock.Lava.CommonMergeFieldsOptions {
                    GetLegacyGlobalMergeFields = false
                });
                mergefields.Add("Note", noteWatch.Note);
                var lavaTemplate = this.GetAttributeValue("WatchedNoteLavaTemplate");

                lWatchedNote.Text = lavaTemplate.ResolveMergeFields(mergefields);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Handles the Click event of the btnSave 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 btnSave_Click(object sender, EventArgs e)
        {
            NoteWatch noteWatch;

            var rockContext      = new RockContext();
            var noteWatchService = new NoteWatchService(rockContext);
            var noteWatchId      = hfNoteWatchId.Value.AsInteger();

            if (noteWatchId == 0)
            {
                noteWatch = new NoteWatch();
                noteWatchService.Add(noteWatch);
            }
            else
            {
                noteWatch = noteWatchService.Get(noteWatchId);
            }

            noteWatch.NoteTypeId   = ddlNoteType.SelectedValue.AsIntegerOrNull();
            noteWatch.EntityTypeId = etpEntityType.SelectedEntityTypeId;

            if (noteWatch.EntityTypeId.HasValue)
            {
                if (noteWatch.EntityTypeId.Value == EntityTypeCache.GetId <Rock.Model.Person>())
                {
                    noteWatch.EntityId = ppWatchedPerson.PersonId;
                }
                else if (noteWatch.EntityTypeId.Value == EntityTypeCache.GetId <Rock.Model.Group>())
                {
                    noteWatch.EntityId = gpWatchedGroup.GroupId;
                }
                else
                {
                    noteWatch.EntityId = nbWatchedEntityId.Text.AsIntegerOrNull();
                }
            }

            noteWatch.WatcherPersonAliasId = ppWatcherPerson.PersonAliasId;
            noteWatch.WatcherGroupId       = gpWatcherGroup.GroupId;
            noteWatch.IsWatching           = cbIsWatching.Checked;
            noteWatch.AllowOverride        = cbAllowOverride.Checked;

            // see if the Watcher parameters are valid
            if (!noteWatch.IsValidWatcher)
            {
                nbWatcherMustBeSelectWarning.Visible = true;
                return;
            }

            // see if the Watch filters parameters are valid
            if (!noteWatch.IsValidWatchFilter)
            {
                nbWatchFilterMustBeSeletedWarning.Visible = true;
                return;
            }

            if (!noteWatch.IsValid)
            {
                return;
            }

            // See if there is a matching filter that doesn't allow overrides
            if (noteWatch.IsWatching == false)
            {
                if (!noteWatch.IsAbleToUnWatch(rockContext))
                {
                    var nonOverridableNoteWatch = noteWatch.GetNonOverridableNoteWatches(rockContext).FirstOrDefault();
                    if (nonOverridableNoteWatch != null)
                    {
                        string otherNoteWatchLink;
                        if (nonOverridableNoteWatch.IsAuthorized(Rock.Security.Authorization.VIEW, this.CurrentPerson))
                        {
                            var otherNoteWatchWatchPageReference = new Rock.Web.PageReference(this.CurrentPageReference);
                            otherNoteWatchWatchPageReference.QueryString = new System.Collections.Specialized.NameValueCollection(otherNoteWatchWatchPageReference.QueryString);
                            otherNoteWatchWatchPageReference.QueryString["NoteWatchId"] = nonOverridableNoteWatch.Id.ToString();
                            otherNoteWatchLink = string.Format("<a href='{0}'>note watch</a>", otherNoteWatchWatchPageReference.BuildUrl());
                        }
                        else
                        {
                            otherNoteWatchLink = "note watch";
                        }

                        nbUnableToOverride.Text = string.Format(
                            "Unable to set Watching to false. This would override another {0} that doesn't allow overrides.",
                            otherNoteWatchLink);

                        nbUnableToOverride.Visible = true;
                        return;
                    }
                }
            }

            // see if the NoteType allows following
            if (noteWatch.NoteTypeId.HasValue)
            {
                var noteTypeCache = NoteTypeCache.Get(noteWatch.NoteTypeId.Value);
                if (noteTypeCache != null)
                {
                    if (noteTypeCache.AllowsWatching == false)
                    {
                        nbNoteTypeWarning.Visible = true;
                        return;
                    }
                }
            }

            rockContext.SaveChanges();
            NavigateToNoteWatchParentPage();
        }