/// <summary>
        /// Rs the filter_ display filter value.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        void rFilter_DisplayFilterValue( object sender, GridFilter.DisplayFilterValueArgs e )
        {
            switch ( e.Key )
            {
                case "Approval Status":

                    int approvalStatusValue = e.Value.AsInteger( false ) ?? Rock.Constants.All.Id;
                    if ( approvalStatusValue != Rock.Constants.All.Id )
                    {
                        e.Value = e.Value.ConvertToEnum<MarketingCampaignAdStatus>().ConvertToString();
                    }
                    else
                    {
                        e.Value = Rock.Constants.All.Text;
                    }

                    break;

                case "Priority Range":
                case "Date Range":

                    string[] values = e.Value.Split( new char[] { ',' }, StringSplitOptions.None );
                    if ( values.Length == 2 )
                    {
                        if ( string.IsNullOrWhiteSpace( values[0] ) && string.IsNullOrWhiteSpace( values[1] ) )
                        {
                            e.Value = Rock.Constants.All.Text;
                        }
                        else if ( string.IsNullOrWhiteSpace( values[0] ) )
                        {
                            e.Value = "less than " + values[1];
                        }
                        else if ( string.IsNullOrWhiteSpace( values[1] ) )
                        {
                            e.Value = "greater than " + values[0];
                        }
                        else
                        {
                            e.Value = string.Format( "{0} to {1}", values[0], values[1] );
                        }
                    }

                    break;

                case "Ad Type":

                    var adType = new MarketingCampaignAdTypeService( new RockContext() ).Get( e.Value.AsInteger() ?? 0 );
                    if ( adType != null )
                    {
                        e.Value = adType.Name;
                    }
                    else
                    {
                        e.Value = Rock.Constants.All.Text;
                    }

                    break;

            }
        }
Ejemplo n.º 2
0
        /// <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>
        /// Loads the drop downs.
        /// </summary>
        private void LoadDropDowns()
        {
            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();
            var adtypes = marketingCampaignAdTypeService.Queryable().OrderBy(a => a.Name).ToList();

            ddlMarketingCampaignAdType.DataSource = adtypes;
            ddlMarketingCampaignAdType.DataBind();
        }
