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>
        /// 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();
        }