Error executing template "Designs/Swift/_parsed/Swift_Preferences.parsed.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
at CompiledRazorTemplates.Dynamic.RazorEngine_605fa019ca5f472e8c46763aac0ed3d2.ExecuteAsync()
at RazorEngine.Templating.TemplateBase.Run(ExecuteContext context, TextWriter reader)
at RazorEngine.Templating.RazorEngineCore.RunTemplate(ICompiledTemplate template, TextWriter writer, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineService.Run(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.DynamicWrapperService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service, String name, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.<RunCompile>b__0(TextWriter writer)
at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service, String name, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineServiceExtensions.RunCompile(IRazorEngineService service, String templateSource, String name, Type modelType, Object model, DynamicViewBag viewBag)
at Dynamicweb.Rendering.RazorTemplateRenderingProvider.Render(Template template)
at Dynamicweb.Rendering.TemplateRenderingService.Render(Template template)
at Dynamicweb.Rendering.Template.RenderRazorTemplate()
1 @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.PageViewModel>
2 @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.PageViewModel>
3 @using System
4 @using System.Text.RegularExpressions
5 @using System.IO
6
7
8
9 @{
10 string layout = Model.Item.GetRawValueString("PreferencesLayout", "modal");
11 layout = Dynamicweb.Context.Current.Request["Layout"] != null ? Dynamicweb.Context.Current.Request["Layout"] : layout;
12 layout = layout == "" ? "modal" : layout;
13 string type = Dynamicweb.Context.Current.Request["Type"] != null ? Dynamicweb.Context.Current.Request["Type"] : "language";
14
15 string groupId = Dynamicweb.Context.Current.Request["GroupID"] != null ? Dynamicweb.Context.Current.Request["GroupID"].ToString() : "";
16 string productId = Dynamicweb.Context.Current.Request["ProductID"] != null ? Dynamicweb.Context.Current.Request["ProductID"].ToString() : "";
17 string variantId = Dynamicweb.Context.Current.Request["VariantID"] != null ? Dynamicweb.Context.Current.Request["VariantID"].ToString() : "";
18
19 string linkFontSize = Dynamicweb.Context.Current.Request["FontSize"] != null ? Dynamicweb.Context.Current.Request["FontSize"].ToString() : "";
20 string linkFontWeight = Dynamicweb.Context.Current.Request["FontWeight"] != null ? Dynamicweb.Context.Current.Request["FontWeight"].ToString() : "";
21 string linkCasing = Dynamicweb.Context.Current.Request["Casing"] != null ? Dynamicweb.Context.Current.Request["Casing"].ToString() : "";
22
23 Regex reg = new Regex(@"\(([^\)]+)\)");
24 List<Dynamicweb.Content.Page> languages = new List<Dynamicweb.Content.Page>();
25
26 int pageId = Dynamicweb.Context.Current.Request["CurrentPageID"] != null ? Convert.ToInt32(Dynamicweb.Context.Current.Request["CurrentPageID"]) : Pageview.ID;
27 var currentPage = Dynamicweb.Content.Services.Pages.GetPage(pageId); //Get the correct page instance
28
29 if (currentPage.Area.IsMaster)
30 {
31 languages.Add(currentPage);
32 if (currentPage.Languages != null)
33 {
34 foreach (var language in currentPage.Languages)
35 {
36 if (language.Area.Active == true)
37 {
38 languages.Add(language);
39 }
40 }
41 }
42 }
43 else
44 {
45 languages.Add(currentPage.MasterPage);
46 if (currentPage.MasterPage != null)
47 {
48 if (currentPage.MasterPage.Languages != null)
49 {
50 foreach (var language in currentPage.MasterPage.Languages)
51 {
52 if (language.Area.Active == true)
53 {
54 languages.Add(language);
55 }
56 }
57 }
58 }
59 }
60
61 languages = languages.Count > 1 ? languages.OrderBy(language => language.Area.CultureInfo.DisplayName).ToList() : languages;
62
63 string siteLanguage = Pageview.Area.CultureInfo.Name;
64 Uri url = Dynamicweb.Context.Current.Request.Url;
65 string hostName = url.Host; // domain.com/da-dk or domain.com/en-us
66
67 var ecomCountries = Dynamicweb.Ecommerce.Services.Countries.GetCountries() != null ? Dynamicweb.Ecommerce.Services.Countries.GetCountries().OrderBy(country => country.GetName(Pageview.Area.EcomLanguageId)) : null;
68 var currencies = Dynamicweb.Ecommerce.Services.Currencies.GetAllCurrencies() != null ? Dynamicweb.Ecommerce.Services.Currencies.GetAllCurrencies().OrderBy(currency => currency.Code) : null;
69
70 string modalTheme = !string.IsNullOrWhiteSpace(Model.Item.GetRawValueString("ModalTheme")) ? " theme " + Model.Item.GetRawValueString("ModalTheme").Replace(" ", "").Trim().ToLower() : "";
71 }
72
73 @* Modal *@
74
75 @if (layout == "modal")
76 {
77 int updatePageId = pageId;
78
79 <div class="modal-content @modalTheme item_@Model.Item.SystemName.ToLower()">
80 <div class="modal-header">
81 <h5 class="modal-title" id="PreferencesModalLabel">@Translate("Preferences")</h5>
82 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
83 </div>
84 <div class="modal-body d-grid gap-3" id="PreferencesModalBody">
85 <input type="hidden" name="CurrentPageID" value="@pageId">
86 @if (groupId != "")
87 {
88 <input type="hidden" name="GroupID" value="@groupId">
89 }
90
91 @if (productId != "")
92 {
93 <input type="hidden" name="ProductID" value="@productId">
94 }
95
96 @if (variantId != "")
97 {
98 <input type="hidden" name="VariantID" value="@variantId">
99 }
100
101
102 @if (languages.Count() > 1 && Model.Item.GetBoolean("LanguageSelector"))
103 {
104 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline">
105 <input type="hidden" name="Layout" value="modal">
106 <input type="hidden" name="CurrentPageID" value="@pageId">
107 <input type="hidden" name="GroupID" value="@groupId">
108 <input type="hidden" name="ProductID" value="@productId">
109 <input type="hidden" name="VariantID" value="@variantId">
110
111 <div class="form-floating">
112 <select id="LanguageSelect" name="ID" class="form-select d-col-12" aria-label="@Translate("Select language")" onchange="swift.PageUpdater.Update(event)">
113 @foreach (var language in languages)
114 {
115 if (language != null && language.Published && language.Area.Active && language.Area.Published)
116 {
117 string selectedLanguage = language.Area.ID == Pageview.Area.ID ? "selected" : "";
118 updatePageId = language.Area.ID == Pageview.Area.ID ? language.ID : updatePageId;
119
120 var masterPageId = Pageview.Page?.MasterPageId != null && Pageview.Page.MasterPageId != 0 ? Pageview.Page.MasterPageId : Pageview.ID;
121 var preferencePageId = Dynamicweb.Content.Services.Pages.GetPageIDByMasterID(masterPageId, language.AreaId);
122 preferencePageId = preferencePageId == 0 ? Pageview.Page.MasterPageId : preferencePageId;
123 preferencePageId = preferencePageId == 0 ? Pageview.Page.ID : preferencePageId;
124
125 <option hreflang="@language.Area.CultureInfo.TwoLetterISOLanguageName" value="@preferencePageId" @selectedLanguage>@language.Area.DisplayName</option>
126 }
127 }
128 </select>
129 <label for="LanguageSelect">@Translate("Language")</label>
130 </div>
131 </form>
132 }
133
134 @if (ecomCountries.Count() > 1 && Model.Item.GetBoolean("CountrySelector"))
135 {
136 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline">
137 <input type="hidden" name="ID" value="@Pageview.ID">
138 <input type="hidden" name="CurrentPageID" value="@pageId">
139 <input type="hidden" name="GroupID" value="@groupId">
140 <input type="hidden" name="ProductID" value="@productId">
141 <input type="hidden" name="VariantID" value="@variantId">
142
143 <div class="form-floating">
144 <select name="CountryCode" id="CountryCodeSelect" class="form-select d-col-12" aria-label="@Translate("Select country")" onchange="swift.PageUpdater.Update(event)">
145 @foreach (var country in ecomCountries)
146 {
147 var selectedCountry = country.Code2 == Dynamicweb.Ecommerce.Common.Context.Country.Code2 ? "selected" : "";
148
149 if (!string.IsNullOrEmpty(country.GetName(Pageview.Area.EcomLanguageId)))
150 {
151 <option value="@country.Code2" @selectedCountry>@country.GetName(Pageview.Area.EcomLanguageId)</option>
152 }
153 }
154 </select>
155 <label for="CountryCodeSelect">@Translate("Delivery country")</label>
156 </div>
157 </form>
158 }
159
160 @if (currencies.Count() > 1 && Model.Item.GetBoolean("CurrencySelector"))
161 {
162 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline">
163 <input type="hidden" name="ID" value="@Pageview.ID">
164 <input type="hidden" name="CurrentPageID" value="@pageId">
165 <input type="hidden" name="GroupID" value="@groupId">
166 <input type="hidden" name="ProductID" value="@productId">
167 <input type="hidden" name="VariantID" value="@variantId">
168
169 <div class="form-floating">
170 <select name="CurrencyCode" id="CurrencySelect" class="form-select d-col-12" aria-label="@Translate("Select currency")" onchange="swift.PageUpdater.Update(event)">
171 @foreach (var currency in currencies)
172 {
173 string selectedCurrency = currency.Code == Dynamicweb.Ecommerce.Common.Context.Currency.Code ? "selected" : "";
174
175 if (!string.IsNullOrEmpty(currency.Code))
176 {
177 <option value="@currency.Code" @selectedCurrency>@currency.Code</option>
178 }
179 }
180 </select>
181 <label for="CurrencySelect">@Translate("Currency")</label>
182 </div>
183 </form>
184 }
185 </div>
186 <div class="modal-footer">
187 @{
188 hostName = !string.IsNullOrEmpty(Pageview.Area.DomainLock) ? Pageview.Area.DomainLock : url.Host;
189 string link = $"Default.aspx?ID={updatePageId}";
190 link += groupId != "" ? $"&GroupID={groupId}" : "";
191 link += productId != "" ? $"&ProductID={productId}" : "";
192 link += variantId != "" ? $"&VariantID={variantId}" : "";
193 var friendlyUrl = $"{url.Scheme}://{hostName}{Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(link)}";
194 }
195
196 <a href="@friendlyUrl" class="btn btn-primary w-100">@Translate("Update preferences")</a>
197 </div>
198 </div>
199 }
200
201 @* Dropdowns *@
202
203 @if (layout == "dropdown" && type == "country")
204 {
205 foreach (var country in ecomCountries)
206 {
207 var link = $"{url.Scheme}://{hostName}?ID={currentPage.ID}&AreaID={Pageview.Area.ID}&CountryCode={country.Code2}";
208 link += groupId != "" ? $"&GroupID={groupId}" : "";
209 link += productId != "" ? $"&ProductID={productId}" : "";
210 link += variantId != "" ? $"&VariantID={variantId}" : "";
211
212 if (!string.IsNullOrEmpty(country.GetName(Pageview.Area.EcomLanguageId)))
213 {
214 <li>
215 <a href="@link" class="dropdown-item nav-link-underline @linkFontSize @linkFontWeight @linkCasing">@country.GetName(Pageview.Area.EcomLanguageId)</a>
216 </li>
217 }
218 }
219 }
220
221 @if (layout == "dropdown" && type == "language")
222 {
223 string iconFolder = "/Files/FlagIcons/";
224 int languagesCount = 0;
225
226 foreach (var language in languages)
227 {
228 if (language.Area != null)
229 {
230 if (language != null && language.Published && language.Area.Active && language.Area.Published && language.Area.ID != Dynamicweb.Frontend.PageView.Current().AreaID)
231 {
232 hostName = !string.IsNullOrEmpty(language.Area.DomainLock) ? language.Area.DomainLock : url.Host;
233 string link = $"Default.aspx?ID={language.ID}";
234 link += groupId != "" ? $"&GroupID={groupId}" : "";
235 link += productId != "" ? $"&ProductID={productId}" : "";
236 link += variantId != "" ? $"&VariantID={variantId}" : "";
237
238 string urlScheme = url.Scheme;
239 int urlPort = url.Port;
240 string portSetting = (urlScheme == "http" && urlPort != 80) || (urlScheme == "https" && urlPort != 443) ? $":{urlPort}" : string.Empty;
241
242 var friendlyUrl = $"{urlScheme}://{hostName}{portSetting}{Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(link)}";
243
244 string icon = iconFolder + language.Area.CultureInfo.Name.ToLower() + ".svg";
245 <li>
246 <a hreflang="@language.Area.CultureInfo.TwoLetterISOLanguageName" href="@friendlyUrl" class="dropdown-item nav-link-underline @linkFontSize @linkFontWeight @linkCasing">
247 @if (Path.GetExtension(icon).ToLower() == ".svg" && !icon.ToLower().Contains("none"))
248 {
249 string iconPath = Dynamicweb.Context.Current.Server.MapPath(icon);
250
251 <span class="icon-2">
252 @ReadFile(iconPath)
253 </span>
254 }
255 <span class="align-middle">@language.Area.DisplayName</span>
256 </a>
257 </li>
258
259 languagesCount++;
260 }
261 }
262 }
263
264 if (languagesCount == 0)
265 {
266 <li>
267 <span class="dropdown-item fs-7 fw-normal">
268 @Translate("Page is not translated")
269 </span>
270 </li>
271 }
272 }
273
274 @if (layout == "dropdown" && type == "currency")
275 {
276 foreach (var currency in currencies)
277 {
278 var link = $"{url.Scheme}://{hostName}?ID={currentPage.ID}&AreaID={Pageview.Area.ID}&CurrencyCode={currency.Code}";
279 link += groupId != "" ? $"&GroupID={groupId}" : "";
280 link += productId != "" ? $"&ProductID={productId}" : "";
281 link += variantId != "" ? $"&VariantID={variantId}" : "";
282
283 <li>
284 <a href="@link" class="dropdown-item nav-link-underline @linkFontSize @linkFontWeight @linkCasing">@currency.Code</a>
285 </li>
286 }
287 }
288
289