Haruki_Kenshin

Untitled

Oct 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.OleDb;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.Configuration;
  9. using System.Net.Mail;
  10. using System.Web;
  11. using System.Web.Mvc;
  12. using umbraco;
  13. using Umbraco.Core;
  14. using Umbraco.Core.Logging;
  15. using Umbraco.Core.Models;
  16. using Umbraco.Web;
  17. using Umbraco.Web.Mvc;
  18. using UmbracoMobil.Models;
  19. using UmbracoMobil.Utils;
  20.  
  21. namespace UmbracoMobil.Controllers
  22. {
  23.     public class ProductsController : SurfaceController
  24.     {
  25.         public ActionResult GetProducts(int ProductID)
  26.         {
  27.             IPublishedContent ProductComponent = Umbraco.TypedContent(ProductID);
  28.  
  29.             var CategoryList = new List<Category>();
  30.             var ProductList = new List<Product>();
  31.             var Pagination = new Pagination();
  32.  
  33.             List<IPublishedContent> ComponentProductList;
  34.  
  35.             if (ProductComponent.DocumentTypeAlias == "manufacturersComponentItem")
  36.                 ComponentProductList = ProductComponent.GetPropertyValue<List<IPublishedContent>>("manufacturersComponentItemProduct");
  37.             else
  38.                 ComponentProductList = ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentProducts");
  39.  
  40.             if (ProductComponent.HasValue("productComponentCategoryFilter"))
  41.             {
  42.  
  43.             }
  44.  
  45.             if (ProductComponent.HasValue("productComponentCategoryDisplayFilter"))
  46.             {
  47.                 CategoryList = GetCategoryList(ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentCategoryDisplayFilter"));
  48.                 ProductList = GetProductWithoutCategoryList(ComponentProductList, out Pagination);
  49.             }
  50.             else
  51.             {
  52.                 ProductList = GetProductList(ComponentProductList, out CategoryList, out Pagination);
  53.             }
  54.  
  55.             Pagination.PageCount = Convert.ToInt32(Math.Ceiling((decimal)Pagination.TotalEntries / 10));
  56.  
  57.             if (Pagination.PageCount > 1)
  58.                 Pagination.ShowButton = true;
  59.  
  60.             return Json(new { CategoryList, ProductList, Pagination });
  61.         }
  62.  
  63.         public ActionResult FilterProducts(int[] CategoriesID, int ProductID, Pagination Pagination)
  64.         {
  65.             var ProductComponent = Umbraco.TypedContent(ProductID);
  66.  
  67.             var ProductComponentList = new List<IPublishedContent>();
  68.             var ProductComponentCategoryFilterList = new List<IPublishedContent>();
  69.  
  70.             if (ProductComponent.HasValue("productComponentCategoryFilter"))
  71.             {
  72.                 foreach (var Node in ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentCategoryFilter"))
  73.                 {
  74.                     if (Node.DocumentTypeAlias == "category")
  75.                     {
  76.                         foreach (var CategoryItem in Node.Children)
  77.                         {
  78.                             ProductComponentCategoryFilterList.Add(CategoryItem);
  79.                         }
  80.                     }
  81.                     else
  82.                     {
  83.                         ProductComponentCategoryFilterList.Add(Node);
  84.                     }
  85.                 }
  86.  
  87.                 ProductComponentCategoryFilterList = ProductComponentCategoryFilterList.DistinctBy(x => x.Id).ToList();
  88.             }
  89.  
  90.             List<IPublishedContent> ComponentProductList;
  91.  
  92.             if (ProductComponent.DocumentTypeAlias == "manufacturersComponentItem")
  93.             {
  94.                 ComponentProductList = ProductComponent.GetPropertyValue<List<IPublishedContent>>("manufacturersComponentItemProduct");
  95.             }
  96.             else
  97.             {
  98.                 ComponentProductList = ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentProducts");
  99.             }
  100.  
  101.             foreach (var Node in ComponentProductList)
  102.             {
  103.                 if (Node.DocumentTypeAlias == "productType")
  104.                 {
  105.                     foreach (var Product in Node.Children)
  106.                     {
  107.                         if (!ProductComponentList.Contains(Product) && (ProductComponentCategoryFilterList.Count == 0 || ProductComponentCategoryFilterList.Any(x => Product.GetPropertyValue<List<IPublishedContent>>("productsComponentItemCategories").Any(y => y.Id == x.Id))))
  108.                             ProductComponentList.Add(Product);
  109.                     }
  110.                 }
  111.                 else
  112.                 {
  113.                     if (!ProductComponentList.Contains(Node) && (ProductComponentCategoryFilterList.Count == 0 || ProductComponentCategoryFilterList.Any(x => Node.GetPropertyValue<List<IPublishedContent>>("productsComponentItemCategories").Any(y => y.Id == x.Id))))
  114.                         ProductComponentList.Add(Node);
  115.                 }
  116.             }
  117.  
  118.             IEnumerable<IPublishedContent> FilteredProducts = null;
  119.  
  120.             if (CategoriesID != null)
  121.                 FilteredProducts = ProductComponentList.Where(x => x.HasValue("productsComponentItemCategories") && CategoriesID.Intersect(GetCategoriesID(x.GetProperty("productsComponentItemCategories"))).Count() == CategoriesID.Length);
  122.             else
  123.                 FilteredProducts = ProductComponentList;
  124.  
  125.             var ProductList = new List<Product>();
  126.  
  127.             Pagination.TotalEntries = FilteredProducts.Count();
  128.             Pagination.PageCount = Convert.ToInt32(Math.Ceiling((decimal)Pagination.TotalEntries / 10));
  129.  
  130.             foreach (var Content in FilteredProducts.Skip(Pagination.CurrentPage * 10).Take(10))
  131.             {
  132.                 ProductList.Add(Util.CreateProduct(Content));
  133.             }
  134.  
  135.             if (Pagination.CurrentPage + 1 >= Pagination.PageCount)
  136.                 Pagination.ShowButton = false;
  137.             else
  138.                 Pagination.ShowButton = true;
  139.  
  140.             return Json(new { ProductList, Pagination });
  141.         }
  142.  
  143.         private int[] GetCategoriesID(IPublishedProperty Property)
  144.         {
  145.             return Property.GetValue<List<IPublishedContent>>().Select(x => x.Id).ToArray();
  146.         }
  147.  
  148.         private string[] GetCategoriesName(IPublishedProperty Property)
  149.         {
  150.             return Property.GetValue<List<IPublishedContent>>().Select(x => x.Name).ToArray();
  151.         }
  152.  
  153.         private List<Category> GetCategoryList(List<IPublishedContent> CategoryComponentList)
  154.         {
  155.             List<Category> CategoryList = new List<Category>();
  156.  
  157.             foreach (var Node in CategoryComponentList)
  158.             {
  159.                 //category
  160.                 if (Node.DocumentTypeAlias == "category")
  161.                 {
  162.                     var Category = new Category()
  163.                     {
  164.                         ID = Node.Id,
  165.                         Name = Node.Name,
  166.                         CategoryItemList = new List<CategoryItem>()
  167.                     };
  168.  
  169.                     foreach (var CategoryItem in Node.Children)
  170.                     {
  171.                         var CategoryItemObject = new CategoryItem()
  172.                         {
  173.                             ID = CategoryItem.Id,
  174.                             Name = CategoryItem.Name
  175.                         };
  176.  
  177.                         Category.CategoryItemList.Add(CategoryItemObject);
  178.                     }
  179.  
  180.                     CategoryList.Add(Category);
  181.                 }
  182.                 //category item
  183.                 else
  184.                 {
  185.                     //check if an category is already created for this category item
  186.                     Category CategoryFound = CategoryList.FirstOrDefault(x => x.ID == Node.Parent.Id);
  187.  
  188.                     if (CategoryFound != null)
  189.                     {
  190.                         //if it is created, just add to the existing category this new item, but not before checking if this item already exists, in this case, just ignore
  191.                         if (!CategoryFound.CategoryItemList.Any(x => x.ID == Node.Id))
  192.                         {
  193.                             var CategoryItemObject = new CategoryItem()
  194.                             {
  195.                                 ID = Node.Id,
  196.                                 Name = Node.Name
  197.                             };
  198.  
  199.                             CategoryFound.CategoryItemList.Add(CategoryItemObject);
  200.                         }
  201.                     }
  202.                     else
  203.                     {
  204.                         Category Category = new Category()
  205.                         {
  206.                             ID = Node.Parent.Id,
  207.                             Name = Node.Parent.Name,
  208.                             CategoryItemList = new List<CategoryItem>()
  209.                             {
  210.                                 new CategoryItem()
  211.                                 {
  212.                                     ID = Node.Id,
  213.                                     Name = Node.Name
  214.                                 }
  215.                             }
  216.                         };
  217.  
  218.                         CategoryList.Add(Category);
  219.                     }
  220.                 }
  221.             }
  222.  
  223.             return CategoryList;
  224.         }
  225.  
  226.         private List<Product> GetProductList(List<IPublishedContent> ComponentProductList, out List<Category> CategoryList, out Pagination Pagination)
  227.         {
  228.             var ProductComponentList = new List<IPublishedContent>();
  229.             var CategoryComponentList = new List<IPublishedContent>();
  230.  
  231.             CategoryList = new List<Category>();
  232.             Pagination = new Pagination();
  233.  
  234.             if (ProductComponent.HasValue("productComponentCategoryFilter"))
  235.             {
  236.                 foreach (var Node in ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentCategoryFilter"))
  237.                 {
  238.                     if (Node.DocumentTypeAlias == "category")
  239.                     {
  240.                         foreach (var CategoryItem in Node.Children)
  241.                         {
  242.                             ProductComponentCategoryFilterList.Add(CategoryItem);
  243.                         }
  244.                     }
  245.                     else
  246.                     {
  247.                         ProductComponentCategoryFilterList.Add(Node);
  248.                     }
  249.                 }
  250.  
  251.                 ProductComponentCategoryFilterList = ProductComponentCategoryFilterList.DistinctBy(x => x.Id).ToList();
  252.             }
  253.  
  254.             foreach (var Node in ComponentProductList)
  255.             {
  256.                 //verifica se é um tipo de produto
  257.                 if (Node.DocumentTypeAlias == "productType")
  258.                 {
  259.                     //varre todos os produtos desse tipo
  260.                     foreach (var Product in Node.Children)
  261.                     {
  262.                         //verifica se o produto já existe na lista
  263.                         if (!ProductComponentList.Any(x => x.Id == Product.Id))
  264.                         {
  265.                             if (Product.HasValue("productsComponentItemCategories"))
  266.                             {
  267.                                 foreach (var Category in Product.GetPropertyValue<List<IPublishedContent>>("productsComponentItemCategories"))
  268.                                 {
  269.                                     if (!CategoryComponentList.Contains(Category))
  270.                                     {
  271.                                         CategoryComponentList.Add(Category);
  272.                                     }
  273.                                 }
  274.                             }
  275.  
  276.                             if (ProductComponentList.Count < 10)
  277.                                 ProductComponentList.Add(Product);
  278.  
  279.                             Pagination.TotalEntries++;
  280.                         }
  281.                     }
  282.                 }
  283.                 else
  284.                 {
  285.                     if (!ProductComponentList.Any(x => x.Id == Node.Id))
  286.                     {
  287.                         if (Node.HasValue("productsComponentItemCategories"))
  288.                         {
  289.                             foreach (var Category in Node.GetPropertyValue<List<IPublishedContent>>("productsComponentItemCategories"))
  290.                             {
  291.                                 if (!CategoryComponentList.Contains(Category))
  292.                                 {
  293.                                     CategoryComponentList.Add(Category);
  294.                                 }
  295.                             }
  296.                         }
  297.  
  298.                         if (ProductComponentList.Count < 10)
  299.                             ProductComponentList.Add(Node);
  300.  
  301.                         Pagination.TotalEntries++;
  302.                     }
  303.                 }
  304.             }
  305.  
  306.             var ProductList = new List<Product>();
  307.  
  308.             foreach (var Product in ProductComponentList)
  309.             {
  310.                 ProductList.Add(Util.CreateProduct(Product));
  311.             }
  312.  
  313.             CategoryList = GetCategoryList(CategoryComponentList);
  314.  
  315.             return ProductList;
  316.         }
  317.  
  318.         private List<Product> GetProductWithoutCategoryList(List<IPublishedContent> ComponentProductList, out Pagination Pagination)
  319.         {
  320.             var ProductComponentList = new List<IPublishedContent>();
  321.  
  322.             Pagination = new Pagination();
  323.  
  324.             foreach (var Node in ComponentProductList)
  325.             {
  326.                 if (Node.DocumentTypeAlias == "productType")
  327.                 {
  328.                     foreach (var Product in Node.Children)
  329.                     {
  330.                         if (!ProductComponentList.Any(x => x.Id == Product.Id))
  331.                         {
  332.                             if (ProductComponentList.Count < 10)
  333.                                 ProductComponentList.Add(Product);
  334.  
  335.                             Pagination.TotalEntries++;
  336.                         }
  337.                     }
  338.                 }
  339.                 else
  340.                 {
  341.                     if (!ProductComponentList.Any(x => x.Id == Node.Id))
  342.                     {
  343.                         if (ProductComponentList.Count < 10)
  344.                             ProductComponentList.Add(Node);
  345.  
  346.                         Pagination.TotalEntries++;
  347.                     }
  348.                 }
  349.             }
  350.  
  351.             var ProductList = new List<Product>();
  352.  
  353.             foreach (var Product in ProductComponentList)
  354.             {
  355.                 ProductList.Add(Util.CreateProduct(Product));
  356.             }
  357.  
  358.             return ProductList;
  359.         }
  360.     }
  361. }
Add Comment
Please, Sign In to add comment