/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Text.RegularExpressions;
using Everest.Library.ExtensionMethod;
using System.Collections.Specialized;
namespace Everest.CmsServices.MvcHelper{
/// <summary>
///
/// </summary>
public class CmsPageRouteData
{
public const string ModuleUrlSegment = "ModuleUrl";
private static string Prefix_DynamicControlId = CmsGlobal.PrefixControlId;
readonly static Regex urlSectionRegex = new Regex(string.Format("\\b{0}[^/]*\\b", Prefix_DynamicControlId), RegexOptions.Compiled | RegexOptions.IgnoreCase);
/// <summary>
/// Gets or sets the route data.
/// </summary>
/// <value>The route data.</value>
public RouteValueDictionary RouteValues { get; private set; }
/// <summary>
/// Gets or sets the module route values.
/// </summary>
/// <value>The module route values.</value>
private RouteValueDictionary ModuleRouteValues { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="CmsPageRouteData"/> class.
/// </summary>
/// <param name="routeData">The route data.</param>
/// <param name="queryString">The query string.</param>
public CmsPageRouteData(RouteData routeData, NameValueCollection queryString)
{
if (routeData != null)
{
this.RouteValues = routeData.Values;
var moduleUrl = queryString[ModuleUrlSegment];
if (routeData.Values[ModuleUrlSegment] != null)
{
moduleUrl = routeData.Values[ModuleUrlSegment].ToString();
}
ParseModuleRouteValues(moduleUrl);
}
else
{
RouteValues = new RouteValueDictionary();
}
}
private void ParseModuleRouteValues(string moduleUrl)
{
ModuleRouteValues = new RouteValueDictionary();
if (!StringExtensions.IsNullOrEmptyTrim(moduleUrl))
{
moduleUrl = System.Web.HttpUtility.UrlDecode(moduleUrl);
MatchCollection matches = urlSectionRegex.Matches(moduleUrl);
for (int i = 0; i < matches.Count; i++)
{
var current = matches[i];
int startIndex = current.Index + current.Value.Length;
string url = string.Empty;
if (i + 1 < matches.Count)
{
var next = matches[i + 1];
url = moduleUrl.Substring(startIndex, next.Index - startIndex - 1);
}
else
{
url = moduleUrl.Substring(startIndex);
}
ModuleRouteValues.Add(current.Value, url);
}
}
}
/// <summary>
/// Gets the module URL section.
/// </summary>
/// <param name="controlId">The control id.</param>
/// <returns></returns>
public static string GetControlId(string controlId)
{
return Prefix_DynamicControlId + controlId;
}
/// <summary>
/// Gets the module URL.
/// </summary>
/// <param name="modulePositionId">The module position id.</param>
/// <returns></returns>
public virtual string GetModuleUrl(string moduleWrapperControlId)
{
if (ModuleRouteValues[moduleWrapperControlId] != null)
{
return ModuleRouteValues[moduleWrapperControlId].ToString();
}
return string.Empty;
}
/// <summary>
/// Gets the route values with module URL.
/// </summary>
/// <returns></returns>
public virtual RouteValueDictionary GetRouteValuesWithModuleUrl(string name, string moduleUrl)
{
RouteValueDictionary values = new RouteValueDictionary(RouteValues);
RouteValueDictionary moduleRouteValues = new RouteValueDictionary(ModuleRouteValues);
moduleRouteValues[name] = moduleUrl;
StringBuilder moduleUrlBuilder = new StringBuilder();
foreach (var item in moduleRouteValues)
{
//item.Value start with '/'
moduleUrlBuilder.AppendFormat("{0}{1}/", item.Key, item.Value);
}
moduleUrlBuilder.RemoveLastSpecifiedChar('/');
values[ModuleUrlSegment] = moduleUrlBuilder.ToString();
return values;
}
}
}
|