/// <summary> /// Query language: /// "./name/name" /// </summary> /// <param name="query"> /// </param> /// <returns> /// </returns> public IPriceMatrixItem SelectSingleItem(string query) { string[] axes = query.Split('/'); IPriceMatrixItem current = null; for (int i = 0; i < axes.Length; i++) { string name = axes[i]; if (!(i == 0 && name.Contains("."))) { if (!string.IsNullOrEmpty(name)) { current = this.GetElement(current, axes[i]); if (current == null) { return(null); } } else { return(null); } } } return(current); }
/// <summary> /// Iterates the recursive to load. /// </summary> /// <param name="priceMatrixItem">The price matrix item.</param> /// <param name="item">The item.</param> protected virtual void IterateRecursiveToLoad(IPriceMatrixItem priceMatrixItem, Item item) { var key = item.Template.Key; if (key.Equals("pricematrix settings")) { var children = item.Children; foreach (Item child in children) { IPriceMatrixItem categoryChild = null; var matrixItem = priceMatrixItem as Category; if (matrixItem != null) { var category = matrixItem; categoryChild = category.GetElement(child.Name); } this.IterateRecursiveToLoad(categoryChild, child); } } else if (key.Equals("pricematrix site")) { foreach (Item child in item.Children) { IPriceMatrixItem categoryChild = null; var matrixItem = priceMatrixItem as Category; if (matrixItem != null) { var category = matrixItem; categoryChild = category.GetElement(child.Name); } this.IterateRecursiveToLoad(categoryChild, child); } } else if (key.Equals("pricematrix price")) { var matrixItem = priceMatrixItem as CategoryItem; var categoryItem = matrixItem; if (categoryItem == null) { return; } var value = categoryItem.Amount ?? string.Empty; var expression = this.Parameters["Pattern"]; var regex = new Regex(expression); if (string.IsNullOrEmpty(value)) { return; } if (!regex.IsMatch(value)) { this.message += string.Format("Price field \"{0}\" has wrong format: \"{1}\".\n", new object[] { this.GetAncestorPath(item), value }); } } }
/// <summary> /// </summary> /// <param name="item"> /// </param> /// <param name="id"> /// </param> /// <returns> /// </returns> private CategoryItem GetItem(IPriceMatrixItem item, string id) { if (item is Category) { return(((Category)item).GetItem(id)); } return(null); }
/// <summary> /// Gets the price matrix price. /// </summary> /// <typeparam name="TProduct">The type of the product.</typeparam> /// <param name="product">The product.</param> /// <param name="priceMatrixName">Name of the price matrix.</param> /// <returns>The price matrix price.</returns> protected virtual decimal GetPriceMatrixPrice <TProduct>(TProduct product, string priceMatrixName) where TProduct : ProductBaseData { Assert.ArgumentNotNull(product, "product"); string field = "Price"; Item productItem = ProductRepositoryUtil.GetRepositoryItem(product, this.Database); if (productItem == null) { return(decimal.Zero); } PriceField priceField; try { priceField = productItem.Fields[field]; } catch (Exception exception) { Log.Error(exception.Message, exception, this); return(decimal.Zero); } if (priceField == null) { return(decimal.Zero); } string sitename = "Shop"; string query = string.Format("./{0}/{1}", sitename, priceMatrixName); IPriceMatrixItem priceMatrixItem = priceField.PriceMatrix.SelectSingleItem(query); CategoryItem categoryItem = priceMatrixItem as CategoryItem; if (categoryItem == null) { return(decimal.Zero); } string price = categoryItem.Amount; decimal priceValue; return(!decimal.TryParse(price, NumberStyles.Float, CultureInfo.InvariantCulture, out priceValue) ? 0 : priceValue); }
/// <summary> /// Gets the price matrix price. /// </summary> /// <typeparam name="TProduct">The type of the product.</typeparam> /// <param name="product">The product.</param> /// <param name="priceMatrixName">Name of the price matrix.</param> /// <returns>The price.</returns> protected override decimal GetPriceMatrixPrice <TProduct>(TProduct product, string priceMatrixName) { PriceMatrix pm = PriceMatrix.Load(this.priceMatrix); string query = string.Format("./Shop/{0}", priceMatrixName); IPriceMatrixItem priceMatrixItem = pm.SelectSingleItem(query); CategoryItem categoryItem = priceMatrixItem as CategoryItem; if (categoryItem == null) { return(decimal.Zero); } string price = categoryItem.Amount; decimal priceValue; return(!decimal.TryParse(price, out priceValue) ? 0 : priceValue); }
/// <summary> /// </summary> /// <param name="priceMatrixItem"> /// </param> /// <param name="id"> /// </param> /// <returns> /// </returns> public IPriceMatrixItem GetElement(IPriceMatrixItem priceMatrixItem, string id) { if (priceMatrixItem == null) { priceMatrixItem = this.MainCategory; } if (priceMatrixItem is Category) { Category category = this.GetCategory(priceMatrixItem, id); if (category != null) { return(category); } return(this.GetItem(priceMatrixItem, id)); } return(null); }
/// <summary> /// </summary> /// <param name="query"> /// </param> /// <returns> /// </returns> public CategoryItem GetDefaultPriceFromCategory(string query) { IPriceMatrixItem priceMatrixItem = this.SelectSingleItem(query); var category = (Category)priceMatrixItem; Item priceMatrix = Sitecore.Context.Database.SelectSingleItem("/*/system/Modules/*[@@templatekey='configuration']").Children["PriceMatrix"]; if (priceMatrix != null) { Item currentPriceItem = priceMatrix.Axes.SelectSingleItem(query); if (currentPriceItem != null) { string id = currentPriceItem["defaultPrice"]; Item priceItem = null; if (!string.IsNullOrEmpty(id)) { priceItem = priceMatrix.Database.GetItem(id); } else { if (currentPriceItem.Children.Count > 0) { priceItem = currentPriceItem.Children[0]; } } if (priceItem != null) { string priceName = priceItem.Name; return(category.GetItem(priceName)); } } } return(null); }
/// <summary> /// </summary> /// <param name="item"> /// </param> /// <param name="id"> /// </param> /// <returns> /// </returns> private CategoryItem GetItem(IPriceMatrixItem item, string id) { if (item is Category) { return ((Category)item).GetItem(id); } return null; }
/// <summary> /// </summary> /// <param name="priceMatrixItem"> /// </param> /// <param name="id"> /// </param> /// <returns> /// </returns> public IPriceMatrixItem GetElement(IPriceMatrixItem priceMatrixItem, string id) { if (priceMatrixItem == null) { priceMatrixItem = this.MainCategory; } if (priceMatrixItem is Category) { Category category = this.GetCategory(priceMatrixItem, id); if (category != null) { return category; } return this.GetItem(priceMatrixItem, id); } return null; }
/// <summary> /// Iterates the recursive to save. /// </summary> /// <param name="parent">The parent.</param> /// <param name="priceMatrixItem">The price matrix item.</param> /// <param name="item">The item.</param> /// <param name="controlCollection">The control collection.</param> private static void IterateRecursiveToSave(Category parent, IPriceMatrixItem priceMatrixItem, Item item, ControlCollection controlCollection) { string key = item.Template.Key; if (key.Equals("pricematrix settings")) { Category category = null; if (priceMatrixItem is Category) { category = (Category)priceMatrixItem; } ChildList children = item.Children; foreach (Item child in children) { IPriceMatrixItem categoryChild = null; if (category != null) { categoryChild = category.GetElement(child.Name); } IterateRecursiveToSave(category, categoryChild, child, controlCollection); } } else if (key.Equals("pricematrix site")) { string itmId = item.Name; /* * siteIds += itmId + "|"; */ string defaultId = item["standardprice"]; string defaultName = string.Empty; if (!string.IsNullOrEmpty(defaultId)) { Item defaultItem = Sitecore.Context.ContentDatabase.GetItem(defaultId); if (defaultItem != null) { /* * defaultName = defaultItem.Name; */ } } Category category; if (priceMatrixItem is Category) { category = (Category)priceMatrixItem; category.Id = itmId; } else { category = new Category(itmId); if (parent != null) { parent.AddCategory(category); } } ChildList children = item.Children; foreach (Item child in children) { IPriceMatrixItem categoryChild = null; if (category != null) { categoryChild = category.GetElement(child.Name); } IterateRecursiveToSave(category, categoryChild, child, controlCollection); } } else if (key.Equals("pricematrix price")) { string aTitle = item["title"]; if (string.IsNullOrEmpty(aTitle)) { /* * aTitle = item.Name; */ } string itmchildId = item.Name; string priceIds = string.Empty; /* * priceIds += itmchildId + "|"; */ string itmchildPrice = string.Empty; // Check that it indeed finds the textbox. if (controlCollection != null) { var textBox = Utils.MainUtil.FindControl(item.Parent.Name + "_" + itmchildId, controlCollection) as Text; if (textBox != null) { itmchildPrice = textBox.Value; } } CategoryItem categoryItem; if (priceMatrixItem is CategoryItem) { categoryItem = (CategoryItem)priceMatrixItem; categoryItem.Id = itmchildId; categoryItem.Amount = itmchildPrice; } else { categoryItem = new CategoryItem(itmchildId, itmchildPrice); if (parent != null) { parent.AddItem(categoryItem); } } } }
/// <summary> /// Iterates the recursive to load. /// </summary> /// <param name="priceMatrixItem">The price matrix item.</param> /// <param name="panel">The panel.</param> /// <param name="item">The item.</param> private void IterateRecursiveToLoad(IPriceMatrixItem priceMatrixItem, System.Web.UI.WebControls.Panel panel, Item item) { string key = item.Template.Key; int level = item.Axes.Level - this.priceMatrixConfigurationLevel - 1; if (key.Equals("pricematrix settings")) { ChildList children = item.Children; foreach (Item child in children) { IPriceMatrixItem categoryChild = null; if (priceMatrixItem is Category) { var category = (Category)priceMatrixItem; categoryChild = category.GetElement(child.Name); } this.IterateRecursiveToLoad(categoryChild, panel, child); } } else if (key.Equals("pricematrix site")) { var subPanel = new System.Web.UI.WebControls.Panel(); subPanel.Attributes.Add("style", string.Format("display:block; padding: 5px, 0, 0, {0}px;", StandardIndentLevel * level)); string title = item["title"]; if (string.IsNullOrEmpty(title)) { title = item.Name; } if (this.Controls == null) { throw new Exception("Container is null"); } // Sitecore.Web.UI.HtmlControls.Panel subPanel = new Sitecore.Web.UI.HtmlControls.Panel(); // subPanel.CssStyle = ""; // subPanel.Padding = string.Format("0, 0, 0, {0}px;", 20 * level); // subPanel.Class = ""; // subPanel.BorderWidth = new Unit(0); // panel.Controls.Add(subPanel); var lblTitle = new System.Web.UI.WebControls.Label(); lblTitle.Text = title; lblTitle.Width = new Unit(StandardIndentTextBox + StandardIndentLevel); // lblTitle.Height = new Unit(20); // lblTitle.Padding = string.Format("0, 0, 0, {0}px;", 20 * level); // subPanel.Controls.Add(lblTitle); subPanel.Controls.Add(lblTitle); panel.Controls.Add(subPanel); ChildList children = item.Children; if (children.Count == 1) { lblTitle.Attributes.Add("style", string.Format("color: #313031; font-weight: bold; padding: 0 0 6px 0;")); // no subprice elements var textbox = new Text(); subPanel.Controls.Add(textbox); Item child = item.Children[0]; textbox.ID = this.GetID(item.Name + "_" + child.Name); textbox.Width = new Unit(100); IPriceMatrixItem categoryChild = null; if (priceMatrixItem is Category) { var category = (Category)priceMatrixItem; categoryChild = category.GetElement(child.Name); } if (categoryChild != null) { if (categoryChild is CategoryItem) { var categoryItem = (CategoryItem)categoryChild; textbox.Value = this.FormatNumber(categoryItem.Amount ?? String.Empty); // if (categoryItem.QuantityPrices.Count > 0) // lblQuantity.Text = string.Format("{0} quantity price", categoryItem.QuantityPrices.Count); } // else // { // // Something went wrong in the datastructure. // // Might be that the datastructure has been changed since last time saved. // // Therefor we'll just leave the field empty. // } } } else { lblTitle.Attributes.Add("style", string.Format("color: #313031; font-weight: bold; padding: 0 0 3px 0;")); foreach (Item child in children) { IPriceMatrixItem categoryChild = null; if (priceMatrixItem is Category) { var category = (Category)priceMatrixItem; categoryChild = category.GetElement(child.Name); } this.IterateRecursiveToLoad(categoryChild, panel, child); } } } else if (key.Equals("pricematrix price")) { var subPanel = new System.Web.UI.WebControls.Panel(); subPanel.Attributes.Add("style", string.Format("display:block; padding: 0, 0, 0, {0}px;", StandardIndentLevel * level)); var lblTitle = new System.Web.UI.WebControls.Label { Width = new Unit(StandardIndentTextBox) }; string aTitle = item["title"]; if (string.IsNullOrEmpty(aTitle)) { aTitle = item.Name; } lblTitle.Text = aTitle; subPanel.Controls.Add(lblTitle); var textbox = new Text(); subPanel.Controls.Add(textbox); textbox.ID = this.GetID(item.Parent.Name + "_" + item.Name); textbox.Width = new Unit(100); // textbox.Float = "left"; // Set the value from the xml structure on the text box. // Commented out not in use // System.Web.UI.WebControls.Label lblQuantity = new System.Web.UI.WebControls.Label(); // subPanel.Controls.Add(lblQuantity); if (priceMatrixItem != null) { if (priceMatrixItem is CategoryItem) { var categoryItem = (CategoryItem)priceMatrixItem; textbox.Value = this.FormatNumber(categoryItem.Amount ?? String.Empty); // if (categoryItem.QuantityPrices.Count > 0) // lblQuantity.Text = string.Format("{0} quantity price", categoryItem.QuantityPrices.Count); } // else // { // // Something went wrong in the datastructure. // // Might be that the datastructure has been changed since last time saved. // // Therefor we'll just leave the field empty. // } } // Sitecore.Web.UI.HtmlControls.Button imgBtn = new Sitecore.Web.UI.HtmlControls.Button(); // imgBtn.ID = GetID("btn_" + item.Parent.Name + "_" + item.Name); ////imgBtn.Float = "left"; // imgBtn.Header = "Add"; ////imgBtn.Src = "/sitecore/shell/Themes/Standard/people/32x32/colors.png"; ////imgBtn.Width = new Unit(32); ////imgBtn.Height = new Unit(32); // *Sitecore.Web.UI.HtmlControls.ThemedImage image = new ThemedImage(); // btnLink.Controls.Add(image); // image.Height = new Unit(16); // image.Width = new Unit(16); // image.Src = */ // subPanel.Controls.Add(imgBtn); ////Sitecore.Web.UI.HtmlControls.Button knap = new Sitecore.Web.UI.HtmlControls.Button(); ////subPanel.Controls.Add(knap); ////btnLink.Attributes.Add("Click", string.Format("{0}.ListItemClick(\"" + GetID(item.Parent.Name + "_" + item.Name) + "\")", this.ID)); // imgBtn.ServerProperties["Click"] = string.Format("{0}.ListItemClick(\"" + GetID(item.Parent.Name + "_" + item.Name) + "\")", this.ID); ////knap.Header = "Add"; panel.Controls.Add(subPanel); } }
/// <summary> /// Iterates the recursive to save. /// </summary> /// <param name="parent">The parent.</param> /// <param name="priceMatrixItem">The price matrix item.</param> /// <param name="item">The item.</param> /// <param name="controlCollection">The control collection.</param> private static void IterateRecursiveToSave(Category parent, IPriceMatrixItem priceMatrixItem, Item item, ControlCollection controlCollection) { string key = item.Template.Key; if (key.Equals("pricematrix settings")) { Category category = null; if (priceMatrixItem is Category) { category = (Category)priceMatrixItem; } ChildList children = item.Children; foreach (Item child in children) { IPriceMatrixItem categoryChild = null; if (category != null) { categoryChild = category.GetElement(child.Name); } IterateRecursiveToSave(category, categoryChild, child, controlCollection); } } else if (key.Equals("pricematrix site")) { string itmId = item.Name; /* siteIds += itmId + "|"; */ string defaultId = item["standardprice"]; string defaultName = string.Empty; if (!string.IsNullOrEmpty(defaultId)) { Item defaultItem = Sitecore.Context.ContentDatabase.GetItem(defaultId); if (defaultItem != null) { /* defaultName = defaultItem.Name; */ } } Category category; if (priceMatrixItem is Category) { category = (Category)priceMatrixItem; category.Id = itmId; } else { category = new Category(itmId); if (parent != null) { parent.AddCategory(category); } } ChildList children = item.Children; foreach (Item child in children) { IPriceMatrixItem categoryChild = null; if (category != null) { categoryChild = category.GetElement(child.Name); } IterateRecursiveToSave(category, categoryChild, child, controlCollection); } } else if (key.Equals("pricematrix price")) { string aTitle = item["title"]; if (string.IsNullOrEmpty(aTitle)) { /* aTitle = item.Name; */ } string itmchildId = item.Name; string priceIds = string.Empty; /* priceIds += itmchildId + "|"; */ string itmchildPrice = string.Empty; // Check that it indeed finds the textbox. if (controlCollection != null) { var textBox = Utils.MainUtil.FindControl(item.Parent.Name + "_" + itmchildId, controlCollection) as Text; if (textBox != null) { itmchildPrice = textBox.Value; } } CategoryItem categoryItem; if (priceMatrixItem is CategoryItem) { categoryItem = (CategoryItem)priceMatrixItem; categoryItem.Id = itmchildId; categoryItem.Amount = itmchildPrice; } else { categoryItem = new CategoryItem(itmchildId, itmchildPrice); if (parent != null) { parent.AddItem(categoryItem); } } } }