Error compiling template "Designs/Rapido/eCom/Productlist/ProductsExport.cshtml"
Line 94: 'ProductService.GetProductByNumber(string, bool)' is obsolete: 'Please use an overload with the languageId parameter'
Line 94: 'Product.GetProductFieldValue(string)' is obsolete: 'Use 'ProductService.GetProductFieldValue' instead'
Line 118: 'ProductService.GetProductByNumber(string, bool)' is obsolete: 'Please use an overload with the languageId parameter'
Line 118: 'Product.GetProductFieldValue(string)' is obsolete: 'Use 'ProductService.GetProductFieldValue' instead'
Line 579: The type name 'ZipArchive' could not be found in the namespace 'System.IO.Compression'. This type has been forwarded to assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Consider adding a reference to that assembly.
Line 579: The type name 'ZipArchive' could not be found in the namespace 'System.IO.Compression'. This type has been forwarded to assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Consider adding a reference to that assembly.
Line 579: The type name 'ZipArchiveMode' could not be found in the namespace 'System.IO.Compression'. This type has been forwarded to assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Consider adding a reference to that assembly.
Line 583: The type name 'ZipArchiveEntry' could not be found in the namespace 'System.IO.Compression'. This type has been forwarded to assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' Consider adding a reference to that assembly.
Line 598: 'ZipFile' is obsolete: 'Do not use'
Line 594: The variable 'ex' is declared but never used

