Error executing template "Designs/Dwsimple/eCom/Productlist/SparePartsProductListPDF.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
at CompiledRazorTemplates.Dynamic.RazorEngine_f1ac2e16100c458e9938d4fad5c0f00a.<RenderHeaderLayout1>b__10_0(TextWriter __razor_helper_writer) in D:\dynamicweb.net\Solutions\Dynamicweb\interspiro.staging.dynamicweb-cms.com\Files\Templates\Designs\Dwsimple\eCom\Productlist\SparePartsProductListPDF.cshtml:line 235
at CompiledRazorTemplates.Dynamic.RazorEngine_f1ac2e16100c458e9938d4fad5c0f00a.<RenderPaperHeader>b__9_0(TextWriter __razor_helper_writer) in D:\dynamicweb.net\Solutions\Dynamicweb\interspiro.staging.dynamicweb-cms.com\Files\Templates\Designs\Dwsimple\eCom\Productlist\SparePartsProductListPDF.cshtml:line 205
at CompiledRazorTemplates.Dynamic.RazorEngine_f1ac2e16100c458e9938d4fad5c0f00a.<>c__DisplayClass8_0.<RenderProductSheet>b__0(TextWriter __razor_helper_writer) in D:\dynamicweb.net\Solutions\Dynamicweb\interspiro.staging.dynamicweb-cms.com\Files\Templates\Designs\Dwsimple\eCom\Productlist\SparePartsProductListPDF.cshtml:line 148
at CompiledRazorTemplates.Dynamic.RazorEngine_f1ac2e16100c458e9938d4fad5c0f00a.Execute() in D:\dynamicweb.net\Solutions\Dynamicweb\interspiro.staging.dynamicweb-cms.com\Files\Templates\Designs\Dwsimple\eCom\Productlist\SparePartsProductListPDF.cshtml:line 136
at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context, TextWriter reader)
at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, 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 Dynamicweb.Rendering.RazorTemplateRenderingProvider.Render(Template template)
at Dynamicweb.Rendering.TemplateRenderingService.Render(Template template)
at Dynamicweb.Rendering.Template.RenderRazorTemplate()
1 @inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
2 @using Dynamicweb.Content.Items
3 @using System
4 @using System.IO
5 @using System.Web
6 @using System.Collections.Generic;
7
8
9 @functions{
10 public string GetParentSettingsItem(string systemName)
11 {
12 string item = null;
13
14 Dynamicweb.Content.Page current = Dynamicweb.Services.Pages.GetPage(Pageview.Page.ID);
15 while (current != null && current.Parent != current)
16 {
17 var temp = current.Item != null ? current.Item[systemName] : "";
18
19 if (temp != null)
20 {
21 item = temp.ToString();
22
23 if (!String.IsNullOrEmpty(item) && !String.Equals("default", item, StringComparison.OrdinalIgnoreCase))
24 {
25 break;
26 }
27 }
28
29 current = current.Parent;
30 }
31
32 return item;
33 }
34 }
35 @inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
36
37
38 @*
39 This is a temporary fallback for the DefaultImage. The image pattern MUST be set up like this:
40
41 ImageSmall = /{ProductNumber}.jpg
42 ImageMedium = /{ProductNumber}{VariantOptionLevel1}.jpg
43 ImageLarge = /{ProductNumber}{VariantComboName}.jpg
44
45 In addition to the ImageDefault setting
46 *@
47
48 @functions {
49 public string GetProductImage(LoopItem productObject = null)
50 {
51 string theImage = "";
52
53 if (productObject == null) {
54 theImage = GetString("Ecom:Product.ImageDefault.Default.Clean");
55 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageLarge.Clean") : theImage;
56 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageMedium.Clean") : theImage;
57 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageSmall.Clean") : theImage;
58 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageLarge.Default.Clean") : theImage;
59 } else {
60 theImage = productObject.GetString("Ecom:Product.ImageDefault.Default.Clean");
61 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageLarge.Clean") : theImage;
62 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageMedium.Clean") : theImage;
63 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageSmall.Clean") : theImage;
64 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageLarge.Default.Clean") : theImage;
65 }
66
67 return theImage;
68 }
69 }
70
71 @functions {
72 int heightBuffer;
73 int contentHeight;
74 int contentWidth;
75 int productItemHeight;
76 string pageOrientation;
77 int pageNumber;
78 }
79
80 @{
81 int productsOnSheet = 14;
82 int sheetMargin = GetParentSettingsItem("Margin") != null ? Convert.ToInt32(GetParentSettingsItem("Margin")) : 10;
83 int sheetHeight = 296;
84 int sheetWidth = 210;
85 int headerHeight = 15;
86 int footerHeight = 15;
87
88 string pageSize = "a4";
89 pageOrientation = "portrait";
90
91 if (pageOrientation == "portrait")
92 {
93 sheetHeight = 296;
94 sheetWidth = 210;
95 }
96 else
97 {
98 sheetHeight = 209;
99 sheetWidth = 296;
100 }
101
102
103 productsOnSheet = 22;
104
105
106 //Add a buffer to the height
107 heightBuffer = 30;
108 sheetHeight = sheetHeight - heightBuffer;
109
110 contentHeight = (sheetHeight - (headerHeight + footerHeight + (sheetMargin * 2)));
111 contentWidth = (sheetWidth - (sheetMargin * 2));
112 productItemHeight = (contentHeight - 2) / productsOnSheet;
113
114 List<LoopItem> productListPart = new List<LoopItem>();
115 int count = 1;
116
117 foreach (LoopItem product in GetLoop("Products"))
118 {
119 productListPart.Add(product);
120
121 if (count < productsOnSheet)
122 {
123 count++;
124 }
125 else
126 {
127 @RenderProductSheet(productListPart);
128
129 productListPart.Clear();
130 count = 1;
131 }
132 }
133
134 if (count != 1)
135 {
136 @RenderProductSheet(productListPart);
137 }
138 }
139
140
141 @helper RenderProductSheet(dynamic products)
142 {
143 string pageMargin = GetParentSettingsItem("Margin") != null ? GetParentSettingsItem("Margin") : "10";
144
145
146 <section class="sheet">
147 <div class="sheet-container sheet-container--flex-col">
148 @RenderPaperHeader()
149
150 <div class="sheet-container__content sheet-container__content--padding-@(pageMargin)mm" style="height: @(contentHeight + heightBuffer)mm">
151 <table class="sheet-product-table">
152
153 @foreach (LoopItem product in products)
154 {
155 string productName = product.GetString("Ecom:Product.Name");
156 productName += !String.IsNullOrEmpty(product.GetString("Ecom:Product.SelectedVariantComboName")) ? ", " + product.GetString("Ecom:Product.SelectedVariantComboName") : "";
157 string productPrice = product.GetString("Ecom:Product.Discount.Price.PriceFormatted") != product.GetString("Ecom:Product.Price.PriceFormatted") ? product.GetString("Ecom:Product.Discount.Price.PriceFormatted") : product.GetString("Ecom:Product.Price.PriceFormatted");
158 string productBeforePrice = product.GetString("Ecom:Product.Discount.Price.PriceFormatted") != product.GetString("Ecom:Product.Price.PriceFormatted") ? product.GetString("Ecom:Product.Price.PriceFormatted") : "";
159 string productNameWidth = productItemHeight < 20 ? "width: 40mm; " : "";
160 int productImageHeight = productItemHeight < 15 ? 6 : productItemHeight - 4;
161
162 <tr class="sheet-product-table-item" style="height: @(productItemHeight)mm">
163
164 <td>
165 <img src="@GetProductImage(product)?hash=@DateTime.Now" style="height: @(productImageHeight)mm" />
166 </td>
167
168 <td>
169 <div class="sheet-product-number">@product.GetString("Ecom:Product.Number")</div>
170 <div class="sheet-product-title">@productName</div>
171 </td>
172
173 @if (!Convert.ToBoolean(Pageview.CurrentParagraph.Item["HidePrice"]))
174 {
175 <td style="width: 30mm">
176 <div class="sheet-product-price">@productPrice</div>
177 <div class="sheet-product-before-price">@productBeforePrice</div>
178 </td>
179 }
180 </tr>
181 }
182 </table>
183 </div>
184 @RenderPaperFooter()
185 </div>
186 </section>
187 }
188 @helper RenderPaperHeader()
189 {
190 string pageMargin = GetParentSettingsItem("Margin") != null ? GetParentSettingsItem("Margin") : "10";
191
192 var colorService = new ColorSwatchService();
193 var headerSettings = Dynamicweb.Content.Items.Item.GetItemById("SparePartsHeader", GetParentSettingsItem("Header"));
194
195 string headerBackgroundColor = headerSettings["BackgroundColor"] != null ? headerSettings["BackgroundColor"].ToString() : "";
196 headerBackgroundColor = !headerBackgroundColor.Contains("#") ? colorService.GetHexColor(Pageview.AreaID, headerBackgroundColor) : headerBackgroundColor;
197
198 string headerFontColor = headerSettings["FontColor"] != null ? headerSettings["FontColor"].ToString() : "";
199 headerFontColor = !headerFontColor.Contains("#") ? colorService.GetHexColor(Pageview.AreaID, headerFontColor) : headerFontColor;
200
201 string headerLayout = headerSettings["Layout"] != null ? headerSettings["Layout"].ToString() : "1";
202
203 <div class="sheet-container__header sheet-container__header--padding-@(pageMargin)mm" style="background-color: @(headerBackgroundColor); color: @(headerFontColor)">
204
205 @RenderHeaderLayout1()
206
207 </div>
208 }
209 @helper RenderHeaderLayout1()
210 {
211 DateTime serverTime = DateTime.Now;
212 string date = serverTime.Date.ToString("dd/MM/yyyy");
213 var headerSettings = Dynamicweb.Content.Items.Item.GetItemById("SparePartsHeader", GetParentSettingsItem("Header"));
214
215 <div class="sheet-header-left">
216 @if (headerSettings["Logo"] != null)
217 {
218 if (!String.IsNullOrEmpty(headerSettings["Logo"].ToString()))
219 {
220 <div class="sheet-footer-logo">
221 <img src="@headerSettings["Logo"].ToString()" />
222 </div>
223 }
224 else
225 {
226 <text> </text>
227 }
228 }
229 else
230 {
231 <text> </text>
232 }
233 </div>
234 <div class="sheet-header-center">
235 @if (!String.IsNullOrEmpty(Pageview.CurrentParagraph.Item["Title"].ToString()))
236 {
237 <div class="sheet-header-title">@Pageview.CurrentParagraph.Item["Title"]</div>
238 }
239 else
240 {
241 <text> </text>
242 }
243 </div>
244 <div class="sheet-header-right">
245 @if (Convert.ToBoolean(headerSettings["ShowDate"]))
246 {
247 <div class="sheet-header-date">@date</div>
248 }
249 else
250 {
251 <text> </text>
252 }
253 </div>
254 }
255 @helper RenderPaperFooter()
256 {
257 string pageMargin = GetParentSettingsItem("Margin") != null ? GetParentSettingsItem("Margin") : "10";
258
259 pageNumber = 1;
260 if (System.Web.HttpContext.Current.Items.Contains("PaperPageNumber"))
261 {
262 pageNumber = Convert.ToInt32(System.Web.HttpContext.Current.Items["PaperPageNumber"]);
263 pageNumber++;
264 System.Web.HttpContext.Current.Items["PaperPageNumber"] = pageNumber;
265 }
266 else
267 {
268 System.Web.HttpContext.Current.Items.Add("PaperPageNumber", pageNumber);
269 }
270
271 var colorService = new ColorSwatchService();
272 var footerSettings = Dynamicweb.Content.Items.Item.GetItemById("SparePartsFooter", GetParentSettingsItem("Footer"));
273
274 string footerBackgroundColor = footerSettings["BackgroundColor"] != null ? footerSettings["BackgroundColor"].ToString() : "";
275 footerBackgroundColor = !footerBackgroundColor.Contains("#") ? colorService.GetHexColor(Pageview.AreaID, footerBackgroundColor) : footerBackgroundColor;
276
277 string footerFontColor = footerSettings["FontColor"] != null ? footerSettings["FontColor"].ToString() : "";
278 footerFontColor = !footerFontColor.Contains("#") ? colorService.GetHexColor(Pageview.AreaID, footerFontColor) : footerFontColor;
279
280 string footerLayout = footerSettings["Layout"] != null ? footerSettings["Layout"].ToString() : "1";
281
282 <div class="sheet-container__footer sheet-container__footer--padding-@(pageMargin)mm" style="background-color: @(footerBackgroundColor); color: @(footerFontColor)">
283 @RenderFooterLayout1()
284 </div>
285 }
286 @helper RenderFooterLayout1()
287 {
288 var footerSettings = Dynamicweb.Content.Items.Item.GetItemById("SparePartsFooter", GetParentSettingsItem("Footer"));
289
290 <div class="sheet-footer-left">
291 @if (footerSettings["Logo"] != null)
292 {
293 if (!String.IsNullOrEmpty(footerSettings["Logo"].ToString()))
294 {
295 string logo = footerSettings["Logo"].ToString();
296 if (Path.GetExtension(logo).ToLower() != ".svg")
297 {
298 logo = "/Admin/Public/GetImage.ashx?height=20&crop=5&Compression=75&image=" + logo;
299 }
300 else
301 {
302 logo = HttpUtility.UrlDecode(logo);
303 }
304
305
306 <div class="sheet-footer-logo">
307 <img src="@logo" />
308 </div>
309 }
310 else
311 {
312 <text> </text>
313 }
314 }
315 else
316 {
317 <text> </text>
318 }
319 </div>
320 <div class="sheet-footer-center">
321
322 </div>
323 <div class="sheet-footer-right">
324 @if (footerSettings["ShowPageNumber"] != null)
325 {
326 if (Convert.ToBoolean(footerSettings["ShowPageNumber"]))
327 {
328 <div class="sheet-footer-page-number">
329 @pageNumber
330 </div>
331 }
332 else
333 {
334 <text> </text>
335 }
336 }
337 else
338 {
339 <text> </text>
340 }
341 </div>
342 }
343