/// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();
            SortProperty sortProperty = gMarketingCampaignAdType.SortProperty;

            if ( sortProperty != null )
            {
                gMarketingCampaignAdType.DataSource = marketingCampaignAdTypeService.Queryable().Sort( sortProperty ).ToList();
            }
            else
            {
                gMarketingCampaignAdType.DataSource = marketingCampaignAdTypeService.Queryable().OrderBy( p => p.Name ).ToList();
            }

            gMarketingCampaignAdType.DataBind();
        }
 /// <summary>
 /// Loads the drop downs.
 /// </summary>
 private void LoadDropDowns()
 {
     MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService( new RockContext() );
     var adtypes = marketingCampaignAdTypeService.Queryable().OrderBy( a => a.Name ).ToList();
     ddlMarketingCampaignAdType.DataSource = adtypes;
     ddlMarketingCampaignAdType.DataBind();
 }
        /// <summary>
        /// Loads the drop downs.
        /// </summary>
        private void LoadDropDowns()
        {
            // Controls on Main Campaign Panel
            GroupService groupService = new GroupService();
            List<Group> groups = groupService.Queryable().Where( a => a.GroupType.Guid.Equals( Rock.SystemGuid.GroupType.GROUPTYPE_EVENTATTENDEES ) ).OrderBy( a => a.Name ).ToList();
            groups.Insert( 0, new Group { Id = None.Id, Name = None.Text } );
            ddlEventGroup.DataSource = groups;
            ddlEventGroup.DataBind();

            PersonService personService = new PersonService();
            List<Person> persons = personService.Queryable().OrderBy( a => a.NickName ).ThenBy( a => a.LastName ).ToList();
            persons.Insert( 0, new Person { Id = None.Id, GivenName = None.Text } );
            ddlContactPerson.DataSource = persons;
            ddlContactPerson.DataBind();

            CampusService campusService = new CampusService();

            cpCampuses.Campuses = campusService.Queryable().OrderBy( a => a.Name ).ToList(); ;

            // Controls on Ad Child Panel
            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();
            var adtypes = marketingCampaignAdTypeService.Queryable().OrderBy( a => a.Name ).ToList();
            ddlMarketingCampaignAdType.DataSource = adtypes;
            ddlMarketingCampaignAdType.DataBind();
        }
        /// <summary>
        /// Binds the filter.
        /// </summary>
        private void BindFilter()
        {
            ddlApprovalStatus.BindToEnum( typeof( MarketingCampaignAdStatus ) );
            ddlApprovalStatus.Items.Insert( 0, Rock.Constants.All.ListItem );

            ddlApprovalStatus.SetValue( rFilter.GetUserPreference( "Approval Status" ) );

            string priorityRangeValues = rFilter.GetUserPreference( "Priority Range" );

            if ( !string.IsNullOrWhiteSpace( priorityRangeValues ) )
            {
                string[] upperLowerValues = priorityRangeValues.Split( new char[] { ',' }, StringSplitOptions.None );

                if ( upperLowerValues.Length == 2 )
                {
                    pPriorityRange.LowerValue = upperLowerValues[0].AsIntegerOrNull();
                    pPriorityRange.UpperValue = upperLowerValues[1].AsIntegerOrNull();
                }
            }

            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService( new RockContext() );
            var adTypeList = marketingCampaignAdTypeService.Queryable().Select( a => new { a.Id, a.Name } ).OrderBy( a => a.Name ).ToList();
            foreach ( var adType in adTypeList )
            {
                ddlAdType.Items.Add( new ListItem( adType.Name, adType.Id.ToString() ) );
            }

            ddlAdType.Items.Insert( 0, Rock.Constants.All.ListItem );

            ddlAdType.SetValue( rFilter.GetUserPreference( "Ad Type" ) );

            string dateRangeValues = rFilter.GetUserPreference( "Date Range" );
            if ( !string.IsNullOrWhiteSpace( dateRangeValues ) )
            {
                string[] upperLowerValues = dateRangeValues.Split( new char[] { ',' }, StringSplitOptions.None );

                if ( upperLowerValues.Length == 2 )
                {
                    string lowerValue = upperLowerValues[0];
                    if ( !string.IsNullOrWhiteSpace( lowerValue ) )
                    {
                        pDateRange.LowerValue = DateTime.Parse( lowerValue );
                    }

                    string upperValue = upperLowerValues[1];
                    if ( !string.IsNullOrWhiteSpace( upperValue ) )
                    {
                        pDateRange.UpperValue = DateTime.Parse( upperValue );
                    }

                }
            }
        }
        /// <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 )
        {
            MarketingCampaignAdType marketingCampaignAdType;
            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();

            int marketingCampaignAdTypeId = int.Parse( hfMarketingCampaignAdTypeId.Value );

            if ( marketingCampaignAdTypeId == 0 )
            {
                marketingCampaignAdType = new MarketingCampaignAdType();
                marketingCampaignAdTypeService.Add( marketingCampaignAdType, CurrentPersonId );
            }
            else
            {
                marketingCampaignAdType = marketingCampaignAdTypeService.Get( marketingCampaignAdTypeId );
            }

            marketingCampaignAdType.Name = tbName.Text;
            marketingCampaignAdType.DateRangeType = (DateRangeTypeEnum)int.Parse( ddlDateRangeType.SelectedValue );

            // check for duplicates
            if ( marketingCampaignAdTypeService.Queryable().Count( a => a.Name.Equals( marketingCampaignAdType.Name, StringComparison.OrdinalIgnoreCase ) && !a.Id.Equals( marketingCampaignAdType.Id ) ) > 0 )
            {
                tbName.ShowErrorMessage( WarningMessage.DuplicateFoundMessage( "name", MarketingCampaignAdType.FriendlyTypeName ) );
                return;
            }

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

            RockTransactionScope.WrapTransaction( () =>
            {
                marketingCampaignAdTypeService.Save( marketingCampaignAdType, CurrentPersonId );

                // get it back to make sure we have a good Id for it for the Attributes
                marketingCampaignAdType = marketingCampaignAdTypeService.Get( marketingCampaignAdType.Guid );

                // delete AdTypeAttributes that are no longer configured in the UI
                AttributeService attributeService = new AttributeService();
                var qry = attributeService.GetByEntityTypeId( new MarketingCampaignAd().TypeId ).AsQueryable()
                    .Where( a => a.EntityTypeQualifierColumn.Equals( "MarketingCampaignAdTypeId", StringComparison.OrdinalIgnoreCase )
                    && a.EntityTypeQualifierValue.Equals( marketingCampaignAdType.Id.ToString() ) );

                var deletedAttributes = from attr in qry
                                        where !( from d in AttributesState
                                                 select d.Guid ).Contains( attr.Guid )
                                        select attr;

                deletedAttributes.ToList().ForEach( a =>
                    {
                        var attr = attributeService.Get( a.Guid );
                        attributeService.Delete( attr, CurrentPersonId );
                        attributeService.Save( attr, CurrentPersonId );
                    } );

                // add/update the AdTypes that are assigned in the UI
                foreach ( var attributeState in AttributesState )
                {
                    Attribute attribute = qry.FirstOrDefault( a => a.Guid.Equals( attributeState.Guid ) );
                    if ( attribute == null )
                    {
                        attribute = attributeState.ToModel();
                        attributeService.Add( attribute, CurrentPersonId );
                    }
                    else
                    {
                        attributeState.Id = attribute.Id;
                        attributeState.CopyToModel( attribute );
                    }

                    attribute.EntityTypeQualifierColumn = "MarketingCampaignAdTypeId";
                    attribute.EntityTypeQualifierValue = marketingCampaignAdType.Id.ToString();
                    attribute.EntityTypeId = Rock.Web.Cache.EntityTypeCache.Read( new MarketingCampaignAd().TypeName ).Id;
                    attributeService.Save( attribute, CurrentPersonId );
                }
            } );

            BindGrid();
            pnlDetails.Visible = false;
            pnlList.Visible = true;
        }