|
17 | 17 | using Nop.Services.Localization; |
18 | 18 | using Nop.Services.Stores; |
19 | 19 | using Nop.Services.Messages; |
20 | | -// customized |
21 | | -using RazorEngine; |
22 | | -using RazorEngine.Templating; |
23 | | -using System.Security.Cryptography; |
24 | | -using System.Text; |
25 | 20 |
|
26 | 21 | namespace ToSic.Nop.Plugins.RazorMessageService |
27 | 22 | { |
@@ -99,15 +94,15 @@ protected virtual int SendNotification(MessageTemplate messageTemplate, |
99 | 94 |
|
100 | 95 | // Razor-Parse Subject |
101 | 96 | bool subjectSuccess; |
102 | | - var subjectRazorParsed = RazorParseSafe(messageTemplate.Id, subject, razorModel, out subjectSuccess); |
| 97 | + var subjectRazorParsed = Services.RazorTemplateParser.ParseSafe(messageTemplate.Id, subject, razorModel, out subjectSuccess); |
103 | 98 | if (subjectSuccess) |
104 | 99 | subject = subjectRazorParsed; |
105 | 100 | else |
106 | 101 | subject += subjectRazorParsed; // in case of an error, append the error-text returned |
107 | 102 |
|
108 | 103 | // Razor-Parse Body |
109 | 104 | bool bodySuccess; |
110 | | - var bodyRazorParsed = RazorParseSafe(messageTemplate.Id, body, razorModel, out bodySuccess); |
| 105 | + var bodyRazorParsed = Services.RazorTemplateParser.ParseSafe(messageTemplate.Id, body, razorModel, out bodySuccess); |
111 | 106 | if (bodySuccess) |
112 | 107 | body = bodyRazorParsed; |
113 | 108 | else |
@@ -144,53 +139,6 @@ protected virtual int SendNotification(MessageTemplate messageTemplate, |
144 | 139 | return email.Id; |
145 | 140 | } |
146 | 141 |
|
147 | | - #region Customized |
148 | | - |
149 | | - /// <summary> |
150 | | - /// work arounf MD5 has for razorengine caching. |
151 | | - /// </summary> |
152 | | - /// <param name="input"></param> |
153 | | - /// <returns></returns> |
154 | | - private static string GetMd5Hash(string input) |
155 | | - { |
156 | | - var md5 = MD5.Create(); |
157 | | - var inputBytes = System.Text.Encoding.ASCII.GetBytes(input); |
158 | | - var hash = md5.ComputeHash(inputBytes); |
159 | | - var sb = new StringBuilder(); |
160 | | - foreach (byte t in hash) |
161 | | - { |
162 | | - sb.Append(t.ToString("X2")); |
163 | | - } |
164 | | - |
165 | | - return sb.ToString(); |
166 | | - } |
167 | | - |
168 | | - /// <summary> |
169 | | - /// Parse text with Razor and handle Template Exception |
170 | | - /// </summary> |
171 | | - private static string RazorParseSafe(int templateId, string text, object model, out bool success) |
172 | | - { |
173 | | - string result; |
174 | | - try |
175 | | - { |
176 | | - var key = "MailTemplate" + templateId + GetMd5Hash(text); |
177 | | - |
178 | | - result = Engine.Razor.RunCompile(text, key, model: model); |
179 | | - |
180 | | - success = true; |
181 | | - } |
182 | | - catch (RazorEngine.Templating.TemplateCompilationException ex) |
183 | | - { |
184 | | - result = "TemplateCompilationException: "; |
185 | | - ex.Errors.ToList().ForEach(p => result += p.ErrorText); |
186 | | - success = false; |
187 | | - } |
188 | | - |
189 | | - return result; |
190 | | - } |
191 | | - #endregion |
192 | | - |
193 | | - |
194 | 142 | protected virtual MessageTemplate GetActiveMessageTemplate(string messageTemplateName, int storeId) |
195 | 143 | { |
196 | 144 | var messageTemplate = _messageTemplateService.GetMessageTemplateByName(messageTemplateName, storeId); |
@@ -1794,38 +1742,9 @@ public virtual int SendTestEmail(int messageTemplateId, string sendToEmail, |
1794 | 1742 | //event notification |
1795 | 1743 | _eventPublisher.MessageTokensAdded(messageTemplate, tokens); |
1796 | 1744 |
|
1797 | | - const string notSupportedText = "not supported in TestEmail"; // customized |
1798 | | - |
1799 | 1745 | return SendNotification(messageTemplate, emailAccount, |
1800 | 1746 | languageId, tokens, |
1801 | | - new |
1802 | | - { |
1803 | | - Store = notSupportedText, |
1804 | | - BlogComment = notSupportedText, |
1805 | | - Customer = notSupportedText, |
1806 | | - BackInStockSubscription = notSupportedText, |
1807 | | - OrderNote = notSupportedText, |
1808 | | - Order = notSupportedText, |
1809 | | - PrivateMessage = notSupportedText, |
1810 | | - ForumPost = notSupportedText, |
1811 | | - ForumTopic = notSupportedText, |
1812 | | - Forum = notSupportedText, |
1813 | | - GiftCard = notSupportedText, |
1814 | | - ReturnRequest = notSupportedText, |
1815 | | - OrderItem = notSupportedText, |
1816 | | - NewsComment = notSupportedText, |
1817 | | - Subscription = notSupportedText, |
1818 | | - VatName = notSupportedText, |
1819 | | - VatAddress = notSupportedText, |
1820 | | - Vendor = notSupportedText, |
1821 | | - RefundedAmount = notSupportedText, |
1822 | | - ProductReview = notSupportedText, |
1823 | | - Product = notSupportedText, |
1824 | | - RecurringPayment = notSupportedText, |
1825 | | - PersonalMessage = notSupportedText, |
1826 | | - CustomerEmail = notSupportedText, |
1827 | | - Shipment = notSupportedText |
1828 | | - }, // customized |
| 1747 | + new DummyMessageModel(), // customized |
1829 | 1748 | sendToEmail, null); |
1830 | 1749 | } |
1831 | 1750 |
|
|
0 commit comments