1 //------------------------------------------------------------------------------ 2 // <auto-generated> 3 // This code was generated by a tool. 4 // Runtime Version:4.0.30319.42000 5 // 6 // Changes to this file may cause incorrect behavior and will be lost if 7 // the code is regenerated. 8 // </auto-generated> 9 //------------------------------------------------------------------------------ 10 11 namespace CompiledRazorTemplates.Dynamic { 12 using System; 13 using System.Collections.Generic; 14 using System.Linq; 15 16 #line 3 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 17 using System.IO.Compression; 18 19 #line default 20 #line hidden 21 22 #line 4 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 23 using System.Text.RegularExpressions; 24 25 #line default 26 #line hidden 27 28 #line 639 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 29 using System.Web; 30 31 #line default 32 #line hidden 33 34 #line 6 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 35 using System.Net; 36 37 #line default 38 #line hidden 39 40 #line 9 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 41 using Custom.Thermex.Helpers; 42 43 #line default 44 #line hidden 45 46 #line 636 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 47 using System.Text; 48 49 #line default 50 #line hidden 51 52 #line 637 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 53 using System.IO; 54 55 #line default 56 #line hidden 57 58 #line 638 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 59 using System.Threading; 60 61 #line default 62 #line hidden 63 64 #line 640 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 65 using System.Xml.Linq; 66 67 #line default 68 #line hidden 69 70 71 public class RazorEngine_a2e201548028490d8efd3aa1a4bef17c : Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> { 72 73 #line hidden 74 75 #line 21 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 76 77 public string GetProductImage(LoopItem productObject = null) 78 { 79 string theImage = ""; 80 string missingImage = "missing_image"; 81 82 if (productObject == null) 83 { 84 theImage = GetString("Ecom:Product.ImageDefault.Default.Clean"); 85 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageLarge.Clean") : theImage; 86 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageMedium.Clean") : theImage; 87 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageSmall.Clean") : theImage; 88 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageLarge.Default.Clean") : theImage; 89 90 if (String.IsNullOrWhiteSpace(theImage) || theImage.Contains(missingImage)) 91 { 92 try 93 { 94 theImage = ImageHelper.GetFirstImage(Dynamicweb.Ecommerce.Services.Products.GetProductByNumber(GetString("Ecom:Product.Number"), true).GetProductFieldValue("ImagePaths").ToString()); 95 if (string.IsNullOrEmpty(theImage)) 96 { 97 theImage = "/Images/missing_image.jpg"; 98 } 99 } 100 catch 101 { 102 // Do nothing 103 } 104 } 105 } 106 else 107 { 108 theImage = productObject.GetString("Ecom:Product.ImageDefault.Default.Clean"); 109 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageLarge.Clean") : theImage; 110 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageMedium.Clean") : theImage; 111 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageSmall.Clean") : theImage; 112 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageLarge.Default.Clean") : theImage; 113 114 if (String.IsNullOrWhiteSpace(theImage) || theImage.Contains(missingImage)) 115 { 116 try 117 { 118 theImage = ImageHelper.GetFirstImage(Dynamicweb.Ecommerce.Services.Products.GetProductByNumber(productObject.GetString("Ecom:Product.Number"), true).GetProductFieldValue("ImagePaths").ToString()); 119 if (string.IsNullOrEmpty(theImage)) 120 { 121 theImage = "/Images/missing_image.jpg"; 122 } 123 } 124 catch 125 { 126 // Do nothing 127 } 128 } 129 } 130 131 return theImage; 132 } 133 134 #line default 135 #line hidden 136 137 138 #line 82 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 139 140 public enum FieldType 141 { 142 System, 143 Standard, 144 Custom, 145 CategoryField 146 }; 147 148 public class Field 149 { 150 public string ID { get; set; } 151 public FieldType FieldType { get; set; } 152 public string Tag { get; set; } 153 public string Label { get; set; } 154 155 public Field(string id, string label, string tag, FieldType type) 156 { 157 ID = id; 158 Label = label; 159 Tag = tag; 160 FieldType = type; 161 } 162 } 163 164 public class ExportImagePurpose 165 { 166 public string Name { get; set; } 167 public int Resolution { get; set; } 168 public int Height { get; set; } 169 public int Width { get; set; } 170 public string Format { get; set; } 171 public bool EmbedICCProfile { get; set; } 172 173 public ExportImagePurpose(string name, int resolution, int height, int width, string format, bool embedIccProfile) 174 { 175 Name = name; 176 Resolution = resolution; 177 Height = height; 178 Width = width; 179 Format = format; 180 EmbedICCProfile = embedIccProfile; 181 } 182 } 183 184 #line default 185 #line hidden 186 187 188 #line 130 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 189 190 191 private List<ExportImagePurpose> _exportPurposes = new List<ExportImagePurpose>() 192 { 193 new ExportImagePurpose("Original", 0, 0, 0, string.Empty, false), 194 new ExportImagePurpose("Print", 140, 800, 800, "jpeg", true), 195 new ExportImagePurpose("Office", 72, 300, 300, "png", false), 196 new ExportImagePurpose("Web", 72, 300, 300, "jpeg", false), 197 }; 198 199 public ExportImagePurpose GetExportPurposeByName(string name) 200 { 201 foreach (var purpose in _exportPurposes) 202 { 203 if (purpose.Name == name) 204 { 205 return purpose; 206 } 207 } 208 209 return null; 210 } 211 212 #line default 213 #line hidden 214 215 216 #line 156 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 217 218 //CSV 219 public string GenerateCsv(IEnumerable<Field> fields, string productId) 220 { 221 string headerFormat = "\"{0}\";"; 222 223 System.Text.StringBuilder contentBuilder = new System.Text.StringBuilder(); 224 contentBuilder.Append(System.Text.Encoding.UTF8.GetString(System.Text.Encoding.UTF8.GetPreamble())); 225 226 System.Text.StringBuilder headerBuilder = new System.Text.StringBuilder(); 227 //Header 228 foreach (var field in fields) 229 { 230 headerBuilder.Append(String.Format(headerFormat, field.Label)); 231 } 232 233 contentBuilder.AppendLine(headerBuilder.ToString()); 234 235 if (string.IsNullOrEmpty(productId)) 236 { 237 foreach (LoopItem product in GetLoop("Products")) 238 { 239 ProcessProductCsv(contentBuilder, fields, product); 240 } 241 } 242 else 243 { 244 ProcessProductCsv(contentBuilder, fields, this); 245 } 246 247 return contentBuilder.ToString(); 248 } 249 250 private void ProcessProductCsv(System.Text.StringBuilder contentBuilder, IEnumerable<Field> fields, LoopItem product) 251 { 252 System.Text.StringBuilder productBuilder = new System.Text.StringBuilder(); 253 foreach (var field in fields) 254 { 255 var fieldContent = product.GetString(field.Tag); 256 257 fieldContent = fieldContent.Replace("\"", "'"); 258 259 var value = String.Format("\"{0}\";", fieldContent); 260 productBuilder.Append(value); 261 } 262 263 contentBuilder.AppendLine(productBuilder.ToString()); 264 } 265 266 private void ProcessProductCsv(System.Text.StringBuilder contentBuilder, IEnumerable<Field> fields, Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> product) 267 { 268 System.Text.StringBuilder productBuilder = new System.Text.StringBuilder(); 269 foreach (var field in fields) 270 { 271 var fieldContent = product.GetString(field.Tag); 272 273 fieldContent = fieldContent.Replace("\"", "'"); 274 275 var value = String.Format("\"{0}\";", fieldContent); 276 productBuilder.Append(value); 277 } 278 279 contentBuilder.AppendLine(productBuilder.ToString()); 280 } 281 282 #line default 283 #line hidden 284 285 286 #line 225 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 287 288 public string GenerateJson(IEnumerable<Field> fields, string productId) 289 { 290 var productFeed = new List<System.Dynamic.ExpandoObject>(); 291 292 if (string.IsNullOrEmpty(productId)) 293 { 294 foreach (LoopItem product in GetLoop("Products")) 295 { 296 ProcessProductJson(productFeed, fields, product); 297 } 298 } 299 else 300 { 301 ProcessProductJson(productFeed, fields, this); 302 } 303 304 return Newtonsoft.Json.JsonConvert.SerializeObject(productFeed); 305 } 306 307 private void ProcessProductJson(List<System.Dynamic.ExpandoObject> productFeed, IEnumerable<Field> fields, LoopItem product) 308 { 309 dynamic expandoProduct = new System.Dynamic.ExpandoObject(); 310 311 foreach (var field in fields) 312 { 313 ((IDictionary<String, Object>)expandoProduct).Add(field.Label, product.GetString(field.Tag)); 314 } 315 316 productFeed.Add(expandoProduct); 317 } 318 319 private void ProcessProductJson(List<System.Dynamic.ExpandoObject> productFeed, IEnumerable<Field> fields, Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> product) 320 { 321 dynamic expandoProduct = new System.Dynamic.ExpandoObject(); 322 323 324 foreach (var field in fields) 325 { 326 ((IDictionary<String, Object>)expandoProduct).Add(field.Label, product.GetString(field.Tag)); 327 } 328 329 productFeed.Add(expandoProduct); 330 } 331 332 #line default 333 #line hidden 334 335 336 #line 274 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 337 338 public string GenerateXml(IEnumerable<Field> fields, string productId) 339 { 340 System.Xml.Linq.XDocument xdoc = new System.Xml.Linq.XDocument(); 341 342 var rootElement = new XElement("Root"); 343 344 if (string.IsNullOrEmpty(productId)) 345 { 346 foreach (LoopItem product in GetLoop("Products")) 347 { 348 ProcessProductXml(rootElement, fields, product); 349 } 350 } 351 else 352 { 353 ProcessProductXml(rootElement, fields, this); 354 } 355 356 xdoc.Add(rootElement); 357 return xdoc.ToString(); 358 } 359 360 private void ProcessProductXml(XElement rootElement, IEnumerable<Field> fields, LoopItem product) 361 { 362 var productElement = new XElement("Product"); 363 var systemFieldsElement = new XElement("SystemFields"); 364 var productFieldsElement = new XElement("ProductFields"); 365 var customFieldsElement = new XElement("CustomFields"); 366 var productCategoryFieldsElement = new XElement("ProductCategoryFields"); 367 var imagesElement = new XElement("Images"); 368 369 foreach (var field in fields) 370 { 371 if (field.FieldType == FieldType.System) 372 { 373 CreateLabelValueSection(systemFieldsElement, field, product); 374 } 375 else if (field.FieldType == FieldType.Standard) 376 { 377 CreateLabelValueSection(productFieldsElement, field, product); 378 } 379 else if (field.FieldType == FieldType.Custom) 380 { 381 CreateLabelValueSection(customFieldsElement, field, product); 382 383 } 384 else if (field.FieldType == FieldType.CategoryField) 385 { 386 CreateLabelValueSection(productCategoryFieldsElement, field, product); 387 } 388 } 389 390 //Images 391 var productImage = GetProductImage(product); 392 393 if (!string.IsNullOrEmpty(productImage)) 394 { 395 imagesElement.Add(createImageElement("ProductImage", productImage)); 396 } 397 398 foreach (LoopItem image in product.GetLoop("Ecom:Product.AlternativeImages")) 399 { 400 var alternativeImage = image.GetString("Ecom:Product.AlternativeImages.Image"); 401 if (!string.IsNullOrEmpty(alternativeImage)) 402 { 403 imagesElement.Add(createImageElement(GetImageNameFromUrl(alternativeImage), alternativeImage)); 404 } 405 } 406 407 productElement.Add(systemFieldsElement); 408 productElement.Add(productFieldsElement); 409 productElement.Add(customFieldsElement); 410 productElement.Add(productCategoryFieldsElement); 411 productElement.Add(imagesElement); 412 413 rootElement.Add(productElement); 414 } 415 416 private void CreateLabelValueSection(XElement section, Field field, LoopItem product) 417 { 418 if (section.Element("Labels") == null) 419 { 420 var labelsElement = new XElement("Labels"); 421 section.Add(labelsElement); 422 } 423 424 if (section.Element("Values") == null) 425 { 426 var valuesElement = new XElement("Values"); 427 section.Add(valuesElement); 428 } 429 430 //Labels 431 section.Element("Labels").Add(createLabelElement(field.ID, field.Label)); 432 433 //Values 434 section.Element("Values").Add(createValueElement(field.ID, product.GetString(field.Tag))); 435 } 436 437 private void CreateLabelValueSection(XElement section, Field field, Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> product) 438 { 439 if (section.Element("Labels") == null) 440 { 441 var labelsElement = new XElement("Labels"); 442 section.Add(labelsElement); 443 } 444 445 if (section.Element("Values") == null) 446 { 447 var valuesElement = new XElement("Values"); 448 section.Add(valuesElement); 449 } 450 451 //Labels 452 section.Element("Labels").Add(createLabelElement(field.ID, field.Label)); 453 454 //Values 455 section.Element("Values").Add(createValueElement(field.ID, product.GetString(field.Tag))); 456 } 457 458 private void ProcessProductXml(XElement rootElement, IEnumerable<Field> fields, Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> product) 459 { 460 var productElement = new XElement("Product"); 461 var systemFieldsElement = new XElement("SystemFields"); 462 var productFieldsElement = new XElement("ProductFields"); 463 var customFieldsElement = new XElement("CustomFields"); 464 var productCategoryFieldsElement = new XElement("ProductCategoryFields"); 465 var imagesElement = new XElement("Images"); 466 467 foreach (var field in fields) 468 { 469 if (field.FieldType == FieldType.System) 470 { 471 CreateLabelValueSection(systemFieldsElement, field, product); 472 } 473 else if (field.FieldType == FieldType.Standard) 474 { 475 CreateLabelValueSection(productFieldsElement, field, product); 476 } 477 else if (field.FieldType == FieldType.Custom) 478 { 479 CreateLabelValueSection(customFieldsElement, field, product); 480 } 481 else if (field.FieldType == FieldType.CategoryField) 482 { 483 CreateLabelValueSection(productCategoryFieldsElement, field, product); 484 } 485 } 486 487 //Images 488 var productImage = product.GetString("Ecom:Product.ImageDefault.Default.Clean"); 489 490 if (!string.IsNullOrEmpty(productImage)) 491 { 492 imagesElement.Add(createImageElement("ProductImage", productImage)); 493 } 494 495 foreach (LoopItem image in product.GetLoop("Ecom:Product.AlternativeImages")) 496 { 497 var alternativeImage = image.GetString("Ecom:Product.AlternativeImages.Image"); 498 if (!string.IsNullOrEmpty(alternativeImage)) 499 { 500 imagesElement.Add(createImageElement(GetImageNameFromUrl(alternativeImage), alternativeImage)); 501 } 502 } 503 504 productElement.Add(systemFieldsElement); 505 productElement.Add(productFieldsElement); 506 productElement.Add(customFieldsElement); 507 productElement.Add(productCategoryFieldsElement); 508 productElement.Add(imagesElement); 509 510 rootElement.Add(productElement); 511 } 512 513 public string GetImageNameFromUrl(string url) 514 { 515 int filenameStartIndex = url.LastIndexOf("/") + 1; 516 int filenameEndIndex = url.Length - filenameStartIndex; 517 518 return url.Substring(filenameStartIndex, filenameEndIndex); 519 } 520 521 public XElement createImageElement(string name, string href) 522 { 523 var imageElement = new XElement("Image"); 524 var nameAttribute = new XAttribute("name", name); 525 var hrefAttribute = new XAttribute("href", string.Format("file:///[ServerURI]{0}", href)); 526 527 imageElement.Add(nameAttribute); 528 imageElement.Add(hrefAttribute); 529 530 return imageElement; 531 } 532 533 public XElement createLabelElement(string fieldId, string fieldLabel) 534 { 535 if (string.IsNullOrEmpty(fieldId)) 536 { 537 fieldId = "empty"; 538 } 539 540 var labelElement = new XElement(string.Format("{0}_Label", fieldId)); 541 var nameAttribute = new XAttribute("name", fieldId); 542 543 labelElement.Add(nameAttribute); 544 labelElement.Value = fieldLabel; 545 546 return labelElement; 547 } 548 549 public XElement createValueElement(string fieldId, string fieldLabel) 550 { 551 var valueElement = new XElement(string.Format("{0}_Value", fieldId)); 552 var nameAttribute = new XAttribute("name", fieldId); 553 554 valueElement.Add(nameAttribute); 555 valueElement.Value = fieldLabel; 556 557 return valueElement; 558 } 559 560 #line default 561 #line hidden 562 563 564 #line 500 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 565 566 /// <summary> 567 /// Creates a new Zip archive and fills it with the given content and returns it to the Response.OutputStream 568 /// </summary> 569 /// <param name="zipEntries">A dictionary containing the name of the file, and the content of the file</param> 570 public void ZipContent(Dictionary<string, byte[]> zipEntries, List<string> documents, string siteUrl) 571 { 572 string filename = string.Format("Files/Assets_{0}.zip", string.Join("_", DateTime.Now.ToString().Split(Path.GetInvalidFileNameChars()))); 573 string zipPath = HttpContext.Current.Server.MapPath(filename); 574 bool zipArchiveCreated = false; 575 try 576 { 577 using (FileStream fileStream = new FileStream(zipPath, FileMode.Create)) 578 { 579 using (System.IO.Compression.ZipArchive archive = new System.IO.Compression.ZipArchive(fileStream, System.IO.Compression.ZipArchiveMode.Create)) 580 { 581 foreach (string zipEntry in zipEntries.Keys) 582 { 583 System.IO.Compression.ZipArchiveEntry entry = archive.CreateEntry(zipEntry); 584 585 using (System.IO.Stream entryStream = entry.Open()) 586 { 587 entryStream.Write(zipEntries[zipEntry], 0, zipEntries[zipEntry].Length); 588 } 589 } 590 } 591 } 592 zipArchiveCreated = true; 593 } 594 catch (Exception ex) 595 { 596 zipArchiveCreated = false; 597 } 598 if (zipArchiveCreated && Dynamicweb.Content.Files.ZipFile.ZipFiles(documents, zipPath)) 599 { 600 HttpContext.Current.Response.Redirect(string.Format("{0}/Admin/Public/download.aspx?ForceDownload=true&DeleteFile=true&File={1}", siteUrl, filename)); 601 } 602 else 603 { 604 try 605 { 606 File.Delete(zipPath); 607 } 608 catch { } 609 HttpContext.Current.Response.Redirect(HttpContext.Current.Request.UrlReferrer.ToString()); 610 } 611 } 612 613 public void AddImagesToZip(List<string> images, ExportImagePurpose exportPurpose, string siteUrl, Dictionary<string, byte[]> zipEntries) 614 { 615 using (var webClient = new System.Net.WebClient()) 616 { 617 618 //images 619 foreach (var image in images) 620 { 621 var imageFormat = exportPurpose.Format; 622 var oldFilename = GetImageNameFromUrl(image); 623 var imageName = Path.GetFileNameWithoutExtension(oldFilename); 624 625 var imageBytes = GetBytesFromImage(webClient, image, siteUrl, exportPurpose); 626 627 if (exportPurpose.Width == 0 && exportPurpose.Width == 0) 628 { 629 imageFormat = Path.GetExtension(image).TrimStart('.'); 630 } 631 632 var newFileName = string.Format("{0}.{1}", imageName, imageFormat); 633 if (!zipEntries.ContainsKey(newFileName)) 634 { 635 zipEntries.Add(newFileName, imageBytes); 636 } 637 } 638 } 639 } 640 641 #line default 642 #line hidden 643 644 645 #line 579 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 646 647 public byte[] GetBytesFromImage(WebClient webClient, string path, string siteUrl, ExportImagePurpose exportPurpose) 648 { 649 var imagePath = path; 650 var imageUrl = string.Empty; 651 652 if (!imagePath.StartsWith("/")) 653 { 654 imagePath = "/" + imagePath; 655 } 656 657 if (exportPurpose.Width > 0 && exportPurpose.Width > 0) 658 { 659 imageUrl = string.Format("{0}/Admin/Public/GetImage.ashx?width={1}&height={2}&crop=5&Resolution={3}&Format={4}&image={5}&icc={6}", siteUrl, exportPurpose.Width, exportPurpose.Height, exportPurpose.Resolution, exportPurpose.Format, imagePath, exportPurpose.EmbedICCProfile); 660 } 661 else 662 { 663 imageUrl = siteUrl + imagePath; 664 } 665 666 return webClient.DownloadData(imageUrl); 667 668 } 669 670 public byte[] GetBytesFromDocument(WebClient webClient, string path, string siteUrl) 671 { 672 var documentPath = path; 673 674 if (!documentPath.StartsWith("/")) 675 { 676 documentPath = "/" + documentPath; 677 } 678 679 var documentUrl = string.Format("{0}/Admin/Public/download.aspx?ForceDownload=true&File={1}", siteUrl, documentPath); 680 681 return webClient.DownloadData(documentUrl); 682 683 } 684 685 686 687 public int GetNumberOfPdfPages(string fileName, string siteUrl) 688 { 689 using (var webClient = new System.Net.WebClient()) 690 { 691 using (StreamReader sr = new StreamReader(new MemoryStream(GetBytesFromDocument(webClient, fileName, siteUrl)))) 692 { 693 Regex regex = new Regex(@"/Type\s*/Page[^s]"); 694 MatchCollection matches = regex.Matches(sr.ReadToEnd()); 695 696 return matches.Count; 697 } 698 } 699 } 700 701 702 #line default 703 #line hidden 704 705 706 public RazorEngine_a2e201548028490d8efd3aa1a4bef17c() { 707 } 708 709 public override void Execute() { 710 WriteLiteral("\r\n"); 711 712 WriteLiteral("\r\n"); 713 714 WriteLiteral("\r\n\r\n"); 715 716 WriteLiteral("\r\n\r\n"); 717 718 WriteLiteral("\r\n"); 719 720 WriteLiteral("\r\n"); 721 722 WriteLiteral("\r\n"); 723 724 WriteLiteral("\r\n"); 725 726 WriteLiteral("\r\n\r\n"); 727 728 WriteLiteral("\r\n"); 729 730 WriteLiteral("\r\n\r\n"); 731 732 WriteLiteral("\r\n"); 733 734 WriteLiteral("\r\n"); 735 736 WriteLiteral("\r\n"); 737 738 WriteLiteral("\r\n"); 739 740 WriteLiteral("\r\n"); 741 742 WriteLiteral("\r\n"); 743 744 WriteLiteral("\r\n"); 745 746 747 #line 642 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 748 749 string exportFormat = HttpContext.Current.Request.Form["format"]; 750 string productId = HttpContext.Current.Request.QueryString.Get("ProductID"); 751 string downloadParameter = HttpContext.Current.Request.Form["download"]; 752 string filename = HttpContext.Current.Request.Form["filename"]; 753 bool download = false; 754 755 if (string.IsNullOrEmpty(filename)) 756 { 757 filename = string.Format("export_{0}", DateTime.Now.ToString()); 758 } 759 760 if (!string.IsNullOrEmpty(downloadParameter)) 761 { 762 download = bool.Parse(downloadParameter); 763 } 764 765 var fields = new Dictionary<string, Field>(); 766 767 fields.Add("ID", new Field("ID", "ID", "Ecom:Product.ID", FieldType.System)); 768 fields.Add("Name", new Field("Name", "Name", "Ecom:Product.Name", FieldType.Standard)); 769 fields.Add("Number", new Field("Number", "Number", "Ecom:Product.Number", FieldType.Standard)); 770 fields.Add("ShortDescription", new Field("ShortDescription", "Short description", "Ecom:Product.ShortDescription", FieldType.Standard)); 771 fields.Add("LongDescription", new Field("LongDescription", "Long description", "Ecom:Product.LongDescription", FieldType.Standard)); 772 fields.Add("Price", new Field("Price", "Price", "Ecom:Product.Price.PriceFormatted", FieldType.Standard)); 773 fields.Add("Created", new Field("Created", "Created", "Ecom:Product.Created", FieldType.System)); 774 fields.Add("Updated", new Field("Updated", "Updated", "Ecom:Product.Updated", FieldType.System)); 775 fields.Add("Publication", new Field("Publication", "Publication Date", "Ecom:Product.Updated", FieldType.System)); 776 777 foreach (LoopItem customField in GetLoop("CustomFieldList")) 778 { 779 var field = new Field(customField.GetString("Ecom:CustomField.System"), customField.GetString("Ecom:CustomField.Name"), "Ecom:Product:Field." + customField.GetString("Ecom:CustomField.Template"), FieldType.Custom); 780 781 if (!fields.ContainsKey(field.ID)) 782 { 783 fields.Add(field.ID, field); 784 } 785 } 786 787 788 foreach (LoopItem product in GetLoop("Products")) 789 { 790 if (product.GetLoop("ProductCategories").Count > 0) 791 { 792 foreach (LoopItem categoryGroup in product.GetLoop("ProductCategories")) 793 { 794 foreach (LoopItem categoryField in categoryGroup.GetLoop("ProductCategoryFields")) 795 { 796 var fieldTag = string.Format("Ecom:Product.CategoryField.{0}.{1}", categoryGroup.GetString("Ecom:Product.Category.ID"), categoryField.GetString("Ecom:Product.CategoryField.TemplateTag")); 797 var key = product.GetString(string.Format("{0}.CategoryID", fieldTag)) + "_" + product.GetString(string.Format("{0}.ID", fieldTag)); 798 799 var field = new Field(key, key, string.Format("{0}.Value", fieldTag), FieldType.CategoryField); 800 801 if (!fields.ContainsKey(field.ID)) 802 { 803 fields.Add(field.ID, field); 804 } 805 } 806 } 807 } 808 } 809 810 if (exportFormat == "csv") 811 { 812 HttpContext.Current.Response.ContentType = "text/csv"; 813 814 HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.csv", filename)); 815 } 816 else if (exportFormat == "json") 817 { 818 HttpContext.Current.Response.ContentType = "application/json"; 819 820 if (download) 821 { 822 HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.json", filename)); 823 } 824 } 825 else if (exportFormat == "xml") 826 { 827 HttpContext.Current.Response.ContentType = "text/xml"; 828 829 if (download) 830 { 831 HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xml", filename)); 832 } 833 } 834 835 836 #line default 837 #line hidden 838 WriteLiteral("\r\n\r\n"); 839 840 841 #line 730 "E:\Solutions\Thermex\Files\Templates\Designs\Rapido\eCom\Productlist\ProductsExport.cshtml" 842 if (exportFormat == "csv") 843 { 844 HttpContext.Current.Response.Write(GenerateCsv(fields.Values, productId)); 845 } 846 else if (exportFormat == "json") 847 { 848 HttpContext.Current.Response.Write(GenerateJson(fields.Values, productId)); 849 } 850 else if (exportFormat == "xml") 851 { 852 HttpContext.Current.Response.Write(GenerateXml(fields.Values, productId)); 853 } 854 855 #line default 856 #line hidden 857 } 858 } 859 } 860