Ejemplo n.º 4
0
        /// <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].AsInteger(false);
                    pPriorityRange.UpperValue = upperLowerValues[1].AsInteger(false);
                }
            }

            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();
            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>
        /// Shows the edit.
        /// </summary>
        /// <param name="marketingCampaignAdTypeId">The marketing campaign ad type id.</param>
        protected void ShowEdit(int marketingCampaignAdTypeId)
        {
            pnlList.Visible    = false;
            pnlDetails.Visible = true;

            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();
            MarketingCampaignAdType        marketingCampaignAdType        = marketingCampaignAdTypeService.Get(marketingCampaignAdTypeId);
            bool readOnly = false;

            AttributesState = new List <Attribute>().ToDto();

            if (marketingCampaignAdType != null)
            {
                hfMarketingCampaignAdTypeId.Value = marketingCampaignAdType.Id.ToString();
                tbName.Text = marketingCampaignAdType.Name;
                ddlDateRangeType.SelectedValue = ((int)marketingCampaignAdType.DateRangeType).ToString();

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

                AttributesState = qry.ToList().ToDto();

                readOnly = marketingCampaignAdType.IsSystem;

                if (marketingCampaignAdType.IsSystem)
                {
                    lActionTitle.Text = ActionTitle.View(MarketingCampaignAdType.FriendlyTypeName);
                    btnCancel.Text    = "Close";
                }
                else
                {
                    lActionTitle.Text = ActionTitle.Edit(MarketingCampaignAdType.FriendlyTypeName);
                    btnCancel.Text    = "Cancel";
                }
            }
            else
            {
                lActionTitle.Text = ActionTitle.Add(MarketingCampaignAdType.FriendlyTypeName);

                hfMarketingCampaignAdTypeId.Value = 0.ToString();
                tbName.Text = string.Empty;
            }

            iconIsSystem.Visible = readOnly;
            btnSave.Visible      = !readOnly;

            BindMarketingCampaignAdAttributeTypeGrid();
        }
        /// <summary>
        /// Operations to be performed during the upgrade process.
        /// </summary>
        public override void Up()
        {
            // MarketingAdType nonspecific attributes
            DeleteAttribute("763175ED-A67F-415E-8843-D2C50EF12B33");
            DeleteAttribute("58C196E9-A731-40BB-A015-8A63C98B6733");
            DeleteAttribute("F090D30A-5BCA-4A73-A42F-54744E78A8F2");
            DeleteAttribute("C5C19024-25EE-4A9A-9DCC-BFB148C84B8E");

            // add memofield (for Summary Text, etc)
            Sql(@"
INSERT INTO [FieldType]
           ([IsSystem]
           ,[Name]
           ,[Description]
           ,[Assembly]
           ,[Class]
           ,[Guid])
     VALUES
           (1
           ,'Memo'
           ,'A Memo Field'
           ,'Rock'
           ,'Rock.Field.Types.MemoField'
           ,'C28C7BF3-A552-4D77-9408-DEDCF760CED0')
");

            // create adtype specific attributes for bulletin and website adtypes
            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();
            int bulletinAdTypeId = marketingCampaignAdTypeService.Get(new Guid("3ED7FECC-BCA3-4014-A55E-93FF0D2263C9")).Id;

            AddEntityAttribute("Rock.Cms.MarketingCampaignAd", "C28C7BF3-A552-4D77-9408-DEDCF760CED0", "MarketingCampaignAdTypeId",
                               bulletinAdTypeId.ToString(), "Summary Text", "", "", 0, "", "D06D03C1-0FB8-488B-A087-56C204B15B3D");

            int websiteAdTypeId = marketingCampaignAdTypeService.Get(new Guid("851745BF-CA38-46DC-B2E4-84F4C5990F54")).Id;

            AddEntityAttribute("Rock.Cms.MarketingCampaignAd", "C28C7BF3-A552-4D77-9408-DEDCF760CED0", "MarketingCampaignAdTypeId",
                               websiteAdTypeId.ToString(), "Summary Text", "", "", 0, "", "2D594CB6-F438-4038-8A64-8530525ABDB5");

            AddEntityAttribute("Rock.Cms.MarketingCampaignAd", "DD7ED4C0-A9E0-434F-ACFE-BD4F56B043DF", "MarketingCampaignAdTypeId",
                               websiteAdTypeId.ToString(), "Detail Html", "", "", 0, "", "D469687D-AA31-4D67-AD49-6F027B3DC7D7");

            AddEntityAttribute("Rock.Cms.MarketingCampaignAd", "97F8157D-A8C8-4AB3-96A2-9CB2A9049E6D", "MarketingCampaignAdTypeId",
                               websiteAdTypeId.ToString(), "Summary Image", "", "", 0, "", "5CD67DD2-71BA-495E-B3DB-45F39FEC0173");

            AddEntityAttribute("Rock.Cms.MarketingCampaignAd", "97F8157D-A8C8-4AB3-96A2-9CB2A9049E6D", "MarketingCampaignAdTypeId",
                               websiteAdTypeId.ToString(), "Detail Image", "", "", 0, "", "0F0784F5-35B1-457C-94D8-9CA1EA9A5948");

            // Delete FacebookAdType example
            Sql("DELETE FROM [MarketingCampaignAdType] WHERE [Guid] = '4D23C6C8-7457-483C-B749-330D63F3B171'");
        }
        /// <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>
        /// Handles the Delete event of the gMarketingCampaignAdType 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 gMarketingCampaignAdType_Delete(object sender, RowEventArgs e)
        {
            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();
            int marketingCampaignAdTypeId = (int)e.RowKeyValue;
            MarketingCampaignAdType marketingCampaignAdType = marketingCampaignAdTypeService.Get(marketingCampaignAdTypeId);

            string errorMessage;

            if (!marketingCampaignAdTypeService.CanDelete(marketingCampaignAdType, out errorMessage))
            {
                mdGridWarning.Show(errorMessage, ModalAlertType.Information);
                return;
            }

            if (CurrentBlock != null)
            {
                marketingCampaignAdTypeService.Delete(marketingCampaignAdType, CurrentPersonId);
                marketingCampaignAdTypeService.Save(marketingCampaignAdType, CurrentPersonId);
            }

            BindGrid();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Handles the Delete event of the gMarketingCampaignAdType 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 gMarketingCampaignAdType_Delete(object sender, RowEventArgs e)
        {
            var rockContext = new RockContext();
            MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService(rockContext);
            MarketingCampaignAdType        marketingCampaignAdType        = marketingCampaignAdTypeService.Get((int)e.RowKeyValue);

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

                marketingCampaignAdTypeService.Delete(marketingCampaignAdType);

                rockContext.SaveChanges();
            }

            BindGrid();
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Handles the Delete event of the gMarketingCampaignAdType 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 gMarketingCampaignAdType_Delete(object sender, RowEventArgs e)
        {
            RockTransactionScope.WrapTransaction(() =>
            {
                MarketingCampaignAdTypeService marketingCampaignAdTypeService = new MarketingCampaignAdTypeService();
                MarketingCampaignAdType marketingCampaignAdType = marketingCampaignAdTypeService.Get((int)e.RowKeyValue);

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

                    marketingCampaignAdTypeService.Delete(marketingCampaignAdType, CurrentPersonId);
                    marketingCampaignAdTypeService.Save(marketingCampaignAdType, CurrentPersonId);
                }
            });

            BindGrid();
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Loads the attribute controls.
        /// </summary>
        /// <param name="marketingCampaignAd">The marketing campaign ad.</param>
        /// <param name="createControls">if set to <c>true</c> [create controls].</param>
        /// <param name="setValues">if set to <c>true</c> [set values].</param>
        private void LoadAdAttributes(Rock.Attribute.IHasAttributes marketingCampaignAd, bool createControls, bool setValues)
        {
            if (string.IsNullOrWhiteSpace(ddlMarketingCampaignAdType.SelectedValue))
            {
                return;
            }

            int marketingAdTypeId = int.Parse(ddlMarketingCampaignAdType.SelectedValue);

            MarketingCampaignAdType marketingCampaignAdType = new MarketingCampaignAdTypeService(new RockContext()).Get(marketingAdTypeId);

            drpAdDateRange.Visible = marketingCampaignAdType.DateRangeType.Equals(DateRangeTypeEnum.DateRange);
            dpAdSingleDate.Visible = marketingCampaignAdType.DateRangeType.Equals(DateRangeTypeEnum.SingleDate);

            List <Rock.Web.Cache.AttributeCache> attributesForAdType = GetAttributesForAdType(marketingAdTypeId);

            marketingCampaignAd.Attributes      = marketingCampaignAd.Attributes ?? new Dictionary <string, Rock.Web.Cache.AttributeCache>();
            marketingCampaignAd.AttributeValues = marketingCampaignAd.AttributeValues ?? new Dictionary <string, List <AttributeValue> >();
            foreach (var attribute in attributesForAdType)
            {
                marketingCampaignAd.Attributes[attribute.Key] = attribute;
                if (marketingCampaignAd.AttributeValues.Count(v => v.Key.Equals(attribute.Key)) == 0)
                {
                    List <AttributeValue> attributeValues = new List <AttributeValue>();
                    attributeValues.Add(new AttributeValue {
                        Value = attribute.DefaultValue
                    });
                    marketingCampaignAd.AttributeValues.Add(attribute.Key, attributeValues);
                }
            }

            if (createControls)
            {
                phAttributes.Controls.Clear();
                Rock.Attribute.Helper.AddEditControls(marketingCampaignAd, phAttributes, setValues);
            }
        }
        /// <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;
        }
        /// <summary>
        /// Shows the detail.
        /// </summary>
        /// <param name="itemKey">The item key.</param>
        /// <param name="itemKeyValue">The item key value.</param>
        public void ShowDetail(string itemKey, int itemKeyValue)
        {
            if (!itemKey.Equals("marketingCampaignAdTypeId"))
            {
                return;
            }

            pnlDetails.Visible = true;
            MarketingCampaignAdType marketingCampaignAdType = null;

            if (!itemKeyValue.Equals(0))
            {
                marketingCampaignAdType = new MarketingCampaignAdTypeService().Get(itemKeyValue);
                lActionTitle.Text       = ActionTitle.Edit(MarketingCampaignAdType.FriendlyTypeName).FormatAsHtmlTitle();
            }
            else
            {
                marketingCampaignAdType = new MarketingCampaignAdType {
                    Id = 0
                };
                lActionTitle.Text = ActionTitle.Add(MarketingCampaignAdType.FriendlyTypeName).FormatAsHtmlTitle();
            }

            LoadDropDowns();

            // load data into UI controls
            AttributesState = new ViewStateList <Attribute>();

            hfMarketingCampaignAdTypeId.Value = marketingCampaignAdType.Id.ToString();
            tbName.Text = marketingCampaignAdType.Name;
            ddlDateRangeType.SetValue((int)marketingCampaignAdType.DateRangeType);

            AttributeService attributeService = new AttributeService();

            string qualifierValue = marketingCampaignAdType.Id.ToString();
            var    qry            = attributeService.GetByEntityTypeId(new MarketingCampaignAd().TypeId).AsQueryable()
                                    .Where(a => a.EntityTypeQualifierColumn.Equals("MarketingCampaignAdTypeId", StringComparison.OrdinalIgnoreCase) &&
                                           a.EntityTypeQualifierValue.Equals(qualifierValue));

            AttributesState.AddAll(qry.ToList());
            BindMarketingCampaignAdAttributeTypeGrid();

            // render UI based on Authorized and IsSystem
            bool readOnly = false;

            nbEditModeMessage.Text = string.Empty;
            if (!IsUserAuthorized("Edit"))
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlyEditActionNotAllowed(MarketingCampaignAdType.FriendlyTypeName);
            }

            if (marketingCampaignAdType.IsSystem)
            {
                readOnly = true;
                nbEditModeMessage.Text = EditModeMessage.ReadOnlySystem(MarketingCampaignAdType.FriendlyTypeName);
            }

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

            tbName.ReadOnly          = readOnly;
            ddlDateRangeType.Enabled = !readOnly;
            gMarketingCampaignAdAttributeTypes.Enabled = !readOnly;

            btnSave.Visible = !readOnly;
        }
        /// <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)
        {
            using (new UnitOfWorkScope())
            {
                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);

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

                RockTransactionScope.WrapTransaction(() =>
                {
                    AttributeService attributeService = new AttributeService();
                    AttributeQualifierService attributeQualifierService = new AttributeQualifierService();
                    CategoryService categoryService = new CategoryService();

                    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);

                    var entityTypeId       = EntityTypeCache.Read(typeof(MarketingCampaignAd)).Id;
                    string qualifierColumn = "MarketingCampaignAdTypeId";
                    string qualifierValue  = marketingCampaignAdType.Id.ToString();

                    // Get the existing attributes for this entity type and qualifier value
                    var attributes = attributeService.Get(entityTypeId, qualifierColumn, qualifierValue);

                    // Delete any of those attributes that were removed in the UI
                    var selectedAttributeGuids = AttributesState.Select(a => a.Guid);
                    foreach (var attr in attributes.Where(a => !selectedAttributeGuids.Contains(a.Guid)))
                    {
                        Rock.Web.Cache.AttributeCache.Flush(attr.Id);

                        attributeService.Delete(attr, CurrentPersonId);
                        attributeService.Save(attr, CurrentPersonId);
                    }

                    // Update the Attributes that were assigned in the UI
                    foreach (var attributeState in AttributesState)
                    {
                        Rock.Attribute.Helper.SaveAttributeEdits(attributeState, attributeService, attributeQualifierService, categoryService,
                                                                 entityTypeId, qualifierColumn, qualifierValue, CurrentPersonId);
                    }
                });
            }

            NavigateToParentPage();
        }
