Пример #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();
                    }
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NoteWatchPersonToNotify" /> class.
 /// </summary>
 /// <param name="person">The person.</param>
 /// <param name="note">The note.</param>
 /// <param name="noteWatch">The note watch.</param>
 public NoteWatchPersonToNotify(Person person, Note note, NoteWatch noteWatch)
 {
     this.Person    = person;
     this.Note      = note;
     this.NoteWatch = noteWatch;
 }
Пример #3
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);
            }
        }
Пример #4
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();
        }