Example #1
0
        /// <summary>
        /// List initalizer - after the request vars have been fetched via InitList.
        /// </summary>
        /// <param name="_options">The options.</param>
        /// <param name="_category">The category.</param>
        /// <param name="_recordsPerPage">The records per page.</param>
        /// <param name="_orderBy">The order by.</param>
        /// <param name="_filter">The filter.</param>
        /// <param name="_listMode">The list mode.</param>
        /// <param name="_page">The page.</param>
        /// <param name="_price">The price.</param>
        /// <param name="_search">The search.</param>
        private void InitListWithValues(ListOptions _options, string _category, string _recordsPerPage, string _orderBy,
            string _filter, string _listMode, string _page, string _price, string _search)
        {
            /* check if the caller
                * has any new options */
            if(_options != null) {
                Options = _options;
                PriceRanges = _options.PriceRanges;
            } else {
                PriceRanges = Options.PriceRanges;
            }
            /* get the context and session
                * then update any listing
                * vars the user may be sending */
            Session session = Main.GetCurrentSession();
            if(_recordsPerPage != null) {
                Rendition.Merchant.UpdateRecordsPerPage(Convert.ToInt32(_recordsPerPage));
            }
            if(_orderBy != null) {
                Rendition.Merchant.UpdateOrderBy(Convert.ToInt32(_orderBy));
            }
            if(_listMode != null) {
                Rendition.Merchant.UpdateListMode(Convert.ToInt32(_listMode));
            }
            /* if a category list was selected
                run the options.category_nameRegexReplace
                on it to replace characters in the category
                name to make their URLS more friendly. */
            if(_category != null) {
                if(Options.CategoryNameRegEx.Length > 0) {
                    CategoryName = Regex.Replace(_category,
                        Options.CategoryNameRegEx, Options.CategoryNameRegExReplace);
                }
                ListType = Options.RequestCategory;
            } else {
                CategoryName = "";
            };

            if(_search != null) {
                Search = _search;
                ListType = Options.RequestSearch;
            } else {
                Search = "";
            };
            if(CategoryName == "" && Search == "") {
                return;
            }
            if(_page != null) {
                Options.RequestPage = _page;
            } else {
                Options.RequestPage = "1";
            };
            ItemList = new List<Commerce.Item>();
            if(_search == null) {
                /* list a category */
                ListTypeValue = CategoryName.UrlEncode();
                Category = Main.Site.Categories.GetCategoryByName(CategoryName);
                if(Category == null) {
                    /* category does not exist - redirect if there's a page to redirect to */
                    if(HttpContext.Current != null && Options.NoItemsFoundRedirect.Length > 0) {
                        HttpContext.Current.Response.Redirect(Options.NoItemsFoundRedirect);
                    }
                    NullReferenceException ex = new NullReferenceException("Category is not found.");
                    throw ex;
                } else {
                    /* remove items that are set
                    * hidden from listings */
                    ItemList.AddRange(Category.Items.FindAll(delegate(Rendition.Commerce.Item item) {
                        return !item.BOMOnly;
                    }));
                }
            } else {
                /* list a search */
                char[] splitBy = { ' ' };
                string[] searchSplit = Search.Split(splitBy);
                ListTypeValue = Search.UrlEncode();
                Category = new Commerce.Category(ListTypeValue, Guid.Empty, true);
                /* generate a menu
                 * first try a keyword */
                foreach(string sh in searchSplit) {
                    _menu = Main.Site.Menus.GetMenuByName("search:" + sh);
                    if(_menu != null) {
                        break;
                    }
                }
                /* if not use the 'search' menu */
                if(_menu == null) {
                    _menu = Main.Site.Menus.GetMenuByName("Search");
                }
                /* finally use the 'default' menu */
                if(_menu == null) {
                    _menu = Main.Site.Menus.GetMenuByName("Default");
                }
                foreach(Commerce.Item it in Main.Site.Items.List) {
                    foreach(string sh in searchSplit) {
                        /* prevents nulls from mucking everything up */
                        if(sh == null) { continue; }
                        if(it == null) { continue; }
                        string sa = sh.Trim().ToLower();
                        if(sa.Length > 0) {
                            if(
                                /* search critera */
                                (
                                it.ShortDescription.ToLower().Contains(sa) ||
                                it.Description.ToLower().Contains(sa) ||
                                it.Html.ToLower().Contains(sa) ||
                                it.ItemNumber.ToLower().Contains(sa) ||
                                it.Keywords.ToLower().Contains(sa) ||
                                it.ProductCopy.ToLower().Contains(sa) ||
                                it.HomeCategory.Name.ToLower().Contains(sa) ||
                                it.AltCategory.Name.ToLower().Contains(sa)
                                ) && it.BOMOnly == false /* don't show hidden items */
                            ) {
                                if(!ItemList.Contains(it)) {
                                    ItemList.Add(it);
                                }
                            }
                        }
                    }
                }
                if(ItemList.Count == 0) {
                    string q = Options.NoItemsFoundRedirect.Contains("?") ? "&" : "?";
                    HttpContext.Current.Response.Redirect(Options.NoItemsFoundRedirect + q + "ref=" +
                    _search.UrlEncode());
                }
            }
            /* get an inital list of price filters */
            List<Dictionary<string, object>> priceFilters = GetPriceFilters(ItemList, PriceRanges);
            Filter = "";
            if(_filter != null) {
                Filter = _filter;
            }
            /* apply property and price filters */
            if(Filter.Length > 0) {
                char[] delimiters = new char[] { '_' };
                string[] filters = Filter.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                for(var x = 0; filters.Length > x; x++) {
                    char[] delimiters2 = new char[] { '|' };
                    string[] hashrow = filters[x].Split(delimiters2, StringSplitOptions.RemoveEmptyEntries);
                    /* apply price filters */
                    if(hashrow[0] == Options.RequestPrice) {
                        Dictionary<string, object> sfilter = priceFilters.Find(delegate(Dictionary<string, object> flt) {
                            return flt["id"].ToString() == hashrow[1];
                        });
                        if(sfilter != null) {
                            List<Commerce.Item> lst = (List<Commerce.Item>)sfilter["list"];
                            if(lst != null) {
                                if(lst.Count > 0) {
                                    ItemList = ItemList.FindAll(delegate(Rendition.Commerce.Item item1) {
                                        return lst.Find(delegate(Rendition.Commerce.Item item2) {
                                            return item1 == item2;
                                        }) != null;
                                    });
                                }
                            }
                        }
                    } else {
                        /* apply property filters */
                        ItemList = ItemList.FindAll(delegate(Rendition.Commerce.Item item) {
                            return item.Properties.FindAll(delegate(Rendition.Commerce.Property prop) {
                                return (prop.Text == hashrow[1] && prop.Name == hashrow[0]);
                            }).Count > 0;
                        });
                    }
                }
            }
            /* now check if there are items
                * in the list and if so do
                * the listing or fail */
            if(ItemList.Count > 0) {
                /* calculate how many
                    * pages there needs
                    * to be etc.. */
                To = ItemList.Count;
                From = 0;
                Regex number = new Regex("[0-9]+");
                if(!number.IsMatch(Options.RequestPage)) {
                    Page = 1;
                } else {
                    try {
                        Page = Convert.ToInt32(Options.RequestPage);
                    } catch {
                        Page = 1;
                    }
                };
                int rpp = session.RecordsPerPage > 0 ? session.RecordsPerPage : Main.Site.Defaults.RecordsPerPage;
                int addMod = ItemList.Count % rpp > 0 ? 1 : 0;
                TotalPages = (ItemList.Count / rpp) + addMod;

                if(Page > TotalPages) {
                    Page = TotalPages;
                }
                /* Users will pass in a 1 based
                    * set, so it is nessessary to
                    * convert it to a zero based set */
                PrevPage = Page - 1;
                NextPage = Page + 1;
                Page--;
                if(Page < 0) {
                    Page = 0;
                }
                From = rpp * Page;
                To = (rpp * Page) + rpp;
                if(To > ItemList.Count) {
                    To = ItemList.Count;
                }
                if(rpp < 1) {
                    session.RecordsPerPage = Main.Site.Defaults.RecordsPerPage;
                    if(session.RecordsPerPage < 1) {
                        session.RecordsPerPage = 100;
                    }
                };
                if(NextPage > TotalPages) {
                    NextPage = TotalPages;
                };
                if(PrevPage < 1) {
                    PrevPage = 1;
                };
                /* get the Properties for this selection of items */
                Properties = Rendition.Main.Site.Properties.List.FindAll(delegate(Rendition.Commerce.Property p) {
                    return ItemList.Exists(delegate(Rendition.Commerce.Item itm) {
                        return p.ItemNumber == itm.ItemNumber;
                    });
                });
                /* now that the filters have been
                    * applied recreate the price list
                    * but only for pepole requesting it */
                if(_filter != null) {
                    priceFilters = GetPriceFilters(ItemList, PriceRanges);
                }
                if(session.ListOrder == 0) {
                    OrderByName = Options.OrderMessageFeaturedItem;
                    /* get the order int respective
                        * to the category id (ix.id)
                        * but on searches order by
                        * item.searchPriority
                        * as there is no category order.
                        */
                    if(Category.Id != Guid.Empty) {
                        ItemList.Sort(delegate(Rendition.Commerce.Item item1, Rendition.Commerce.Item item2) {
                            return item1.CategoryOrder[Category.Id].CompareTo(item2.CategoryOrder[Category.Id]);
                        });
                    } else {
                        ItemList.Sort(delegate(Rendition.Commerce.Item item1, Rendition.Commerce.Item item2) {
                            return item1.SearchPriority.CompareTo(item2.SearchPriority);
                        });
                    }
                } else if(session.ListOrder == 1) {
                    OrderByName = Options.OrderMessagePriceLowToHigh;
                    ItemList.Sort(delegate(Rendition.Commerce.Item item1, Rendition.Commerce.Item item2) {
                        return item1.Price.CompareTo(item2.Price);
                    });
                } else if(session.ListOrder == 2) {
                    OrderByName = Options.OrderMessagePriceHighToLow;
                    ItemList.Sort(delegate(Rendition.Commerce.Item item1, Rendition.Commerce.Item item2) {
                        return item2.Price.CompareTo(item1.Price);
                    });
                } else if(session.ListOrder == 3) {
                    OrderByName = Options.OrderMessageShortDescAtoZ;
                    ItemList.Sort(delegate(Rendition.Commerce.Item item1, Rendition.Commerce.Item item2) {
                        return item1.ShortDescription.CompareTo(item2.ShortDescription);
                    });
                } else if(session.ListOrder == 4) {
                    OrderByName = Options.OrderMessageShortDescZtoA;
                    ItemList.Sort(delegate(Rendition.Commerce.Item item1, Rendition.Commerce.Item item2) {
                        return item2.ShortDescription.CompareTo(item1.ShortDescription);
                    });
                }
                /* build price filter links table */
                pfltBuilder.Append("<table class=\"priceFiltertable\">");
                /* start counting how many times we loop */
                int x = 0;
                int highest = priceFilters.Count - 1;
                foreach(Dictionary<string, object> priceFilter in priceFilters) {
                    if(((int)priceFilter["count"]) > 0) {
                        string low = ((decimal)priceFilter["low"]).ToString("c");
                        string high = ((decimal)priceFilter["high"]).ToString("c");
                        HasPriceFilters = true;
                        /* there are items that are this price, make a link to the items */
                        pfltBuilder.Append("<tr><td>");
                        string pFilterLink = "";
                        string _pflt = Options.PriceFilterFormat;
                        if(x == 0) {
                            _pflt = Options.PriceFilterLowestFormat;
                        } else if(x == highest) {
                            _pflt = Options.PriceFilterHighestFormat;
                        }
                        pFilterLink = "<a rel=\"nofollow\" href=\"/" + Options.ScriptName + "?" + ListType +
                        "=" + ListTypeValue + "&amp;page=" + (Page + 1).ToString() +
                        "&amp;filter=" + HttpUtility.UrlEncode(Filter.Replace(Options.RequestPrice + "|" + priceFilter["id"], "") +
                        "_" + Options.RequestPrice + "|" + priceFilter["id"]) + "\">" +
                        String.Format(_pflt, low, high, priceFilter["count"]) + "</a>";

                        PriceFilterLinks.Add(pFilterLink);
                        pfltBuilder.Append(pFilterLink);
                        pfltBuilder.Append("</td></tr>");
                        x++;
                    }
                }
                pfltBuilder.Append("</table>");
                /* build remove filter links and table */
                string currentValue = "";
                char[] delimiters = new char[] { '_' };
                string[] filters = Filter.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                if(filters.Length > 0) {
                    rfltBuilder.Append("<table class=\"removeFiltertable\">");
                    foreach(string flt in filters) {
                        HasRemoveFilters = true;
                        string fltName = flt.Split('|')[0];
                        string fltValue = flt.Split('|')[1];
                        if(currentValue != fltValue && fltName != Options.RequestPrice) {
                            rfltBuilder.Append("<tr><th>");
                            rfltBuilder.Append(String.Format(Options.PropertyFilterRemoveFormat, fltName));
                            rfltBuilder.Append("</th></tr>");
                        }
                        rfltBuilder.Append("<tr><td>");
                        string rFiltLink = "<a rel=\"nofollow\" href=\"/" + Options.ScriptName + "?" + ListType +
                        "=" + ListTypeValue + "&amp;page=" + (Page + 1).ToString() +
                        "&amp;filter=" + HttpUtility.UrlEncode(Filter.Replace(fltName + "|" + fltValue, "")) +
                        "\" title=\"" + String.Format(Options.PropertyFilterRemoveTitle, Options.RequestPrice) + "\">" +
                        String.Format(Options.PropertyFilterValueRemoveFormat, fltValue.HtmlEncode()) + "</a>";
                        if(fltName == Options.RequestPrice) {
                            rFiltLink = "<a rel=\"nofollow\" href=\"/" + Options.ScriptName + "?" + ListType +
                            "=" + ListTypeValue + "&amp;page=" + (Page + 1).ToString() +
                            "&amp;filter=" + HttpUtility.UrlEncode(Filter.Replace(fltName + "|" + fltValue, "")) +
                            "\" title=\"" + String.Format(Options.PropertyFilterRemoveTitle, Options.RequestPrice.HtmlEncode()) + "\">" +
                            Options.PropertyFilterRemovePrice + "</a>";
                        }
                        currentValue = fltValue;
                        rfltBuilder.Append(rFiltLink);
                        RemoveFilterLinks.Add(rFiltLink);
                        rfltBuilder.Append("</td></tr>");
                    }
                    rfltBuilder.Append("</table>");
                }
                /* build filter links and table */
                string currentProperty = "";
                string currentText = "";
                string count = "";
                fltBuilder.Append("<table class=\"filtertable\">");
                for(var y = 0; Properties.Count > y; y++) {
                    if((currentProperty != Properties[y].Name ||
                    currentText != Properties[y].Text)
                    && Properties[y].Text.Trim().Length > 0
                    && Properties[y].ShowInFilter == true
                    && !Filter.Contains(Properties[y].Name)) {
                        HasFilters = true;
                        if(currentProperty != Properties[y].Name) {
                            fltBuilder.Append("<tr><th>");
                            if(currentProperty != Properties[y].Name) {
                                fltBuilder.Append(Properties[y].Name);
                                currentProperty = Properties[y].Name;
                            }
                            fltBuilder.Append("</th></tr>");
                        }
                        fltBuilder.Append("<tr><td>");
                        if(currentText != Properties[y].Text) {
                            count = Properties.FindAll(delegate(Rendition.Commerce.Property p) {
                                return p.Text.Trim() == Properties[y].Text.Trim() &&
                                Properties[y].Name.Trim() == p.Name.Trim();
                            }).Count.ToString();
                            string f_link = "<a rel=\"nofollow\" href=\"/" + Options.ScriptName + "?" + ListType + "=" +
                            ListTypeValue + "&amp;page=" + (Page + 1).ToString() +
                            "&amp;filter=" + HttpUtility.UrlEncode(Filter + "_" + Properties[y].Name.Trim() + '|' + Properties[y].Text.Trim()) +
                            "\" title=\"" + String.Format(Options.PropertyFilterFormatTitle.HtmlEncode(), Properties[y].Text.Trim()) + "\">" +
                            String.Format(Options.PropertyFilterFormat, Properties[y].Text.Trim().HtmlEncode(), count) + "</a>";
                            fltBuilder.Append(f_link);
                            FilterLinks.Add(f_link);
                            currentText = Properties[y].Text;
                        }
                        fltBuilder.Append("</td></tr>");
                    }
                }
                fltBuilder.Append("</table>");
            }
        }
Example #2
0
 /// <summary>
 /// Refreshes the home and alt category lists associated with this item.
 /// </summary>
 private void RefreshCategoryList()
 {
     _homeCategory = Main.Site.Categories.List.Find(delegate(Commerce.Category cat) {
         return cat.Id == this.HomeCategoryId;
     });
     _altCategory = Main.Site.Categories.List.Find(delegate(Commerce.Category cat) {
         return cat.Id == this.AltCategoryId;
     });
     try {
         /* sort categories by their featured list order */
         _homeCategory.Items.Sort(delegate(Item i1, Item i2) {
             return i1.CategoryOrder[_homeCategory.Id].CompareTo(i2.CategoryOrder[_homeCategory.Id]);
         });
         _altCategory.Items.Sort(delegate(Item i1, Item i2) {
             return i1.CategoryOrder[_altCategory.Id].CompareTo(i2.CategoryOrder[_altCategory.Id]);
         });
     } catch {
         string.Format("Item {0} is missing its home or alternate category",this.Number).Debug(0);
     }
 }