Exemplo n.º 1
0
        /// <summary>
        /// Binds the marketing campaign ads grid.
        /// </summary>
        private void BindMarketingCampaignAdsGrid()
        {
            MarketingCampaignAdService marketingCampaignAdService = new MarketingCampaignAdService();
            int marketingCampaignId = int.Parse(hfMarketingCampaignId.Value);
            var qry = marketingCampaignAdService.Queryable().Where(a => a.MarketingCampaignId.Equals(marketingCampaignId));

            gMarketingCampaignAds.DataSource = qry.OrderBy(a => a.StartDate).ThenBy(a => a.Priority).ThenBy(a => a.MarketingCampaignAdType.Name).ToList();
            gMarketingCampaignAds.DataBind();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Renders the Ads using Liquid.
        /// </summary>
        private void Render()
        {
            var rockContext = new RockContext();

            MarketingCampaignAdService marketingCampaignAdService = new MarketingCampaignAdService( rockContext );
            var qry = marketingCampaignAdService.Queryable();

            // limit to date range
            DateTime currentDateTime = RockDateTime.Now.Date;
            qry = qry.Where( a => ( a.StartDate <= currentDateTime ) && ( currentDateTime <= a.EndDate ) );

            // limit to approved
            qry = qry.Where( a => a.MarketingCampaignAdStatus == MarketingCampaignAdStatus.Approved );

            /* Block Attributes */

            // Audience
            string audience = GetAttributeValue( "Audience" );
            if ( !string.IsNullOrWhiteSpace( audience ) )
            {
                var idList = new List<int>();
                foreach ( string guid in audience.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ) )
                {
                    var definedValue = DefinedValueCache.Read( new Guid( guid ) );
                    if ( definedValue != null )
                    {
                        idList.Add( definedValue.Id );
                    }
                }
                qry = qry.Where( a => a.MarketingCampaign.MarketingCampaignAudiences.Any( x => idList.Contains( x.AudienceTypeValueId ) ) );
            }

            // AudiencePrimarySecondary
            string audiencePrimarySecondary = GetAttributeValue( "AudiencePrimarySecondary" );
            if ( !string.IsNullOrWhiteSpace( audiencePrimarySecondary ) )
            {
                // 1 = Primary, 2 = Secondary
                List<int> idlist = audiencePrimarySecondary.SplitDelimitedValues().Select( a => int.Parse( a ) ).ToList();

                if ( idlist.Contains( 1 ) && !idlist.Contains( 2 ) )
                {
                    // only show to Primary Audiences
                    qry = qry.Where( a => a.MarketingCampaign.MarketingCampaignAudiences.Any( x => x.IsPrimary == true ) );
                }
                else if ( idlist.Contains( 2 ) && !idlist.Contains( 1 ) )
                {
                    // only show to Secondary Audiences
                    qry = qry.Where( a => a.MarketingCampaign.MarketingCampaignAudiences.Any( x => x.IsPrimary == false ) );
                }
            }

            // Campuses
            string campuses = GetAttributeValue( "Campuses" );
            if ( !string.IsNullOrWhiteSpace( campuses ) )
            {
                List<int> idlist = campuses.SplitDelimitedValues().Select( a => int.Parse( a ) ).ToList();
                qry = qry.Where( a => a.MarketingCampaign.MarketingCampaignCampuses.Any( x => idlist.Contains( x.CampusId ) ) );
            }

            // Ad Types
            string adtypes = GetAttributeValue( "AdTypes" );
            if ( !string.IsNullOrWhiteSpace( adtypes ) )
            {
                List<int> idlist = adtypes.SplitDelimitedValues().Select( a => int.Parse( a ) ).ToList();
                qry = qry.Where( a => idlist.Contains( a.MarketingCampaignAdTypeId ) );
            }

            // Image Types
            string imageTypes = GetAttributeValue( "ImageTypes" );
            List<string> imageTypeFilter = null;
            if ( !string.IsNullOrWhiteSpace( imageTypes ) )
            {
                imageTypeFilter = imageTypes.SplitDelimitedValues().ToList();
            }

            // Campus Context
            Campus campusContext = this.ContextEntity<Campus>();
            if ( campusContext != null )
            {
                // limit to ads that are targeted to the current campus context
                qry = qry.Where( a => a.MarketingCampaign.MarketingCampaignCampuses.Any( x => x.CampusId.Equals( campusContext.Id ) ) );
            }

            // Max Items
            string maxItems = GetAttributeValue( "MaxItems" );
            int? maxAdCount = null;
            if ( !string.IsNullOrWhiteSpace( maxItems ) )
            {
                int parsedCount = 0;
                if ( int.TryParse( maxItems, out parsedCount ) )
                {
                    maxAdCount = parsedCount;
                }
            }

            List<MarketingCampaignAd> marketingCampaignAdList;
            qry = qry.OrderBy( a => a.Priority ).ThenBy( a => a.StartDate ).ThenBy( a => a.MarketingCampaign.Title );
            if ( maxAdCount == null )
            {
                marketingCampaignAdList = qry.ToList();
            }
            else
            {
                marketingCampaignAdList = qry.Take( maxAdCount.Value ).ToList();
            }

            // build dictionary for liquid
            var ads = new List<Dictionary<string, object>>();

            foreach ( var marketingCampaignAd in marketingCampaignAdList )
            {
                var ad = new Dictionary<string, object>();

                // DetailPage
                string detailPageUrl = string.Empty;
                string detailPageGuid = GetAttributeValue( "DetailPage" );
                if ( !string.IsNullOrWhiteSpace( detailPageGuid ) )
                {
                    Rock.Model.Page detailPage = new PageService( rockContext ).Get( new Guid( detailPageGuid ) );
                    if ( detailPage != null )
                    {
                        Dictionary<string, string> queryString = new Dictionary<string, string>();
                        queryString.Add( "ad", marketingCampaignAd.Id.ToString() );
                        detailPageUrl = new PageReference( detailPage.Id, 0, queryString ).BuildUrl();
                    }
                }

                string eventGroupName = marketingCampaignAd.MarketingCampaign.EventGroup != null ? marketingCampaignAd.MarketingCampaign.EventGroup.Name : string.Empty;

                // Marketing Campaign Fields
                ad.Add( "Title", marketingCampaignAd.MarketingCampaign.Title );
                ad.Add( "ContactEmail", marketingCampaignAd.MarketingCampaign.ContactEmail );
                ad.Add( "ContactFullName", marketingCampaignAd.MarketingCampaign.ContactFullName );
                ad.Add( "ContactPhoneNumber", marketingCampaignAd.MarketingCampaign.ContactPhoneNumber );
                ad.Add( "LinkedEvent", eventGroupName );

                // Specific Ad Fields
                ad.Add( "AdType", marketingCampaignAd.MarketingCampaignAdType.Name );
                ad.Add( "StartDate", marketingCampaignAd.StartDate.ToString() );
                ad.Add( "EndDate", marketingCampaignAd.EndDate.ToString() );
                ad.Add( "Priority", marketingCampaignAd.Priority );
                ad.Add( "Url", marketingCampaignAd.Url );
                ad.Add( "DetailPageUrl", detailPageUrl );

                // Ad Attributes
                var attributes = new List<Dictionary<string, object>>();
                ad.Add( "Attributes", attributes );

                marketingCampaignAd.LoadAttributes();
                Rock.Attribute.Helper.AddDisplayControls( marketingCampaignAd, phContent );

                // create image resize width/height from block settings
                Dictionary<string, Rock.Field.ConfigurationValue> imageConfig = new Dictionary<string, Rock.Field.ConfigurationValue>();
                if ( !string.IsNullOrWhiteSpace( GetAttributeValue( "ImageWidth" ) )
                    && Int32.Parse( GetAttributeValue( "ImageWidth" ) ) != Int16.MinValue )
                    imageConfig.Add( "width", new Rock.Field.ConfigurationValue( GetAttributeValue( "ImageWidth" ) ) );

                if ( !string.IsNullOrWhiteSpace( GetAttributeValue( "ImageHeight" ) )
                    && Int32.Parse( GetAttributeValue( "ImageHeight" ) ) != Int16.MinValue )
                    imageConfig.Add( "height", new Rock.Field.ConfigurationValue( GetAttributeValue( "ImageHeight" ) ) );

                foreach ( var item in marketingCampaignAd.Attributes )
                {
                    AttributeCache attribute = item.Value;
                    List<AttributeValue> attributeValues = marketingCampaignAd.AttributeValues[attribute.Key];
                    foreach ( AttributeValue attributeValue in attributeValues )
                    {
                        string valueHtml = string.Empty;

                        // if block attributes limit image types, limit images
                        if ( attribute.FieldType.Guid.Equals( new Guid( Rock.SystemGuid.FieldType.IMAGE ) ) )
                        {
                            if ( imageTypeFilter != null )
                            {
                                if ( !imageTypeFilter.Contains( attribute.Key ) )
                                {
                                    // skip to next attribute if this is an image attribute and it doesn't match the image key filter
                                    continue;
                                }
                                else
                                {
                                    valueHtml = attribute.FieldType.Field.FormatValue( this, attributeValue.Value, imageConfig, false );
                                }
                            }
                        }
                        else
                        {
                            valueHtml = attribute.FieldType.Field.FormatValue( this, attributeValue.Value, attribute.QualifierValues, false );
                        }

                        var valueNode = new Dictionary<string, object>();
                        valueNode.Add( "Key", attribute.Key );
                        valueNode.Add( "Name", attribute.Name );
                        valueNode.Add( "Value", valueHtml );
                        attributes.Add( valueNode );
                    }
                }

                ads.Add( ad );
            }

            var data = new Dictionary<string, object>();
            data.Add( "Ads", ads );
            data.Add( "ApplicationPath", HttpRuntime.AppDomainAppVirtualPath );

            string content;
            try
            {
                content = GetTemplate().Render( Hash.FromDictionary( data ) );
            }
            catch ( Exception ex )
            {
                // liquid compile error
                string exMessage = "An excception occurred while compiling the Liquid template.";

                if ( ex.InnerException != null )
                    exMessage += "<br /><em>" + ex.InnerException.Message + "</em>";

                content = "<div class='alert warning' style='margin: 24px auto 0 auto; max-width: 500px;' ><strong>Liquid Compile Error</strong><p>" + exMessage + "</p></div>";
            }

            // check for errors
            if (content.Contains("No such template"))
            {
                // get template name
                Match match = Regex.Match(GetAttributeValue("Template"), @"'([^']*)");
                if (match.Success)
                {
                    content = String.Format("<div class='alert alert-warning'><h4>Warning</h4>Could not find the template _{1}.liquid in {0}.</div>", ResolveRockUrl("~~/Assets/Liquid"), match.Groups[1].Value);
                }
                else
                {
                    content = "<div class='alert alert-warning'><h4>Warning</h4>Unable to parse the template name from settings.</div>";
                }
            }

            if (content.Contains("error"))
            {
                content = "<div class='alert alert-warning'><h4>Warning</h4>" + content + "</div>";
            }

            phContent.Controls.Clear();
            phContent.Controls.Add( new LiteralControl( content ) );

            // add debug info
            if (GetAttributeValue("EnableDebug").AsBoolean())
            {
                StringBuilder debugInfo = new StringBuilder();
                debugInfo.Append("<p /><div class='alert alert-info'><h4>Debug Info</h4>");

                debugInfo.Append("<pre>");

                debugInfo.Append("<p /><strong>Ad Data</strong> (referenced as 'Ads.' in Liquid)<br>");
                debugInfo.Append(data.LiquidHelpText() + "</pre>");

                debugInfo.Append("</div>");
                phContent.Controls.Add(new LiteralControl(debugInfo.ToString()));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Binds the marketing campaign ads grid.
        /// </summary>
        private void BindGrid()
        {
            MarketingCampaignAdService marketingCampaignAdService = new MarketingCampaignAdService();
            var qry = marketingCampaignAdService.Queryable("MarketingCampaign, MarketingCampaignAdType");

            // limit to current marketingCampaign context (if there is one)
            int?marketingCampaignId = hfMarketingCampaignId.Value.AsInteger(false);

            if (marketingCampaignId.HasValue)
            {
                qry = qry.Where(a => a.MarketingCampaignId == marketingCampaignId);
            }

            // Approval Status
            int approvalStatus = ddlApprovalStatus.SelectedValueAsInt(false) ?? Rock.Constants.All.Id;

            if (approvalStatus != Rock.Constants.All.Id)
            {
                MarketingCampaignAdStatus marketingCampaignAdStatus = ddlApprovalStatus.SelectedValueAsEnum <MarketingCampaignAdStatus>();
                qry = qry.Where(a => a.MarketingCampaignAdStatus == marketingCampaignAdStatus);
            }

            // Priority Range
            if (pPriorityRange.LowerValue.HasValue)
            {
                int lowerValue = pPriorityRange.LowerValue.Value;
                qry = qry.Where(a => a.Priority >= lowerValue);
            }

            if (pPriorityRange.UpperValue.HasValue)
            {
                int upperValue = pPriorityRange.UpperValue.Value;
                qry = qry.Where(a => a.Priority <= upperValue);
            }

            // Ad Type
            int adTypeId = ddlAdType.SelectedValueAsInt() ?? Rock.Constants.All.Id;

            if (adTypeId != Rock.Constants.All.Id)
            {
                qry = qry.Where(a => a.MarketingCampaignAdTypeId == adTypeId);
            }

            // Date Range
            if (pDateRange.LowerValue.HasValue)
            {
                DateTime startDate = pDateRange.LowerValue.Value.Date;
                qry = qry.Where(a => a.StartDate >= startDate);
            }

            if (pDateRange.UpperValue.HasValue)
            {
                // add a whole day to the selected endDate since users will expect to see all the stuff that happened
                // on the endDate up until the very end of that day.

                // calculate the query endDate before including it in the qry statement to avoid Linq error
                var endDate = pDateRange.UpperValue.Value.AddDays(1);
                qry = qry.Where(a => a.StartDate < endDate);
            }

            SortProperty sortProperty = gMarketingCampaignAds.SortProperty;

            if (sortProperty != null)
            {
                qry = qry.Sort(sortProperty);
            }
            else
            {
                qry = qry.OrderBy(a => a.StartDate).ThenBy(a => a.Priority).ThenBy(a => a.MarketingCampaignAdType.Name);
            }

            gMarketingCampaignAds.DataSource = qry.ToList();
            gMarketingCampaignAds.DataBind();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Renders the Ads using Liquid.
        /// </summary>
        private void Render()
        {
            MarketingCampaignAdService marketingCampaignAdService = new MarketingCampaignAdService();
            var qry = marketingCampaignAdService.Queryable();

            // limit to date range
            DateTime currentDateTime = DateTime.Now.Date;

            qry = qry.Where(a => (a.StartDate <= currentDateTime) && (currentDateTime <= a.EndDate));

            // limit to approved
            qry = qry.Where(a => a.MarketingCampaignAdStatus == MarketingCampaignAdStatus.Approved);

            /* Block Attributes */

            // Audience
            string audience = GetAttributeValue("Audience");

            if (!string.IsNullOrWhiteSpace(audience))
            {
                var idList = new List <int>();
                foreach (string guid in audience.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var definedValue = DefinedValueCache.Read(new Guid(guid));
                    if (definedValue != null)
                    {
                        idList.Add(definedValue.Id);
                    }
                }
                qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignAudiences.Any(x => idList.Contains(x.AudienceTypeValueId)));
            }

            // AudiencePrimarySecondary
            string audiencePrimarySecondary = GetAttributeValue("AudiencePrimarySecondary");

            if (!string.IsNullOrWhiteSpace(audiencePrimarySecondary))
            {
                // 1 = Primary, 2 = Secondary
                List <int> idlist = audiencePrimarySecondary.SplitDelimitedValues().Select(a => int.Parse(a)).ToList();

                if (idlist.Contains(1) && !idlist.Contains(2))
                {
                    // only show to Primary Audiences
                    qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignAudiences.Any(x => x.IsPrimary == true));
                }
                else if (idlist.Contains(2) && !idlist.Contains(1))
                {
                    // only show to Secondary Audiences
                    qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignAudiences.Any(x => x.IsPrimary == false));
                }
            }

            // Campuses
            string campuses = GetAttributeValue("Campuses");

            if (!string.IsNullOrWhiteSpace(campuses))
            {
                List <int> idlist = campuses.SplitDelimitedValues().Select(a => int.Parse(a)).ToList();
                qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignCampuses.Any(x => idlist.Contains(x.CampusId)));
            }

            // Ad Types
            string adtypes = GetAttributeValue("AdTypes");

            if (!string.IsNullOrWhiteSpace(adtypes))
            {
                List <int> idlist = adtypes.SplitDelimitedValues().Select(a => int.Parse(a)).ToList();
                qry = qry.Where(a => idlist.Contains(a.MarketingCampaignAdTypeId));
            }

            // Image Types
            string        imageTypes      = GetAttributeValue("ImageTypes");
            List <string> imageTypeFilter = null;

            if (!string.IsNullOrWhiteSpace(imageTypes))
            {
                imageTypeFilter = imageTypes.SplitDelimitedValues().ToList();
            }

            // Campus Context
            Campus campusContext = this.ContextEntity <Campus>();

            if (campusContext != null)
            {
                // limit to ads that are targeted to the current campus context
                qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignCampuses.Any(x => x.Id.Equals(campusContext.Id)));
            }


            // Max Items
            string maxItems   = GetAttributeValue("MaxItems");
            int?   maxAdCount = null;

            if (!string.IsNullOrWhiteSpace(maxItems))
            {
                int parsedCount = 0;
                if (int.TryParse(maxItems, out parsedCount))
                {
                    maxAdCount = parsedCount;
                }
            }

            List <MarketingCampaignAd> marketingCampaignAdList;

            qry = qry.OrderBy(a => a.Priority).ThenBy(a => a.StartDate).ThenBy(a => a.MarketingCampaign.Title);
            if (maxAdCount == null)
            {
                marketingCampaignAdList = qry.ToList();
            }
            else
            {
                marketingCampaignAdList = qry.Take(maxAdCount.Value).ToList();
            }

            // build dictionary for liquid
            var ads = new List <Dictionary <string, object> >();

            foreach (var marketingCampaignAd in marketingCampaignAdList)
            {
                var ad = new Dictionary <string, object>();

                // DetailPage
                string detailPageUrl  = string.Empty;
                string detailPageGuid = GetAttributeValue("DetailPage");
                if (!string.IsNullOrWhiteSpace(detailPageGuid))
                {
                    Rock.Model.Page detailPage = new PageService().Get(new Guid(detailPageGuid));
                    if (detailPage != null)
                    {
                        Dictionary <string, string> queryString = new Dictionary <string, string>();
                        queryString.Add("ad", marketingCampaignAd.Id.ToString());
                        detailPageUrl = new PageReference(detailPage.Id, 0, queryString).BuildUrl();
                    }
                }

                string eventGroupName = marketingCampaignAd.MarketingCampaign.EventGroup != null ? marketingCampaignAd.MarketingCampaign.EventGroup.Name : string.Empty;

                // Marketing Campaign Fields
                ad.Add("Title", marketingCampaignAd.MarketingCampaign.Title);
                ad.Add("ContactEmail", marketingCampaignAd.MarketingCampaign.ContactEmail);
                ad.Add("ContactFullName", marketingCampaignAd.MarketingCampaign.ContactFullName);
                ad.Add("ContactPhoneNumber", marketingCampaignAd.MarketingCampaign.ContactPhoneNumber);
                ad.Add("LinkedEvent", eventGroupName);

                // Specific Ad Fields
                ad.Add("AdType", marketingCampaignAd.MarketingCampaignAdType.Name);
                ad.Add("StartDate", marketingCampaignAd.StartDate.ToString());
                ad.Add("EndDate", marketingCampaignAd.EndDate.ToString());
                ad.Add("Priority", marketingCampaignAd.Priority);
                ad.Add("Url", marketingCampaignAd.Url);
                ad.Add("DetailPageUrl", detailPageUrl);

                // Ad Attributes
                var attributes = new List <Dictionary <string, object> >();
                ad.Add("Attributes", attributes);

                marketingCampaignAd.LoadAttributes();
                Rock.Attribute.Helper.AddDisplayControls(marketingCampaignAd, phContent);

                // create image resize width/height from block settings
                Dictionary <string, Rock.Field.ConfigurationValue> imageConfig = new Dictionary <string, Rock.Field.ConfigurationValue>();
                if (!string.IsNullOrWhiteSpace(GetAttributeValue("ImageWidth")) &&
                    Int32.Parse(GetAttributeValue("ImageWidth")) != Int16.MinValue)
                {
                    imageConfig.Add("width", new Rock.Field.ConfigurationValue(GetAttributeValue("ImageWidth")));
                }

                if (!string.IsNullOrWhiteSpace(GetAttributeValue("ImageHeight")) &&
                    Int32.Parse(GetAttributeValue("ImageHeight")) != Int16.MinValue)
                {
                    imageConfig.Add("height", new Rock.Field.ConfigurationValue(GetAttributeValue("ImageHeight")));
                }

                foreach (var item in marketingCampaignAd.Attributes)
                {
                    AttributeCache        attribute       = item.Value;
                    List <AttributeValue> attributeValues = marketingCampaignAd.AttributeValues[attribute.Key];
                    foreach (AttributeValue attributeValue in attributeValues)
                    {
                        string valueHtml = string.Empty;

                        // If Block Attributes limit image types, limit images
                        if (attribute.FieldType.Guid.Equals(new Guid(Rock.SystemGuid.FieldType.IMAGE)))
                        {
                            if (imageTypeFilter != null)
                            {
                                if (!imageTypeFilter.Contains(attribute.Key))
                                {
                                    // skip to next attribute if this is an image attribute and it doesn't match the image key filter
                                    continue;
                                }
                                else
                                {
                                    valueHtml = attribute.FieldType.Field.FormatValue(this, attributeValue.Value, imageConfig, false);
                                }
                            }
                        }
                        else
                        {
                            valueHtml = attribute.FieldType.Field.FormatValue(this, attributeValue.Value, attribute.QualifierValues, false);
                        }

                        var valueNode = new Dictionary <string, object>();
                        valueNode.Add("Key", attribute.Key);
                        valueNode.Add("Name", attribute.Name);
                        valueNode.Add("Value", valueHtml);
                        attributes.Add(valueNode);
                    }
                }

                ads.Add(ad);
            }

            var data = new Dictionary <string, object>();

            data.Add("Ads", ads);
            data.Add("ApplicationPath", HttpRuntime.AppDomainAppVirtualPath);

            string showDebugValue = GetAttributeValue("ShowDebug") ?? string.Empty;
            bool   showDebug      = showDebugValue.Equals("true", StringComparison.OrdinalIgnoreCase);

            string content;

            try
            {
                content = GetTemplate().Render(Hash.FromDictionary(data));
            }
            catch (Exception ex)
            {
                // xslt compile error
                string exMessage = "An excception occurred while compiling the XSLT template.";

                if (ex.InnerException != null)
                {
                    exMessage += "<br /><em>" + ex.InnerException.Message + "</em>";
                }

                content = "<div class='alert warning' style='margin: 24px auto 0 auto; max-width: 500px;' ><strong>XSLT Compile Error</strong><p>" + exMessage + "</p></div>";
            }

            phContent.Controls.Clear();
            phContent.Controls.Add(new LiteralControl(content));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Binds the marketing campaign ads grid.
        /// </summary>
        private void BindGrid()
        {
            MarketingCampaignAdService marketingCampaignAdService = new MarketingCampaignAdService( new RockContext() );
            var qry = marketingCampaignAdService.Queryable( "MarketingCampaign, MarketingCampaignAdType" );

            // limit to current marketingCampaign context (if there is one)
            int? marketingCampaignId = hfMarketingCampaignId.Value.AsIntegerOrNull();
            if ( marketingCampaignId.HasValue )
            {
                qry = qry.Where( a => a.MarketingCampaignId == marketingCampaignId );
            }

            // Approval Status
            int approvalStatus = ddlApprovalStatus.SelectedValueAsInt( false ) ?? Rock.Constants.All.Id;
            if ( approvalStatus != Rock.Constants.All.Id )
            {
                MarketingCampaignAdStatus marketingCampaignAdStatus = ddlApprovalStatus.SelectedValueAsEnum<MarketingCampaignAdStatus>();
                qry = qry.Where( a => a.MarketingCampaignAdStatus == marketingCampaignAdStatus );
            }

            // Priority Range
            if ( pPriorityRange.LowerValue.HasValue )
            {
                int lowerValue = (int)pPriorityRange.LowerValue.Value;
                qry = qry.Where( a => a.Priority >= lowerValue );
            }

            if ( pPriorityRange.UpperValue.HasValue )
            {
                int upperValue = (int)pPriorityRange.UpperValue.Value;
                qry = qry.Where( a => a.Priority <= upperValue );
            }

            // Ad Type
            int adTypeId = ddlAdType.SelectedValueAsInt() ?? Rock.Constants.All.Id;
            if ( adTypeId != Rock.Constants.All.Id )
            {
                qry = qry.Where( a => a.MarketingCampaignAdTypeId == adTypeId );
            }

            // Date Range
            if ( pDateRange.LowerValue.HasValue )
            {
                DateTime startDate = pDateRange.LowerValue.Value.Date;
                qry = qry.Where( a => a.StartDate >= startDate );
            }

            if ( pDateRange.UpperValue.HasValue )
            {
                // add a whole day to the selected endDate since users will expect to see all the stuff that happened
                // on the endDate up until the very end of that day.

                // calculate the query endDate before including it in the qry statement to avoid Linq error
                var endDate = pDateRange.UpperValue.Value.AddDays( 1 );
                qry = qry.Where( a => a.StartDate < endDate );
            }

            SortProperty sortProperty = gMarketingCampaignAds.SortProperty;

            if ( sortProperty != null )
            {
                qry = qry.Sort( sortProperty );
            }
            else
            {
                qry = qry.OrderBy( a => a.StartDate ).ThenBy( a => a.Priority ).ThenBy( a => a.MarketingCampaignAdType.Name );
            }

            gMarketingCampaignAds.DataSource = qry.ToList();
            gMarketingCampaignAds.DataBind();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Transforms the XML.
        /// </summary>
        private void TransformXml()
        {
            MarketingCampaignAdService marketingCampaignAdService = new MarketingCampaignAdService();
            var qry = marketingCampaignAdService.Queryable();

            // limit to date range
            DateTime currentDateTime = DateTime.Now.Date;

            qry = qry.Where(a => (a.StartDate <= currentDateTime) && (currentDateTime <= a.EndDate));

            // limit to approved
            qry = qry.Where(a => a.MarketingCampaignAdStatus == MarketingCampaignAdStatus.Approved);

            /* Block Attributes */

            // Audience
            string audience = GetAttributeValue("Audience");

            if (!string.IsNullOrWhiteSpace(audience))
            {
                var idList = new List <int>();
                foreach (string guid in audience.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var definedValue = DefinedValueCache.Read(new Guid(guid));
                    if (definedValue != null)
                    {
                        idList.Add(definedValue.Id);
                    }
                }
                qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignAudiences.Any(x => idList.Contains(x.AudienceTypeValueId)));
            }

            // AudiencePrimarySecondary
            string audiencePrimarySecondary = GetAttributeValue("AudiencePrimarySecondary");

            if (!string.IsNullOrWhiteSpace(audiencePrimarySecondary))
            {
                // 1 = Primary, 2 = Secondary
                List <int> idlist = audiencePrimarySecondary.SplitDelimitedValues().Select(a => int.Parse(a)).ToList();

                if (idlist.Contains(1) && !idlist.Contains(2))
                {
                    // only show to Primary Audiences
                    qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignAudiences.Any(x => x.IsPrimary == true));
                }
                else if (idlist.Contains(2) && !idlist.Contains(1))
                {
                    // only show to Secondary Audiences
                    qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignAudiences.Any(x => x.IsPrimary == false));
                }
            }

            // Campuses
            string campuses = GetAttributeValue("Campuses");

            if (!string.IsNullOrWhiteSpace(campuses))
            {
                List <int> idlist = campuses.SplitDelimitedValues().Select(a => int.Parse(a)).ToList();
                qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignCampuses.Any(x => idlist.Contains(x.Id)));
            }

            // Ad Types
            string adtypes = GetAttributeValue("AdTypes");

            if (!string.IsNullOrWhiteSpace(adtypes))
            {
                List <int> idlist = adtypes.SplitDelimitedValues().Select(a => int.Parse(a)).ToList();
                qry = qry.Where(a => idlist.Contains(a.MarketingCampaignAdTypeId));
            }

            // Image Attribute Keys
            string        imageAttributeKeys      = GetAttributeValue("ImageAttributeKeys");
            List <string> imageAttributeKeyFilter = null;

            if (!string.IsNullOrWhiteSpace(imageAttributeKeys))
            {
                imageAttributeKeyFilter = imageAttributeKeys.SplitDelimitedValues().ToList();
            }

            // Campus Context
            Campus campusContext = this.ContextEntity <Campus>();

            if (campusContext != null)
            {
                // limit to ads that are targeted to the current campus context
                qry = qry.Where(a => a.MarketingCampaign.MarketingCampaignCampuses.Any(x => x.Id.Equals(campusContext.Id)));
            }


            // Max Items
            string maxItems   = GetAttributeValue("MaxItems");
            int?   maxAdCount = null;

            if (!string.IsNullOrWhiteSpace(maxItems))
            {
                int parsedCount = 0;
                if (int.TryParse(maxItems, out parsedCount))
                {
                    maxAdCount = parsedCount;
                }
            }

            List <MarketingCampaignAd> marketingCampaignAdList;

            qry = qry.OrderBy(a => a.Priority).ThenBy(a => a.StartDate).ThenBy(a => a.MarketingCampaign.Title);
            if (maxAdCount == null)
            {
                marketingCampaignAdList = qry.ToList();
            }
            else
            {
                marketingCampaignAdList = qry.Take(maxAdCount.Value).ToList();
            }

            // build Xml doc
            XDocument doc      = new XDocument();
            XElement  rootNode = new XElement("Ads");

            doc.Add(rootNode);

            foreach (var marketingCampaignAd in marketingCampaignAdList)
            {
                XElement xmlElement = new XElement("Ad");

                // DetailPage
                string detailPageUrl  = string.Empty;
                string detailPageGuid = GetAttributeValue("DetailPage");
                if (!string.IsNullOrWhiteSpace(detailPageGuid))
                {
                    Rock.Model.Page detailPage = new PageService().Get(new Guid("detailPageGuid"));
                    if (detailPage != null)
                    {
                        Dictionary <string, string> queryString = new Dictionary <string, string>();
                        queryString.Add("marketingCampaignAd", marketingCampaignAd.Id.ToString());
                        detailPageUrl = new PageReference(detailPage.Id, 0, queryString).BuildUrl();
                    }
                }

                string eventGroupName = marketingCampaignAd.MarketingCampaign.EventGroup != null ? marketingCampaignAd.MarketingCampaign.EventGroup.Name : string.Empty;

                // Marketing Campaign Fields
                xmlElement.Add(
                    new XAttribute("Title", marketingCampaignAd.MarketingCampaign.Title),
                    new XAttribute("ContactEmail", marketingCampaignAd.MarketingCampaign.ContactEmail),
                    new XAttribute("ContactFullName", marketingCampaignAd.MarketingCampaign.ContactFullName),
                    new XAttribute("ContactPhoneNumber", marketingCampaignAd.MarketingCampaign.ContactPhoneNumber),
                    new XAttribute("LinkedEvent", eventGroupName));

                // Specific Ad Fields
                xmlElement.Add(
                    new XAttribute("AdType", marketingCampaignAd.MarketingCampaignAdType.Name),
                    new XAttribute("StartDate", marketingCampaignAd.StartDate.ToString()),
                    new XAttribute("EndDate", marketingCampaignAd.EndDate.ToString()),
                    new XAttribute("Priority", marketingCampaignAd.Priority),
                    new XAttribute("Url", marketingCampaignAd.Url),
                    new XAttribute("DetailPageUrl", detailPageUrl));

                // Ad Attributes
                XElement attribsNode = new XElement("Attributes");
                xmlElement.Add(attribsNode);

                marketingCampaignAd.LoadAttributes();
                Rock.Attribute.Helper.AddDisplayControls(marketingCampaignAd, phContent);
                foreach (var item in marketingCampaignAd.Attributes)
                {
                    AttributeCache        attribute       = item.Value;
                    List <AttributeValue> attributeValues = marketingCampaignAd.AttributeValues[attribute.Key];
                    foreach (AttributeValue attributeValue in attributeValues)
                    {
                        // If Block Attributes limit image types, limit images
                        if (attribute.FieldType.Guid.Equals(new Guid(Rock.SystemGuid.FieldType.IMAGE)))
                        {
                            if (imageAttributeKeyFilter != null)
                            {
                                if (!imageAttributeKeyFilter.Contains(attribute.Key))
                                {
                                    // skip to next attribute if this is an image attribute and it doesn't match the image key filter
                                    continue;
                                }
                            }
                        }

                        string   valueHtml = attribute.FieldType.Field.FormatValue(this, attributeValue.Value, attribute.QualifierValues, false);
                        XElement valueNode = new XElement("Attribute");
                        valueNode.Add(new XAttribute("Key", attribute.Key));
                        valueNode.Add(new XAttribute("Name", attribute.Name));
                        valueNode.Add(new XAttribute("Value", valueHtml));
                        attribsNode.Add(valueNode);
                    }
                }

                rootNode.Add(xmlElement);
            }

            string showDebugValue = GetAttributeValue("ShowDebug") ?? string.Empty;
            bool   showDebug      = showDebugValue.Equals("true", StringComparison.OrdinalIgnoreCase);

            string fileName = GetAttributeValue("XSLTFile");
            string outputXml;

            if (showDebug || string.IsNullOrWhiteSpace(fileName))
            {
                outputXml = HttpUtility.HtmlEncode(doc.ToString());
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                TextWriter    tw = new StringWriter(sb);

                XslCompiledTransform xslTransformer = new XslCompiledTransform();
                xslTransformer.Load(Server.MapPath(fileName));
                xslTransformer.Transform(doc.CreateReader(), null, tw);
                outputXml = sb.ToString();
            }

            phContent.Controls.Clear();

            phContent.Controls.Add(new LiteralControl(outputXml));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Binds the marketing campaign ads grid.
        /// </summary>
        private void BindMarketingCampaignAdsGrid()
        {
            MarketingCampaignAdService marketingCampaignAdService = new MarketingCampaignAdService();
            int marketingCampaignId = int.Parse( hfMarketingCampaignId.Value );
            var qry = marketingCampaignAdService.Queryable().Where( a => a.MarketingCampaignId.Equals( marketingCampaignId ) );

            gMarketingCampaignAds.DataSource = qry.OrderBy( a => a.StartDate ).ThenBy( a => a.Priority ).ThenBy( a => a.MarketingCampaignAdType.Name ).ToList();
            gMarketingCampaignAds.DataBind();
        }