using System.Globalization;
using System.Text;
using System.Web;
using System.Collections.Specialized;
namespace Nop.Web.Extensions
{
public class QueryString : NameValueCollection
{
public static string PriceMax = "1500";
private int LEN;
public string Prefix { set; get; }
public string CatPrefix { set; get; }
public int Type;
public QueryString(int type)
{
if(type==1)
{
this.CatPrefix = "Search?Cat=";
this.Prefix = "Search";
this.LEN = 6;
this.Type = type;
}
else if (type == 2)
{
this.CatPrefix = "Recommendations?Sid=";
this.Prefix = "Recommendations";
this.LEN = 15;
this.Type = type;
}
else if (type == 3)
{
this.CatPrefix = "MyWishlist?";
this.Prefix = "MyWishlist";
this.LEN = 10;
this.Type = type;
}
}
public QueryString(string queryString) {
FillFromString(queryString);
}
public QueryString Current { get { return new QueryString(Type).FromCurrent(); } }
/// <summary>
/// extracts a querystring from a full URL
/// </summary>
/// <param name="s">the string to extract the querystring from</param>
/// <returns>a string representing only the querystring</returns>
public string ExtractQuerystring(string s) {
if (!string.IsNullOrEmpty(s)) {
if (s.Contains("?"))
return s.Substring(s.IndexOf("?") + 1);
}
return s;
}
/// <summary>
/// returns a querystring object based on a string
/// </summary>
/// <param name="s">the string to parse</param>
/// <returns>the QueryString object </returns>
public QueryString FillFromString(string s) {
base.Clear();
if (string.IsNullOrEmpty(s)) return this;
foreach (string keyValuePair in ExtractQuerystring(s).Split('&')) {
if (string.IsNullOrEmpty(keyValuePair)) continue;
string[] split = keyValuePair.Split('=');
base.Add(split[0], split.Length == 2 ? split[1] : "");
}
return this;
}
/// <summary>
/// returns a QueryString object based on the current querystring of the request
/// </summary>
/// <returns>the QueryString object </returns>
public QueryString FromCurrent() {
if (HttpContext.Current != null) {
return FillFromString(HttpContext.Current.Request.QueryString.ToString());
}
base.Clear();
return this;
}
/// <summary>
/// add a name value pair to the collection
/// </summary>
/// <param name="name">the name</param>
/// <param name="value">the value associated to the name</param>
/// <returns>the QueryString object </returns>
public new QueryString Add(string name, string value) {
return Add(name, value, false);
}
public QueryString AddInt(string name, int value) {
return Add(name, value.ToString(CultureInfo.InvariantCulture), false);
}
/// <summary>
/// adds a name value pair to the collection
/// </summary>
/// <param name="name">the name</param>
/// <param name="value">the value associated to the name</param>
/// <param name="isUnique">true if the name is unique within the querystring. This allows us to override existing values</param>
/// <returns>the QueryString object </returns>
public QueryString Add(string name, string value, bool isUnique) {
string existingValue = base[name];
if (string.IsNullOrEmpty(existingValue))
base.Add(name, value); // HttpUtility.UrlEncodeUnicode value 3x
else if (isUnique)
base[name] = value;
else
base[name] += "," + value;
return this;
}
/// <summary>
/// removes a name value pair from the querystring collection
/// </summary>
/// <param name="name">name of the querystring value to remove</param>
/// <returns>the QueryString object</returns>
public new QueryString Remove(string name) {
string existingValue = base[name];
if (!string.IsNullOrEmpty(existingValue))
base.Remove(name);
return this;
}
/// <summary>
/// clears the collection
/// </summary>
/// <returns>the QueryString object </returns>
public QueryString Reset() {
base.Clear();
return this;
}
/// <summary>
/// overrides the default
/// </summary>
/// <param name="name"></param>
/// <returns>the associated decoded value for the specified name</returns>
public new string this[string name] { get { return base[name]; } }
//public new string this[string name] { get { return HttpUtility.UrlDecode(base[name]); } }
/// <summary>
/// overrides the default indexer
/// </summary>
/// <param name="index"></param>
/// <returns>the associated decoded value for the specified index</returns>
public new string this[int index] { get { return base[index]; } }
//public new string this[int index] { get { return HttpUtility.UrlDecode(base[index]); } }
/// <summary>
/// checks if a name already exists within the query string collection
/// </summary>
/// <param name="name">the name to check</param>
/// <returns>a boolean if the name exists</returns>
public bool Contains(string name) {
string existingValue = base[name];
return !string.IsNullOrEmpty(existingValue);
}
/// <summary>
/// outputs the querystring object to a string
/// </summary>
/// <returns>the encoded querystring as it would appear in a browser</returns>
public override string ToString() {
StringBuilder builder = new StringBuilder(Prefix);
for (var i = 0; i < base.Keys.Count; i++) {
if (!string.IsNullOrEmpty(base.Keys[i])) {
//foreach (string val in base[base.Keys[i]].Split(','))
// builder.Append((builder.Length == 0) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(val); // removed HttpUtility.UrlEncodeUnicode keys
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
return builder.ToString();
}
public string AddToString(string key, string value)
{
StringBuilder builder = new StringBuilder(Prefix);
for (var i = 0; i < base.Keys.Count; i++)
{
if (!string.IsNullOrEmpty(base.Keys[i]))
{
//foreach (string val in base[base.Keys[i]].Split(','))
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(key).Append("=").Append(value);
return builder.ToString();
}
public string AppendToString(string key, string value)
{
StringBuilder builder = new StringBuilder(Prefix);
string ex = null;
for (var i = 0; i < base.Keys.Count; i++)
{
if (!string.IsNullOrEmpty(base.Keys[i]))
{
if (base.Keys[i].Equals(key))
{
ex = base[base.Keys[i]];
continue;
}
/*foreach (string val in base[base.Keys[i]].Split(','))
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(val);*/
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(key).Append("=").Append(ex).Append(",").Append(value);
return builder.ToString();
}
public string ReplaceToString(string key, string value)
{
StringBuilder builder = new StringBuilder(Prefix);
for (var i = 0; i < base.Keys.Count; i++)
{
if (!string.IsNullOrEmpty(base.Keys[i]))
{
if (base.Keys[i].Equals(key))
{
continue;
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(key).Append("=").Append(value);
return builder.ToString();
}
public string AddBrandToCat(string catId, string brandId)
{
// use only single Brand
if (brandId.Split(',').Length > 0) brandId = brandId.Split(',')[0];
StringBuilder builder = new StringBuilder(Prefix);
for (var i = 0; i < base.Keys.Count; i++)
{
if (!string.IsNullOrEmpty(base.Keys[i]))
{
if (base.Keys[i].Equals("Cat"))
{
continue;
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append("Cat=").Append(catId).Append("&Bd=").Append(brandId);
return builder.ToString();
}
public string DeleteToString(string key, string value) // for delete link at accordian top
{
StringBuilder builder = new StringBuilder(Prefix);
string ex = null;
for (var i = 0; i < base.Keys.Count; i++)
{
if (!string.IsNullOrEmpty(base.Keys[i]))
{
if (base.Keys[i].Equals(key))
{
//ex = base[base.Keys[i]].Replace(value, "");
bool added = false;
foreach (string val in base[base.Keys[i]].Split(','))
{
// todo bug
if (!val.Equals(value))
{
if (!added)
{
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(val);
added = true;
}
else { builder.Append(",").Append(val); }
}
}
continue;
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
return builder.ToString();
}
public string DeleteAllToString(string key) // for delete link at accordian top
{
StringBuilder builder = new StringBuilder(Prefix);
for (var i = 0; i < base.Keys.Count; i++)
{
if (!string.IsNullOrEmpty(base.Keys[i]))
{
if (base.Keys[i].Equals(key))
{
continue;
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
return builder.ToString();
}
public string DeleteAllThenAdd(string key,string value) // for delete link at accordian top
{
StringBuilder builder = new StringBuilder(Prefix);
for (var i = 0; i < base.Keys.Count; i++)
{
if (!string.IsNullOrEmpty(base.Keys[i]))
{
if (base.Keys[i].Equals(key))
{
continue;
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(key).Append("=").Append(value);
return builder.ToString();
}
public string DeletePriceToString() // for delete link at accordian top
{
StringBuilder builder = new StringBuilder(Prefix);
for (var i = 0; i < base.Keys.Count; i++)
{
if (!string.IsNullOrEmpty(base.Keys[i]))
{
if (base.Keys[i].Equals("Pt") || base.Keys[i].Equals("Pf"))
{
continue;
}
builder.Append((builder.Length == LEN) ? "?" : "&").Append(base.Keys[i]).Append("=").Append(base[base.Keys[i]]);
}
}
return builder.ToString();
}
public string ClearAll(string catId) // for delete link at accordian top
{
StringBuilder builder = new StringBuilder(Prefix);
builder.Append("?");
if (string.IsNullOrEmpty(catId))
{
builder.Append("Cat=").Append(catId);
}
else
{
builder.Append("Cat=").Append(1);
}
return builder.ToString();
}
}
}