HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/App_Code/Pux/Kentico/Macros/PuxEmailMacrosModule.cs
using CMS.EmailEngine;
using CMS.Helpers;
using CMS.Localization;
using CMS.MacroEngine;
using CMS.SiteProvider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Pux.Kentico.Macros.Emails
{
    /// <summary>
    /// Custom PUX email macros registered in PuxEmailStaticMethods
    /// </summary>
    public class PuxEmailStaticMethods : MacroMethodContainer
    {
        /// <summary>
        /// Includes content from a specified file on filesystem.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        [MacroMethod(typeof(string), "Includes specified template", 1)]
        [MacroMethodParam(0, "templatePath", typeof(string), "Path to the template")]
        public static object IncludeTemplate(EvaluationContext context, params object[] parameters)
        {
            if (parameters.Length < 1)
                return "Invalid number of parameters.";

            string templateName = ValidationHelper.GetString(parameters[0], "");

            return GetResolvedTemplate(templateName, context.Culture, context.Resolver);
        }


        /// <summary>
        /// Includes content from a specified file on filesystem and substitutes ##TITLE## macro inside
        /// the template with provided value
        /// </summary>
        /// <param name="context"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        [MacroMethod(typeof(string), "Includes header template", 2)]
        [MacroMethodParam(0, "templatePath", typeof(string), "Path to the template")]
        [MacroMethodParam(1, "title", typeof(string), "Email title")]
        public static object IncludeHeader(EvaluationContext context, params object[] parameters)
        {
            if (parameters.Length < 2)
                return "Invalid number of parameters.";

            string templateName = ValidationHelper.GetString(parameters[0], "");
            var title = ValidationHelper.GetString(parameters[1], "");
            var resolver = context.Resolver;

            resolver.SetNamedSourceData("CurrentEmailTitle", title);

            return GetResolvedTemplate(templateName, context.Culture, resolver);
        }

        [MacroMethod(typeof(string), "Includes primary styled button", 2)]
        [MacroMethodParam(0, "targetUrl", typeof(string), "Target URL")]
        [MacroMethodParam(1, "text", typeof(string), "Link text")]
        public static object GetButton(EvaluationContext context, params object[] parameters)
        {
            if (parameters.Length < 2)
                return "Invalid number of parameters";

            string type = parameters.Length > 2 ? parameters[2] as string : "text";

            // try to load style from resource string
            var resourceStringKey = string.Format("{0}.EmailTemplates.Buttons.{1}", SiteContext.CurrentSiteName, type);
            var resourceString = ResHelper.GetString(resourceStringKey);

            var style = resourceString.Equals(resourceStringKey) ? string.Empty : resourceString;

            return GetLink(parameters[0] as string, parameters[1] as string, style);
        }

        #region "Helper methods"

        private static string GetResolvedTemplate(string templateName, string culture, MacroResolver resolver)
        {
            try
            {
                if (string.IsNullOrEmpty(culture))
                {
                    if (!string.IsNullOrEmpty(resolver.Culture))
                    {
                        // take culture from resolver
                        culture = resolver.Culture;
                    }
                    else if (LocalizationContext.CurrentCulture != null)
                    {
                        // take current UI culture
                        culture = LocalizationContext.CurrentCulture.CultureCode;
                        resolver.Culture = culture;
                    }
                }

                string content = LoadTemplate(templateName, culture);

                //content = MacroResolver.GetInstance().ResolveMacros(content);
                content = resolver.ResolveMacros(content);

                return content;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private static string LoadTemplate(string filename, string culture)
        {
            // try to load localized template from e-mail templates
            // (sorted by priority descending)
            string[] dbLookupNames = new string[]
            {
                // localized template (Template_cs-cz)
                filename + "_" + culture.ToLowerInvariant(),
                // default language template (Template_en-us)
                filename + "_" + CultureHelper.GetDefaultCultureCode(SiteContext.CurrentSiteName),
                // only template, not localized (Template)
                filename
            };

            EmailTemplateInfo emailTemplate = null;
            foreach (var dbLookupName in dbLookupNames)
            {
                emailTemplate = EmailTemplateProvider.GetEmailTemplate(dbLookupName, SiteContext.CurrentSiteID);
                if (emailTemplate != null)
                {
                    // template found
                    break;
                }
            }

            if (emailTemplate != null)
            {
                return emailTemplate.TemplateText;
            }

            // try to load template from file system

            // force templates to be inside App_Data/EmailTemplates directory
            string safePath = filename.Replace("..", "");

            // file names (sorted by priority descending)
            string[] lookupFileNames = new string[] {
                // site specific template (CurrentSiteName-path)
                SiteContext.CurrentSiteName + "-" + safePath,
                // no customization
                safePath
            };

            foreach (var lookupFileName in lookupFileNames)
            {
                string path = HttpContext.Current.Server.MapPath(string.Format("~/App_Data/EmailTemplates/{0}.html", lookupFileName));
                if (System.IO.File.Exists(path))
                {
                    // file found
                    return System.IO.File.ReadAllText(path);
                }
            }

            // path was not found
            throw new Exception("Invalid path " + filename);
        }

        private static string GetLink(string url, string text, string style)
        {
            return string.Format("<a href=\"{0}\" style=\"{1}\">{2}</a>", url, style, HTMLHelper.HTMLEncode(text));
        }

        #endregion

    }
}