Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.OleDb;
- using System.IO;
- using System.Linq;
- using System.Net.Configuration;
- using System.Net.Mail;
- using System.Web;
- using System.Web.Mvc;
- using umbraco;
- using Umbraco.Core;
- using Umbraco.Core.Logging;
- using Umbraco.Core.Models;
- using Umbraco.Web;
- using Umbraco.Web.Mvc;
- using UmbracoMobil.Models;
- using UmbracoMobil.Utils;
- namespace UmbracoMobil.Controllers
- {
- public class ProductsController : SurfaceController
- {
- public ActionResult GetProducts(int ProductID)
- {
- IPublishedContent ProductComponent = Umbraco.TypedContent(ProductID);
- var CategoryList = new List<Category>();
- var ProductList = new List<Product>();
- var Pagination = new Pagination();
- List<IPublishedContent> ComponentProductList;
- if (ProductComponent.DocumentTypeAlias == "manufacturersComponentItem")
- ComponentProductList = ProductComponent.GetPropertyValue<List<IPublishedContent>>("manufacturersComponentItemProduct");
- else
- ComponentProductList = ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentProducts");
- if (ProductComponent.HasValue("productComponentCategoryFilter"))
- {
- }
- if (ProductComponent.HasValue("productComponentCategoryDisplayFilter"))
- {
- CategoryList = GetCategoryList(ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentCategoryDisplayFilter"));
- ProductList = GetProductWithoutCategoryList(ComponentProductList, out Pagination);
- }
- else
- {
- ProductList = GetProductList(ComponentProductList, out CategoryList, out Pagination);
- }
- Pagination.PageCount = Convert.ToInt32(Math.Ceiling((decimal)Pagination.TotalEntries / 10));
- if (Pagination.PageCount > 1)
- Pagination.ShowButton = true;
- return Json(new { CategoryList, ProductList, Pagination });
- }
- public ActionResult FilterProducts(int[] CategoriesID, int ProductID, Pagination Pagination)
- {
- var ProductComponent = Umbraco.TypedContent(ProductID);
- var ProductComponentList = new List<IPublishedContent>();
- var ProductComponentCategoryFilterList = new List<IPublishedContent>();
- if (ProductComponent.HasValue("productComponentCategoryFilter"))
- {
- foreach (var Node in ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentCategoryFilter"))
- {
- if (Node.DocumentTypeAlias == "category")
- {
- foreach (var CategoryItem in Node.Children)
- {
- ProductComponentCategoryFilterList.Add(CategoryItem);
- }
- }
- else
- {
- ProductComponentCategoryFilterList.Add(Node);
- }
- }
- ProductComponentCategoryFilterList = ProductComponentCategoryFilterList.DistinctBy(x => x.Id).ToList();
- }
- List<IPublishedContent> ComponentProductList;
- if (ProductComponent.DocumentTypeAlias == "manufacturersComponentItem")
- {
- ComponentProductList = ProductComponent.GetPropertyValue<List<IPublishedContent>>("manufacturersComponentItemProduct");
- }
- else
- {
- ComponentProductList = ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentProducts");
- }
- foreach (var Node in ComponentProductList)
- {
- if (Node.DocumentTypeAlias == "productType")
- {
- foreach (var Product in Node.Children)
- {
- if (!ProductComponentList.Contains(Product) && (ProductComponentCategoryFilterList.Count == 0 || ProductComponentCategoryFilterList.Any(x => Product.GetPropertyValue<List<IPublishedContent>>("productsComponentItemCategories").Any(y => y.Id == x.Id))))
- ProductComponentList.Add(Product);
- }
- }
- else
- {
- if (!ProductComponentList.Contains(Node) && (ProductComponentCategoryFilterList.Count == 0 || ProductComponentCategoryFilterList.Any(x => Node.GetPropertyValue<List<IPublishedContent>>("productsComponentItemCategories").Any(y => y.Id == x.Id))))
- ProductComponentList.Add(Node);
- }
- }
- IEnumerable<IPublishedContent> FilteredProducts = null;
- if (CategoriesID != null)
- FilteredProducts = ProductComponentList.Where(x => x.HasValue("productsComponentItemCategories") && CategoriesID.Intersect(GetCategoriesID(x.GetProperty("productsComponentItemCategories"))).Count() == CategoriesID.Length);
- else
- FilteredProducts = ProductComponentList;
- var ProductList = new List<Product>();
- Pagination.TotalEntries = FilteredProducts.Count();
- Pagination.PageCount = Convert.ToInt32(Math.Ceiling((decimal)Pagination.TotalEntries / 10));
- foreach (var Content in FilteredProducts.Skip(Pagination.CurrentPage * 10).Take(10))
- {
- ProductList.Add(Util.CreateProduct(Content));
- }
- if (Pagination.CurrentPage + 1 >= Pagination.PageCount)
- Pagination.ShowButton = false;
- else
- Pagination.ShowButton = true;
- return Json(new { ProductList, Pagination });
- }
- private int[] GetCategoriesID(IPublishedProperty Property)
- {
- return Property.GetValue<List<IPublishedContent>>().Select(x => x.Id).ToArray();
- }
- private string[] GetCategoriesName(IPublishedProperty Property)
- {
- return Property.GetValue<List<IPublishedContent>>().Select(x => x.Name).ToArray();
- }
- private List<Category> GetCategoryList(List<IPublishedContent> CategoryComponentList)
- {
- List<Category> CategoryList = new List<Category>();
- foreach (var Node in CategoryComponentList)
- {
- //category
- if (Node.DocumentTypeAlias == "category")
- {
- var Category = new Category()
- {
- ID = Node.Id,
- Name = Node.Name,
- CategoryItemList = new List<CategoryItem>()
- };
- foreach (var CategoryItem in Node.Children)
- {
- var CategoryItemObject = new CategoryItem()
- {
- ID = CategoryItem.Id,
- Name = CategoryItem.Name
- };
- Category.CategoryItemList.Add(CategoryItemObject);
- }
- CategoryList.Add(Category);
- }
- //category item
- else
- {
- //check if an category is already created for this category item
- Category CategoryFound = CategoryList.FirstOrDefault(x => x.ID == Node.Parent.Id);
- if (CategoryFound != null)
- {
- //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
- if (!CategoryFound.CategoryItemList.Any(x => x.ID == Node.Id))
- {
- var CategoryItemObject = new CategoryItem()
- {
- ID = Node.Id,
- Name = Node.Name
- };
- CategoryFound.CategoryItemList.Add(CategoryItemObject);
- }
- }
- else
- {
- Category Category = new Category()
- {
- ID = Node.Parent.Id,
- Name = Node.Parent.Name,
- CategoryItemList = new List<CategoryItem>()
- {
- new CategoryItem()
- {
- ID = Node.Id,
- Name = Node.Name
- }
- }
- };
- CategoryList.Add(Category);
- }
- }
- }
- return CategoryList;
- }
- private List<Product> GetProductList(List<IPublishedContent> ComponentProductList, out List<Category> CategoryList, out Pagination Pagination)
- {
- var ProductComponentList = new List<IPublishedContent>();
- var CategoryComponentList = new List<IPublishedContent>();
- CategoryList = new List<Category>();
- Pagination = new Pagination();
- if (ProductComponent.HasValue("productComponentCategoryFilter"))
- {
- foreach (var Node in ProductComponent.GetPropertyValue<List<IPublishedContent>>("productComponentCategoryFilter"))
- {
- if (Node.DocumentTypeAlias == "category")
- {
- foreach (var CategoryItem in Node.Children)
- {
- ProductComponentCategoryFilterList.Add(CategoryItem);
- }
- }
- else
- {
- ProductComponentCategoryFilterList.Add(Node);
- }
- }
- ProductComponentCategoryFilterList = ProductComponentCategoryFilterList.DistinctBy(x => x.Id).ToList();
- }
- foreach (var Node in ComponentProductList)
- {
- //verifica se é um tipo de produto
- if (Node.DocumentTypeAlias == "productType")
- {
- //varre todos os produtos desse tipo
- foreach (var Product in Node.Children)
- {
- //verifica se o produto já existe na lista
- if (!ProductComponentList.Any(x => x.Id == Product.Id))
- {
- if (Product.HasValue("productsComponentItemCategories"))
- {
- foreach (var Category in Product.GetPropertyValue<List<IPublishedContent>>("productsComponentItemCategories"))
- {
- if (!CategoryComponentList.Contains(Category))
- {
- CategoryComponentList.Add(Category);
- }
- }
- }
- if (ProductComponentList.Count < 10)
- ProductComponentList.Add(Product);
- Pagination.TotalEntries++;
- }
- }
- }
- else
- {
- if (!ProductComponentList.Any(x => x.Id == Node.Id))
- {
- if (Node.HasValue("productsComponentItemCategories"))
- {
- foreach (var Category in Node.GetPropertyValue<List<IPublishedContent>>("productsComponentItemCategories"))
- {
- if (!CategoryComponentList.Contains(Category))
- {
- CategoryComponentList.Add(Category);
- }
- }
- }
- if (ProductComponentList.Count < 10)
- ProductComponentList.Add(Node);
- Pagination.TotalEntries++;
- }
- }
- }
- var ProductList = new List<Product>();
- foreach (var Product in ProductComponentList)
- {
- ProductList.Add(Util.CreateProduct(Product));
- }
- CategoryList = GetCategoryList(CategoryComponentList);
- return ProductList;
- }
- private List<Product> GetProductWithoutCategoryList(List<IPublishedContent> ComponentProductList, out Pagination Pagination)
- {
- var ProductComponentList = new List<IPublishedContent>();
- Pagination = new Pagination();
- foreach (var Node in ComponentProductList)
- {
- if (Node.DocumentTypeAlias == "productType")
- {
- foreach (var Product in Node.Children)
- {
- if (!ProductComponentList.Any(x => x.Id == Product.Id))
- {
- if (ProductComponentList.Count < 10)
- ProductComponentList.Add(Product);
- Pagination.TotalEntries++;
- }
- }
- }
- else
- {
- if (!ProductComponentList.Any(x => x.Id == Node.Id))
- {
- if (ProductComponentList.Count < 10)
- ProductComponentList.Add(Node);
- Pagination.TotalEntries++;
- }
- }
- }
- var ProductList = new List<Product>();
- foreach (var Product in ProductComponentList)
- {
- ProductList.Add(Util.CreateProduct(Product));
- }
- return ProductList;
- }
- }
- }
Add Comment
Please, Sign In to add comment