Пример #1
0
        /// <summary>
        /// Populates the email template according to the values of the
        /// variables within the EmailScope.
        /// </summary>
        /// <param name="scope">The scope containing all the populated variables
        /// to insert into the template.</param>
        /// <returns></returns>
        public EmailScope FinalizeTemplate(EmailScope scope)
        {
            //First, let's build a list of each of the string variables that actually has a value now.
            var stringVarList = scope.StringVariables.Where(a => !string.IsNullOrWhiteSpace(a.Value)).ToList();

            //Now, go through the scope email body and populate all the string variables.
            var htmlBody  = PopulateStringVariables(stringVarList, scope.HtmlEmailBody);
            var plainBody = PopulateStringVariables(stringVarList, scope.PlainEmailBody);
            var subject   = PopulateStringVariables(stringVarList, scope.Subject);

            //Next, go through and populate all the boolean logics
            htmlBody  = PopulateBooleanVariables(scope.BooleanLogics.ToList(), htmlBody);
            plainBody = PopulateBooleanVariables(scope.BooleanLogics.ToList(), plainBody);
            subject   = PopulateBooleanVariables(scope.BooleanLogics.ToList(), subject);

            //Create the new email scope
            var newTemplateScope = new EmailScope
            {
                Subject        = subject,
                HtmlEmailBody  = htmlBody,
                PlainEmailBody = plainBody
            };

            //And we're done!
            return(newTemplateScope);
        }