1 @inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> 2 3 @using System.IO.Compression; 4 @using System.Text.RegularExpressions; 5 @using System.Web; 6 @using System.Net; 7 8 @inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> 9 @using Custom.Thermex.Helpers; 10 11 @* 12 This is a temporary fallback for the DefaultImage. The image pattern MUST be set up like this: 13 14 ImageSmall = /{ProductNumber}.jpg 15 ImageMedium = /{ProductNumber}{VariantOptionLevel1}.jpg 16 ImageLarge = /{ProductNumber}{VariantComboName}.jpg 17 18 In addition to the ImageDefault setting 19 *@ 20 21 @functions { 22 public string GetProductImage(LoopItem productObject = null) 23 { 24 string theImage = ""; 25 string missingImage = "missing_image"; 26 27 if (productObject == null) 28 { 29 theImage = GetString("Ecom:Product.ImageDefault.Default.Clean"); 30 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageLarge.Clean") : theImage; 31 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageMedium.Clean") : theImage; 32 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageSmall.Clean") : theImage; 33 theImage = String.IsNullOrEmpty(theImage) ? GetString("Ecom:Product.ImageLarge.Default.Clean") : theImage; 34 35 if (String.IsNullOrWhiteSpace(theImage) || theImage.Contains(missingImage)) 36 { 37 try 38 { 39 theImage = ImageHelper.GetFirstImage(Dynamicweb.Ecommerce.Services.Products.GetProductByNumber(GetString("Ecom:Product.Number"), true).GetProductFieldValue("ImagePaths").ToString()); 40 if (string.IsNullOrEmpty(theImage)) 41 { 42 theImage = "/Images/missing_image.jpg"; 43 } 44 } 45 catch 46 { 47 // Do nothing 48 } 49 } 50 } 51 else 52 { 53 theImage = productObject.GetString("Ecom:Product.ImageDefault.Default.Clean"); 54 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageLarge.Clean") : theImage; 55 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageMedium.Clean") : theImage; 56 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageSmall.Clean") : theImage; 57 theImage = String.IsNullOrEmpty(theImage) ? productObject.GetString("Ecom:Product.ImageLarge.Default.Clean") : theImage; 58 59 if (String.IsNullOrWhiteSpace(theImage) || theImage.Contains(missingImage)) 60 { 61 try 62 { 63 theImage = ImageHelper.GetFirstImage(Dynamicweb.Ecommerce.Services.Products.GetProductByNumber(productObject.GetString("Ecom:Product.Number"), true).GetProductFieldValue("ImagePaths").ToString()); 64 if (string.IsNullOrEmpty(theImage)) 65 { 66 theImage = "/Images/missing_image.jpg"; 67 } 68 } 69 catch 70 { 71 // Do nothing 72 } 73 } 74 } 75 76 return theImage; 77 } 78 } 79 80 81 @functions 82 { 83 public enum FieldType 84 { 85 System, 86 Standard, 87 Custom, 88 CategoryField 89 }; 90 91 public class Field 92 { 93 public string ID { get; set; } 94 public FieldType FieldType { get; set; } 95 public string Tag { get; set; } 96 public string Label { get; set; } 97 98 public Field(string id, string label, string tag, FieldType type) 99 { 100 ID = id; 101 Label = label; 102 Tag = tag; 103 FieldType = type; 104 } 105 } 106 107 public class ExportImagePurpose 108 { 109 public string Name { get; set; } 110 public int Resolution { get; set; } 111 public int Height { get; set; } 112 public int Width { get; set; } 113 public string Format { get; set; } 114 public bool EmbedICCProfile { get; set; } 115 116 public ExportImagePurpose(string name, int resolution, int height, int width, string format, bool embedIccProfile) 117 { 118 Name = name; 119 Resolution = resolution; 120 Height = height; 121 Width = width; 122 Format = format; 123 EmbedICCProfile = embedIccProfile; 124 } 125 } 126 } 127 128 @*Image export purposes*@ 129 @functions 130 { 131 132 private List<ExportImagePurpose> _exportPurposes = new List<ExportImagePurpose>() 133 { 134 new ExportImagePurpose("Original", 0, 0, 0, string.Empty, false), 135 new ExportImagePurpose("Print", 140, 800, 800, "jpeg", true), 136 new ExportImagePurpose("Office", 72, 300, 300, "png", false), 137 new ExportImagePurpose("Web", 72, 300, 300, "jpeg", false), 138 }; 139 140 public ExportImagePurpose GetExportPurposeByName(string name) 141 { 142 foreach (var purpose in _exportPurposes) 143 { 144 if (purpose.Name == name) 145 { 146 return purpose; 147 } 148 } 149 150 return null; 151 } 152 } 153 154 @*CSV Product Export*@ 155 @functions 156 { 157 //CSV 158 public string GenerateCsv(IEnumerable<Field> fields, string productId) 159 { 160 string headerFormat = "\"{0}\";"; 161 162 System.Text.StringBuilder contentBuilder = new System.Text.StringBuilder(); 163 contentBuilder.Append(System.Text.Encoding.UTF8.GetString(System.Text.Encoding.UTF8.GetPreamble())); 164 165 System.Text.StringBuilder headerBuilder = new System.Text.StringBuilder(); 166 //Header 167 foreach (var field in fields) 168 { 169 headerBuilder.Append(String.Format(headerFormat, field.Label)); 170 } 171 172 contentBuilder.AppendLine(headerBuilder.ToString()); 173 174 if (string.IsNullOrEmpty(productId)) 175 { 176 foreach (LoopItem product in GetLoop("Products")) 177 { 178 ProcessProductCsv(contentBuilder, fields, product); 179 } 180 } 181 else 182 { 183 ProcessProductCsv(contentBuilder, fields, this); 184 } 185 186 return contentBuilder.ToString(); 187 } 188 189 private void ProcessProductCsv(System.Text.StringBuilder contentBuilder, IEnumerable<Field> fields, LoopItem product) 190 { 191 System.Text.StringBuilder productBuilder = new System.Text.StringBuilder(); 192 foreach (var field in fields) 193 { 194 var fieldContent = product.GetString(field.Tag); 195 196 fieldContent = fieldContent.Replace("\"", "'"); 197 198 var value = String.Format("\"{0}\";", fieldContent); 199 productBuilder.Append(value); 200 } 201 202 contentBuilder.AppendLine(productBuilder.ToString()); 203 } 204 205 private void ProcessProductCsv(System.Text.StringBuilder contentBuilder, IEnumerable<Field> fields, Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> product) 206 { 207 System.Text.StringBuilder productBuilder = new System.Text.StringBuilder(); 208 foreach (var field in fields) 209 { 210 var fieldContent = product.GetString(field.Tag); 211 212 fieldContent = fieldContent.Replace("\"", "'"); 213 214 var value = String.Format("\"{0}\";", fieldContent); 215 productBuilder.Append(value); 216 } 217 218 contentBuilder.AppendLine(productBuilder.ToString()); 219 } 220 } 221 222 223 @*JSON Product Export*@ 224 @functions 225 { 226 public string GenerateJson(IEnumerable<Field> fields, string productId) 227 { 228 var productFeed = new List<System.Dynamic.ExpandoObject>(); 229 230 if (string.IsNullOrEmpty(productId)) 231 { 232 foreach (LoopItem product in GetLoop("Products")) 233 { 234 ProcessProductJson(productFeed, fields, product); 235 } 236 } 237 else 238 { 239 ProcessProductJson(productFeed, fields, this); 240 } 241 242 return Newtonsoft.Json.JsonConvert.SerializeObject(productFeed); 243 } 244 245 private void ProcessProductJson(List<System.Dynamic.ExpandoObject> productFeed, IEnumerable<Field> fields, LoopItem product) 246 { 247 dynamic expandoProduct = new System.Dynamic.ExpandoObject(); 248 249 foreach (var field in fields) 250 { 251 ((IDictionary<String, Object>)expandoProduct).Add(field.Label, product.GetString(field.Tag)); 252 } 253 254 productFeed.Add(expandoProduct); 255 } 256 257 private void ProcessProductJson(List<System.Dynamic.ExpandoObject> productFeed, IEnumerable<Field> fields, Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> product) 258 { 259 dynamic expandoProduct = new System.Dynamic.ExpandoObject(); 260 261 262 foreach (var field in fields) 263 { 264 ((IDictionary<String, Object>)expandoProduct).Add(field.Label, product.GetString(field.Tag)); 265 } 266 267 productFeed.Add(expandoProduct); 268 } 269 } 270 271 272 @*XML Product Export*@ 273 @functions 274 { 275 public string GenerateXml(IEnumerable<Field> fields, string productId) 276 { 277 System.Xml.Linq.XDocument xdoc = new System.Xml.Linq.XDocument(); 278 279 var rootElement = new XElement("Root"); 280 281 if (string.IsNullOrEmpty(productId)) 282 { 283 foreach (LoopItem product in GetLoop("Products")) 284 { 285 ProcessProductXml(rootElement, fields, product); 286 } 287 } 288 else 289 { 290 ProcessProductXml(rootElement, fields, this); 291 } 292 293 xdoc.Add(rootElement); 294 return xdoc.ToString(); 295 } 296 297 private void ProcessProductXml(XElement rootElement, IEnumerable<Field> fields, LoopItem product) 298 { 299 var productElement = new XElement("Product"); 300 var systemFieldsElement = new XElement("SystemFields"); 301 var productFieldsElement = new XElement("ProductFields"); 302 var customFieldsElement = new XElement("CustomFields"); 303 var productCategoryFieldsElement = new XElement("ProductCategoryFields"); 304 var imagesElement = new XElement("Images"); 305 306 foreach (var field in fields) 307 { 308 if (field.FieldType == FieldType.System) 309 { 310 CreateLabelValueSection(systemFieldsElement, field, product); 311 } 312 else if (field.FieldType == FieldType.Standard) 313 { 314 CreateLabelValueSection(productFieldsElement, field, product); 315 } 316 else if (field.FieldType == FieldType.Custom) 317 { 318 CreateLabelValueSection(customFieldsElement, field, product); 319 320 } 321 else if (field.FieldType == FieldType.CategoryField) 322 { 323 CreateLabelValueSection(productCategoryFieldsElement, field, product); 324 } 325 } 326 327 //Images 328 var productImage = GetProductImage(product); 329 330 if (!string.IsNullOrEmpty(productImage)) 331 { 332 imagesElement.Add(createImageElement("ProductImage", productImage)); 333 } 334 335 foreach (LoopItem image in product.GetLoop("Ecom:Product.AlternativeImages")) 336 { 337 var alternativeImage = image.GetString("Ecom:Product.AlternativeImages.Image"); 338 if (!string.IsNullOrEmpty(alternativeImage)) 339 { 340 imagesElement.Add(createImageElement(GetImageNameFromUrl(alternativeImage), alternativeImage)); 341 } 342 } 343 344 productElement.Add(systemFieldsElement); 345 productElement.Add(productFieldsElement); 346 productElement.Add(customFieldsElement); 347 productElement.Add(productCategoryFieldsElement); 348 productElement.Add(imagesElement); 349 350 rootElement.Add(productElement); 351 } 352 353 private void CreateLabelValueSection(XElement section, Field field, LoopItem product) 354 { 355 if (section.Element("Labels") == null) 356 { 357 var labelsElement = new XElement("Labels"); 358 section.Add(labelsElement); 359 } 360 361 if (section.Element("Values") == null) 362 { 363 var valuesElement = new XElement("Values"); 364 section.Add(valuesElement); 365 } 366 367 //Labels 368 section.Element("Labels").Add(createLabelElement(field.ID, field.Label)); 369 370 //Values 371 section.Element("Values").Add(createValueElement(field.ID, product.GetString(field.Tag))); 372 } 373 374 private void CreateLabelValueSection(XElement section, Field field, Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> product) 375 { 376 if (section.Element("Labels") == null) 377 { 378 var labelsElement = new XElement("Labels"); 379 section.Add(labelsElement); 380 } 381 382 if (section.Element("Values") == null) 383 { 384 var valuesElement = new XElement("Values"); 385 section.Add(valuesElement); 386 } 387 388 //Labels 389 section.Element("Labels").Add(createLabelElement(field.ID, field.Label)); 390 391 //Values 392 section.Element("Values").Add(createValueElement(field.ID, product.GetString(field.Tag))); 393 } 394 395 private void ProcessProductXml(XElement rootElement, IEnumerable<Field> fields, Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> product) 396 { 397 var productElement = new XElement("Product"); 398 var systemFieldsElement = new XElement("SystemFields"); 399 var productFieldsElement = new XElement("ProductFields"); 400 var customFieldsElement = new XElement("CustomFields"); 401 var productCategoryFieldsElement = new XElement("ProductCategoryFields"); 402 var imagesElement = new XElement("Images"); 403 404 foreach (var field in fields) 405 { 406 if (field.FieldType == FieldType.System) 407 { 408 CreateLabelValueSection(systemFieldsElement, field, product); 409 } 410 else if (field.FieldType == FieldType.Standard) 411 { 412 CreateLabelValueSection(productFieldsElement, field, product); 413 } 414 else if (field.FieldType == FieldType.Custom) 415 { 416 CreateLabelValueSection(customFieldsElement, field, product); 417 } 418 else if (field.FieldType == FieldType.CategoryField) 419 { 420 CreateLabelValueSection(productCategoryFieldsElement, field, product); 421 } 422 } 423 424 //Images 425 var productImage = product.GetString("Ecom:Product.ImageDefault.Default.Clean"); 426 427 if (!string.IsNullOrEmpty(productImage)) 428 { 429 imagesElement.Add(createImageElement("ProductImage", productImage)); 430 } 431 432 foreach (LoopItem image in product.GetLoop("Ecom:Product.AlternativeImages")) 433 { 434 var alternativeImage = image.GetString("Ecom:Product.AlternativeImages.Image"); 435 if (!string.IsNullOrEmpty(alternativeImage)) 436 { 437 imagesElement.Add(createImageElement(GetImageNameFromUrl(alternativeImage), alternativeImage)); 438 } 439 } 440 441 productElement.Add(systemFieldsElement); 442 productElement.Add(productFieldsElement); 443 productElement.Add(customFieldsElement); 444 productElement.Add(productCategoryFieldsElement); 445 productElement.Add(imagesElement); 446 447 rootElement.Add(productElement); 448 } 449 450 public string GetImageNameFromUrl(string url) 451 { 452 int filenameStartIndex = url.LastIndexOf("/") + 1; 453 int filenameEndIndex = url.Length - filenameStartIndex; 454 455 return url.Substring(filenameStartIndex, filenameEndIndex); 456 } 457 458 public XElement createImageElement(string name, string href) 459 { 460 var imageElement = new XElement("Image"); 461 var nameAttribute = new XAttribute("name", name); 462 var hrefAttribute = new XAttribute("href", string.Format("file:///[ServerURI]{0}", href)); 463 464 imageElement.Add(nameAttribute); 465 imageElement.Add(hrefAttribute); 466 467 return imageElement; 468 } 469 470 public XElement createLabelElement(string fieldId, string fieldLabel) 471 { 472 if (string.IsNullOrEmpty(fieldId)) 473 { 474 fieldId = "empty"; 475 } 476 477 var labelElement = new XElement(string.Format("{0}_Label", fieldId)); 478 var nameAttribute = new XAttribute("name", fieldId); 479 480 labelElement.Add(nameAttribute); 481 labelElement.Value = fieldLabel; 482 483 return labelElement; 484 } 485 486 public XElement createValueElement(string fieldId, string fieldLabel) 487 { 488 var valueElement = new XElement(string.Format("{0}_Value", fieldId)); 489 var nameAttribute = new XAttribute("name", fieldId); 490 491 valueElement.Add(nameAttribute); 492 valueElement.Value = fieldLabel; 493 494 return valueElement; 495 } 496 } 497 498 @*Zip functionality*@ 499 @functions 500 { 501 /// <summary> 502 /// Creates a new Zip archive and fills it with the given content and returns it to the Response.OutputStream 503 /// </summary> 504 /// <param name="zipEntries">A dictionary containing the name of the file, and the content of the file</param> 505 public void ZipContent(Dictionary<string, byte[]> zipEntries, List<string> documents, string siteUrl) 506 { 507 string filename = string.Format("Files/Assets_{0}.zip", string.Join("_", DateTime.Now.ToString().Split(Path.GetInvalidFileNameChars()))); 508 string zipPath = HttpContext.Current.Server.MapPath(filename); 509 bool zipArchiveCreated = false; 510 try 511 { 512 using (FileStream fileStream = new FileStream(zipPath, FileMode.Create)) 513 { 514 using (System.IO.Compression.ZipArchive archive = new System.IO.Compression.ZipArchive(fileStream, System.IO.Compression.ZipArchiveMode.Create)) 515 { 516 foreach (string zipEntry in zipEntries.Keys) 517 { 518 System.IO.Compression.ZipArchiveEntry entry = archive.CreateEntry(zipEntry); 519 520 using (System.IO.Stream entryStream = entry.Open()) 521 { 522 entryStream.Write(zipEntries[zipEntry], 0, zipEntries[zipEntry].Length); 523 } 524 } 525 } 526 } 527 zipArchiveCreated = true; 528 } 529 catch (Exception ex) 530 { 531 zipArchiveCreated = false; 532 } 533 if (zipArchiveCreated && Dynamicweb.Content.Files.ZipFile.ZipFiles(documents, zipPath)) 534 { 535 HttpContext.Current.Response.Redirect(string.Format("{0}/Admin/Public/download.aspx?ForceDownload=true&DeleteFile=true&File={1}", siteUrl, filename)); 536 } 537 else 538 { 539 try 540 { 541 File.Delete(zipPath); 542 } 543 catch { } 544 HttpContext.Current.Response.Redirect(HttpContext.Current.Request.UrlReferrer.ToString()); 545 } 546 } 547 548 public void AddImagesToZip(List<string> images, ExportImagePurpose exportPurpose, string siteUrl, Dictionary<string, byte[]> zipEntries) 549 { 550 using (var webClient = new System.Net.WebClient()) 551 { 552 553 //images 554 foreach (var image in images) 555 { 556 var imageFormat = exportPurpose.Format; 557 var oldFilename = GetImageNameFromUrl(image); 558 var imageName = Path.GetFileNameWithoutExtension(oldFilename); 559 560 var imageBytes = GetBytesFromImage(webClient, image, siteUrl, exportPurpose); 561 562 if (exportPurpose.Width == 0 && exportPurpose.Width == 0) 563 { 564 imageFormat = Path.GetExtension(image).TrimStart('.'); 565 } 566 567 var newFileName = string.Format("{0}.{1}", imageName, imageFormat); 568 if (!zipEntries.ContainsKey(newFileName)) 569 { 570 zipEntries.Add(newFileName, imageBytes); 571 } 572 } 573 } 574 } 575 } 576 577 @*Misc*@ 578 @functions 579 { 580 public byte[] GetBytesFromImage(WebClient webClient, string path, string siteUrl, ExportImagePurpose exportPurpose) 581 { 582 var imagePath = path; 583 var imageUrl = string.Empty; 584 585 if (!imagePath.StartsWith("/")) 586 { 587 imagePath = "/" + imagePath; 588 } 589 590 if (exportPurpose.Width > 0 && exportPurpose.Width > 0) 591 { 592 imageUrl = string.Format("{0}/Admin/Public/GetImage.ashx?width={1}&height={2}&crop=5&Resolution={3}&Format={4}&image={5}&icc={6}", siteUrl, exportPurpose.Width, exportPurpose.Height, exportPurpose.Resolution, exportPurpose.Format, imagePath, exportPurpose.EmbedICCProfile); 593 } 594 else 595 { 596 imageUrl = siteUrl + imagePath; 597 } 598 599 return webClient.DownloadData(imageUrl); 600 601 } 602 603 public byte[] GetBytesFromDocument(WebClient webClient, string path, string siteUrl) 604 { 605 var documentPath = path; 606 607 if (!documentPath.StartsWith("/")) 608 { 609 documentPath = "/" + documentPath; 610 } 611 612 var documentUrl = string.Format("{0}/Admin/Public/download.aspx?ForceDownload=true&File={1}", siteUrl, documentPath); 613 614 return webClient.DownloadData(documentUrl); 615 616 } 617 618 619 620 public int GetNumberOfPdfPages(string fileName, string siteUrl) 621 { 622 using (var webClient = new System.Net.WebClient()) 623 { 624 using (StreamReader sr = new StreamReader(new MemoryStream(GetBytesFromDocument(webClient, fileName, siteUrl)))) 625 { 626 Regex regex = new Regex(@"/Type\s*/Page[^s]"); 627 MatchCollection matches = regex.Matches(sr.ReadToEnd()); 628 629 return matches.Count; 630 } 631 } 632 } 633 634 } 635 636 @using System.Text; 637 @using System.IO; 638 @using System.Threading; 639 @using System.Web; 640 @using System.Xml.Linq; 641 642 @{ 643 string exportFormat = HttpContext.Current.Request.Form["format"]; 644 string productId = HttpContext.Current.Request.QueryString.Get("ProductID"); 645 string downloadParameter = HttpContext.Current.Request.Form["download"]; 646 string filename = HttpContext.Current.Request.Form["filename"]; 647 bool download = false; 648 649 if (string.IsNullOrEmpty(filename)) 650 { 651 filename = string.Format("export_{0}", DateTime.Now.ToString()); 652 } 653 654 if (!string.IsNullOrEmpty(downloadParameter)) 655 { 656 download = bool.Parse(downloadParameter); 657 } 658 659 var fields = new Dictionary<string, Field>(); 660 661 fields.Add("ID", new Field("ID", "ID", "Ecom:Product.ID", FieldType.System)); 662 fields.Add("Name", new Field("Name", "Name", "Ecom:Product.Name", FieldType.Standard)); 663 fields.Add("Number", new Field("Number", "Number", "Ecom:Product.Number", FieldType.Standard)); 664 fields.Add("ShortDescription", new Field("ShortDescription", "Short description", "Ecom:Product.ShortDescription", FieldType.Standard)); 665 fields.Add("LongDescription", new Field("LongDescription", "Long description", "Ecom:Product.LongDescription", FieldType.Standard)); 666 fields.Add("Price", new Field("Price", "Price", "Ecom:Product.Price.PriceFormatted", FieldType.Standard)); 667 fields.Add("Created", new Field("Created", "Created", "Ecom:Product.Created", FieldType.System)); 668 fields.Add("Updated", new Field("Updated", "Updated", "Ecom:Product.Updated", FieldType.System)); 669 fields.Add("Publication", new Field("Publication", "Publication Date", "Ecom:Product.Updated", FieldType.System)); 670 671 foreach (LoopItem customField in GetLoop("CustomFieldList")) 672 { 673 var field = new Field(customField.GetString("Ecom:CustomField.System"), customField.GetString("Ecom:CustomField.Name"), "Ecom:Product:Field." + customField.GetString("Ecom:CustomField.Template"), FieldType.Custom); 674 675 if (!fields.ContainsKey(field.ID)) 676 { 677 fields.Add(field.ID, field); 678 } 679 } 680 681 682 foreach (LoopItem product in GetLoop("Products")) 683 { 684 if (product.GetLoop("ProductCategories").Count > 0) 685 { 686 foreach (LoopItem categoryGroup in product.GetLoop("ProductCategories")) 687 { 688 foreach (LoopItem categoryField in categoryGroup.GetLoop("ProductCategoryFields")) 689 { 690 var fieldTag = string.Format("Ecom:Product.CategoryField.{0}.{1}", categoryGroup.GetString("Ecom:Product.Category.ID"), categoryField.GetString("Ecom:Product.CategoryField.TemplateTag")); 691 var key = product.GetString(string.Format("{0}.CategoryID", fieldTag)) + "_" + product.GetString(string.Format("{0}.ID", fieldTag)); 692 693 var field = new Field(key, key, string.Format("{0}.Value", fieldTag), FieldType.CategoryField); 694 695 if (!fields.ContainsKey(field.ID)) 696 { 697 fields.Add(field.ID, field); 698 } 699 } 700 } 701 } 702 } 703 704 if (exportFormat == "csv") 705 { 706 HttpContext.Current.Response.ContentType = "text/csv"; 707 708 HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.csv", filename)); 709 } 710 else if (exportFormat == "json") 711 { 712 HttpContext.Current.Response.ContentType = "application/json"; 713 714 if (download) 715 { 716 HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.json", filename)); 717 } 718 } 719 else if (exportFormat == "xml") 720 { 721 HttpContext.Current.Response.ContentType = "text/xml"; 722 723 if (download) 724 { 725 HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xml", filename)); 726 } 727 } 728 } 729 730 @if (exportFormat == "csv") 731 { 732 HttpContext.Current.Response.Write(GenerateCsv(fields.Values, productId)); 733 } 734 else if (exportFormat == "json") 735 { 736 HttpContext.Current.Response.Write(GenerateJson(fields.Values, productId)); 737 } 738 else if (exportFormat == "xml") 739 { 740 HttpContext.Current.Response.Write(GenerateXml(fields.Values, productId)); 741 }