public IHttpActionResult AddReferralAgencyToOrganisation(Guid id, ReferralAgency agency) {
      if (!ModelState.IsValid)
        return badModelState();

      try {
        var allAgencies = repo_.AddReferralAgency(id, agency);

        return Ok(allAgencies);
      } catch (Exception e) {
        return BadRequest(e.Message);
      }
    } // AddReferralAgencyToOrganisation
    } // FetchStaffMembers

    //////////////////////
    public IList<ReferralAgency> AddReferralAgency(Guid orgId, ReferralAgency agency) {
      agency.Id = Guid.NewGuid();
      agency.Address.Id = Guid.NewGuid();

      var pod = POD(orgId);
      if (pod.ReferralAgencies == null)
        pod.ReferralAgencies = new List<ReferralAgencyData>();
      pod.ReferralAgencies.Add(agency.referralAgencyData());

      Commit(pod);

      return FetchReferralAgencies(orgId);
    } // AddReferralAgency
 public void add(Guid riskId, ReferralAgency referral)
 {
     if (referrals_.ContainsKey(riskId))
     {
         referrals_[riskId].Add(referral);
     }
     else
     {
         var l = new List <ReferralAgency>();
         l.Add(referral);
         referrals_[riskId] = l;
     }
 }
    } // AddReferralAgency

    public IList<ReferralAgency> UpdateReferralAgency(Guid orgId, ReferralAgency agency) {
      var pod = POD(orgId);
      if (pod.ReferralAgencies == null)
        throw new Exception("Organisation has no referral agencies");
      var currentAgency = pod.ReferralAgencies.Find(p => p.Id == agency.Id);
      if (currentAgency == null)
        throw new Exception("Referral Agency does not belong to the organisation");

      currentAgency.CopyFrom(agency.referralAgencyData());

      Commit(currentAgency);

      return FetchReferralAgencies(orgId);
    } // UpdateProject
    public IHttpActionResult UpdateReferralAgency(Guid id, Guid refId, ReferralAgency ra) {
      if (!ModelState.IsValid)
        return badModelState();

      if (refId != ra.Id)
        return BadRequest("Bad Referral Agency Id");

      try {
        var allAgencies = repo_.UpdateReferralAgency(id, ra);

        return Ok(allAgencies);
      } catch (Exception e) {
        return BadRequest(e.Message);
      }
    } // UpdateReferralAgency
        public override List<BreadCrumb> GetBreadCrumbs( Rock.Web.PageReference pageReference )
        {
            var breadCrumbs = new List<BreadCrumb>();

            string crumbName = ActionTitle.Add( ReferralAgency.FriendlyTypeName );

            int? referralAgencyId = PageParameter( "referralAgencyId" ).AsIntegerOrNull();
            if ( referralAgencyId.HasValue )
            {
                _referralAgency = new ReferralAgencyService( new SampleProjectContext() ).Get( referralAgencyId.Value );
                if ( _referralAgency != null )
                {
                    crumbName = _referralAgency.Name;
                }
            }

            breadCrumbs.Add( new BreadCrumb( crumbName, pageReference ) );

            return breadCrumbs;
        }
        public override List <BreadCrumb> GetBreadCrumbs(Rock.Web.PageReference pageReference)
        {
            var breadCrumbs = new List <BreadCrumb>();

            string crumbName = ActionTitle.Add(ReferralAgency.FriendlyTypeName);

            int?referralAgencyId = PageParameter("referralAgencyId").AsInteger(false);

            if (referralAgencyId.HasValue)
            {
                _referralAgency = new ReferralAgencyService(new SampleProjectContext()).Get(referralAgencyId.Value);
                if (_referralAgency != null)
                {
                    crumbName = _referralAgency.Name;
                }
            }

            breadCrumbs.Add(new BreadCrumb(crumbName, pageReference));

            return(breadCrumbs);
        }
        /// <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)
        {
            ReferralAgency referralAgency;
            var            dataContext = new SampleProjectContext();
            var            service     = new ReferralAgencyService(dataContext);

            int campusId = int.Parse(hfReferralAgencyId.Value);

            if (campusId == 0)
            {
                referralAgency = new ReferralAgency();
                service.Add(referralAgency);
            }
            else
            {
                referralAgency = service.Get(campusId);
            }

            referralAgency.Name              = tbName.Text;
            referralAgency.Description       = tbDescription.Text;
            referralAgency.CampusId          = cpCampus.SelectedCampusId;
            referralAgency.AgencyTypeValueId = ddlAgencyType.SelectedValueAsId();
            referralAgency.ContactName       = tbContactName.Text;
            referralAgency.PhoneNumber       = tbPhoneNumber.Text;
            referralAgency.Website           = tbWebsite.Text;

            if (!referralAgency.IsValid || !Page.IsValid)
            {
                // Controls will render the error messages
                return;
            }

            dataContext.SaveChanges();

            NavigateToParentPage();
        }
        /// <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 )
        {
            ReferralAgency referralAgency;
            var dataContext = new SampleProjectContext();
            var service = new ReferralAgencyService( dataContext );

            int referralAgencyId = int.Parse( hfReferralAgencyId.Value );

            if ( referralAgencyId == 0 )
            {
                referralAgency = new ReferralAgency();
                service.Add( referralAgency );
            }
            else
            {
                referralAgency = service.Get( referralAgencyId );
            }

            referralAgency.Name = tbName.Text;
            referralAgency.Description = tbDescription.Text;
            referralAgency.CampusId = cpCampus.SelectedCampusId;
            referralAgency.AgencyTypeValueId = ddlAgencyType.SelectedValueAsId();
            referralAgency.ContactName = tbContactName.Text;
            referralAgency.PhoneNumber = tbPhoneNumber.Text;
            referralAgency.Website = tbWebsite.Text;

            if ( !referralAgency.IsValid || !Page.IsValid )
            {
                // Controls will render the error messages
                return;
            }

            dataContext.SaveChanges();

            NavigateToParentPage();
        }
        private void ShowDetail()
        {
            pnlDetails.Visible = true;

            int? referralAgencyId = PageParameter( "referralAgencyId" ).AsIntegerOrNull();
            int? campusId = PageParameter( "campusId" ).AsIntegerOrNull();
            int? agencyTypeValueId = PageParameter( "agencyTypeId" ).AsIntegerOrNull();

            ReferralAgency referralAgency = null;
            if (referralAgencyId.HasValue)
            {
                referralAgency = _referralAgency ?? new ReferralAgencyService( new SampleProjectContext() ).Get( referralAgencyId.Value );
            }

            if (referralAgency != null)
            {
                RockPage.PageTitle = referralAgency.Name;
                lActionTitle.Text = ActionTitle.Edit( referralAgency.Name ).FormatAsHtmlTitle();
            }
            else
            {
                referralAgency = new ReferralAgency { Id = 0, CampusId = campusId, AgencyTypeValueId = agencyTypeValueId };
                RockPage.PageTitle = ActionTitle.Add( ReferralAgency.FriendlyTypeName );
                lActionTitle.Text = ActionTitle.Add( ReferralAgency.FriendlyTypeName ).FormatAsHtmlTitle();
            }

            hfReferralAgencyId.Value = referralAgency.Id.ToString();
            tbName.Text = referralAgency.Name;
            tbDescription.Text = referralAgency.Description;
            cpCampus.SelectedCampusId = referralAgency.CampusId;
            ddlAgencyType.SetValue( referralAgency.AgencyTypeValueId );
            tbContactName.Text = referralAgency.ContactName;
            tbPhoneNumber.Text = referralAgency.PhoneNumber;
            tbWebsite.Text = referralAgency.Website;

            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;
            if (!IsUserAuthorized(Rock.Security.Authorization.EDIT))
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed( ReferralAgency.FriendlyTypeName );
            }

            if (readOnly)
            {
                lActionTitle.Text = ActionTitle.View( ReferralAgency.FriendlyTypeName );
                btnCancel.Text = "Close";
            }

            tbName.ReadOnly = readOnly;
            tbDescription.ReadOnly = readOnly;
            tbContactName.ReadOnly = readOnly;
            tbPhoneNumber.ReadOnly = readOnly;
            tbWebsite.ReadOnly = readOnly;

            btnSave.Visible = !readOnly;
        }
        private void ShowDetail()
        {
            pnlDetails.Visible = true;

            int?referralAgencyId  = PageParameter("referralAgencyId").AsInteger(false);
            int?campusId          = PageParameter("campusId").AsInteger(false);
            int?agencyTypeValueId = PageParameter("agencyTypeId").AsInteger(false);

            ReferralAgency referralAgency = null;

            if (referralAgencyId.HasValue)
            {
                referralAgency = _referralAgency ?? new ReferralAgencyService(new SampleProjectContext()).Get(referralAgencyId.Value);
            }

            if (referralAgency != null)
            {
                RockPage.PageTitle = referralAgency.Name;
                lActionTitle.Text  = ActionTitle.Edit(referralAgency.Name).FormatAsHtmlTitle();
            }
            else
            {
                referralAgency = new ReferralAgency {
                    Id = 0, CampusId = campusId, AgencyTypeValueId = agencyTypeValueId
                };
                RockPage.PageTitle = ActionTitle.Add(ReferralAgency.FriendlyTypeName);
                lActionTitle.Text  = ActionTitle.Add(ReferralAgency.FriendlyTypeName).FormatAsHtmlTitle();
            }

            hfReferralAgencyId.Value = referralAgency.Id.ToString();
            tbName.Text               = referralAgency.Name;
            tbDescription.Text        = referralAgency.Description;
            cpCampus.SelectedCampusId = referralAgency.CampusId;
            ddlAgencyType.SetValue(referralAgency.AgencyTypeValueId);
            tbContactName.Text = referralAgency.ContactName;
            tbPhoneNumber.Text = referralAgency.PhoneNumber;
            tbWebsite.Text     = referralAgency.Website;

            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;
            if (!IsUserAuthorized(Rock.Security.Authorization.EDIT))
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(ReferralAgency.FriendlyTypeName);
            }

            if (readOnly)
            {
                lActionTitle.Text = ActionTitle.View(ReferralAgency.FriendlyTypeName);
                btnCancel.Text    = "Close";
            }

            tbName.ReadOnly        = readOnly;
            tbDescription.ReadOnly = readOnly;
            tbContactName.ReadOnly = readOnly;
            tbPhoneNumber.ReadOnly = readOnly;
            tbWebsite.ReadOnly     = readOnly;

            btnSave.Visible = !readOnly;
        }