Ejemplo n.º 15
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)
        {
            using (new UnitOfWorkScope())
            {
                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);

                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();
                    string qualifierValue             = marketingCampaignAdType.Id.ToString();
                    int typeId = new MarketingCampaignAd().TypeId;
                    var qry    = attributeService.GetByEntityTypeId(typeId).AsQueryable()
                                 .Where(a => a.EntityTypeQualifierColumn == "MarketingCampaignAdTypeId" &&
                                        a.EntityTypeQualifierValue.Equals(qualifierValue));

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

                    deletedAttributes.ToList().ForEach(a =>
                    {
                        var attr = attributeService.Get(a.Guid);
                        if (attr != null)
                        {
                            Rock.Web.Cache.AttributeCache.Flush(attr.Id);
                            attributeService.Delete(attr, CurrentPersonId);
                            attributeService.Save(attr, CurrentPersonId);
                        }
                    });

                    // add/update the AdTypes that are assigned in the UI
                    foreach (var attributeState in AttributesState)
                    {
                        // remove old qualifiers in case they changed
                        var qualifierService = new AttributeQualifierService();
                        foreach (var oldQualifier in qualifierService.GetByAttributeId(attributeState.Id).ToList())
                        {
                            qualifierService.Delete(oldQualifier, CurrentPersonId);
                            qualifierService.Save(oldQualifier, CurrentPersonId);
                        }

                        Attribute attribute = qry.FirstOrDefault(a => a.Guid.Equals(attributeState.Guid));
                        if (attribute == null)
                        {
                            attribute = attributeState.Clone() as Rock.Model.Attribute;
                            attributeService.Add(attribute, CurrentPersonId);
                        }
                        else
                        {
                            attributeState.Id = attribute.Id;
                            attribute.FromDictionary(attributeState.ToDictionary());

                            foreach (var qualifier in attributeState.AttributeQualifiers)
                            {
                                attribute.AttributeQualifiers.Add(qualifier.Clone() as AttributeQualifier);
                            }
                        }

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