Ejemplo n.º 1
0
        /// <summary>
        /// Renders the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="result">The result.</param>
        public override void Render(Context context, TextWriter result)
        {
            // First, ensure that this command is allowed in the context.
            if (!this.IsAuthorized(context))
            {
                result.Write(string.Format(RockLavaBlockBase.NotAuthorizedMessage, this.Name));
                return;
            }

            // Parse the Lava Command markup to retrieve paramters.
            var parms = new Dictionary <string, string>();

            LavaHelper.ParseCommandMarkup(_markup, context, parms);

            // Set local variables from parsed parameters.
            // InteractionChannel variables.
            int?channelTypeMediumValueId = parms.GetValueOrNull(ParameterKey.ChannelTypeMediumValueId).AsIntegerOrNull();

            if (!channelTypeMediumValueId.HasValue)
            {
                // Do nothing if a ChannelTypeMediumValueId wasn't specified.
                return;
            }

            DefinedValueCache channelTypeMediumValue = DefinedValueCache.Get(channelTypeMediumValueId.Value);

            if (channelTypeMediumValue == null)
            {
                // Do nothing if an invalid ChannelTypeMediumValueId was specified.
                return;
            }

            int?   channelEntityId = parms.GetValueOrNull(ParameterKey.ChannelEntityId).AsIntegerOrNull();
            string channelName     = parms.GetValueOrNull(ParameterKey.ChannelName);

            int?componentEntityTypeId   = parms.GetValueOrNull(ParameterKey.ComponentEntityTypeId).AsIntegerOrNull();
            int?interactionEntityTypeId = parms.GetValueOrNull(ParameterKey.InteractionEntityTypeId).AsIntegerOrNull();

            // InteractionComponent variables.
            int?   componentEntityId = parms.GetValueOrNull(ParameterKey.ComponentEntityId).AsIntegerOrNull();
            string componentName     = parms.GetValueOrNull(ParameterKey.ComponentName);

            // Interaction variables.
            int?entityId = parms.GetValueOrNull(ParameterKey.EntityId).AsIntegerOrNull();

            string operation = parms.GetValueOrNull(ParameterKey.Operation);
            string summary   = parms.GetValueOrNull(ParameterKey.Summary);
            string source    = parms.GetValueOrNull(ParameterKey.Source);
            string medium    = parms.GetValueOrNull(ParameterKey.Medium);
            string campaign  = parms.GetValueOrNull(ParameterKey.Campaign);
            string content   = parms.GetValueOrNull(ParameterKey.Content);
            string term      = parms.GetValueOrNull(ParameterKey.Term);

            int?relatedEntityTypeId = parms.GetValueOrNull(ParameterKey.RelatedEntityTypeId).AsIntegerOrNull();
            int?relatedEntityId     = parms.GetValueOrNull(ParameterKey.RelatedEntityId).AsIntegerOrNull();

            string channelCustom1        = parms.GetValueOrNull(ParameterKey.ChannelCustom1);
            string channelCustom2        = parms.GetValueOrNull(ParameterKey.ChannelCustom2);
            string channelCustomIndexed1 = parms.GetValueOrNull(ParameterKey.ChannelCustomIndexed1);

            int?personAliasId = parms.GetValueOrNull(ParameterKey.PersonAliasId).AsIntegerOrNull();

            if (!personAliasId.HasValue)
            {
                Person currentPerson = LavaHelper.GetCurrentPerson(context);
                personAliasId = LavaHelper.GetPrimaryPersonAliasId(currentPerson);
            }

            string data = null;

            using (TextWriter twData = new StringWriter())
            {
                base.Render(context, twData);
                data = twData.ToString();
            }

            // Write the Interaction by way of a transaction.
            var info = new InteractionTransactionInfo
            {
                ChannelTypeMediumValueId = channelTypeMediumValueId,
                ChannelEntityId          = channelEntityId,
                ChannelName             = channelName,
                ComponentEntityTypeId   = componentEntityTypeId,
                InteractionEntityTypeId = interactionEntityTypeId,

                ComponentEntityId = componentEntityId,
                ComponentName     = componentName,

                InteractionEntityId              = entityId,
                InteractionOperation             = operation,
                InteractionSummary               = summary,
                InteractionData                  = data,
                InteractionRelatedEntityTypeId   = relatedEntityTypeId,
                InteractionRelatedEntityId       = relatedEntityId,
                InteractionChannelCustom1        = channelCustom1,
                InteractionChannelCustom2        = channelCustom2,
                InteractionChannelCustomIndexed1 = channelCustomIndexed1,
                PersonAliasId       = personAliasId,
                InteractionSource   = source,
                InteractionMedium   = medium,
                InteractionCampaign = campaign,
                InteractionContent  = content,
                InteractionTerm     = term
            };

            var interactionTransaction = new InteractionTransaction(info);

            interactionTransaction.Enqueue();
        }
        /// <summary>
        /// Renders the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="result">The result.</param>
        public override void Render(Context context, TextWriter result)
        {
            // First, ensure that this command is allowed in the context.
            if (!LavaHelper.IsAuthorized(context, this.GetType().Name))
            {
                result.Write(string.Format(RockLavaBlockBase.NotAuthorizedMessage, this.Name));
                base.Render(context, result);
                return;
            }

            // Parse the Lava Command markup to retrieve paramters.
            var parms = new Dictionary <string, string>
            {
                { ParameterKey.Operation, "View" }
            };

            LavaHelper.ParseCommandMarkup(_markup, context, parms);

            // Set local variables from parsed parameters.
            int?contentChannelItemId = parms.GetValueOrNull(ParameterKey.ContentChannelItemId).AsIntegerOrNull();

            if (!contentChannelItemId.HasValue)
            {
                // Do nothing if a ContentChannelItem ID wasn't specified.
                return;
            }

            ContentChannelItem contentChannelItem = null;

            using (var rockContext = new RockContext())
            {
                contentChannelItem = new ContentChannelItemService(rockContext)
                                     .Queryable("ContentChannel")
                                     .AsNoTracking()
                                     .FirstOrDefault(c => c.Id == contentChannelItemId.Value);
            }

            ContentChannel contentChannel = contentChannelItem.ContentChannel;

            if (contentChannelItem == null || contentChannel == null)
            {
                // The caller supplied an invalid ContentChannelItem ID; nothing to do.
                return;
            }

            string operation = parms.GetValueOrNull(ParameterKey.Operation);
            string summary   = parms.GetValueOrNull(ParameterKey.Summary);
            string source    = parms.GetValueOrNull(ParameterKey.Source);
            string medium    = parms.GetValueOrNull(ParameterKey.Medium);
            string campaign  = parms.GetValueOrNull(ParameterKey.Campaign);
            string content   = parms.GetValueOrNull(ParameterKey.Content);
            string term      = parms.GetValueOrNull(ParameterKey.Term);

            int?personAliasId = parms.GetValueOrNull(ParameterKey.PersonAliasId).AsIntegerOrNull();

            if (!personAliasId.HasValue)
            {
                Person currentPerson = LavaHelper.GetCurrentPerson(context);
                personAliasId = LavaHelper.GetPrimaryPersonAliasId(currentPerson);
            }

            // Write the Interaction by way of a transaction.
            DefinedValueCache mediumType = DefinedValueCache.Get(Rock.SystemGuid.DefinedValue.INTERACTIONCHANNELTYPE_CONTENTCHANNEL.AsGuid());

            if (mediumType == null)
            {
                return;
            }

            var info = new InteractionTransactionInfo
            {
                ChannelTypeMediumValueId = mediumType.Id,
                ChannelEntityId          = contentChannel.Id,
                ChannelName           = contentChannel.ToString(),
                ComponentEntityTypeId = contentChannel.TypeId,
                ComponentEntityId     = contentChannelItem.Id,
                ComponentName         = contentChannelItem.ToString(),
                InteractionOperation  = operation,
                InteractionSummary    = summary ?? contentChannelItem.Title,
                PersonAliasId         = personAliasId,
                InteractionSource     = source,
                InteractionMedium     = medium,
                InteractionCampaign   = campaign,
                InteractionContent    = content,
                InteractionTerm       = term
            };

            var interactionTransaction = new InteractionTransaction(info);

            interactionTransaction.Enqueue();
        }