Пример #2
0
        /// <summary>
        /// Initially parses the email for each of the tags, then
        /// creates an email scope object for storing this information.
        /// </summary>
        /// <param name="template">Email template to parse.</param>
        /// <returns></returns>
        public EmailScope InitializeEmail(IEmailTemplate template)
        {
            //Iterate through the emailbody and identify each of the tags
            var result = new EmailScope
            {
                HtmlEmailBody   = template.HtmlTemplate,
                PlainEmailBody  = template.TextTemplate,
                Subject         = template.Subject,
                BooleanLogics   = new List <BooleanLogic>(),
                StringVariables = new List <StringVariable>()
            };

            var searchIndex = 0;

            //Run this once for the HTML body, again for the plain body and once more for the subject.
            if (!string.IsNullOrEmpty(template.HtmlTemplate))
            {
                while (searchIndex < template.HtmlTemplate.Length)
                {
                    //Retrieve the index of the next occurence of {{ in the email
                    var startIndexValue = GetIndexOfNextOpenTag(template.HtmlTemplate, searchIndex);
                    if (startIndexValue == null)
                    {
                        //No more start tags
                        searchIndex = template.HtmlTemplate.Length;
                        break;
                        //return result;
                    }

                    //Set the search index to this value now
                    searchIndex = (int)startIndexValue;

                    //Get the end index value of this tag
                    var endIndexValue = GetIndexOfCloseTag(template.HtmlTemplate, searchIndex);

                    //Verify that this tag has an ending
                    if (endIndexValue == null)
                    {
                        //This tag doesn't actually end, let's bow out of this loop
                        break;
                    }

                    //Now, set the search index to this value
                    searchIndex = (int)endIndexValue;

                    //Extract the contents of the tag
                    var tagBody = template.HtmlTemplate.Substring((int)startIndexValue + 2,
                                                                  (int)endIndexValue - (int)startIndexValue - 2);

                    //Set the values of the name and comment
                    var tagName        = tagBody;
                    var tagDescription = "";

                    //Identify if there's a comment in the tag
                    if (tagBody.Contains("|"))
                    {
                        var splitValues = tagBody.Split('|');
                        tagName        = splitValues[0];
                        tagDescription = splitValues[1];
                    }

                    //Look at the first character in the tag name (and see if it's a special character)
                    if (tagName[0] == '#')
                    {
                        //This is a boolean logic tag
                        var booleanTag = new BooleanLogic
                        {
                            Name        = tagName.Substring(1),                      //Remove the # from the name
                            Description = tagDescription
                        };

                        //Check to see that this tag doesn't already exist (name search)
                        if (result.BooleanLogics.All(t => t.Name != booleanTag.Name))
                        {
                            //Append this tag to the email scope object
                            result.BooleanLogics.Add(booleanTag);
                        }
                    }
                    else if (tagName[0] == '/')
                    {
                        //This is a close tag for a boolean statement - ignore
                    }
                    else
                    {
                        //This is a variable tag
                        var variableTag = new StringVariable()
                        {
                            Name        = tagName,
                            Description = tagDescription
                        };

                        //Check to see that this tag doesn't already exist (name match)
                        if (result.StringVariables.All(t => t.Name != variableTag.Name))
                        {
                            //Append this tag to the email scope object
                            result.StringVariables.Add(variableTag);
                        }
                    }
                }
            }

            //Now for the plain text body
            searchIndex = 0;
            if (!string.IsNullOrEmpty(template.TextTemplate))
            {
                while (searchIndex < template.TextTemplate.Length)
                {
                    //Retrieve the index of the next occurence of {{ in the email
                    var startIndexValue = GetIndexOfNextOpenTag(template.TextTemplate, searchIndex);
                    if (startIndexValue == null)
                    {
                        //No more start tags
                        searchIndex = template.TextTemplate.Length;
                        break;
                        //return result;
                    }

                    //Set the search index to this value now
                    searchIndex = (int)startIndexValue;

                    //Get the end index value of this tag
                    var endIndexValue = GetIndexOfCloseTag(template.TextTemplate, searchIndex);

                    //Verify that this tag has an ending
                    if (endIndexValue == null)
                    {
                        //This tag doesn't actually end - let's bow out of this loop
                        break;
                    }

                    //Now, set the search index to this value
                    searchIndex = (int)endIndexValue;

                    //Extract the contents of the tag
                    var tagBody = template.TextTemplate.Substring((int)startIndexValue + 2,
                                                                  (int)endIndexValue - (int)startIndexValue - 2);

                    //Set the values of the name and comment
                    var tagName        = tagBody;
                    var tagDescription = "";

                    //Identify if there's a comment in the tag
                    if (tagBody.Contains("|"))
                    {
                        var splitValues = tagBody.Split('|');
                        tagName        = splitValues[0];
                        tagDescription = splitValues[1];
                    }

                    //Look at the first character in the tag name (and see if it's a special character)
                    if (tagName[0] == '#')
                    {
                        //This is a boolean logic tag
                        var booleanTag = new BooleanLogic
                        {
                            Name        = tagName.Substring(1),                      //Remove the # from the name
                            Description = tagDescription
                        };

                        //Check to see that this tag doesn't already exist (name search)
                        if (result.BooleanLogics.All(t => t.Name != booleanTag.Name))
                        {
                            //Append this tag to the email scope object
                            result.BooleanLogics.Add(booleanTag);
                        }
                    }
                    else if (tagName[0] == '/')
                    {
                        //This is a close tag for a boolean statement - ignore
                    }
                    else
                    {
                        //This is a variable tag
                        var variableTag = new StringVariable()
                        {
                            Name        = tagName,
                            Description = tagDescription
                        };

                        //Check to see that this tag doesn't already exist (name match)
                        if (result.StringVariables.All(t => t.Name != variableTag.Name))
                        {
                            //Append this tag to the email scope object
                            result.StringVariables.Add(variableTag);
                        }
                    }
                }
            }

            //Finally for the subject
            searchIndex = 0;
            if (!string.IsNullOrEmpty(template.Subject))
            {
                while (searchIndex < template.Subject.Length)
                {
                    //Retrieve the index of the next occurence of {{ in the email
                    var startIndexValue = GetIndexOfNextOpenTag(template.Subject, searchIndex);
                    if (startIndexValue == null)
                    {
                        //No more start tags
                        searchIndex = template.Subject.Length;
                        break;
                        //return result;
                    }

                    //Set the search index to this value now
                    searchIndex = (int)startIndexValue;

                    //Get the end index value of this tag
                    var endIndexValue = GetIndexOfCloseTag(template.Subject, searchIndex);

                    //Verify that this tag has an ending
                    if (endIndexValue == null)
                    {
                        //This tag doesn't actually end - let's bow out of this loop
                        break;
                    }

                    //Now, set the search index to this value
                    searchIndex = (int)endIndexValue;

                    //Extract the contents of the tag
                    var tagBody = template.Subject.Substring((int)startIndexValue + 2,
                                                             (int)endIndexValue - (int)startIndexValue - 2);

                    //Set the values of the name and comment
                    var tagName        = tagBody;
                    var tagDescription = "";

                    //Identify if there's a comment in the tag
                    if (tagBody.Contains("|"))
                    {
                        var splitValues = tagBody.Split('|');
                        tagName        = splitValues[0];
                        tagDescription = splitValues[1];
                    }

                    //Look at the first character in the tag name (and see if it's a special character)
                    if (tagName[0] == '#')
                    {
                        //This is a boolean logic tag
                        var booleanTag = new BooleanLogic
                        {
                            Name        = tagName.Substring(1),                      //Remove the # from the name
                            Description = tagDescription
                        };

                        //Check to see that this tag doesn't already exist (name search)
                        if (result.BooleanLogics.All(t => t.Name != booleanTag.Name))
                        {
                            //Append this tag to the email scope object
                            result.BooleanLogics.Add(booleanTag);
                        }
                    }
                    else if (tagName[0] == '/')
                    {
                        //This is a close tag for a boolean statement - ignore
                    }
                    else
                    {
                        //This is a variable tag
                        var variableTag = new StringVariable()
                        {
                            Name        = tagName,
                            Description = tagDescription
                        };

                        //Check to see that this tag doesn't already exist (name match)
                        if (result.StringVariables.All(t => t.Name != variableTag.Name))
                        {
                            //Append this tag to the email scope object
                            result.StringVariables.Add(variableTag);
                        }
                    }
                }
            }

            return(result);
        }